# peer-id
[](http://ipn.io)
[](http://webchat.freenode.net/?channels=%23ipfs)
[](https://travis-ci.org/libp2p/js-peer-id)
[](https://coveralls.io/github/libp2p/js-peer-id?branch=master)
[](https://david-dm.org/libp2p/js-peer-id)
[](https://github.com/feross/standard)


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