mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-07-04 14:21:46 +00:00
Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
3ef704ba32 | |||
1be7d888d6 | |||
0d3d9f835b | |||
c1ed9751e3 | |||
01957fa095 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -27,3 +27,4 @@ build/Release
|
|||||||
node_modules
|
node_modules
|
||||||
|
|
||||||
dist
|
dist
|
||||||
|
docs
|
111
README.md
111
README.md
@ -22,22 +22,6 @@
|
|||||||
- [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers)
|
- [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers)
|
||||||
- [Browser: `<script>` Tag](#browser-script-tag)
|
- [Browser: `<script>` Tag](#browser-script-tag)
|
||||||
- [API](#api)
|
- [API](#api)
|
||||||
- [Create](#create)
|
|
||||||
- [`new PeerId(id[, privKey, pubKey])`](#new-peeridid-privkey-pubkey)
|
|
||||||
- [`create([opts], callback)`](#createopts-callback)
|
|
||||||
- [Import](#import)
|
|
||||||
- [`createFromHexString(str)`](#createfromhexstringstr)
|
|
||||||
- [`createFromBytes(buf)`](#createfrombytesbuf)
|
|
||||||
- [`createFromB58String(str)`](#createfromb58stringstr)
|
|
||||||
- [`createFromPubKey(pubKey)`](#createfrompubkeypubkey)
|
|
||||||
- [`createFromPrivKey(privKey)`](#createfromprivkeyprivkey)
|
|
||||||
- [`createFromJSON(obj)`](#createfromjsonobj)
|
|
||||||
- [Export](#export)
|
|
||||||
- [`toHexString()`](#tohexstring)
|
|
||||||
- [`toBytes()`](#tobytes)
|
|
||||||
- [`toB58String()`](#tob58string)
|
|
||||||
- [`toJSON()`](#tojson)
|
|
||||||
- [`toPrint()`](#toprint)
|
|
||||||
- [License](#license)
|
- [License](#license)
|
||||||
|
|
||||||
# Description
|
# Description
|
||||||
@ -55,7 +39,6 @@ to the multihash for ID generation.*
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
var PeerId = require('peer-id')
|
var PeerId = require('peer-id')
|
||||||
var bs58 = require('bs58')
|
|
||||||
|
|
||||||
PeerId.create({ bits: 1024 }, (err, id) => {
|
PeerId.create({ bits: 1024 }, (err, id) => {
|
||||||
console.log(JSON.stringify(id.toJSON(), null, 2)
|
console.log(JSON.stringify(id.toJSON(), null, 2)
|
||||||
@ -110,100 +93,6 @@ the global namespace.
|
|||||||
|
|
||||||
# API
|
# 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.
|
|
||||||
|
|
||||||
```
|
|
||||||
<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>
|
|
||||||
```
|
|
||||||
|
|
||||||
### `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()`.
|
|
||||||
|
|
||||||
|
|
||||||
# License
|
# License
|
||||||
|
|
||||||
MIT
|
MIT
|
||||||
|
5
documentation.yml
Normal file
5
documentation.yml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
toc:
|
||||||
|
- name: Introduction
|
||||||
|
file: intro.md
|
||||||
|
- PeerId
|
||||||
|
- PeerIdJson
|
61
intro.md
Normal file
61
intro.md
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
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/multiformats/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')
|
||||||
|
|
||||||
|
PeerId.create({ bits: 1024 }, (err, id) => {
|
||||||
|
console.log(JSON.stringify(id.toJSON(), null, 2)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "Qma9T5YraSnpRDZqRR4krcSJabThc8nwZuJV3LercPHufi",
|
||||||
|
"privKey": "CAAS4AQwggJcAgEAAoGBAMBgbIqyOL26oV3nGPBYrdpbv..",
|
||||||
|
"pubKey": "CAASogEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMBgbIqyOL26oV3nGPBYrdpbvzCY..."
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
### npm
|
||||||
|
|
||||||
|
```sh
|
||||||
|
> npm i peer-id
|
||||||
|
```
|
||||||
|
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
### Node.js
|
||||||
|
|
||||||
|
```js
|
||||||
|
var PeerId = require('peer-id')
|
||||||
|
```
|
||||||
|
|
||||||
|
### Browser: Browserify, Webpack, other bundlers
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
```js
|
||||||
|
var PeerId = require('peer-id')
|
||||||
|
```
|
||||||
|
|
||||||
|
### Browser: `<script>` Tag
|
||||||
|
|
||||||
|
Loading this module through a script tag will make the `PeerId` obj available in
|
||||||
|
the global namespace.
|
||||||
|
|
||||||
|
```html
|
||||||
|
<script src="https://unpkg.com/peer-id/dist/index.min.js"></script>
|
||||||
|
<!-- OR -->
|
||||||
|
<script src="https://unpkg.com/peer-id/dist/index.js"></script>
|
||||||
|
```
|
@ -14,7 +14,8 @@
|
|||||||
"release-minor": "aegir-release --type minor",
|
"release-minor": "aegir-release --type minor",
|
||||||
"release-major": "aegir-release --type major",
|
"release-major": "aegir-release --type major",
|
||||||
"coverage": "aegir-coverage",
|
"coverage": "aegir-coverage",
|
||||||
"coverage-publish": "aegir-coverage publish"
|
"coverage-publish": "aegir-coverage publish",
|
||||||
|
"docs": "aegir-docs"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"IPFS"
|
"IPFS"
|
||||||
|
174
src/index.js
174
src/index.js
@ -1,7 +1,3 @@
|
|||||||
/*
|
|
||||||
* Id is an object representation of a peer Id. a peer Id is a multihash
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const mh = require('multihashes')
|
const mh = require('multihashes')
|
||||||
@ -9,7 +5,28 @@ const crypto = require('libp2p-crypto')
|
|||||||
const assert = require('assert')
|
const assert = require('assert')
|
||||||
const waterfall = require('async/waterfall')
|
const waterfall = require('async/waterfall')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name PeerIdJson
|
||||||
|
* @type {Object}
|
||||||
|
* @typedef {Object} PeerIdJson
|
||||||
|
* @param {string} id - `Base58` encoded peer id
|
||||||
|
* @param {string=} privKey - The RSA private key in protobuf format, encoded in `base64`.
|
||||||
|
* @param {string=} pubKey - The RSA public key in protobuf format, encoded in `base64`.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Peer ID is the SHA-256
|
||||||
|
* [multihash](https://github.com/multiformats/multihash) of a
|
||||||
|
* public key.
|
||||||
|
* @class PeerId
|
||||||
|
* @see [libp2p-crypto](htttps://github.com/libp2p/js-libp2p-crypto)
|
||||||
|
*/
|
||||||
class PeerId {
|
class PeerId {
|
||||||
|
/**
|
||||||
|
* @param {Buffer} id
|
||||||
|
* @param {RSAPrivateKey=} privKey
|
||||||
|
* @param {RSAPublickKey=} pubKey
|
||||||
|
*/
|
||||||
constructor (id, privKey, pubKey) {
|
constructor (id, privKey, pubKey) {
|
||||||
assert(Buffer.isBuffer(id), 'invalid id provided')
|
assert(Buffer.isBuffer(id), 'invalid id provided')
|
||||||
|
|
||||||
@ -17,15 +34,28 @@ class PeerId {
|
|||||||
assert(privKey.public.bytes.equals(pubKey.bytes), 'inconsistent arguments')
|
assert(privKey.public.bytes.equals(pubKey.bytes), 'inconsistent arguments')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {Buffer}
|
||||||
|
*/
|
||||||
this.id = id
|
this.id = id
|
||||||
this._privKey = privKey
|
this._privKey = privKey
|
||||||
this._pubKey = pubKey
|
this._pubKey = pubKey
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The private key of this id, if it exists.
|
||||||
|
*
|
||||||
|
* @type {RSAPrivateKey|undefined}
|
||||||
|
*/
|
||||||
get privKey () {
|
get privKey () {
|
||||||
return this._privKey
|
return this._privKey
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The public key of this id, if it exists.
|
||||||
|
*
|
||||||
|
* @type {(RSAPublicKey|undefined)}
|
||||||
|
*/
|
||||||
get pubKey () {
|
get pubKey () {
|
||||||
if (this._pubKey) {
|
if (this._pubKey) {
|
||||||
return this._pubKey
|
return this._pubKey
|
||||||
@ -36,29 +66,43 @@ class PeerId {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the protobuf version of the public key,
|
/** Create the protobuf version of the public key,
|
||||||
// matching go ipfs formatting
|
* matching go-ipfs formatting.
|
||||||
|
*
|
||||||
|
* @returns {Buffer} - The marshalled public key
|
||||||
|
*/
|
||||||
marshalPubKey () {
|
marshalPubKey () {
|
||||||
if (this.pubKey) {
|
if (this.pubKey) {
|
||||||
return crypto.marshalPublicKey(this.pubKey)
|
return crypto.marshalPublicKey(this.pubKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the protobuf version of the private key,
|
/** Create the protobuf version of the private key,
|
||||||
// matching go ipfs formatting
|
* matching go-ipfs formatting.
|
||||||
|
*
|
||||||
|
* @returns {Buffer}
|
||||||
|
*/
|
||||||
marshalPrivKey () {
|
marshalPrivKey () {
|
||||||
if (this.privKey) {
|
if (this.privKey) {
|
||||||
return crypto.marshalPrivateKey(this.privKey)
|
return crypto.marshalPrivateKey(this.privKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// pretty print
|
/**
|
||||||
|
* Alias for `toJSON`.
|
||||||
|
*
|
||||||
|
* @returns {PeerIdJson}
|
||||||
|
*/
|
||||||
toPrint () {
|
toPrint () {
|
||||||
return this.toJSON()
|
return this.toJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
// return the jsonified version of the key, matching the formatting
|
/**
|
||||||
// of go-ipfs for its config file
|
* Return the jsonified version of the key, matching the formatting
|
||||||
|
* of go-ipfs for its config file.
|
||||||
|
*
|
||||||
|
* @returns {PeerIdJson}
|
||||||
|
*/
|
||||||
toJSON () {
|
toJSON () {
|
||||||
return {
|
return {
|
||||||
id: mh.toB58String(this.id),
|
id: mh.toB58String(this.id),
|
||||||
@ -67,25 +111,53 @@ class PeerId {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// encode/decode functions
|
/**
|
||||||
|
* Returns the Peer ID's `id` as a hex string.
|
||||||
|
*
|
||||||
|
* @returns {String}
|
||||||
|
*/
|
||||||
toHexString () {
|
toHexString () {
|
||||||
return mh.toHexString(this.id)
|
return mh.toHexString(this.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the Peer ID's `id` as a buffer.
|
||||||
|
*
|
||||||
|
* @returns {Buffer}
|
||||||
|
*/
|
||||||
toBytes () {
|
toBytes () {
|
||||||
return this.id
|
return this.id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the Peer ID's `id` as a base58 string.
|
||||||
|
*
|
||||||
|
* @returns {String}
|
||||||
|
*/
|
||||||
toB58String () {
|
toB58String () {
|
||||||
return mh.toB58String(this.id)
|
return mh.toB58String(this.id)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
exports = module.exports = PeerId
|
/**
|
||||||
exports.Buffer = Buffer
|
* Create a new `PeerId` by generating a new public/private keypair.
|
||||||
|
*
|
||||||
// generation
|
* @param {Object=} opts - Configuration object.
|
||||||
exports.create = function (opts, callback) {
|
* @param {number} [opts.bits=2048] - How many bits to use for the RSA key generation.
|
||||||
|
* @param {function(Error, PeerId)} callback - Node.js style callback.
|
||||||
|
* @returns {undefined}
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* const PeerId = require('peer-id')
|
||||||
|
*
|
||||||
|
* PeerId.create((err, id) => {
|
||||||
|
* if (err) {
|
||||||
|
* throw err
|
||||||
|
* }
|
||||||
|
* console.log('id', JSON.stringify(id))
|
||||||
|
* })
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
static create (opts, callback) {
|
||||||
if (typeof opts === 'function') {
|
if (typeof opts === 'function') {
|
||||||
callback = opts
|
callback = opts
|
||||||
opts = {}
|
opts = {}
|
||||||
@ -107,20 +179,45 @@ exports.create = function (opts, callback) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromHexString = function (str) {
|
/**
|
||||||
|
* Creates a Peer ID from hex string representing the key's multihash.
|
||||||
|
*
|
||||||
|
* @param {string} str - Hex encoded id
|
||||||
|
* @returns {PeerId}
|
||||||
|
*/
|
||||||
|
static createFromHexString (str) {
|
||||||
return new PeerId(mh.fromHexString(str))
|
return new PeerId(mh.fromHexString(str))
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromBytes = function (buf) {
|
/**
|
||||||
|
* Creates a Peer ID from a buffer representing the key's multihash.
|
||||||
|
*
|
||||||
|
* @param {Buffer} buf
|
||||||
|
* @returns {PeerId}
|
||||||
|
*/
|
||||||
|
static createFromBytes (buf) {
|
||||||
return new PeerId(buf)
|
return new PeerId(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromB58String = function (str) {
|
/**
|
||||||
|
* Creates a Peer ID from a `base58` string representing the
|
||||||
|
* key's multihash.
|
||||||
|
*
|
||||||
|
* @param {string} str - `base58` encoded id
|
||||||
|
* @returns {PeerId}
|
||||||
|
*/
|
||||||
|
static createFromB58String (str) {
|
||||||
return new PeerId(mh.fromB58String(str))
|
return new PeerId(mh.fromB58String(str))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Public Key input will be a buffer
|
/**
|
||||||
exports.createFromPubKey = function (key, callback) {
|
* Creates a Peer ID from a buffer containing a public key.
|
||||||
|
*
|
||||||
|
* @param {string|Buffer} key
|
||||||
|
* @param {function(Error, PeerId)} callback
|
||||||
|
* @returns {undefined}
|
||||||
|
*/
|
||||||
|
static createFromPubKey (key, callback) {
|
||||||
let buf = key
|
let buf = key
|
||||||
if (typeof buf === 'string') {
|
if (typeof buf === 'string') {
|
||||||
buf = new Buffer(key, 'base64')
|
buf = new Buffer(key, 'base64')
|
||||||
@ -140,8 +237,15 @@ exports.createFromPubKey = function (key, callback) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private key input will be a string
|
/**
|
||||||
exports.createFromPrivKey = function (key, callback) {
|
* Creates a Peer ID from a buffer containing a private key.
|
||||||
|
*
|
||||||
|
* @param {string|Buffer} key - The private key, if passed as
|
||||||
|
* string `base64` encoding is assumed.
|
||||||
|
* @param {function(Error, PeerId)} callback
|
||||||
|
* @returns {undefined}
|
||||||
|
*/
|
||||||
|
static createFromPrivKey (key, callback) {
|
||||||
let buf = key
|
let buf = key
|
||||||
if (typeof buf === 'string') {
|
if (typeof buf === 'string') {
|
||||||
buf = new Buffer(key, 'base64')
|
buf = new Buffer(key, 'base64')
|
||||||
@ -165,7 +269,14 @@ exports.createFromPrivKey = function (key, callback) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromJSON = function (obj, callback) {
|
/**
|
||||||
|
* Import a `PeerId` from a serialized JSON object.
|
||||||
|
*
|
||||||
|
* @param {PeerIdJson} obj
|
||||||
|
* @param {function(Error, PeerId)} callback
|
||||||
|
* @returns {undefined}
|
||||||
|
*/
|
||||||
|
static createFromJSON (obj, callback) {
|
||||||
if (typeof callback !== 'function') {
|
if (typeof callback !== 'function') {
|
||||||
throw new Error('callback is required')
|
throw new Error('callback is required')
|
||||||
}
|
}
|
||||||
@ -209,9 +320,20 @@ exports.createFromJSON = function (obj, callback) {
|
|||||||
callback(null, new PeerId(id, null, pub))
|
callback(null, new PeerId(id, null, pub))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a given `Buffer` to a `base64` encoded string.
|
||||||
|
* If no `val` is given it just returns `undefined`.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {Buffer=} val
|
||||||
|
* @returns {string|undefined}
|
||||||
|
*/
|
||||||
function toB64Opt (val) {
|
function toB64Opt (val) {
|
||||||
if (val) {
|
if (val) {
|
||||||
return val.toString('base64')
|
return val.toString('base64')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exports = module.exports = PeerId
|
||||||
|
Reference in New Issue
Block a user