Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dht-rpc

Package Overview
Dependencies
Maintainers
1
Versions
109
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dht-rpc

Make RPC calls over a Kademlia based DHT.

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.7K
increased by36.21%
Maintainers
1
Weekly downloads
 
Created
Source

dht-rpc

Make RPC calls over a Kademlia based DHT.

npm install dht-rpc

build status

Usage

Here is an example implementing a simple key value store

First spin up a bootstrap node. You can make multiple if you want for redundancy.

var dht = require('dht-rpc')

// Set ephemeral: true so other peers do not add us to the peer list, simply bootstrap
var bootstrap = dht({ephemeral: true})

bootstrap.listen(10001)

Now lets make some dht nodes that can store values in our key value store.

var dht = require('dht-rpc')
var crypto = require('crypto')

// Let's create 100 dht nodes for our example.
for (var i = 0; i < 100; i++) createNode()

function createNode () {
  var node = dht({
    bootstrap: ['localhost:10001']
  })

  var values = {}

  // When we are the closest node and someone is sending us a "store" command
  node.on('closest:store', function (query, cb) {
    if (!query.value) return cb()

    // Use the hash of the value as the key
    var key = sha256(query.value).toString('hex')
    values[key] = query.value
    console.log('Storing', key, '-->', query.value.toString())
    cb()
  })

  // When someone is querying for a "lookup" command
  node.on('query:lookup', function (query, cb) {
    var value = values[query.target.toString('hex')]
    cb(null, value)
  })
}

function sha256 (val) {
  return crypto.createHash('sha256').update(val).digest('hex')
}

To insert a value into this dht make another script that does this following

// Set ephemeral: true as we are not part of the network.
var node = dht({ephemeral: true})

node.closest({command: 'store', target: sha256(val), value: val}, function (err, res) {
  if (err) throw err
  console.log('Inserted', sha256(val).toString('hex'))
})

Then after inserting run this script to query for a value

node.query({command: 'lookup', target: new Buffer(hexFromAbove, 'hex')})
  .on('data', function (data) {
    if (data.value && sha256(data.value).toString('hex') === hexFromAbove) {
      // We found the value! Destroy the query stream as there is no need to continue.
      console.log(val, '-->', data.value.toString())
      this.destroy()
    }
  })
  .on('end', function () {
    console.log('(query finished)')
  })

License

MIT

FAQs

Package last updated on 18 Jan 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc