Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

@bnb-chain/javascript-sdk

Index

Classes

Other Functions

amino Functions

amino

js amino UnmarshalBinaryBare Functions

amino

js amino UnmarshalBinaryLengthPrefixed Functions

crypto Functions

Other Functions

Const DefaultBroadcastDelegate

DefaultBroadcastDelegate(this: BncClient, signedTx: Transaction): Promise‹object›

The default broadcast delegate which immediately broadcasts a transaction.

Parameters:

Name Type Description
this BncClient -
signedTx Transaction the signed transaction

Returns: Promise‹object›


Const DefaultSigningDelegate

DefaultSigningDelegate(this: BncClient, tx: Transaction, signMsg?: any): Promise‹Transaction

The default signing delegate which uses the local private key.

Parameters:

Name Type Description
this BncClient -
tx Transaction the transaction
signMsg? any the canonical sign bytes for the msg

Returns: Promise‹Transaction


Const LedgerSigningDelegate

LedgerSigningDelegate(ledgerApp: LedgerApp, preSignCb: function, postSignCb: function, errCb: function, hdPath: number[]): typeof DefaultSigningDelegate

The Ledger signing delegate.

Parameters:

ledgerApp: LedgerApp

preSignCb: function

▸ (preSignCb: Buffer): void

Parameters:

Name Type
preSignCb Buffer

postSignCb: function

▸ (pubKeyResp: PublicKey, sigResp: SignedSignature): void

Parameters:

Name Type
pubKeyResp PublicKey
sigResp SignedSignature

errCb: function

▸ (error: any): void

Parameters:

Name Type
error any

hdPath: number[]

Returns: typeof DefaultSigningDelegate


Const ab2hexstring

ab2hexstring(arr: any): string

Parameters:

Name Type
arr any

Returns: string

HEX string


Const ab2str

ab2str(buf: Uint8Array): void

Parameters:

Name Type
buf Uint8Array

Returns: void

ASCII string


Const calInputCoins

calInputCoins(inputs: Array‹object›, coins: Coin[]): void

sum corresponding input coin

Parameters:

Name Type Description
inputs Array‹object› -
coins Coin[]

Returns: void


Const calculateRandomNumberHash

calculateRandomNumberHash(randomNumber: string, timestamp: number): string

Computes sha256 of random number and timestamp

Parameters:

Name Type
randomNumber string
timestamp number

Returns: string

sha256 result


Const calculateSwapID

calculateSwapID(randomNumberHash: string, sender: string, senderOtherChain: string): string

Computes swapID

Parameters:

Name Type
randomNumberHash string
sender string
senderOtherChain string

Returns: string

sha256 result


Const checkCoins

checkCoins(coins: Coin[]): void

basic validation of coins

Parameters:

Name Type Description
coins Coin[]

Returns: void


Const checkNumber

checkNumber(value: BigSource, name: string): void

validate the input number.

Parameters:

Name Type Default Description
value BigSource -
name string "input number" -

Returns: void


Const checkOutputs

checkOutputs(outputs: Transfer[]): void

validate the input number.

Parameters:

Name Type Description
outputs Transfer[]

Returns: void


Const ensureHex

ensureHex(str: string): void

Throws an error if input is not hexstring.

Parameters:

Name Type Description
str string

Returns: void


Const hexXor

hexXor(str1: string, str2: string): string

XORs two hexstrings

Parameters:

Name Type Description
str1 string HEX string
str2 string HEX string

Returns: string

XOR output as a HEX string


Const hexstring2ab

hexstring2ab(str: string): Uint8Array‹›

Parameters:

Name Type Description
str string HEX string

Returns: Uint8Array‹›


Const hexstring2str

hexstring2str(hexstring: string): void

Parameters:

Name Type Description
hexstring string HEX string

Returns: void

ASCII string


Const int2hex

int2hex(num: number): string

convert an integer to big endian hex and add leading zeros

Parameters:

Name Type
num number

Returns: string


Const isHex

isHex(str: string): boolean

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

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

Parameters:

Name Type
str string

Returns: boolean


