Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

Modules

client
crypto
amino-decode
amino-encode
ledger
utils

Classes

Transaction

Creates a new transaction object.

client

client.BncClient

The Binance Chain client.

Kind: static class of client

new exports.BncClient(server, useAsyncBroadcast)

Param Type Default Description
server string Binance Chain public url
useAsyncBroadcast Boolean false use async broadcast mode, faster but less guarantees (default off)

bncClient.initChain() ⇒ Promise

Initialize the client with the chain's ID. Asynchronous.

Kind: instance method of BncClient

bncClient.chooseNetwork(network)

Sets the client network (testnet or mainnet).

Kind: instance method of BncClient

Param Type Description
network String Indicate testnet or mainnet

bncClient.setPrivateKey() ⇒ Promise

Sets the client's private key for calls made by this client. Asynchronous.

Kind: instance method of BncClient

bncClient.useAsyncBroadcast(useAsyncBroadcast) ⇒ BncClient

Use async broadcast mode. Broadcasts faster with less guarantees (default off)

Kind: instance method of BncClient
Returns: BncClient - this instance (for chaining)

Param Type Default
useAsyncBroadcast Boolean true

bncClient.setSigningDelegate(delegate) ⇒ BncClient

Sets the signing delegate (for wallet integrations).

Kind: instance method of BncClient
Returns: BncClient - this instance (for chaining)

Param Type
delegate function

bncClient.setBroadcastDelegate(delegate) ⇒ BncClient

Sets the broadcast delegate (for wallet integrations).

Kind: instance method of BncClient
Returns: BncClient - this instance (for chaining)

Param Type
delegate function

bncClient.useDefaultSigningDelegate() ⇒ BncClient

Applies the default signing delegate.

Kind: instance method of BncClient
Returns: BncClient - this instance (for chaining)

bncClient.useDefaultBroadcastDelegate() ⇒ BncClient

Applies the default broadcast delegate.

Kind: instance method of BncClient
Returns: BncClient - this instance (for chaining)

bncClient.useLedgerSigningDelegate(ledgerApp, preSignCb, postSignCb, errCb) ⇒ BncClient

Applies the Ledger signing delegate.

Kind: instance method of BncClient
Returns: BncClient - this instance (for chaining)

Param Type
ledgerApp function
preSignCb function
postSignCb function
errCb function

bncClient.transfer(fromAddress, toAddress, amount, asset, memo, sequence) ⇒ Promise

Transfer tokens from one address to another.

Kind: instance method of BncClient
Returns: Promise - resolves with response (success or fail)

Param Type Default Description
fromAddress String
toAddress String
amount Number
asset String
memo String optional memo
sequence Number optional sequence

bncClient.multiSend(fromAddress, outputs, memo, sequence) ⇒ Promise

Create and sign a multi send tx

Kind: instance method of BncClient
Returns: Promise - resolves with response (success or fail)

Param Type Default Description
fromAddress String
outputs Array
memo String optional memo
sequence Number optional sequence

Example

const outputs = [
{
  "to": "tbnb1p4kpnj5qz5spsaf0d2555h6ctngse0me5q57qe",
  "coins": [{
    "denom": "BNB",
    "amount": 10
  },{
    "denom": "BTC",
    "amount": 10
  }]
},
{
  "to": "tbnb1scjj8chhhp7lngdeflltzex22yaf9ep59ls4gk",
  "coins": [{
    "denom": "BTC",
    "amount": 10
  },{
    "denom": "BNB",
    "amount": 10
  }]
}]

bncClient.cancelOrder(fromAddress, symbol, refid, sequence) ⇒ Promise

Cancel an order.

Kind: instance method of BncClient
Returns: Promise - resolves with response (success or fail)

Param Type Default Description
fromAddress String
symbol String the market pair
refid String the order ID of the order to cancel
sequence Number optional sequence

bncClient.placeOrder(address, symbol, side, price, quantity, sequence, timeinforce) ⇒ Promise

Place an order.

Kind: instance method of BncClient
Returns: Promise - resolves with response (success or fail)

Param Type Default Description
address String
symbol String the market pair
side Number (1-Buy, 2-Sell)
price Number
quantity Number
sequence Number optional sequence
timeinforce Number 1 (1-GTC(Good Till Expire), 3-IOC(Immediate or Cancel))

