1.11.1 • Published 3 years ago

secux-connect v1.11.1

Weekly downloads
12
License
ISC
Repository
-
Last release
3 years ago

SecuX Connector Engine API version 1.7.5

SecuX Connector Engine API provides the communication bridge between SecuX V20/W20/W10 hardware devices and 3rd Party wallet via USB and/or Bluetooth interface. SecuX Connector Engine API equips SecuX developers or 3rd Party Company with functionalities in accessing public keys and signing transactions with the cryptocurrency's private keys and its Elliptic Curves supported by SecuX V20/W20/W10 hardware devices

Bitcoin(BTC), Litecoin(LTC), Bitcoin Cash(BCH), Ethereum(ETH/ERC20), and Ripple(XRP), GroestlCoin(GRS), DigiByte(DGB), DASH, Dogecoin(DOGE) are currently being supported by the hardware device in the current API release.

Installation in HTML

  1. Include the below library in a tag
<head>
  <script src="/path/to/SecuX-1.7.5.min.js"></script>
</head>
  1. npm module will be released soon!!!

Initialization in Your Javascript

Inline

const secux = SecuXConnect.default;

npm

import SecuXConnect from 'secux-connect'
// const SecuXConnect = require('secux-connect') // nodejs

Initialization in web environment

Set an interface connecting to our device.

Must be handling a user gesture to show a permission request.

<button id="webusb">webusb connect</button>
<script>
  console.log(SecuXConnect)
  const secux = SecuXConnect.default;
  const { TRANSPORT } = SecuXConnect

  document.getElementById("webusb").addEventListener("click",
    async () => {
      const secuxUSB = await secux.connectDevice(TRANSPORT.WEB_USB);
    })
</script>
//  Via the WebUSB interface
const secuxUSB = await secux.connectDevice(TRANSPORT.WEB_USB);

// Via the WebBLE interface
const secuxBLE = await secux.connectDevice(TRANSPORT.WEB_BLE);

Initialization in mobile environment(React Native)

// Via the RN BLE interface
const secuxRN = await SecuXConnect.connectDevice(TRANSPORT.REACT_NATIVE_BLE, { deviceId: deviceId })

// scan BLE device
const event = SecuXConnect.accessDevice()
event.listen(({device}) => {})
event.remove()

HD_KEY API (Bitcoin, Bitcoin Cash, Litecoin, Bitcoin TestBnet)

API returns a Promise via WebUSB (secuxUSB) or WebBLE (secuxBLE). The following instructions demonstrate the secuxUSB APIs.

Return Public Key

const { publicKey, chainCode, fingerPrint } = await secuxUSB.getPublicKey(path, isCompressed);

Parms

  1. path--required a string from BIP44.
  2. isCompressed--optional boolean. The default value is true.

Example

// return second address of first bitcoin account
await secuxUSB.getPublicKey("m/49'/0'/0'/0/0");

Result

publicKey: string
chainCode: string
fingerPrint: string

Return Generic Public Key(Support SECP256K1 and ED25519 curve)

const publicKey = await secuxUSB.getPublicKeyGeneric(path, ellipticCurve);

Parms

  1. path--required a string from BIP44.
  2. ellipticCurve--optional number. The default value is 0(SECP256K1)/1(ED25519).

Example

// return second address of first bitcoin account
await secuxUSB.getPublicKey("m/44'/0'/0'/0/0", 0);

Result

publicKey: Uint8Array

Return an Extended Public Key

const result = await secuxUSB.getXPublicKey(path, isSegwit);

Parms

  1. path--required a string from BIP44.
  2. isSegwit--optional boolen. The default value is false.

Example

// return second address of first bitcoin account
await secuxUSB.getXPublicKey("m/49'/0'/0'/0/0", true);

Result

extended public key: string
/* expected result:
xpub6ELHKXNimKbxMCytPh7EdC2QXx46T9qLDJWGnTraz1H9kMMFdcduoU69wh9cxP12wDxqAAfbaESWGYt5rREsX1J8iR2TEunvzvddduAPYcY */

Get a Specific Address from a Coin-based Account

ES6

const result = await secuxUSB.getAddress(path, coin, isSegwit);

Params

  1. path--required string from BIP44.
  2. coin--optional string. The default string is 'btc'.
    • option 1: 'btc'
    • option 2: 'bch'
    • option 3: 'ltc'
    • option 4: 'tbtc'
    • option 5: 'grs'
    • option 6: 'dgb'
    • option 7: 'dash'
    • option 8: 'doge'
  3. isSegwit--optional boolean. The default value is true.

Example

// return the second address of a bitcoin account
await secuxUSB.getAddress("m/44'/0'/0'/0/1", "btc");

Result

