All Projects → nknorg → Nkn Client Js

nknorg / Nkn Client Js

Licence: apache-2.0
[Deprecated, use nkn-sdk-js instead] JavaScript implementation of NKN client

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Nkn Client Js

Nkn
Official Go implementation of NKN full node.
Stars: ✭ 287 (+441.51%)
Mutual labels:  blockchain, p2p, distributed-systems
Ebookcoin
Ebookcoin has been updated to DDN Blockchain,please get it from https://github.com/ddnlink/ddn
Stars: ✭ 789 (+1388.68%)
Mutual labels:  blockchain, p2p
Lbry Sdk
The LBRY SDK for building decentralized, censorship resistant, monetized, digital content apps.
Stars: ✭ 7,169 (+13426.42%)
Mutual labels:  blockchain, p2p
Abci Host
Clojure host/server for Tendermint's ABCI protocol.
Stars: ✭ 18 (-66.04%)
Mutual labels:  blockchain, distributed-systems
Monero
Monero: the secure, private, untraceable cryptocurrency
Stars: ✭ 6,503 (+12169.81%)
Mutual labels:  blockchain, p2p
Node
Mysterium Network Node - official implementation of distributed VPN network (dVPN) protocol
Stars: ✭ 681 (+1184.91%)
Mutual labels:  blockchain, distributed-systems
Blockchain Reading List
Blockchain Manchester Meetups, Talks and Reading List
Stars: ✭ 17 (-67.92%)
Mutual labels:  blockchain, distributed-systems
Tendermint
⟁ Tendermint Core (BFT Consensus) in Go
Stars: ✭ 4,491 (+8373.58%)
Mutual labels:  blockchain, distributed-systems
Go Ethereum
Official Go implementation of the Ethereum protocol
Stars: ✭ 34,169 (+64369.81%)
Mutual labels:  blockchain, p2p
Lethean Vpn
Lethean Virtual Private Network (VPN)
Stars: ✭ 29 (-45.28%)
Mutual labels:  blockchain, distributed-systems
Enigma P2p
The Enigma Worker Peer-to-Peer (P2P) package written in Node.js based on libp2p-js
Stars: ✭ 31 (-41.51%)
Mutual labels:  blockchain, p2p
Firo
The privacy-focused cryptocurrency
Stars: ✭ 528 (+896.23%)
Mutual labels:  blockchain, p2p
Iotex Core
Official implementation of IoTeX blockchain protocol in Go.
Stars: ✭ 505 (+852.83%)
Mutual labels:  blockchain, distributed-systems
Ocsystem
🚀ONLYCHAIN blockchain underlying system-OCSystem
Stars: ✭ 713 (+1245.28%)
Mutual labels:  blockchain, p2p
Awesome Ethereum
⚡️ Awesome Ethereum Resources
Stars: ✭ 459 (+766.04%)
Mutual labels:  blockchain, p2p
Filenation
The simplest way to send your files around the world using IPFS. ✏️ 🗃
Stars: ✭ 805 (+1418.87%)
Mutual labels:  blockchain, p2p
Dbfs
Distributed Blockchain-based File Storage 📡
Stars: ✭ 45 (-15.09%)
Mutual labels:  blockchain, distributed-systems
Swarm
swarm docs
Stars: ✭ 403 (+660.38%)
Mutual labels:  blockchain, p2p
Decentralized Internet
A SDK/library for decentralized web and distributing computing projects
Stars: ✭ 406 (+666.04%)
Mutual labels:  blockchain, p2p
Lnbook
Mastering the Lightning Network (LN)
Stars: ✭ 931 (+1656.6%)
Mutual labels:  blockchain, p2p

Note: This repo is deprecated in favor of nkn-sdk-js.

CircleCI Status

nkn-client-js

EnglishРусский

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.

Usage

For npm:

npm install nkn-client

And then in your code:

const nkn = require('nkn-client');

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.seed, client.key.privateKey, client.key.publicKey);

Create a client using an existing secret seed:

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

Secret seed should be kept SECRET! Never put it in version control system like here.

By default the client will use bootstrap RPC server (for getting node address) provided by NKN. Any NKN full node can serve as a bootstrap RPC server. To create a client using customized bootstrap RPC server:

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

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 text message to other clients:

client.send(
  'another client address',
  'hello world!',
);

You can also send byte array directly:

client.send(
  'another client address',
  Uint8Array.from([1,2,3,4,5]),
);

Or publish text message to a topic (subscribe is done through nkn-wallet-js):

client.publish(
  'topic',
  'hello world!',
);

Receive data from other clients:

// can also be async (src, payload, payloadType, encrypt) => {}
client.on('message', (src, payload, payloadType, encrypt) => {
  if (payloadType === nkn.PayloadType.TEXT) {
    console.log('Receive text message:', src, payload);
  } else if (payloadType === nkn.PayloadType.BINARY) {
    console.log('Receive binary message:', src, payload);
  }
  console.log('Message is', encrypt ? 'encrypted' : 'unencrypted');
});

If a valid data (string or Uint8Array) is returned at the end of the handler, the data will be sent back to sender as response:

client.on('message', (src, payload, payloadType, encrypt) => {
  return 'Well received!';
  // You can also return a byte array:
  // return Uint8Array.from([1,2,3,4,5]);
});

Note that if multiple onmessage handlers are added, the result returned by the first handler (in the order of being added) will be sent as response.

The send method will return a Promise that will be resolved when sender receives a response, or rejected if not receiving acknowledgement within timeout period. Similar to message, response can be either string or byte array:

client.send(
  'another client address',
  'hello world!',
).then((response) => {
  // The response here can be either string or Uint8Array
  console.log('Receive response:', response);
}).catch((e) => {
  // This will most likely to be timeout
  console.log('Catch:', e);
});

Client receiving data will automatically send an acknowledgement back to sender if no response is returned by any handler so that sender will be able to know if the packet has been delivered. From the sender's perspective, it's almost the same as receiving a response, except that the Promise is resolved without a value:

client.send(
  'another client address',
  'hello world!',
).then(() => {
  console.log('Receive ACK');
}).catch((e) => {
  // This will most likely to be timeout
  console.log('Catch:', e);
});

Timeout for receiving response or acknowledgement can be set when initializing client:

const client = nkn({
  responseTimeout: 5, // in seconds
});

or when sending a packet:

client.send(
  'another client address',
  'Hello world!',
  {
    responseTimeout: 5, // in seconds
  },
)

Check examples for full examples.

Contributing

Can I submit a bug, suggestion or feature request?

Yes. Please open an issue for that.

Can I contribute patches?

Yes, we appreciate your help! To make contributions, please fork the repo, push your changes to the forked repo with signed-off commits, and open a pull request here.

Please sign off your commit. This means adding a line "Signed-off-by: Name " at the end of each commit, indicating that you wrote the code and have the right to pass it on as an open source patch. This can be done automatically by adding -s when committing:

git commit -s

Community

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].