IPFS Peer Id implementation in Node.js
npm install peer-id







> IPFS Peer ID implementation in JavaScript.
- peer-id
- Lead Maintainer
- Table of Contents
- Description
- Example
- Installation
- npm
- Setup
- Node.js
- Browser: Browserify, Webpack, other bundlers
- Browser: Tag
- CLI
- API
- Create
- [new PeerId(id[, privKey, pubKey])](#new-peeridid-privkey-pubkey)
- [create([opts])](#createopts)
- Import
- createFromHexString(str)
- createFromBytes(buf)
- createFromCID(cid)
- createFromB58String(str)
- createFromPubKey(pubKey)
- createFromPrivKey(privKey)
- createFromJSON(obj)
- createFromProtobuf(buf)
- parse(str)
- Export
- toHexString()
- toBytes()
- toString()
- toB58String()
- toJSON()
- marshal(excludePrivateKey)
- marshalPubKey()
- toPrint()
- equals(id)
- isEqual(id)
- Others
- isPeerId(id)
- License
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.
``JavaScript
const PeerId = require('peer-id')
const id = await PeerId.create({ bits: 1024, keyType: 'RSA' })
console.log(JSON.stringify(id.toJSON(), null, 2))
`
`bash`
{
"id": "Qma9T5YraSnpRDZqRR4krcSJabThc8nwZuJV3LercPHufi",
"privKey": "CAAS4AQwggJcAgEAAoGBAMBgbIqyOL26oV3nGPBYrdpbv..",
"pubKey": "CAASogEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMBgbIqyOL26oV3nGPBYrdpbvzCY..."
}
`sh`
> npm i peer-id
`js`
const PeerId = require('peer-id')
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.
`js`
const PeerId = require('peer-id')
CLI
After installing
peer-id, npm install peer-id, you can leverage the cli to generate keys exported as JSON. You can specify the type for the key and size, as detailed in [create([opts])](#createopts). The defaults are shown here.`sh
> peer-id --type rsa --bits 2048
`API
`js
const PeerId = require('peer-id')
`Create
$3
-
id: Buffer - The multihash of the public key as Buffer
- privKey: RsaPrivateKey - The private key
- pubKey: RsaPublicKey - The public keyThe key format is detailed in libp2p-crypto.
$3
Generates a new Peer ID, complete with public/private keypair.
-
opts.bits: number - The size of the key. Default: 2048
- opts.keyType: string - The key type, one of: ['RSA', 'Ed25519', 'secp256k1']. Default: RSAReturns
Promise.Import
$3
Creates a Peer ID from hex string representing the key's multihash.
Returns
PeerId.$3
Creates a Peer ID from a buffer representing the key's multihash.
Returns
PeerId.$3
-
cid: CID - The multihash encoded as CID objectCreates a Peer ID from a CID representation of the key's multihash (RFC 0001).
Returns
PeerId.$3
Creates a Peer ID from a Base58 string representing the key's multihash.
Returns
PeerId.$3
-
publicKey: BufferCreates a Peer ID from a buffer containing a public key.
Returns
Promise.$3
-
privKey: BufferCreates a Peer ID from a buffer containing a private key.
Returns
Promise.$3
-
obj.id: String - The multihash encoded in base58
- obj.pubKey: String - The public key in protobuf format, encoded in base64
- obj.privKey: String - The private key in protobuf format, encoded in base64Returns
Promise.$3
buf is a protocol-buffers encoded PeerId (see marshal())$3
Attempts to create a PeerId from a base58btc encoded string or a CID encoded as a string.
Export
$3
Returns the Peer ID's
id as a hex string.`
1220d6243998f2fc56343ad7ed0342ab7886a4eb18d736f1b67d44b37fcc81e0f39f
`$3
Returns the Peer ID's
id as a buffer.`
`
$3
Returns the Peer ID's
id as a self-describing CIDv1 in Base32 (RFC 0001)`
bafzbeigweq4zr4x4ky2dvv7nanbkw6egutvrrvzw6g3h2rftp7gidyhtt4
`$3
Returns the Peer ID's
id as a base58 string (multihash/CIDv0).`
QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi
`$3
Returns an
obj of the form-
obj.id: String - The multihash encoded in base58
- obj.pubKey: String - The public key in protobuf format, encoded in 'base64'
- obj.privKey: String - The private key in protobuf format, encoded in 'base 64'$3
Returns a protocol-buffers encoded version of the id, public key and, if
excludePrivateKey is not set, the private key.$3
Returns a protobuf of just the public key, compatible with
libp2p-crypto (unlike marshal above).For example:
`js
const crypto = require('libp2p-crypto')PeerId.create({ bits: 256, keyType: 'ed25519' }).then( pid => {
let pk = crypto.keys.unmarshalPublicKey(pid.marshalPubKey())
// your code here
}
`$3
Returns the Peer ID as a printable string without the
Qm prefix.Example:
$3
Returns
true if the given PeerId is equal to the current instance.-
id can be a PeerId or a Buffer containing the id$3
Deprecation Notice: Use equals, isEqual will be removed in 0.14.0.-
id can be a PeerId or a Buffer containing the idOthers
$3
Returns
true if the given id is an instance of PeerId-
id` should be an instance of PeerIdMIT