mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-07-04 02:21:48 +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
|
||||
|
||||
dist
|
||||
docs
|
111
README.md
111
README.md
@ -22,22 +22,6 @@
|
||||
- [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers)
|
||||
- [Browser: `<script>` Tag](#browser-script-tag)
|
||||
- [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)
|
||||
|
||||
# Description
|
||||
@ -55,7 +39,6 @@ to the multihash for ID generation.*
|
||||
|
||||
```js
|
||||
var PeerId = require('peer-id')
|
||||
var bs58 = require('bs58')
|
||||
|
||||
PeerId.create({ bits: 1024 }, (err, id) => {
|
||||
console.log(JSON.stringify(id.toJSON(), null, 2)
|
||||
@ -110,100 +93,6 @@ the global namespace.
|
||||
|
||||
# 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
|
||||
|
||||
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-major": "aegir-release --type major",
|
||||
"coverage": "aegir-coverage",
|
||||
"coverage-publish": "aegir-coverage publish"
|
||||
"coverage-publish": "aegir-coverage publish",
|
||||
"docs": "aegir-docs"
|
||||
},
|
||||
"keywords": [
|
||||
"IPFS"
|
||||
|
384
src/index.js
384
src/index.js
@ -1,7 +1,3 @@
|
||||
/*
|
||||
* Id is an object representation of a peer Id. a peer Id is a multihash
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
const mh = require('multihashes')
|
||||
@ -9,7 +5,28 @@ const crypto = require('libp2p-crypto')
|
||||
const assert = require('assert')
|
||||
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 {
|
||||
/**
|
||||
* @param {Buffer} id
|
||||
* @param {RSAPrivateKey=} privKey
|
||||
* @param {RSAPublickKey=} pubKey
|
||||
*/
|
||||
constructor (id, privKey, pubKey) {
|
||||
assert(Buffer.isBuffer(id), 'invalid id provided')
|
||||
|
||||
@ -17,15 +34,28 @@ class PeerId {
|
||||
assert(privKey.public.bytes.equals(pubKey.bytes), 'inconsistent arguments')
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {Buffer}
|
||||
*/
|
||||
this.id = id
|
||||
this._privKey = privKey
|
||||
this._pubKey = pubKey
|
||||
}
|
||||
|
||||
/**
|
||||
* The private key of this id, if it exists.
|
||||
*
|
||||
* @type {RSAPrivateKey|undefined}
|
||||
*/
|
||||
get privKey () {
|
||||
return this._privKey
|
||||
}
|
||||
|
||||
/**
|
||||
* The public key of this id, if it exists.
|
||||
*
|
||||
* @type {(RSAPublicKey|undefined)}
|
||||
*/
|
||||
get pubKey () {
|
||||
if (this._pubKey) {
|
||||
return this._pubKey
|
||||
@ -36,29 +66,43 @@ class PeerId {
|
||||
}
|
||||
}
|
||||
|
||||
// Return the protobuf version of the public key,
|
||||
// matching go ipfs formatting
|
||||
/** Create the protobuf version of the public key,
|
||||
* matching go-ipfs formatting.
|
||||
*
|
||||
* @returns {Buffer} - The marshalled public key
|
||||
*/
|
||||
marshalPubKey () {
|
||||
if (this.pubKey) {
|
||||
return crypto.marshalPublicKey(this.pubKey)
|
||||
}
|
||||
}
|
||||
|
||||
// Return the protobuf version of the private key,
|
||||
// matching go ipfs formatting
|
||||
/** Create the protobuf version of the private key,
|
||||
* matching go-ipfs formatting.
|
||||
*
|
||||
* @returns {Buffer}
|
||||
*/
|
||||
marshalPrivKey () {
|
||||
if (this.privKey) {
|
||||
return crypto.marshalPrivateKey(this.privKey)
|
||||
}
|
||||
}
|
||||
|
||||
// pretty print
|
||||
/**
|
||||
* Alias for `toJSON`.
|
||||
*
|
||||
* @returns {PeerIdJson}
|
||||
*/
|
||||
toPrint () {
|
||||
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 () {
|
||||
return {
|
||||
id: mh.toB58String(this.id),
|
||||
@ -67,151 +111,229 @@ class PeerId {
|
||||
}
|
||||
}
|
||||
|
||||
// encode/decode functions
|
||||
/**
|
||||
* Returns the Peer ID's `id` as a hex string.
|
||||
*
|
||||
* @returns {String}
|
||||
*/
|
||||
toHexString () {
|
||||
return mh.toHexString(this.id)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Peer ID's `id` as a buffer.
|
||||
*
|
||||
* @returns {Buffer}
|
||||
*/
|
||||
toBytes () {
|
||||
return this.id
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Peer ID's `id` as a base58 string.
|
||||
*
|
||||
* @returns {String}
|
||||
*/
|
||||
toB58String () {
|
||||
return mh.toB58String(this.id)
|
||||
}
|
||||
}
|
||||
|
||||
exports = module.exports = PeerId
|
||||
exports.Buffer = Buffer
|
||||
|
||||
// generation
|
||||
exports.create = function (opts, callback) {
|
||||
if (typeof opts === 'function') {
|
||||
callback = opts
|
||||
opts = {}
|
||||
}
|
||||
opts = opts || {}
|
||||
opts.bits = opts.bits || 2048
|
||||
|
||||
waterfall([
|
||||
(cb) => crypto.generateKeyPair('RSA', opts.bits, cb),
|
||||
(privKey, cb) => privKey.public.hash((err, digest) => {
|
||||
cb(err, digest, privKey)
|
||||
})
|
||||
], (err, digest, privKey) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
/**
|
||||
* Create a new `PeerId` by generating a new public/private keypair.
|
||||
*
|
||||
* @param {Object=} opts - Configuration object.
|
||||
* @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') {
|
||||
callback = opts
|
||||
opts = {}
|
||||
}
|
||||
opts = opts || {}
|
||||
opts.bits = opts.bits || 2048
|
||||
|
||||
callback(null, new PeerId(digest, privKey))
|
||||
})
|
||||
}
|
||||
|
||||
exports.createFromHexString = function (str) {
|
||||
return new PeerId(mh.fromHexString(str))
|
||||
}
|
||||
|
||||
exports.createFromBytes = function (buf) {
|
||||
return new PeerId(buf)
|
||||
}
|
||||
|
||||
exports.createFromB58String = function (str) {
|
||||
return new PeerId(mh.fromB58String(str))
|
||||
}
|
||||
|
||||
// Public Key input will be a buffer
|
||||
exports.createFromPubKey = function (key, callback) {
|
||||
let buf = key
|
||||
if (typeof buf === 'string') {
|
||||
buf = new Buffer(key, 'base64')
|
||||
}
|
||||
|
||||
if (typeof callback !== 'function') {
|
||||
throw new Error('callback is required')
|
||||
}
|
||||
|
||||
const pubKey = crypto.unmarshalPublicKey(buf)
|
||||
pubKey.hash((err, digest) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
callback(null, new PeerId(digest, null, pubKey))
|
||||
})
|
||||
}
|
||||
|
||||
// Private key input will be a string
|
||||
exports.createFromPrivKey = function (key, callback) {
|
||||
let buf = key
|
||||
if (typeof buf === 'string') {
|
||||
buf = new Buffer(key, 'base64')
|
||||
}
|
||||
|
||||
if (typeof callback !== 'function') {
|
||||
throw new Error('callback is required')
|
||||
}
|
||||
|
||||
waterfall([
|
||||
(cb) => crypto.unmarshalPrivateKey(buf, cb),
|
||||
(privKey, cb) => privKey.public.hash((err, digest) => {
|
||||
cb(err, digest, privKey)
|
||||
})
|
||||
], (err, digest, privKey) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
callback(null, new PeerId(digest, privKey))
|
||||
})
|
||||
}
|
||||
|
||||
exports.createFromJSON = function (obj, callback) {
|
||||
if (typeof callback !== 'function') {
|
||||
throw new Error('callback is required')
|
||||
}
|
||||
|
||||
const id = mh.fromB58String(obj.id)
|
||||
const rawPrivKey = obj.privKey && new Buffer(obj.privKey, 'base64')
|
||||
const rawPubKey = obj.pubKey && new Buffer(obj.pubKey, 'base64')
|
||||
const pub = rawPubKey && crypto.unmarshalPublicKey(rawPubKey)
|
||||
|
||||
if (rawPrivKey) {
|
||||
waterfall([
|
||||
(cb) => crypto.unmarshalPrivateKey(rawPrivKey, cb),
|
||||
(priv, cb) => priv.public.hash((err, digest) => {
|
||||
cb(err, digest, priv)
|
||||
}),
|
||||
(privDigest, priv, cb) => {
|
||||
if (pub) {
|
||||
pub.hash((err, pubDigest) => {
|
||||
cb(err, privDigest, priv, pubDigest)
|
||||
})
|
||||
} else {
|
||||
cb(null, privDigest, priv)
|
||||
}
|
||||
}
|
||||
], (err, privDigest, priv, pubDigest) => {
|
||||
(cb) => crypto.generateKeyPair('RSA', opts.bits, cb),
|
||||
(privKey, cb) => privKey.public.hash((err, digest) => {
|
||||
cb(err, digest, privKey)
|
||||
})
|
||||
], (err, digest, privKey) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
if (pub && !privDigest.equals(pubDigest)) {
|
||||
return callback(new Error('Public and private key do not match'))
|
||||
}
|
||||
|
||||
if (id && !privDigest.equals(id)) {
|
||||
return callback(new Error('Id and private key do not match'))
|
||||
}
|
||||
|
||||
callback(null, new PeerId(id, priv, pub))
|
||||
callback(null, new PeerId(digest, privKey))
|
||||
})
|
||||
} else {
|
||||
callback(null, new PeerId(id, null, pub))
|
||||
}
|
||||
|
||||
/**
|
||||
* 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))
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Peer ID from a buffer representing the key's multihash.
|
||||
*
|
||||
* @param {Buffer} buf
|
||||
* @returns {PeerId}
|
||||
*/
|
||||
static createFromBytes (buf) {
|
||||
return new PeerId(buf)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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))
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
if (typeof buf === 'string') {
|
||||
buf = new Buffer(key, 'base64')
|
||||
}
|
||||
|
||||
if (typeof callback !== 'function') {
|
||||
throw new Error('callback is required')
|
||||
}
|
||||
|
||||
const pubKey = crypto.unmarshalPublicKey(buf)
|
||||
pubKey.hash((err, digest) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
callback(null, new PeerId(digest, null, pubKey))
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
if (typeof buf === 'string') {
|
||||
buf = new Buffer(key, 'base64')
|
||||
}
|
||||
|
||||
if (typeof callback !== 'function') {
|
||||
throw new Error('callback is required')
|
||||
}
|
||||
|
||||
waterfall([
|
||||
(cb) => crypto.unmarshalPrivateKey(buf, cb),
|
||||
(privKey, cb) => privKey.public.hash((err, digest) => {
|
||||
cb(err, digest, privKey)
|
||||
})
|
||||
], (err, digest, privKey) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
callback(null, new PeerId(digest, privKey))
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 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') {
|
||||
throw new Error('callback is required')
|
||||
}
|
||||
|
||||
const id = mh.fromB58String(obj.id)
|
||||
const rawPrivKey = obj.privKey && new Buffer(obj.privKey, 'base64')
|
||||
const rawPubKey = obj.pubKey && new Buffer(obj.pubKey, 'base64')
|
||||
const pub = rawPubKey && crypto.unmarshalPublicKey(rawPubKey)
|
||||
|
||||
if (rawPrivKey) {
|
||||
waterfall([
|
||||
(cb) => crypto.unmarshalPrivateKey(rawPrivKey, cb),
|
||||
(priv, cb) => priv.public.hash((err, digest) => {
|
||||
cb(err, digest, priv)
|
||||
}),
|
||||
(privDigest, priv, cb) => {
|
||||
if (pub) {
|
||||
pub.hash((err, pubDigest) => {
|
||||
cb(err, privDigest, priv, pubDigest)
|
||||
})
|
||||
} else {
|
||||
cb(null, privDigest, priv)
|
||||
}
|
||||
}
|
||||
], (err, privDigest, priv, pubDigest) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
if (pub && !privDigest.equals(pubDigest)) {
|
||||
return callback(new Error('Public and private key do not match'))
|
||||
}
|
||||
|
||||
if (id && !privDigest.equals(id)) {
|
||||
return callback(new Error('Id and private key do not match'))
|
||||
}
|
||||
|
||||
callback(null, new PeerId(id, priv, pub))
|
||||
})
|
||||
} else {
|
||||
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) {
|
||||
if (val) {
|
||||
return val.toString('base64')
|
||||
}
|
||||
}
|
||||
|
||||
exports = module.exports = PeerId
|
||||
|
Reference in New Issue
Block a user