address: string;
// bitcoin segwit address: 394bPYZrhpmHToFvMymiSihowqv3yXmyYd

Sign a Transaction

ES6

const result = await secuxUSB.signTransaction(trx);

Params

  1. trx--required Object
    • inputs--required Array
    • outputs--required Array
    • coin--optional string. The default is 'btc'
      • option 1: 'btc'
      • option 2: 'bch'
      • option 3: 'ltc'
      • option 4: 'tbtc'
      • option 5: 'grs'
      • option 6: 'dgb'
      • option 7: 'dash'
      • option 8: 'doge'

Example

A P2PKH Transaction with outputs to an address of the new owner and an address of the current owner, called the change address (e.g. path: "m/44'/0'/0'/1/0")

await secuxUSB.signTransaction({
  inputs: [
    {
      path: "m/44'/0'/0'/0/0",
      prevVout: 0,
      prevTrxHash:
        "b035d89d4543ce5713c553d69431698116a822c57c03ddacf3f04b763d1999ac",
      scriptType: "P2PKH",
      satoshis: 152518
    }
  ],
  outputs: [
    {
      path: "m/44'/0'/0'/1/0",
      amount: 10000,
      scriptType: "P2PKH"
    },
    {
      address: "3CE6RLmdtv5McjjaUbdkkPssZFkH3EXUK8",
      amount: 200000
    }
  ],
  coin: "btc"
});

A P2SH_P2WPKH (Segwit) Transaction with outputs to an address of the new owner and an address of the current owner, called the change address (e.g. path: "m/44'/0'/0'/1/0")

await secuxUSB.signTransaction({
  inputs: [
    {
      path: "m/49'/0'/0'/0/0",
      prevVout: 0,
      prevTrxHash:
        "b035d89d4543ce5713c553d69431698116a822c57c03ddacf3f04b763d1999ac",
      scriptType: "P2SH_P2WPKH",
      satoshis: 152518
    },
    {
      path: "m/49'/0'/0'/0/0",
      prevVout: 1,
      prevTrxHash:
        "1c1e0ebf83c1fc1c75906b3c9be1913bd10e3ce59fb87252cc62e3fa7985d1e2",
      scriptType: "P2SH_P2WPKH",
      satoshis: 200000
    }
  ],
  outputs: [
    {
      path: "m/49'/0'/0'/1/0",
      amount: 100000,
      scriptType: "P2SH_P2WPKH"
    },
    {
      address: "1PX7MaEHU4e2Lrv1vSVdYzWBq6chNY6U7Z ",
      amount: 200000
    }
  ],
  coin: "btc"
});

Result

signature: string;
// 0100000001c2c22789b15db4c653b5ef9e146b95826dc9c6a8000000000000000000000000000000006a47304402202d7d22cc77b756cad27c90b4fb6feb6265df3552b613e2afca03e4e856d54b8602202082541bad4f85b128e090fe73c2a3a10f66c7b4c0121a5b42dab043bc2f8a53012103aaeb52dd7494c361049de67cc680e83ebcbbbdbeb13637d92cd845f70308af5effffffff02282300000000000017a914d28f8c8309322c085021f00861f27c973bff03b787e8030000000000001976a914bae93c8e7fb682422d24780b1a12a550eff428f288ac00000000

Transaction API (Ethereum)

API returns a Promise via WebUSB (secuxUSB) or WebBLE (secuxBLE). The following instructions demonstrate the secuxUSB APIs.

get ETH address

const signature = await secuxUSB.ethGetAddress(path)

Params

  1. path -- required string. ex: "m/44'/60'/0'/0/0"

Example

const signature = await secuxUSB.ethGetAddress("m/44'/60'/0'/0/0")

Result

address: string;
# 0x32Be343B94f860124dC4fEe278FDCBD38C102D88

sign ETH transaction

const signature = await secuxUSB.ethSignTransaction(
  network,
  path,
  {
    nonce
    gasPrice
    gasLimit
    receiver
    amount
    data
  }
)

Params

  1. network--required string. This value must be "all"
  2. path -- required string from BIP44.
  3. nonce -- required string.
  4. gasPrice -- required string.
  5. gasLimit -- required string.
  6. receiver -- required string.
  7. amount -- required string/number.
  8. data -- required string.

Example

const signature = await secuxUSB.ethSignTransaction("all", "m/44'/60'/0'/0/0", {
  nonce: "0x31",
  gasPrice: "0x04a817c800",
  gasLimit: "0x0249f0",
  receiver: "0xED7CAE6fC076EafbF2938BC371164b3dB09f09b2",
  amount: 100000, // '0x186a0'
  data: "0x0102"
});

Result

signature: string;

Transaction API(Ripple)

