Skip to content

zbruceli/nkn-client-js-1

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nkn-client-js

JavaScript implementation of NKN client.

Send and receive data between any NKN clients without setting up a server.

Note: This is a client version of the NKN protocol, which can send and receive data but not relay data (mining). For node implementation which can mine NKN token by relaying data, please refer to nkn.

Note: This repository is in the early development stage and may not have all functions working properly. It should be used only for testing now.

Usage

For npm:

const nkn = require('./lib/nkn');

For browser, use dist/nkn.js or dist/nkn.min.js.

Create a client with a generated key pair:

const client = nkn();

Or with an identifier (used to distinguish different clients sharing the same key pair):

const client = nkn({
  identifier: 'any string',
});

Get client key pair:

console.log(client.key.privateKey, client.key.publicKey);

Create a client using an existing private key:

const client = nkn({
  identifier: 'any string',
  privateKey: 'cd5fa29ed5b0e951f3d1bce5997458706186320f1dd89156a73d54ed752a7f37',
});

Create a client using customized bootstrap RPC server (for getting node address):

const client = nkn({
  identifier: 'any string',
  seedRpcServerAddr: 'https://xxx',
});

Private key should be kept SECRET! Never put it in version control system like here.

Get client identifier:

console.log(client.identifier);

And client NKN address, which is used to receive data from other clients:

console.log(client.addr);

Listen for connection established:

client.on('connect', () => {
  console.log('Connection opened.');
});

Send data to other clients:

client.send(
  'another client address',
  'some message',
);

Receive data from other clients:

client.on('message', (src, payload) => {
  console.log(src, payload);
});

About

JavaScript implementation of NKN client demo

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%