nkn-sdk

1.2.5

Client

NKN client that sends data to and receives data from other NKN clients. Typically you might want to use MultiClient for better reliability and lower latency.

new Client(options: Object)
Parameters
options (Object = {}) Client configuration
Name Description
options.seed string (default undefined) Secret seed (64 hex characters). If empty, a random seed will be used.
options.identifier string (default undefined) Identifier used to differentiate multiple clients sharing the same secret seed.
options.reconnectIntervalMin number (default 1000) Minimal reconnect interval in ms.
options.reconnectIntervalMax number (default 64000) Maximal reconnect interval in ms.
options.responseTimeout number (default 5000) Message response timeout in ms. Zero disables timeout.
options.msgHoldingSeconds number (default 0) Maximal message holding time in second. Message might be cached and held by node up to this duration if destination client is not online. Zero disables cache.
options.encrypt boolean (default true) Whether to end to end encrypt message.
options.rpcServerAddr string (default 'https://mainnet-rpc-node-0001.nkn.org/mainnet/api/wallet') RPC server address used to join the network.
options.tls boolean (default undefined) Force to use wss instead of ws protocol. If not defined, wss will only be used in https location.
options.worker (boolean | function) (default false) Whether to use web workers (if available) to compute signatures. Can also be a function that returns web worker. Typically you only need to set it to a function if you import nkn-sdk as a module and are NOT using browserify or webpack worker-loader to bundle js file. The worker file is located at lib/worker/webpack.worker.js .
Instance Members
addr
close()
deleteName(name, options)
getBalance(address)
getLatestBlock()
getNonce(address, options)
getPublicKey()
getRegistrant(name)
getSeed()
getSubscribers(topic, options)
getSubscribersCount(topic)
getSubscription(topic, subscriber)
identifier
isClosed
isFailed
isReady
on(evt, func)
onConnect(func)
onConnectFailed(func)
onMessage(func)
onWsError(func)
publish(topic, data, options)
registerName(name, options)
send(dest, data, options)
sendTransaction(txn)
subscribe(topic, duration, identifier, meta, options)
transferName(name, recipient, options)
transferTo(toAddress, amount, options)
unsubscribe(topic, identifier, options)

MultiClient

NKN client that sends data to and receives data from other NKN clients.

new MultiClient(options: Object)
Parameters
options (Object = {}) Client configuration
Name Description
options.seed string (default undefined) Secret seed (64 hex characters). If empty, a random seed will be used.
options.identifier string (default undefined) Identifier used to differentiate multiple clients sharing the same secret seed.
options.reconnectIntervalMin number (default 1000) Minimal reconnect interval in ms.
options.reconnectIntervalMax number (default 64000) Maximal reconnect interval in ms.
options.responseTimeout number (default 5000) Message response timeout in ms. Zero disables timeout.
options.msgHoldingSeconds number (default 0) Maximal message holding time in second. Message might be cached and held by node up to this duration if destination client is not online. Zero disables cache.
options.encrypt boolean (default true) Whether to end to end encrypt message.
options.rpcServerAddr string (default 'https://mainnet-rpc-node-0001.nkn.org/mainnet/api/wallet') RPC server address used to join the network.
options.tls boolean (default undefined) Force to use wss instead of ws protocol. If not defined, wss will only be used in https location.
options.worker (boolean | function) (default false) Whether to use web workers (if available) to compute signatures. Can also be a function that returns web worker. Typically you only need to set it to a function if you import nkn-sdk as a module and are NOT using browserify or webpack worker-loader to bundle js file. The worker file is located at lib/worker/webpack.worker.js .
options.numSubClients number (default 3) Number of sub clients to create.
options.originalClient boolean (default false) Whether to create client with no additional identifier prefix added. This client is not counted towards sub clients controlled by options.numSubClients .
options.msgCacheExpiration number (default 300000) Message cache expiration time in ms. This cache is used to remove duplicate messages received by different clients.
options.sessionConfig Object (default {}) Session configuration
Instance Members
addr
clients
close()
defaultClient
deleteName(name, options)
dial(remoteAddr, options)
getBalance(address)
getLatestBlock()
getNonce(address, options)
getPublicKey()
getRegistrant(name)
getSeed()
getSubscribers(topic, options)
getSubscribersCount(topic)
getSubscription(topic, subscriber)
identifier
isClosed
isFailed
isReady
listen(addrs)
on(evt, func)
onConnect(func)
onConnectFailed(func)
onMessage(func)
onSession(func)
onWsError(func)
publish(topic, data, options)
readyClientIDs()
registerName(name, options)
send(dest, data, options)
sendTransaction(txn)
sendWithClient(clientID, dest, data, options)
subscribe(topic, duration, identifier, meta, options)
transferName(name, recipient, options)
transferTo(toAddress, amount, options)
unsubscribe(topic, identifier, options)

Wallet

NKN client that sends data to and receives data from other NKN clients.

