js-peer-id/README.md

94 lines
2.8 KiB
Markdown
Raw Normal View History

2015-11-05 17:47:44 +00:00
peer-id JavaScript implementation
=================================
2015-07-08 15:34:34 -07:00
2016-03-03 17:43:30 +00:00
[![](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/diasdavid/js-peer-id.svg?style=flat-square)](https://travis-ci.org/diasdavid/js-peer-id)
![](https://img.shields.io/badge/coverage-95%25-yellow.svg?style=flat-square)
[![Dependency Status](https://david-dm.org/diasdavid/js-peer-id.svg?style=flat-square)](https://david-dm.org/diasdavid/js-peer-id)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](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-13 07:49:51 -08: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.
2015-07-08 15:34:34 -07:00
# Usage
2015-11-05 17:47:44 +00:00
### In Node.js through npm
2015-07-08 15:34:34 -07:00
2015-11-05 17:47:44 +00:00
```bash
> npm install --save peer-id
2015-07-08 15:34:34 -07:00
```
2015-11-05 17:47:44 +00:00
```javascript
const PeerId = require('peer-id')
2015-07-08 15:34:34 -07:00
```
2015-11-05 17:47:44 +00:00
### In the Browser through browserify
2016-03-03 14:19:10 -08:00
TODO: Figure out how to get this working with browserify and webpack. Browserify can't bundle our replacement of ```fs.readFileSync``` with ```require('buffer!./file')```.
2015-11-05 17:47:44 +00:00
### In the Browser through `<script>` tag
2016-03-03 14:19:10 -08:00
Make the [peer-id.js](/dist/peer-id.js) available through your server and load it using a normal `<script>` tag, this will export the `PeerId` object, such that:
2015-11-05 17:47:44 +00:00
```JavaScript
2016-03-03 14:19:10 -08:00
const Id = PeerId
2015-11-05 17:47:44 +00:00
```
#### Gotchas
You will need to use Node.js `Buffer` API compatible, if you are running inside the browser, you can access it by `PeerId.Buffer` or you can install Feross's [Buffer](https://github.com/feross/buffer).
2015-07-08 15:34:34 -07:00
### Creating a new Id
```
const PeerId = require('ipfs-peer')
2015-07-08 15:34:34 -07:00
// Create a new Id
const id = PeerId.create()
2015-07-08 15:34:34 -07:00
// Recreate an Id from Hex string
const id = PeerId.createFromHexString(str)
2015-07-08 15:34:34 -07:00
// Recreate an Id from a Buffer
const id = PeerId.createFromBytes(buf)
2015-07-08 15:34:34 -07:00
// Recreate an B58 String
const id = PeerId.createFromB58String(str)
2015-07-08 15:34:34 -07:00
// Recreate from a Public Key
const id = PeerId.createFromPubKey(pubKey)
2015-07-08 15:34:34 -07:00
// Recreate from a Private Key
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
```