# peer-id [![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) [![Build Status](https://travis-ci.org/libp2p/js-peer-id.svg?style=flat-square)](https://travis-ci.org/libp2p/js-peer-id) [![Coverage Status](https://coveralls.io/repos/github/libp2p/js-peer-id/badge.svg?branch=master)](https://coveralls.io/github/libp2p/js-peer-id?branch=master) [![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) ![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square) ![](https://img.shields.io/badge/Node.js-%3E%3D6.0.0-orange.svg?style=flat-square) > [IPFS](https://github.com/ipfs/ipfs) Peer ID implementation in JavaScript. - [Description](#description) - [Example](#example) - [Installation](#installation) - [npm](#npm) - [Setup](#setup) - [Node.js](#nodejs) - [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers) - [Browser: ` ``` # API ```js const PeerId = require('peer-id') ``` ## Create ### `new PeerId(id[, privKey, pubKey])` - `id: Buffer` - The multihash of the publick 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], callback)` Generates a new Peer ID, complete with public/private keypair. - `opts: Object`: Default: `{bits: 2048}` - `callback: Function` Calls back `callback` with `err, id`. ## Import ### `createFromHexString(str)` Creates a Peer ID from hex string representing the key's multihash. ### `createFromBytes(buf)` Creates a Peer ID from a buffer representing the key's multihash. ### `createFromB58String(str)` Creates a Peer ID from a Base58 string representing the key's multihash. ### `createFromPubKey(pubKey)` - `publicKey: Buffer` Creates a Peer ID from a buffer containing a public key. ### `createFromPrivKey(privKey)` - `privKey: Buffer` Creates a Peer ID from a buffer containing a private key. ### `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 'base 64' ## Export ### `toHexString()` Returns the Peer ID's `id` as a hex string. ``` 1220d6243998f2fc56343ad7ed0342ab7886a4eb18d736f1b67d44b37fcc81e0f39f ``` ### `toBytes()` Returns the Peer ID's `id` as a buffer. ``` ``` ### `toB58String()` Returns the Peer ID's `id` as a base58 string. ``` 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' ### `toPrint()` Alias for `.toJSON()`. ### `isEqual(id)` - `id` can be a PeerId or a Buffer containing the id # License MIT