2015-11-05 17:47:44 +00:00
peer-id JavaScript implementation
2016-03-03 17:31:33 +00:00
=================================
2015-07-08 15:34:34 -07:00
2016-03-03 17:43:30 +00:00
[](http://ipn.io)
[](http://webchat.freenode.net/?channels=%23ipfs)
2016-03-03 17:31:33 +00:00
[](https://travis-ci.org/diasdavid/js-peer-id)

[](https://david-dm.org/diasdavid/js-peer-id)
[](https://github.com/feross/standard)
2015-11-05 17:47:44 +00:00
> IPFS Peer Id implementation in JavaScript
2015-07-08 15:34:34 -07:00
# Description
2016-02-13 07:49:51 -08:00
An IPFS Peer Id is based on a sha256 hash of the peer public key, using [multihash ](https://github.com/jbenet/multihash )
2016-02-02 15:50:45 -08:00
2016-04-07 19:45:21 -04:00
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.
2016-02-02 15:50:45 -08:00
2016-04-07 19:45:21 -04:00
# Installation
2015-07-08 15:34:34 -07:00
2016-04-07 19:45:21 -04:00
## npm
2015-07-08 15:34:34 -07:00
2016-04-07 19:45:21 -04:00
```sh
> npm i peer-id
2015-07-08 15:34:34 -07:00
```
2015-11-05 17:47:44 +00:00
2016-04-07 19:45:21 -04:00
## Use in Node.js
2015-07-08 15:34:34 -07:00
2016-04-07 19:45:21 -04:00
```JavaScript
var PeerId = require('peer-id')
```
2016-03-10 19:36:47 +00:00
2016-04-07 19:45:21 -04:00
## Use in a browser with browserify, webpack or any other bundler
2016-03-10 19:36:47 +00:00
2016-04-07 19:45:21 -04:00
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.
2015-11-05 17:47:44 +00:00
2016-04-07 19:45:21 -04:00
```JavaScript
var PeerId = require('peer-id')
```
2015-11-05 17:47:44 +00:00
2016-04-07 19:45:21 -04:00
## Use in a browser Using a script tag
2015-11-05 17:47:44 +00:00
2016-04-07 19:45:21 -04:00
Loading this module through a script tag will make the `PeerId` obj available in the global namespace.
2015-11-05 17:47:44 +00:00
2016-04-07 19:45:21 -04:00
```html
< script src = "https://npmcdn.com/peer-id/dist/index.min.js" > < / script >
<!-- OR -->
< script src = "https://npmcdn.com/peer-id/dist/index.js" > < / script >
2015-11-05 17:47:44 +00:00
```
2016-04-07 19:45:21 -04:00
# Usage
2015-11-05 17:47:44 +00:00
2015-07-08 15:34:34 -07:00
### Creating a new Id
```
2016-03-03 17:31:33 +00:00
const PeerId = require('ipfs-peer')
2015-07-08 15:34:34 -07:00
// Create a new Id
2016-03-03 17:31:33 +00:00
const id = PeerId.create()
2015-07-08 15:34:34 -07:00
// Recreate an Id from Hex string
2016-03-03 17:31:33 +00:00
const id = PeerId.createFromHexString(str)
2015-07-08 15:34:34 -07:00
// Recreate an Id from a Buffer
2016-03-03 17:31:33 +00:00
const id = PeerId.createFromBytes(buf)
2015-07-08 15:34:34 -07:00
// Recreate an B58 String
2016-03-03 17:31:33 +00:00
const id = PeerId.createFromB58String(str)
2015-07-08 15:34:34 -07:00
// Recreate from a Public Key
2016-03-03 17:31:33 +00:00
const id = PeerId.createFromPubKey(pubKey)
2015-07-08 15:34:34 -07:00
// Recreate from a Private Key
2016-03-03 17:31:33 +00:00
const id = PeerId.createFromPrivKey(privKey)
2015-07-08 15:34:34 -07:00
```
### Exporting an Id
```
// Print friendly format
id.toPrint() // returns an object with id, privKey and pubKey in hex format
// Export to an hex string
id.toHexString()
// Export to Buffer
id.toBytes() (same as id.id)
// Export to a B58 string
id.toB58String()
```
### Id format
```
id.pubKey // Buffer containing the Public Key
id.privKey // Buffer containing the Private Key
id.id // Buffer containing the multihash
```