Const num2VarInt

num2VarInt(num: number): string

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

Parameters:

Name Type Description
num number The number

Returns: string

hexstring of the variable Int.


Const num2hexstring

num2hexstring(num: number, size: number, littleEndian: boolean): string

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

Parameters:

Name Type Default Description
num number - -
size number 1 The required size in bytes, eg 1 for Uint8, 2 for Uint16. Defaults to 1.
littleEndian boolean false Encode the hex in little endian form

Returns: string


Const reverseArray

reverseArray(arr: any[]): Uint8Array‹›

Reverses an array. Accepts arrayBuffer.

Parameters:

Name Type
arr any[]

Returns: Uint8Array‹›


Const reverseHex

reverseHex(hex: string): string

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

example reverseHex('abcdef') = 'efcdab'

Parameters:

Name Type Description
hex string HEX string

Returns: string

HEX string reversed in 2s.


Const sha256

sha256(hex: string): string

Computes a single SHA256 digest.

Parameters:

Name Type Description
hex string message to hash

Returns: string

hash output


Const sha256ripemd160

sha256ripemd160(hex: string): string

Computes a SHA256 followed by a RIPEMD160.

Parameters:

Name Type Description
hex string message to hash

Returns: string

hash output


Const sha3

sha3(hex: string): string

Computes a single SHA3 (Keccak) digest.

Parameters:

Name Type Description
hex string message to hash

Returns: string

hash output


Const str2ab

str2ab(str: string): Uint8Array‹›

Parameters:

Name Type Description
str string ASCII string

Returns: Uint8Array‹›


Const str2hexstring

str2hexstring(str: string): string

Parameters:

Name Type Description
str string ASCII string

Returns: string

HEX string


amino Functions

Const convertObjectToSignBytes

convertObjectToSignBytes(obj: any): Buffer‹›

Parameters:

Name Type Description
obj any -- {object}

Returns: Buffer‹›

bytes {Buffer}


Const encodeArrayBinary

encodeArrayBinary(fieldNum: number | undefined, arr: any[], isByteLenPrefix?: undefined | false | true): Buffer‹›

Parameters:

Name Type Description
fieldNum number | undefined object field index
arr any[] -
isByteLenPrefix? undefined | false | true -

Returns: Buffer‹›

bytes of array


Const encodeBinary

encodeBinary(val: any, fieldNum?: undefined | number, isByteLenPrefix?: undefined | false | true): any

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

Parameters:

Name Type
val any
fieldNum? undefined | number
isByteLenPrefix? undefined | false | true

Returns: any

binary of object.


Const encodeBinaryByteArray

encodeBinaryByteArray(bytes: Buffer): Buffer‹›

prefixed with bytes length

Parameters:

Name Type
bytes Buffer

Returns: Buffer‹›

with bytes length prefixed


Const encodeBool

encodeBool(b: boolean): any

encode bool

Parameters:

Name Type Description
b boolean

Returns: any


Const encodeNumber

encodeNumber(num: number): any

encode number

Parameters:

Name Type Description
num number

Returns: any


Const encodeObjectBinary

encodeObjectBinary(obj: any, isByteLenPrefix?: undefined | false | true): Buffer‹›

Parameters:

Name Type
obj any
isByteLenPrefix? undefined | false | true

Returns: Buffer‹›

with bytes length prefixed


Const encodeString

encodeString(str: string): Buffer‹›

encode string

Parameters:

Name Type Description
str string

Returns: Buffer‹›


Const encodeTime

encodeTime(value: string | Date): Buffer‹›

encode time

Parameters:

Name Type Description
value string | Date

Returns: Buffer‹›


Const marshalBinary

marshalBinary(obj: any): any

js amino MarshalBinary

Parameters:

Name Type Description
obj any

Returns: any


Const marshalBinaryBare

marshalBinaryBare(obj: any): any

js amino MarshalBinaryBare

Parameters:

Name Type Description
obj any

Returns: any


amino

js amino UnmarshalBinaryBare Functions

Const unMarshalBinaryBare