bncClient._prepareTransaction(msg, stdSignMsg, address, sequence, memo) ⇒ Transaction

Prepare a serialized raw transaction for sending to the blockchain.

Kind: instance method of BncClient
Returns: Transaction - signed transaction

Param Type Default Description
msg Object the msg object
stdSignMsg Object the sign doc object used to generate a signature
address String
sequence Number optional sequence
memo String optional memo

bncClient.sendTransaction(tx, sync) ⇒ Promise

Broadcast a transaction to the blockchain.

Kind: instance method of BncClient
Returns: Promise - resolves with response (success or fail)

Param Type Description
tx signedTx signed Transaction object
sync Boolean use synchronous mode, optional

bncClient.sendRawTransaction(signedBz, sync) ⇒ Promise

Broadcast a raw transaction to the blockchain.

Kind: instance method of BncClient
Returns: Promise - resolves with response (success or fail)

Param Type Description
signedBz String signed and serialized raw transaction
sync Boolean use synchronous mode, optional

bncClient._sendTransaction(msg, stdSignMsg, address, sequence, memo, sync) ⇒ Promise

Broadcast a raw transaction to the blockchain.

Kind: instance method of BncClient
Returns: Promise - resolves with response (success or fail)

Param Type Default Description
msg Object the msg object
stdSignMsg Object the sign doc object used to generate a signature
address String
sequence Number optional sequence
memo String optional memo
sync Boolean use synchronous mode, optional

bncClient.getAccount(address) ⇒ Promise

get account

Kind: instance method of BncClient
Returns: Promise - resolves with http response

Param Type
address String

bncClient.getBalance(address) ⇒ Promise

get balances

Kind: instance method of BncClient
Returns: Promise - resolves with http response

Param Type Description
address String optional address

bncClient.getMarkets(offset, limit,) ⇒ Promise

get markets

Kind: instance method of BncClient
Returns: Promise - resolves with http response

Param Type Default Description
offset Number 0 from beggining, default 0
limit, Number max 1000 is default

bncClient.createAccount() ⇒ object

Creates a private key and returns it and its address.

Kind: instance method of BncClient
Returns: object - the private key and address in an object. { address, privateKey }

bncClient.createAccountWithKeystore(password)

Creates an account keystore object, and returns the private key and address.

Kind: instance method of BncClient

Param Type Description
password String { privateKey, address, keystore }

bncClient.createAccountWithMneomnic() ⇒ object

Creates an account from mnemonic seed phrase.

Kind: instance method of BncClient
Returns: object - { privateKey, address, mnemonic }

bncClient.recoverAccountFromKeystore(keystore, keystore)

Recovers an account from a keystore object.

Kind: instance method of BncClient

Param Type Description
keystore object object.
keystore string password. { privateKey, address }

bncClient.recoverAccountFromMnemonic(mneomnic)

Recovers an account from a mnemonic seed phrase.

Kind: instance method of BncClient

Param Type Description
mneomnic string { privateKey, address }

bncClient.recoverAccountFromPrivateKey(privateKey)

Recovers an account using private key.

Kind: instance method of BncClient

Param Type Description
privateKey String { privateKey, address }

bncClient.checkAddress(address) ⇒ Boolean

Validates an address.

Kind: instance method of BncClient

Param Type
address String

bncClient.getClientKeyAddress() ⇒ String

Returns the address for the current account if setPrivateKey has been called on this client.

Kind: instance method of BncClient

client.DefaultSigningDelegate ⇒ Transaction

The default signing delegate which uses the local private key.

Kind: static constant of client

Param Type Description
tx Transaction the transaction
signMsg Object the canonical sign bytes for the msg

client.DefaultBroadcastDelegate

The default broadcast delegate which immediately broadcasts a transaction.

Kind: static constant of client

Param Type Description
signedTx Transaction the signed transaction

client.LedgerSigningDelegate ⇒ function

The Ledger signing delegate.

Kind: static constant of client

Param Type
ledgerApp LedgerApp
function preSignCb
function postSignCb
function errCb

client.checkNumber

validate the input number.

Kind: static constant of client

Param Type
value Number

client~checkOutputs(outputs)

validate the input number.

Kind: inner method of client

Param Type
outputs Array

client~calInputCoins(inputs, coins)

