2016-04-11 15:23:34 -07:00
|
|
|
# peer-id
|
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)
|
2016-09-11 16:40:09 -04:00
|
|
|
[](https://coveralls.io/github/libp2p/js-peer-id?branch=master)
|
|
|
|
[](https://david-dm.org/libp2p/js-peer-id)
|
2016-03-03 17:31:33 +00:00
|
|
|
[](https://github.com/feross/standard)
|
2016-04-11 15:23:34 -07:00
|
|
|
|
|
|
|
> [IPFS](https://github.com/ipfs/ipfs) Peer ID implementation in JavaScript.
|
2015-07-08 15:34:34 -07:00
|
|
|
|
|
|
|
# Description
|
|
|
|
|
2016-04-11 15:23:34 -07:00
|
|
|
Generate, import, and export PeerIDs, for use with [IPFS](https://github.com/ipfs/ipfs).
|
|
|
|
|
|
|
|
*A Peer ID is the SHA-256 [multihash](https://github.com/jbenet/multihash) of a
|
|
|
|
public key.*
|
|
|
|
|
|
|
|
*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.*
|
|
|
|
|
|
|
|
# Example
|
|
|
|
|
|
|
|
```js
|
|
|
|
var PeerId = require('peer-id')
|
|
|
|
var bs58 = require('bs58')
|
|
|
|
|
|
|
|
var id = PeerId.create({ bits: 32 })
|
|
|
|
|
|
|
|
console.log('id ', id.toB58String())
|
2016-05-24 14:03:31 +02:00
|
|
|
console.log('priv key ', bs58.encode(id.privKey.bytes))
|
|
|
|
console.log('pub key ', bs58.encode(id.pubKey.bytes))
|
2016-04-11 15:23:34 -07:00
|
|
|
```
|
2016-02-02 15:50:45 -08:00
|
|
|
|
2016-04-11 15:23:34 -07:00
|
|
|
```
|
|
|
|
id QmeeLFb92nkZJGj3gXLqXrEMzCMYs6uBgQLVNbrcXEvYXk
|
|
|
|
priv key 6ibrcPAbevzvPpkq6EA6XmLyuhmUrJrEvUfgQDtEiSEPzGnGU8Ejwf6b11DVm6opnFGo
|
|
|
|
pub key 2BeBZVKJ9RQs4i4LbGv4ReEeuBA5dck2Gje3wt67e44XuyyPq5jE
|
|
|
|
```
|
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-11 15:23:34 -07:00
|
|
|
# Setup
|
|
|
|
|
|
|
|
## Node.js
|
2015-07-08 15:34:34 -07:00
|
|
|
|
2016-05-24 14:03:31 +02:00
|
|
|
```js
|
2016-04-07 19:45:21 -04:00
|
|
|
var PeerId = require('peer-id')
|
|
|
|
```
|
2016-03-10 19:36:47 +00:00
|
|
|
|
2016-04-12 09:04:02 -07:00
|
|
|
## Browser: Browserify, Webpack, other bundlers
|
2016-03-10 19:36:47 +00:00
|
|
|
|
2016-04-11 15:23:34 -07: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-05-24 14:03:31 +02:00
|
|
|
```js
|
2016-04-07 19:45:21 -04:00
|
|
|
var PeerId = require('peer-id')
|
|
|
|
```
|
2015-11-05 17:47:44 +00:00
|
|
|
|
2016-04-11 15:23:34 -07:00
|
|
|
## Browser: `<script>` Tag
|
2015-11-05 17:47:44 +00:00
|
|
|
|
2016-04-11 15:23:34 -07: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
|
2016-09-11 12:29:33 +01:00
|
|
|
<script src="https://unpkg.com/peer-id/dist/index.min.js"></script>
|
2016-04-07 19:45:21 -04:00
|
|
|
<!-- OR -->
|
2016-09-11 12:29:33 +01:00
|
|
|
<script src="https://unpkg.com/peer-id/dist/index.js"></script>
|
2015-11-05 17:47:44 +00:00
|
|
|
```
|
|
|
|
|
2016-04-11 15:23:34 -07:00
|
|
|
# API
|
2015-07-08 15:34:34 -07:00
|
|
|
|
2016-04-11 15:23:34 -07:00
|
|
|
```js
|
|
|
|
const PeerId = require('peer-id')
|
2015-07-08 15:34:34 -07:00
|
|
|
```
|
|
|
|
|
2016-04-11 15:23:34 -07:00
|
|
|
## Create
|
2015-07-08 15:34:34 -07:00
|
|
|
|
2016-05-24 14:03:31 +02:00
|
|
|
### `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/ipfs/js-libp2p-crypto).
|
|
|
|
|
|
|
|
### `create([opts])`
|
2015-07-08 15:34:34 -07:00
|
|
|
|
2016-05-24 14:03:31 +02:00
|
|
|
Generates a new Peer ID, complete with public/private keypair.
|
2015-07-08 15:34:34 -07:00
|
|
|
|
2016-05-24 14:03:31 +02:00
|
|
|
- `opts: Object`: Default: `{bits: 2048}`
|
2015-07-08 15:34:34 -07:00
|
|
|
|
2016-04-11 15:23:34 -07:00
|
|
|
## Import
|
2015-07-08 15:34:34 -07:00
|
|
|
|
2016-05-24 14:03:31 +02:00
|
|
|
### `createFromHexString(str)`
|
2015-07-08 15:34:34 -07:00
|
|
|
|
2016-04-11 15:23:34 -07:00
|
|
|
Creates a Peer ID from hex string representing the key's multihash.
|
2015-07-08 15:34:34 -07:00
|
|
|
|
2016-05-24 14:03:31 +02:00
|
|
|
### `createFromBytes(buf)`
|
2016-04-11 15:23:34 -07:00
|
|
|
|
|
|
|
Creates a Peer ID from a buffer representing the key's multihash.
|
2015-07-08 15:34:34 -07:00
|
|
|
|
2016-05-24 14:03:31 +02:00
|
|
|
### `createFromB58String(str)`
|
2016-04-11 15:23:34 -07:00
|
|
|
Creates a Peer ID from a Base58 string representing the key's multihash.
|
2015-07-08 15:34:34 -07:00
|
|
|
|
2016-05-24 14:03:31 +02:00
|
|
|
### `createFromPubKey(pubKey)`
|
2015-07-08 15:34:34 -07:00
|
|
|
|
2016-04-11 15:23:34 -07:00
|
|
|
Creates a Peer ID from a buffer containing a public key.
|
|
|
|
|
2016-05-24 14:03:31 +02:00
|
|
|
### `createFromPrivKey(privKey)`
|
2016-04-11 15:23:34 -07:00
|
|
|
|
|
|
|
Creates a Peer ID from a buffer containing a private key.
|
|
|
|
|
2016-05-24 14:03:31 +02:00
|
|
|
### `createFromJSON(obj)`
|
2016-04-11 15:23:34 -07:00
|
|
|
|
2016-05-24 14:03:31 +02:00
|
|
|
- `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'
|
2016-04-11 15:23:34 -07:00
|
|
|
|
2015-07-08 15:34:34 -07:00
|
|
|
|
2016-05-24 14:03:31 +02:00
|
|
|
## Export
|
|
|
|
|
|
|
|
### `toHexString()`
|
2016-04-11 15:23:34 -07:00
|
|
|
|
|
|
|
Returns the Peer ID's `id` as a hex string.
|
|
|
|
|
2016-04-13 10:00:26 -07:00
|
|
|
```
|
|
|
|
1220d6243998f2fc56343ad7ed0342ab7886a4eb18d736f1b67d44b37fcc81e0f39f
|
|
|
|
```
|
2016-04-11 15:23:34 -07:00
|
|
|
|
2016-05-24 14:03:31 +02:00
|
|
|
### `toBytes()`
|
2015-07-08 15:34:34 -07:00
|
|
|
|
2016-04-11 15:23:34 -07:00
|
|
|
Returns the Peer ID's `id` as a buffer.
|
|
|
|
|
2016-04-13 10:00:26 -07:00
|
|
|
```
|
2016-04-11 15:23:34 -07:00
|
|
|
<Buffer 12 20 d6 24 39 98 f2 fc 56 34 3a d7 ed 03 42 ab 78 86 a4 eb 18 d7 36 f1 b6 7d 44 b3 7f cc 81 e0 f3 9f>
|
2015-07-08 15:34:34 -07:00
|
|
|
```
|
2016-04-11 15:23:34 -07:00
|
|
|
|
2016-05-24 14:03:31 +02:00
|
|
|
### `toB58String()`
|
2016-04-11 15:23:34 -07:00
|
|
|
|
|
|
|
Returns the Peer ID's `id` as a base58 string.
|
|
|
|
|
2016-04-13 10:00:26 -07:00
|
|
|
```
|
2016-04-11 15:23:34 -07:00
|
|
|
QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi
|
2015-07-08 15:34:34 -07:00
|
|
|
```
|
2016-04-11 15:23:34 -07:00
|
|
|
|
2016-05-24 14:03:31 +02:00
|
|
|
### `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()`.
|
|
|
|
|
|
|
|
|
2016-04-11 15:23:34 -07:00
|
|
|
# License
|
|
|
|
|
|
|
|
MIT
|