0.0.9 • Published 6 years ago

nano-node v0.0.9

Weekly downloads
42
License
MIT
Repository
github
Last release
6 years ago

Node.js Nano Node

Build Status

Partial (light, leech) Node for Nano currency (Formerly Raiblocks) for Node.js.

Examples:

  • Pending: How to listen for new pending blocks on a specific account
  • Receive: How to publish a block
  • Pull: How to fetch an account blockchain history
  • Votes: How to listen for representative votes
  • State Blocks: How to listen for state blocks

Table of Contents

Installation

npm install --save nano-node

class NanoNode extends EventEmitter

Events Emitted

NameListener ArgumentsDescription
messagemsg, rinfoNetwork message received
blockblock, rinfoBlock received (messages of publish type)
votemsg, rinfoVote received (messages of confirm_ack type)
readyNoneServer is listening
errorerrorAn error has occurred

Properties

May be set after construction.

NameDescription
peersArray of strings containing hostname concatenated with a colon and the portDefault: ['rai.raiblocks.net:7075']
maxPeersMaximum number of latest peers to publish new messagesDefault: 200
minimalConfirmAckParsing and validating each confirm_ack message as it arrives is very compute intensive due to the 2 blake2b hashes calculated on each receive. Set this value to false to parse and validate comfirm_ack messages. By default, (true) only the account public key is parsed.
tcpTimeoutDuration to wait when performing TCP operations in millisecondsDefault: 4000

constructor(port)

  • port <Integer> Optional, random if unspecified

Create a new listening UDP service.

publish(msg, accountKey, callback)

  • msg <Object> Required, message definition, supports types keepalive, publish, confirm_req. May also pass fully rendered messages as Buffer.
  • accountKey <String> If publish message and no signature provided, pass account private key as hex string for block signing.
  • callback <Function> Optional, callback function

Publish a message to known peers. Known peers are managed automatically. Upon receiving a message, the peer is added to the top of the list. Up to maxPeers peers are kept.

Returns only on publish and confirm_req messages: hex block hash

Publishing regular keepalive messages is important to continure receiving messages:

const NanoNode = require('nano-node');
const node = new NanoNode();
setInterval(() => {
  console.log('Sending keepalive to ', node.peers.length, 'peers...');
  node.publish({type: 'keepalive'});
}, 30000);

Message Properties

NameDefaultTypeDescription
typeNoneStringRequired, keepalive or publish
mainnettrueBooleanOptional, True (default) for mainnet, false for testnet
versionMax7IntegerOptional
versionUsing7IntegerOptional
versionMin1IntegerOptional
extensions0IntegerOptional, overwritten with block type for publish messages
bodyNoneObjectRequired for publish messages

Publish Body Object Properties

NameRequired TypesTypeDescription
typeAllStringsend, receive, open, change, state
previoussend, receive, change, state64 character hex stringHash of previous block in account
destinationsend64 character hex stringAccount public key of recipient
balancesend, state32 character hex stringNew balance of account
sourcereceive, open64 character hex stringHash of pending send block
representativeopen, change, state64 character hex stringPublic key of representative account to assign
accountopen, state64 character hex stringPublic key of the current account
linkstate64 character hex stringPublic key of the current account
signatureOptional128 character hex stringPass precomputed signature in this property. Otherwise, pass accountKey argument for block signing.
workAll16 character hex stringRequired for all block types, calculated from account public key for open type blocks, previous block hash for all other block types. See raiblocks-pow NPM package for generating this value.

fetchAccount(publicKey, callback)

  • publicKey <String> Required, account public key to fetch block history
  • callback <Function> error, result

Connect to known peers over TCP and send a bulk_pull message for a single account. Wait until timeout from tcpTimeout property, then determine the longest valid chain returned.

Without a full lattice database, the balance of an account can not be determined unless the frontier block is a send block.

Result object properties:

NameTypeDescription
blocksArrayTransaction history sorted newest to oldest
matchProportionNumberBetween 0 and 1 indicating the proportion of peer responses that match this chain. A value of 1 means all responding peers agree on this length.
returnCountNumberCount of peer responses returned before timeout

Static parseMessage(buf, minimalConfirmAck)

  • buf <Buffer> Required, full UDP message
  • minimalConfirmAck <Boolean> Optional, default: true. Only parse account value of vote (confirm_ack) messages

Returns an object with the properties of the message

Static renderMessage(msg, accountKey)

Useful for obtaining a block's hash without publishing it yet.

  • msg <Object> Required, message properties as described above
  • accountKey <String> Required to sign blocks for publish, confirm_req messages, otherwise provide signature property

Returns an object { message: <Buffer>, hash: <String|null> } hash is block hash if available

Static keyFromAccount(account)

  • account <String> Required, account address to convert

Return the public key for a given address

Static accountFromKey(key)

  • key <String> Required, public key to convert

Return the address for a given account public key

Static accountPair(seed, index)

  • seed <String|Buffer> Required, wallet seed as 32 byte Buffer or 64 character hex string
  • index <Number> Required, 32-bit unsigned integer specifying account index

Returns an object {privateKey: <String>, publicKey: <String>, address: <string>}

License

MIT

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago