mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-04-25 16:32:26 +00:00
peer-id JavaScript implementation
[
](https://travis-ci.org/diasdavid/js-peer-id)
IPFS Peer Id implementation in JavaScript
Description
A IPFS Peer Id is based on a sha256 has of the peer public key, using multihash
Usage
In Node.js through npm
$ npm install --save peer-id
var PeerId = require('peer-id')
In the Browser through browserify
Same as in Node.js, you just have to browserify the code before serving it. See the browserify repo for how to do that.
In the Browser through <script>
tag
Make the peer-id.min.js available through your server and load it using a normal <script>
tag, this will export the peerId
constructor on the window
object, such that:
var PeerId = window.PeerId
Gotchas
You will need to use Node.js Buffer
API compatible, if you are running inside the browser, you can access it by PeerId.Buffer
or you can install Feross's Buffer.
Creating a new Id
var PeerId = require('ipfs-peer')
// Create a new Id
var id = PeerId.create()
// Recreate an Id from Hex string
var id = PeerId.createFromHexString(str)
// Recreate an Id from a Buffer
var id = PeerId.createFromBytes(buf)
// Recreate an B58 String
var id = PeerId.createFromB58String(str)
// Recreate from a Public Key
var id = PeerId.createFromPubKey(pubKey)
// Recreate from a Private Key
var id = PeerId.createFromPrivKey(privKey)
Exporting an Id
// Print friendly format
id.toPrint() // returns an object with id, privKey and pubKey in hex format
// Export to an hex string
id.toHexString()
// Export to Buffer
id.toBytes() (same as id.id)
// Export to a B58 string
id.toB58String()
Id format
id.pubKey // Buffer containing the Public Key
id.privKey // Buffer containing the Private Key
id.id // Buffer containing the multihash
Description
Languages
JavaScript
100%