unMarshalBinaryBare(bytes: Buffer, type: any): any

Parameters:

Name Type
bytes Buffer
type any

Returns: any


amino

js amino UnmarshalBinaryLengthPrefixed Functions

Const unMarshalBinaryLengthPrefixed

unMarshalBinaryLengthPrefixed(bytes: Buffer, type: any): any

Parameters:

Name Type
bytes Buffer
type any

Returns: any


crypto Functions

Const checkAddress

checkAddress(address: string, hrp: string): boolean

Checks whether an address is valid.

Parameters:

Name Type Description
address string the bech32 address to decode
hrp string the prefix to check for the bech32 address

Returns: boolean


Const decodeAddress

decodeAddress(value: string): Buffer

Decodes an address in bech32 format.

Parameters:

Name Type Description
value string the bech32 address to decode

Returns: Buffer


Const encodeAddress

encodeAddress(value: string | Buffer, prefix: string, type: BufferEncoding): string

Encodes an address from input data bytes.

Parameters:

Name Type Default Description
value string | Buffer - the public key to encode
prefix string "tbnb" the address prefix
type BufferEncoding "hex" the output type (default: hex)

Returns: string


Const generateKeyStore

generateKeyStore(privateKeyHex: string, password: string): KeyStore

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

Parameters:

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

Returns: KeyStore

the keystore object.


Const generateMnemonic

generateMnemonic(): string

Generates mnemonic phrase words using random entropy.

Returns: string


Const generatePrivateKey

generatePrivateKey(len: number): string

Generates 32 bytes of random entropy

Parameters:

Name Type Default Description
len number PRIVKEY_LEN output length (default: 32 bytes)

Returns: string

entropy bytes hexstring


Const generatePubKey

generatePubKey(privateKey: Buffer): BasePoint

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

Parameters:

Name Type
privateKey Buffer

Returns: BasePoint

PubKey


Const generateRandomArray

generateRandomArray(length: number): ArrayBuffer

Generates an arrayBuffer filled with random bits.

Parameters:

Name Type Description
length number Length of buffer.

Returns: ArrayBuffer


Const generateSignature

generateSignature(signBytesHex: string, privateKey: string | Buffer): Buffer

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

Parameters:

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

Returns: Buffer

Signature. Does not include tx.


Const getAddressFromPrivateKey

getAddressFromPrivateKey(privateKeyHex: string, prefix?: undefined | string): string

Gets an address from a private key.

Parameters:

Name Type Description
privateKeyHex string the private key hexstring
prefix? undefined | string the address prefix

Returns: string


Const getAddressFromPublicKey

getAddressFromPublicKey(publicKeyHex: string, prefix?: undefined | string): string

Gets an address from a public key hex.

Parameters:

Name Type Description
publicKeyHex string the public key hexstring
prefix? undefined | string the address prefix

Returns: string


Const getPrivateKeyFromKeyStore

getPrivateKeyFromKeyStore(keystore: string, password: string): string

Gets a private key from a keystore given its password.

Parameters:

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

Returns: string


Const getPrivateKeyFromMnemonic

getPrivateKeyFromMnemonic(mnemonic: string, derive: boolean, index: number, password: string): string

Get a private key from mnemonic words.

Parameters:

Name Type Default Description
mnemonic string - the mnemonic phrase words
derive boolean true derive a private key using the default HD path (default: true)
index number 0 the bip44 address index (default: 0)
password string "" according to bip39

Returns: string

hexstring


Const getPublicKey

getPublicKey(publicKey: string): BasePoint‹›

Parameters:

Name Type Description
publicKey string Encoded public key

Returns: BasePoint‹›

public key hexstring


Const getPublicKeyFromPrivateKey

getPublicKeyFromPrivateKey(privateKeyHex: string): string

Calculates the public key from a given private key.

Parameters:

Name Type Description
privateKeyHex string the private key hexstring

Returns: string

public key hexstring


Const verifySignature

verifySignature(sigHex: string, signBytesHex: string, publicKeyHex: string): boolean

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

Parameters:

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

Returns: boolean