Typescript library with support for the integration of BSC for third party application, include the functionalities of generation of addresses and signed transactions.
npm install @coolwallet/bsc
import BSC from '@coolwallet/bsc'
import { crypto } from '@coolwallet/core';
import { createTransport } from '@coolwallet/transport-web-ble';
const bsc = new BSC();
const transport = await createTransport();
const { privateKey: appPrivateKey } = crypto.key.generateKeyPair();
const appId = 'appId that had been registered by wallet';
const addressIndex = 0;
const address = await bsc.getAddress(transport, appPrivateKey, appId, addressIndex);
const transaction = {
nonce: "0x87",
gasPrice: "0x12a05f200",
gasLimit: "0xcf08",
to: "0x8A1628c2397F6cA75579A45E81EE3e17DF19720e",
value: "xe8d4a51000",
data: "",
chainId: 56
}
const signTxData = {
transport,
appPrivateKey,
appId,
transaction,
addressIndex
}
const normalTx = await bsc.signTransaction(signTxData);
const bep20Transaction = {
nonce: "0x87",
gasPrice: "0x12a05f200",
gasLimit: "0x5208",
to: "0x55d398326f99059ff775485246999027b3197955",
value: "0x0",
data: "0xa9059cbb000000000000000000000000BDCc4DBd6",
chainId: 56,
option: {
symbol: "USDT",
unit: "18"
}
}
const bep20SignTxData = {
transport,
appPrivateKey,
appId,
transaction: bep20Transaction,
addressIndex
}
const bep20Tx = await bsc.signBEP20Transaction(bep20SignTxData);
The address generated is compatible to BIP44 with account and change set to 0, which means calling getAddress
with addressIndex = i
will get the address of folllowing BIP44 path:
m/44'/60'/0'/0/{i}
In the design of current hardware, we only support path m/44'/60'/0'/0/{i}
for speed optimization. This might change in the future and we will then open a more general interface to deal with custom path.
async getAddress(
transport: types.Transport,
appPrivateKey: string,
appId: string,
addressIndex: number
): Promise<string>
Arg | Description | Type | Required |
---|---|---|---|
transport | Object to communicate with CoolWallet device | Transport | True |
appPrivateKey | Private key for the connected application | string | True |
appId | ID for the connected application | string | True |
addressIndex | The from address index in BIP44 derivation | number | True |
Sign BSC Transaction. If the transaction has non-empty data
field, the card will display SMART
instead of transfering amount.
async signTransaction(signTxData: types.signTx)
Arg | Description | Type | Required |
---|---|---|---|
transport | Object to communicate with CoolWallet device | Transport | True |
appPrivateKey | Private key for the connected application | string | True |
appId | ID for the connected application | string | True |
transaction | Essential information/property for BSC Transaction | Transaction | True |
addressIndex | The from address index in BIP44 derivation | number | True |
publicKey | Public key of the from address | string | True |
confirmCB | Callback of confirmation data to the connected application | Function | False |
authorizedCB | Callback of authorized transaction data to the connected application | Function | False |
Perform BSC personal_sign
.
async signMessage(signMsgData: types.signMsg): Promise<string>
Arg | Description | Type | Required |
---|---|---|---|
transport | Object to communicate with CoolWallet device | Transport | True |
appPrivateKey | Private key for the connected application | string | True |
appId | ID for the connected application | string | True |
message | Message to sign | string | True |
addressIndex | The from address index in BIP44 derivation | number | True |
confirmCB | Callback of confirmation data to the connected application | Function | False |
authorizedCB | Callback of authorized data to the connected application | Function | False |
Perform BSC sign_typed_data
.
async signTypedData(typedData: types.signTyped): Promise<string>
Arg | Description | Type | Required |
---|---|---|---|
transport | Object to communicate with CoolWallet device | Transport | True |
appPrivateKey | Private key for the connected application | string | True |
appId | ID for the connected application | string | True |
typedData | Typed structured data to be signed | any | True |
addressIndex | The from address index in BIP44 derivation | number | True |
confirmCB | Callback of confirmation data to the connected application | Function | False |
authorizedCB | Callback of authorized data to the connected application | Function | False |