# peer-id [![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://protocol.ai) [![](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/) [![](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p) [![Discourse posts](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io) [![](https://img.shields.io/codecov/c/github/libp2p/js-peer-id.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-peer-id) [![](https://img.shields.io/travis/libp2p/js-peer-id.svg?style=flat-square)](https://travis-ci.com/libp2p/js-peer-id) [![Dependency Status](https://david-dm.org/libp2p/js-peer-id.svg?style=flat-square)](https://david-dm.org/libp2p/js-peer-id) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) > [IPFS](https://github.com/ipfs/ipfs) Peer ID implementation in JavaScript. ## Lead Maintainer [Vasco Santos](https://github.com/vasco-santos) ## Table of Contents - [peer-id](#peer-id) - [Lead Maintainer](#lead-maintainer) - [Table of Contents](#table-of-contents) - [Description](#description) - [Example](#example) - [Installation](#installation) - [npm](#npm) - [Setup](#setup) - [Node.js](#nodejs) - [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers) - [Browser: ` ``` # 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 ### `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](https://github.com/libp2p/js-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`. ## 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|String|Buffer` - The multihash encoded as [CID](https://github.com/ipld/js-cid) (object, `String` or `Buffer`) Creates a Peer ID from a CID representation of the key's multihash ([RFC 0001](https://github.com/libp2p/specs/blob/master/RFC/0001-text-peerid-cid.md)). 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`. ### `createFromPrivKey(privKey)` - `privKey: Buffer` Creates a Peer ID from a buffer containing a private key. Returns `Promise`. ### `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`. ### `createFromProtobuf(buf)` `buf` is a protocol-buffers encoded PeerId (see `marshal()`) ## Export ### `toHexString()` Returns the Peer ID's `id` as a hex string. ``` 1220d6243998f2fc56343ad7ed0342ab7886a4eb18d736f1b67d44b37fcc81e0f39f ``` ### `toBytes()` Returns the Peer ID's `id` as a buffer. ``` ``` ### `toString()` Returns the Peer ID's `id` as a self-describing CIDv1 in Base32 ([RFC 0001](https://github.com/libp2p/specs/blob/master/RFC/0001-text-peerid-cid.md)) ``` 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: ```js 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: `` ### `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`](#equalsid), `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