peer-id
IPFS Peer Id implementation in Node.js
Last updated 9 years ago by daviddias .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install peer-id 
SYNC missed versions from official npm registry.

peer-id

Discourse posts Dependency Status js-standard-style

IPFS Peer ID implementation in JavaScript.

Lead Maintainer

Vasco Santos

Table of Contents

Description

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

const PeerId = require('peer-id')

const id = await PeerId.create({ bits: 1024, keyType: 'RSA' })
console.log(JSON.stringify(id.toJSON(), null, 2))
{
  "id": "Qma9T5YraSnpRDZqRR4krcSJabThc8nwZuJV3LercPHufi",
  "privKey": "CAAS4AQwggJcAgEAAoGBAMBgbIqyOL26oV3nGPBYrdpbv..",
  "pubKey": "CAASogEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMBgbIqyOL26oV3nGPBYrdpbvzCY..."
}

Installation

npm

> npm i peer-id

Setup

Node.js

const 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.

const 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>

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]). The defaults are shown here.

> peer-id --type rsa --bits 2048

API

const PeerId = require('peer-id')

Create

new PeerId(id[, privKey, pubKey])

  • id: Buffer - The multihash of the public key as Buffer
  • privKey: RsaPrivateKey - The private key
  • pubKey: RsaPublicKey - The public key

The key format is detailed in libp2p-crypto.

create([opts])

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: RSA

Returns Promise<PeerId>.

Import

createFromHexString(str)

Creates a Peer ID from hex string representing the key's multihash.

Returns PeerId.

createFromBytes(buf)

Creates a Peer ID from a buffer representing the key's multihash.

Returns PeerId.

createFromCID(cid)

  • cid: CID - The multihash encoded as CID object

Creates a Peer ID from a CID representation of the key's multihash (RFC 0001).

Returns PeerId.

createFromB58String(str)

Creates a Peer ID from a Base58 string representing the key's multihash.

Returns PeerId.

createFromPubKey(pubKey)

  • publicKey: Buffer

Creates a Peer ID from a buffer containing a public key.

Returns Promise<PeerId>.

createFromPrivKey(privKey)

  • privKey: Buffer

Creates a Peer ID from a buffer containing a private key.

Returns Promise<PeerId>.

createFromJSON(obj)

  • 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 base64

Returns Promise<PeerId>.

createFromProtobuf(buf)

buf is a protocol-buffers encoded PeerId (see marshal())

parse(str)

Attempts to create a PeerId from a base58btc encoded string or a CID encoded as a string.

Export

toHexString()

Returns the Peer ID's id as a hex string.

1220d6243998f2fc56343ad7ed0342ab7886a4eb18d736f1b67d44b37fcc81e0f39f

toBytes()

Returns the Peer ID's id as a buffer.

<Buffer 12 20 d6 24 39 98 f2 fc 56 34 3a d7 ed 03 42 ab 78 86 a4 eb 18 d7 36 f1 b6 7d 44 b3 7f cc 81 e0 f3 9f>

toString()

Returns the Peer ID's id as a self-describing CIDv1 in Base32 (RFC 0001)

bafzbeigweq4zr4x4ky2dvv7nanbkw6egutvrrvzw6g3h2rftp7gidyhtt4

toB58String()

Returns the Peer ID's id as a base58 string (multihash/CIDv0).

QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi

toJSON()

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'

marshal(excludePrivateKey)

Returns a protocol-buffers encoded version of the id, public key and, if excludePrivateKey is not set, the private key.

marshalPubKey()

Returns a protobuf of just the public key, compatible with libp2p-crypto (unlike marshal above).

For example:

const crypto = require('libp2p-crypto')

PeerId.create({ bits: 256, keyType: 'ed25519' }).then( pid => {
  let pk = crypto.keys.unmarshalPublicKey(pid.marshalPubKey())
  // your code here
}

toPrint()

Returns the Peer ID as a printable string without the Qm prefix.

Example: <peer.ID xxxxxx>

equals(id)

Returns true if the given PeerId is equal to the current instance.

  • id can be a PeerId or a Buffer containing the id

isEqual(id)

Deprecation Notice: Use equals, isEqual will be removed in 0.14.0.

  • id can be a PeerId or a Buffer containing the id

Others

isPeerId(id)

Returns true if the given id is an instance of PeerId

  • id should be an instance of PeerId

License

MIT

Current Tags

  • 0.16.0                                ...           latest (3 years ago)

67 Versions

  • 0.16.0                                ...           3 years ago
  • 0.15.4                                ...           3 years ago
  • 0.15.3                                ...           3 years ago
  • 0.15.2                                ...           3 years ago
  • 0.15.1                                ...           3 years ago
  • 0.15.0                                ...           3 years ago
  • 0.14.8                                ...           4 years ago
  • 0.14.7                                ...           4 years ago
  • 0.14.6                                ...           4 years ago
  • 0.14.4                                ...           4 years ago
  • 0.14.3                                ...           4 years ago
  • 0.14.2                                ...           4 years ago
  • 0.14.1                                ...           4 years ago
  • 0.14.0                                ...           4 years ago
  • 0.13.13                                ...           5 years ago
  • 0.13.12                                ...           5 years ago
  • 0.13.11                                ...           5 years ago
  • 0.13.10                                ...           5 years ago
  • 0.13.9                                ...           5 years ago
  • 0.13.8                                ...           5 years ago
  • 0.13.7                                ...           5 years ago
  • 0.13.6                                ...           5 years ago
  • 0.13.5                                ...           5 years ago
  • 0.13.4                                ...           5 years ago
  • 0.13.3                                ...           5 years ago
  • 0.12.5                                ...           5 years ago
  • 0.12.4                                ...           5 years ago
  • 0.13.2                                ...           5 years ago
  • 0.13.1                                ...           5 years ago
  • 0.13.0                                ...           5 years ago
  • 0.12.3                                ...           5 years ago
  • 0.12.2                                ...           6 years ago
  • 0.12.1                                ...           6 years ago
  • 0.12.0                                ...           6 years ago
  • 0.11.0                                ...           6 years ago
  • 0.10.7                                ...           7 years ago
  • 0.10.6                                ...           7 years ago
  • 0.10.5                                ...           7 years ago
  • 0.10.4                                ...           7 years ago
  • 0.10.3                                ...           7 years ago
  • 0.10.2                                ...           7 years ago
  • 0.10.1                                ...           7 years ago
  • 0.10.0                                ...           7 years ago
  • 0.9.0                                ...           7 years ago
  • 0.8.8                                ...           7 years ago
  • 0.8.7                                ...           8 years ago
  • 0.8.6                                ...           8 years ago
  • 0.8.5                                ...           8 years ago
  • 0.8.4                                ...           8 years ago
  • 0.8.2                                ...           8 years ago
  • 0.8.1                                ...           8 years ago
  • 0.8.0                                ...           8 years ago
  • 0.7.0                                ...           9 years ago
  • 0.6.7                                ...           9 years ago
  • 0.6.6                                ...           9 years ago
  • 0.6.5                                ...           9 years ago
  • 0.6.4                                ...           9 years ago
  • 0.6.3                                ...           9 years ago
  • 0.6.2                                ...           9 years ago
  • 0.6.1                                ...           9 years ago
  • 0.6.0                                ...           9 years ago
  • 0.5.3                                ...           9 years ago
  • 0.5.1                                ...           9 years ago
  • 0.5.0                                ...           9 years ago
  • 0.4.0                                ...           9 years ago
  • 0.3.4                                ...           9 years ago
  • 0.3.3                                ...           9 years ago

© 2010 - cnpmjs.org x YWFE | Home | YWFE