sum corresponding input coin

Kind: inner method of client

Param Type
inputs Array
coins Array

crypto

crypto.decodeAddress

Decodes an address in bech32 format.

Kind: static constant of crypto

Param Type Description
value string the bech32 address to decode

crypto.checkAddress ⇒ boolean

Checks whether an address is valid.

Kind: static constant of crypto

Param Type Description
address string the bech32 address to decode

crypto.encodeAddress

Encodes an address from input data bytes.

Kind: static constant of crypto

Param Type Description
value string the public key to encode
prefix * the address prefix
type * the output type (default: hex)

crypto.generatePrivateKey ⇒ string

Generates 32 bytes of random entropy

Kind: static constant of crypto
Returns: string - entropy bytes hexstring

Param Type Description
len number output length (default: 32 bytes)

crypto.generateRandomArray ⇒ ArrayBuffer

Generates an arrayBuffer filled with random bits.

Kind: static constant of crypto

Param Type Description
length number Length of buffer.

crypto.getPublicKey ⇒ Elliptic.PublicKey

Kind: static constant of crypto
Returns: Elliptic.PublicKey - public key hexstring

Param Type Description
publicKey string Encoded public key

crypto.getPublicKeyFromPrivateKey ⇒ string

Calculates the public key from a given private key.

Kind: static constant of crypto
Returns: string - public key hexstring

Param Type Description
privateKeyHex string the private key hexstring

crypto.generatePubKey ⇒ Elliptic.PublicKey

PubKey performs the point-scalar multiplication from the privKey on the generator point to get the pubkey.

Kind: static constant of crypto
Returns: Elliptic.PublicKey - PubKey

Param Type
privateKey Buffer

crypto.getAddressFromPublicKey

Gets an address from a public key hex.

Kind: static constant of crypto

Param Type Description
publicKeyHex string the public key hexstring
prefix string the address prefix

crypto.getAddressFromPrivateKey

Gets an address from a private key.

Kind: static constant of crypto

Param Type Description
privateKeyHex string the private key hexstring

crypto.generateSignature ⇒ Buffer

Generates a signature (64 byte <r,s>) for a transaction based on given private key.

Kind: static constant of crypto
Returns: Buffer - Signature. Does not include tx.

Param Type Description
signBytesHex string Unsigned transaction sign bytes hexstring.
privateKey string | Buffer The private key.

crypto.verifySignature ⇒ Buffer

Verifies a signature (64 byte <r,s>) given the sign bytes and public key.

Kind: static constant of crypto
Returns: Buffer - Signature. Does not include tx.

Param Type Description
sigHex string The signature hexstring.
signBytesHex string Unsigned transaction sign bytes hexstring.
publicKeyHex string The public key.

crypto.generateKeyStore ⇒ object

Generates a keystore object (web3 secret storage format) given a private key to store and a password.

Kind: static constant of crypto
Returns: object - the keystore object.

Param Type Description
privateKeyHex string the private key hexstring.
password string the password.

crypto.getPrivateKeyFromKeyStore

Gets a private key from a keystore given its password.

Kind: static constant of crypto

Param Type Description
keystore string the keystore in json format
password string the password.

crypto.generateMnemonic

Generates mnemonic phrase words using random entropy.

Kind: static constant of crypto

crypto.validateMnemonic ⇒ bool

Validates mnemonic phrase words.

Kind: static constant of crypto
Returns: bool - validation result

Param Type Description
mnemonic string the mnemonic phrase words

crypto.getPrivateKeyFromMnemonic ⇒ string

Get a private key from mnemonic words.

Kind: static constant of crypto
Returns: string - hexstring

Param Type Description
mnemonic string the mnemonic phrase words
derive Boolean derive a private key using the default HD path (default: true)
index number the bip44 address index (default: 0)

amino-decode

amino-decode.unMarshalBinaryLengthPrefixed

js amino UnmarshalBinaryLengthPrefixed

Kind: static constant of amino-decode

Param Type
bytes Buffer
type Object

amino-decode.unMarshalBinaryBare

js amino UnmarshalBinaryBare

Kind: static constant of amino-decode

Param Type
bytes Buffer
type Object

amino-encode

amino-encode.encodeNumber

encode number

Kind: static constant of amino-encode

Param
num

amino-encode.encodeBool

encode bool

