PeerId
A Peer ID is the SHA-256 +font-weight: bold;}.hljs-deletion{background: #fdd;}.hljs-addition{background: #dfd;}.hljs-emphasis{font-style: italic;}.hljs-strong{font-weight: bold;}
Introduction
Generate, import, and export PeerIDs, for use with IPFS.
+++A Peer ID is the SHA-256 multihash of a public key.
+The public key is a base64 encoded string of a protobuf containing an RSA DER buffer. This uses a node buffer to pass the base64 encoded public key protobuf to the multihash for ID generation.
+
Example
+var PeerId = require('peer-id')
+
+PeerId.create({ bits: 1024 }, (err, id) => {
+ console.log(JSON.stringify(id.toJSON(), null, 2)
+})
+{
+ "id": "Qma9T5YraSnpRDZqRR4krcSJabThc8nwZuJV3LercPHufi",
+ "privKey": "CAAS4AQwggJcAgEAAoGBAMBgbIqyOL26oV3nGPBYrdpbv..",
+ "pubKey": "CAASogEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMBgbIqyOL26oV3nGPBYrdpbvzCY..."
+}
+Installation
+npm
+> npm i peer-id+
Setup
+Node.js
+var PeerId = require('peer-id')
+Browser: Browserify, Webpack, other bundlers
+The code published to npm that gets loaded on require is in fact a ES5 +transpiled version with the right shims added. This means that you can require +it and use with your favourite bundler without having to adjust asset management +process.
+var PeerId = require('peer-id')
+Browser: <script>
Tag
+Loading this module through a script tag will make the PeerId
obj available in
+the global namespace.
<script src="https://unpkg.com/peer-id/dist/index.min.js"></script>
+<!-- OR -->
+<script src="https://unpkg.com/peer-id/dist/index.js"></script>
+PeerId
A Peer ID is the SHA-256 multihash of a public key.
See
static
PeerId.create
create(opts: Object?, callback: function (Error, PeerId))
Create a new PeerId
by generating a new public/private keypair.
Returns
Example
const PeerId = require('peer-id')
PeerId.create((err, id) => {
if (err) {
throw err
}
console.log('id', JSON.stringify(id))
-})
PeerId.createFromB58String
createFromB58String(str: string): PeerId
Creates a Peer ID from a base58
string representing the
key's multihash.
Parameters
str: string
base58
encoded id
Returns
PeerId
-PeerId.createFromPubKey
createFromPubKey(key: (string | Buffer), callback: function (Error, PeerId)): undefined
Creates a Peer ID from a buffer containing a public key.
+Returns
PeerId.createFromPrivKey
createFromPrivKey(key: (string | Buffer), callback: function (Error, PeerId)): undefined
Creates a Peer ID from a buffer containing a private key.
PeerId.createFromJSON
createFromJSON(obj: PeerIdJson, callback: function (Error, PeerId))
Import a PeerId
from a serialized JSON object.
Parameters
obj: PeerIdJson
instance
constructor
constructor(id: Buffer, privKey: RSAPrivateKey?, pubKey: RSAPublickKey?)