API returns a Promise via WebUSB (secuxUSB) or WebBLE (secuxBLE). The following instructions demonstrate the secuxUSB APIs.

get XRP address

const signature = await secuxUSB.xrpGetAddress(path)

Params

  1. path -- required string. ex: "m/44'/144'/0'/0/0"

Example

const signature = await secuxUSB.xrpGetAddress("m/44'/144'/0'/0/0")

Result

address: string;
# rNFSN9v7wCJ3G7YrzzALBVx5m21bLNnCSS

sign XRP transaction

const signature = await secuxUSB.xrpSignTransaction(
  path,
  {
    amount: string,
    destination: string,
    fee: string,
    destinationTag: number,
    sourceTag: number,
    sequence: number,
    ledgerVersion: number
  }
)

Params

  1. path -- required string from BIP44.
  2. amount -- required string.
  3. destination -- required string.
  4. fee -- optional string.
  5. destinationTag -- optional number.
  6. sourceTag -- optional number.
  7. sequence -- required number.
  8. ledgerVersion -- required number.

Example

const signature = await secuxUSB.xrpSignTransaction("m/44'/144'/0'/0/0", {
  amount: '10000000', // unit: drops
  destination: 'rE7QMaepphpCMGvGsQV1ZJPybg1wu4R82h',
  fee: '15',
  ledgerVersion: 8820051,
  sequence: 23
});

Result

signature: string;

Device API

Set the account information on a devcie, and display it on the screen of the device.

Create a New Account, or Renew the Account Information

Create/Renew account information on the device. Several ERC-20 accounts can be created. One ERC-20 account identifies one of ERC-20 tokens.

await secuxUSB.setDisplayedAccount(
  accountPath,
  balance,
  accountName,
  isToken,
  contractAddress,
  decimal
);

Params

  1. accountPath--required string. Ex: "m/44'/60'/0". Renew the existing account or creat a new account for the accountPath.
  2. balance--required string.
  3. accountName--required string.
  4. isToken--optional boolean for an ERC-20 account. The default value is false.
  5. contractAddress--optional string for an ERC-20 account.
  6. decimal--optional number for an ERC-20 account.

example

await secuxUSB.setDisplayedAccount(
  "m/44'/60'/0'",
  "1000",
  "ETH_Account",
  true,
  "0xB8c77482e45F1F44dE1745F52C74426C631bDD52",
  6
);

Delete An Existing Account

Delete an existing account from the device.

await secuxUSB.deleteDisplayedAccount(account_path);

Params

  1. account_path--required string. Ex: "m/44'/60'/0'".

Example

await secuxUSB.deleteDisplayedAccount("m/44'/60'/0'");

Get All Account Information

Return the records of all created accounts.

const result = await secuxUSB.getAllDisplayedAccount();

Result

result: Array[
  {
    path: string, // m/44'/0'/0'
    balance: string,
    accountName: string,
    contractAddress: string, // if not set, it would not contain this property
    decimal: string // if not set, it would not contain this property
  }
];

Get the Firmware Version

Return the Firmware Version

const { mcuFwVersion, seFwVersion } = await secuxUSB.getFwVersion();

Result

result: Object
{
  mcuFwVersion: String, // 1.13
  seFwVersion: String // 0177
}

Return the Wallet Type

There are two wallet types in the device, normal type and hidden type. Return 2 under the normal-type operation, or return 3 under the hidden-type operation.

const { mode, state } = await secuxUSB.getSeModeState();

Result

result: Object
{
  mode: Number, // 2
  state: Number
}

Check the initialization Status

Check if the device confirms the 24 words

const { status } = await secuxUSB.getWalletStatus();

Result

reault: Object;
{
  status: Number;
}

Display the Receive Address

Display the receive address on the screen of a device.

await secuxUSB.addressFromDevice(path, isCompressed, confirmOrNot);

Params

  1. path--required string. Ex: "m/44'/60'/0'/0/0"
  2. isCompressed - boolean. Default value is true.
  3. confirmOrNot boolean. Default value is true.

Example

await secuxUSB.addressFromDevice("m/44'/60'/0'/0/0");

Disconnect with The Device

await secuxUSB.close();

Event API

onDisconnected

Write an event happened as the device disconnection

secux.onDisconnected(params, event => {
  // do something here
});

Params

  • webusb
  • webble

onConnected

Write an event happened as the device connection

secux.onConnected(params, event => {
  // do something here
});

Params

  • webusb
  • webble

onOtpSend

Write a conditional event as the one-time password fails or passes during BLE pairing process. Return 'Success' or 'Fail' with the correct or an incorrent one-time password, repectively.

secux.onOtpSend(res => {
  // res = 'Success' || 'Fail'
  if (res === "Success") {
    // do something
  } else {
    // do something
  }
});