Kind: static constant of amino-encode

Param
b

amino-encode.encodeString

encode string

Kind: static constant of amino-encode

Param
str

amino-encode.encodeTime

encode time

Kind: static constant of amino-encode

Param
value

amino-encode.convertObjectToSignBytes ⇒ Buffer

Kind: static constant of amino-encode
Returns: Buffer - bytes

Param Type Description
obj object -

amino-encode.marshalBinary

js amino MarshalBinary

Kind: static constant of amino-encode

Param Type
obj Object

amino-encode.marshalBinaryBare

js amino MarshalBinaryBare

Kind: static constant of amino-encode

Param Type
obj Object

amino-encode.encodeBinary ⇒ Buffer

This is the main entrypoint for encoding all types in binary form.

Kind: static constant of amino-encode
Returns: Buffer - binary of object.

Param Type Description
js * data type (not null, not undefined)
field Number index of object
isByteLenPrefix Boolean

amino-encode.encodeBinaryByteArray ⇒ Buffer

prefixed with bytes length

Kind: static constant of amino-encode
Returns: Buffer - with bytes length prefixed

Param Type
bytes Buffer

amino-encode.encodeObjectBinary ⇒ Buffer

Kind: static constant of amino-encode
Returns: Buffer - with bytes length prefixed

Param Type
obj Object

amino-encode.encodeArrayBinary ⇒ Buffer

Kind: static constant of amino-encode
Returns: Buffer - bytes of array

Param Type Description
fieldNum Number object field index
arr Array
isByteLenPrefix Boolean

ledger

ledger~LedgerApp

Ledger app interface.

Kind: inner class of ledger

new LedgerApp(transport, interactiveTimeout, nonInteractiveTimeout)

Constructs a new LedgerApp.

Param Type Description
transport Transport Ledger Transport, a subclass of ledgerjs Transport.
interactiveTimeout Number The interactive (user input) timeout in ms. Default 45s.
nonInteractiveTimeout Number The non-interactive timeout in ms. Default 3s.

ledgerApp.getVersion()

Gets the version of the Ledger app that is currently open on the device.

Kind: instance method of LedgerApp
Throws:

  • Will throw Error if a transport error occurs, or if the firmware app is not open.

ledgerApp.publicKeySecp256k1(hdPath)

Gets the public key from the Ledger app that is currently open on the device.

Kind: instance method of LedgerApp
Throws:

  • Will throw Error if a transport error occurs, or if the firmware app is not open.
Param Type Description
hdPath array The HD path to use to get the public key. Default is [44, 714, 0, 0, 0]

ledgerApp.signSecp256k1(signBytes, hdPath)

Sends a transaction sign doc to the Ledger app to be signed.

Kind: instance method of LedgerApp
Throws:

  • Will throw Error if a transport error occurs, or if the firmware app is not open.
Param Type Description
signBytes Buffer The TX sign doc bytes to sign
hdPath array The HD path to use to get the public key. Default is [44, 714, 0, 0, 0]

ledgerApp.showAddress(hrp, hdPath)

Shows the user's address for the given HD path on the device display.

Kind: instance method of LedgerApp
Throws:

  • Will throw Error if a transport error occurs, or if the firmware app is not open.
Param Type Default Description
hrp string "bnb" The bech32 human-readable prefix
hdPath array The HD path to use to get the public key. Default is [44, 714, 0, 0, 0]

ledgerApp.getPublicKey(hdPath)

Gets the public key from the Ledger app that is currently open on the device.

Kind: instance method of LedgerApp
Throws:

  • Will throw Error if a transport error occurs, or if the firmware app is not open.
Param Type Description
hdPath array The HD path to use to get the public key. Default is [44, 714, 0, 0, 0]

ledgerApp.sign(signBytes, hdPath)

Sends a transaction sign doc to the Ledger app to be signed.

Kind: instance method of LedgerApp
Throws:

  • Will throw Error if a transport error occurs, or if the firmware app is not open.
Param Type Description
signBytes Buffer The TX sign doc bytes to sign
hdPath array The HD path to use to get the public key. Default is [44, 714, 0, 0, 0]

utils

utils.ab2str ⇒ string

Kind: static constant of utils
Returns: string - ASCII string

Param Type
buf arrayBuffer

utils.str2ab ⇒ arrayBuffer