new Wallet(options: Object)
Parameters
options (Object = {}) Wallet options.
Name Description
options.seed string (default undefined) Secret seed (64 hex characters). If empty, a random seed will be used.
options.password string Wallet password.
options.rpcServerAddr string (default 'https://mainnet-rpc-node-0001.nkn.org/mainnet/api/wallet') RPC server address.
options.iv string (default undefined) AES iv, typically you should use Wallet.fromJSON instead of this field.
options.masterKey string (default undefined) AES master key, typically you should use Wallet.fromJSON instead of this field.
options.worker (boolean | function) (default false) Whether to use web workers (if available) to compute signatures. Can also be a function that returns web worker. Typically you only need to set it to a function if you import nkn-sdk as a module and are NOT using browserify or webpack worker-loader to bundle js file. The worker file is located at lib/worker/webpack.worker.js .
Static Members
fromJSON(walletJson, options)
getBalance(address, options)
getLatestBlock(options)
getNonce(address, options)
getRegistrant(name, options)
getSubscribers(topic, options)
getSubscribersCount(topic, options)
getSubscription(topic, subscriber, options)
publicKeyToAddress(publicKey)
sendTransaction(txn, options)
verifyAddress(addr)
Instance Members
address
createOrUpdateNanoPay(toAddress, amount, expiration, id, options)
deleteName(name, options)
getBalance(address)
getLatestBlock()
getNonce(address, options)
getPublicKey()
getRegistrant(name)
getSeed()
getSubscribers(topic, options)
getSubscribersCount(topic)
getSubscription(topic, subscriber)
registerName(name, options)
sendTransaction(txn)
subscribe(topic, duration, identifier, meta, options)
toJSON()
transferName(name, recipient, options)
transferTo(toAddress, amount, options)
unsubscribe(topic, identifier, options)
verifyPassword(password, options)
version

Amount

Amount of NKN tokens. See documentation at decimal.js.

new Amount()

Extends Decimal

ConnectFailedHandler

Connect Failed handler function type.

ConnectFailedHandler

Type: function (): void

ConnectHandler

Connect handler function type.

ConnectHandler

Type: function ({addr: string}): void

CreateTransactionOptions

Create transaction options type.

CreateTransactionOptions

Type: {fee: (number | string | common.Amount | null | void), attrs: string?, buildOnly: boolean?}

Properties
fee ((number | string)?) : Transaction fee.
attrs (string?) : Transaction attributes, cannot exceed 100 bytes.
buildOnly (boolean?) : Whether to only build transaction but not send it.

Destination

One or multiple NKN address type. Each NKN address should either be the form of 'identifier.publicKey', or a name registered using wallet.

Destination

Type: (string | Array<string>)

DialOptions

Dial session options type.

DialOptions

Type: {dialTimeout: number?, sessionConfig: {}?}

Properties
dialTimeout (number?) : Dial timeout in ms. Zero disables timeout.
sessionConfig ({}?)

Message

Message type.

Message

Type: {src: string, payload: MessageData, payloadType: common.pb.payloads.PayloadType, isEncrypted: boolean, messageId: Uint8Array, noReply: boolean}

Properties
src (string) : Message sender address.
payload (MessageData) : Message payload.
payloadType (nkn.pb.payloads.PayloadType) : Message payload type.
isEncrypted (boolean) : Whether message is end to end encrypted.
messageId (Uint8Array) : Unique message ID.
noReply (boolean) : Indicating no reply should be sent back as sender will not process it.

MessageData

Message data type.

MessageData

Type: (Uint8Array | string)

MessageHandler

Message handler function type.

MessageHandler

Type: function (Message): (ReplyData | false | void | Promise<(ReplyData | false | void)>)

PublishOptions

Publish message options type.

PublishOptions

Type: {encrypt: boolean?, msgHoldingSeconds: number?, messageId: Uint8Array?, replyToId: Uint8Array?, txPool: boolean?, offset: number?, limit: number?}

Properties
txPool (boolean?) : Whether to send message to subscribers whose subscribe transaction is still in txpool. Enabling this will cause subscribers to receive message sooner after sending subscribe transaction, but might affect the correctness of subscribers because transactions in txpool is not guaranteed to be packed into a block.
encrypt (boolean?) : Whether to end to end encrypt message.
msgHoldingSeconds (number?) : Maximal message holding time in second. Message might be cached and held by node up to this duration if destination client is not online. Zero disables cache.
messageId (Uint8Array?)
replyToId (Uint8Array?)
offset (number?)
limit (number?)

ReplyData

Reply data type, null means ACK instead of reply is received.

ReplyData

Type: (MessageData | null)

SendOptions

Send message options type.

SendOptions

Type: {responseTimeout: number?, encrypt: boolean?, msgHoldingSeconds: number?, noReply: boolean?, messageId: Uint8Array?, replyToId: Uint8Array?}

Properties
responseTimeout (number?) : Message response timeout in ms. Zero disables timeout.
encrypt (boolean?) : Whether to end to end encrypt message.
msgHoldingSeconds (number?) : Maximal message holding time in second. Message might be cached and held by node up to this duration if destination client is not online. Zero disables cache.
noReply (boolean?) : Do not allocate any resources to wait for reply. Returned promise will resolve with null immediately when send success.
messageId (Uint8Array?)
replyToId (Uint8Array?)

SessionHandler

Accept session handler function type.

SessionHandler

Type: function (ncp.Session): void

TransactionOptions

Transaction options type.

TransactionOptions

Type: any

Properties
fee ((number | string)?) : Transaction fee.
attrs (string?) : Transaction attributes, cannot exceed 100 bytes.
buildOnly (boolean?) : Whether to only build transaction but not send it.
nonce (number?) : Transaction nonce, will get from RPC node if not provided.

TxnOrHash

Transaction hash if options.buildOnly=false, otherwise the transaction object.

TxnOrHash

Type: (string | common.pb.transaction.Transaction)

WsErrorHandler

Websocket error handler function type.

WsErrorHandler

Type: function (Event): void