Kind: static constant of utils

Param Type Description
str string ASCII string

utils.hexstring2ab ⇒ Array.<number>

Kind: static constant of utils

Param Type Description
str string HEX string

utils.ab2hexstring ⇒ string

Kind: static constant of utils
Returns: string - HEX string

Param Type
arr arrayBuffer

utils.str2hexstring ⇒ string

Kind: static constant of utils
Returns: string - HEX string

Param Type Description
str string ASCII string

utils.hexstring2str ⇒ string

Kind: static constant of utils
Returns: string - ASCII string

Param Type Description
hexstring string HEX string

utils.int2hex ⇒ string

convert an integer to big endian hex and add leading zeros

Kind: static constant of utils

Param Type
num Number

utils.num2hexstring ⇒ string

Converts a number to a big endian hexstring of a suitable size, optionally little endian

Kind: static constant of utils

Param Type Description
num Number
size Number The required size in bytes, eg 1 for Uint8, 2 for Uint16. Defaults to 1.
littleEndian Boolean Encode the hex in little endian form

utils.num2VarInt ⇒ string

Converts a number to a variable length Int. Used for array length header

Kind: static constant of utils
Returns: string - hexstring of the variable Int.

Param Type Description
num Number The number

utils.hexXor ⇒ string

XORs two hexstrings

Kind: static constant of utils
Returns: string - XOR output as a HEX string

Param Type Description
str1 string HEX string
str2 string HEX string

utils.reverseArray ⇒ Uint8Array

Reverses an array. Accepts arrayBuffer.

Kind: static constant of utils

Param Type
arr Array

utils.reverseHex ⇒ string

Reverses a HEX string, treating 2 chars as a byte.

Kind: static constant of utils
Returns: string - HEX string reversed in 2s.

Param Type Description
hex string HEX string

Example

reverseHex('abcdef') = 'efcdab'

utils.isHex ⇒ boolean

Checks if input is a hexstring. Empty string is considered a hexstring.

Kind: static constant of utils

Param Type
str string

Example

isHex('0101') = true
isHex('') = true
isHex('0x01') = false

utils.ensureHex

Throws an error if input is not hexstring.

Kind: static constant of utils

Param Type
str string

utils.sha256ripemd160 ⇒ string

Computes a SHA256 followed by a RIPEMD160.

Kind: static constant of utils
Returns: string - hash output

Param Type Description
hex string message to hash

utils.sha256 ⇒ string

Computes a single SHA256 digest.

Kind: static constant of utils
Returns: string - hash output

Param Type Description
hex string message to hash

utils.sha3 ⇒ string

Computes a single SHA3 (Keccak) digest.

Kind: static constant of utils
Returns: string - hash output

Param Type Description
hex string message to hash

Transaction

Creates a new transaction object.

Kind: global class
Properties

Name Type Description
raw Buffer The raw vstruct encoded transaction

new Transaction(type)

Param Type Description
data.account_number Number account number
data.chain_id String bnbChain Id
data.memo String transaction memo
type String transaction type
data.msg Object object data of tx type
data.sequence Number transaction counts

Example

var rawTx = {
  account_number: 1,
  chain_id: 'bnbchain-1000',
  memo: '',
  msg: {},
  type: 'NewOrderMsg',
  sequence: 29,
};
var tx = new Transaction(rawTx);

transaction.getSignBytes(concrete) ⇒ Buffer

generate the sign bytes for a transaction, given a msg

Kind: instance method of Transaction

Param Type Description
concrete Object msg object

transaction.addSignature(pubKey, signature) ⇒ Transaction

attaches a signature to the transaction

Kind: instance method of Transaction

Param Type
pubKey Elliptic.PublicKey
signature Buffer

transaction.sign(privateKey, concrete) ⇒ Transaction

sign transaction with a given private key and msg

Kind: instance method of Transaction

Param Type Description
privateKey string private key hex string
concrete Object msg object

transaction.serialize(opts)

encode signed transaction to hex which is compatible with amino

Kind: instance method of Transaction

Param Type Description
opts object msg field

transaction._serializePubKey(unencodedPubKey) ⇒ Buffer

serializes a public key in a 33-byte compressed format.

Kind: instance method of Transaction

Param Type
unencodedPubKey Elliptic.PublicKey