mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-07-04 20:31:54 +00:00
Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
4fbf859efc | |||
52bd949b43 | |||
544ca7d74b | |||
11d4ec10bd | |||
4d5bb2cfff | |||
a2ac32ac0f | |||
ed211a943b | |||
8ea480a3b0 | |||
f39fb24321 | |||
911aa634d3 |
@ -22,7 +22,6 @@ jobs:
|
|||||||
|
|
||||||
- stage: check
|
- stage: check
|
||||||
script:
|
script:
|
||||||
- npx aegir commitlint --travis
|
|
||||||
- npx aegir dep-check
|
- npx aegir dep-check
|
||||||
- npm run lint
|
- npm run lint
|
||||||
|
|
||||||
|
25
CHANGELOG.md
25
CHANGELOG.md
@ -1,3 +1,28 @@
|
|||||||
|
<a name="0.13.4"></a>
|
||||||
|
## [0.13.4](https://github.com/libp2p/js-peer-id/compare/v0.13.3...v0.13.4) (2019-11-04)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* bang in bin.js ([#106](https://github.com/libp2p/js-peer-id/issues/106)) ([11d4ec1](https://github.com/libp2p/js-peer-id/commit/11d4ec1))
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* support Peer ID represented as CID ([#105](https://github.com/libp2p/js-peer-id/issues/105)) ([544ca7d](https://github.com/libp2p/js-peer-id/commit/544ca7d))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<a name="0.13.3"></a>
|
||||||
|
## [0.13.3](https://github.com/libp2p/js-peer-id/compare/v0.13.2...v0.13.3) (2019-09-25)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* allow nested PeerIds to support pubKey function when using identity encoding ([#101](https://github.com/libp2p/js-peer-id/issues/101)) ([f39fb24](https://github.com/libp2p/js-peer-id/commit/f39fb24))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a name="0.13.2"></a>
|
<a name="0.13.2"></a>
|
||||||
## [0.13.2](https://github.com/libp2p/js-peer-id/compare/v0.13.1...v0.13.2) (2019-07-12)
|
## [0.13.2](https://github.com/libp2p/js-peer-id/compare/v0.13.1...v0.13.2) (2019-07-12)
|
||||||
|
|
||||||
|
41
README.md
41
README.md
@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
## Lead Maintainer
|
## Lead Maintainer
|
||||||
|
|
||||||
[Pedro Teixeira](https://github.com/pgte)
|
[Vasco Santos](https://github.com/vasco-santos)
|
||||||
|
|
||||||
## Table of Contents
|
## Table of Contents
|
||||||
|
|
||||||
@ -32,14 +32,16 @@
|
|||||||
- [Import](#import)
|
- [Import](#import)
|
||||||
- [`createFromHexString(str)`](#createfromhexstringstr)
|
- [`createFromHexString(str)`](#createfromhexstringstr)
|
||||||
- [`createFromBytes(buf)`](#createfrombytesbuf)
|
- [`createFromBytes(buf)`](#createfrombytesbuf)
|
||||||
|
- [`createFromCID(cid)`](#createfromcidcid)
|
||||||
- [`createFromB58String(str)`](#createfromb58stringstr)
|
- [`createFromB58String(str)`](#createfromb58stringstr)
|
||||||
- [`createFromPubKey(pubKey)`](#createfrompubkeypubkey)
|
- [`createFromPubKey(pubKey)`](#createfrompubkeypubkey)
|
||||||
- [`createFromPrivKey(privKey)`](#createfromprivkeyprivkey)
|
- [`createFromPrivKey(privKey)`](#createfromprivkeyprivkey)
|
||||||
- [`createFromJSON(obj)`](#createfromjsonobj)
|
- [`createFromJSON(obj)`](#createfromjsonobj)
|
||||||
- [Export](#export)
|
- [Export](#export)
|
||||||
- [`toHexString()`](#tohexstring)
|
|
||||||
- [`toBytes()`](#tobytes)
|
- [`toBytes()`](#tobytes)
|
||||||
|
- [`toString()`](#tostring)
|
||||||
- [`toB58String()`](#tob58string)
|
- [`toB58String()`](#tob58string)
|
||||||
|
- [`toHexString()`](#tohexstring)
|
||||||
- [`toJSON()`](#tojson)
|
- [`toJSON()`](#tojson)
|
||||||
- [`toPrint()`](#toprint)
|
- [`toPrint()`](#toprint)
|
||||||
- [License](#license)
|
- [License](#license)
|
||||||
@ -145,6 +147,14 @@ Creates a Peer ID from a buffer representing the key's multihash.
|
|||||||
|
|
||||||
Returns `PeerId`.
|
Returns `PeerId`.
|
||||||
|
|
||||||
|
### `createFromCID(cid)`
|
||||||
|
|
||||||
|
- `cid: CID|String|Buffer` - The multihash encoded as [CID](https://github.com/ipld/js-cid) (object, `String` or `Buffer`)
|
||||||
|
|
||||||
|
Creates a Peer ID from a CID representation of the key's multihash ([RFC 0001](https://github.com/libp2p/specs/blob/master/RFC/0001-text-peerid-cid.md)).
|
||||||
|
|
||||||
|
Returns `PeerId`.
|
||||||
|
|
||||||
### `createFromB58String(str)`
|
### `createFromB58String(str)`
|
||||||
|
|
||||||
Creates a Peer ID from a Base58 string representing the key's multihash.
|
Creates a Peer ID from a Base58 string representing the key's multihash.
|
||||||
@ -197,9 +207,18 @@ 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>
|
<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>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### `toString()`
|
||||||
|
|
||||||
|
Returns the Peer ID's `id` as a self-describing CIDv1 in Base32 ([RFC 0001](https://github.com/libp2p/specs/blob/master/RFC/0001-text-peerid-cid.md))
|
||||||
|
|
||||||
|
```
|
||||||
|
bafzbeigweq4zr4x4ky2dvv7nanbkw6egutvrrvzw6g3h2rftp7gidyhtt4
|
||||||
|
```
|
||||||
|
|
||||||
### `toB58String()`
|
### `toB58String()`
|
||||||
|
|
||||||
Returns the Peer ID's `id` as a base58 string.
|
Returns the Peer ID's `id` as a base58 string (multihash/CIDv0).
|
||||||
|
|
||||||
```
|
```
|
||||||
QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi
|
QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi
|
||||||
@ -213,10 +232,24 @@ Returns an `obj` of the form
|
|||||||
- `obj.pubKey: String` - The public key in protobuf format, encoded in 'base64'
|
- `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'
|
- `obj.privKey: String` - The private key in protobuf format, encoded in 'base 64'
|
||||||
|
|
||||||
### `marshal(excludePrivatKey)`
|
### `marshal(excludePrivateKey)`
|
||||||
|
|
||||||
Returns a protocol-buffers encoded version of the id, public key and, if `excludePrivateKey` is not set, the private key.
|
Returns a protocol-buffers encoded version of the id, public key and, if `excludePrivateKey` is not set, the private key.
|
||||||
|
|
||||||
|
### `marshalPubKey()`
|
||||||
|
|
||||||
|
Returns a protobuf of just the public key, compatible with `libp2p-crypto` (unlike `marshal` above).
|
||||||
|
|
||||||
|
For example:
|
||||||
|
```js
|
||||||
|
const crypto = require('libp2p-crypto')
|
||||||
|
|
||||||
|
PeerId.create({ bits: 256, keyType: 'ed25519' }).then( pid => {
|
||||||
|
let pk = crypto.keys.unmarshalPublicKey(pid.marshalPubKey())
|
||||||
|
// your code here
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### `toPrint()`
|
### `toPrint()`
|
||||||
|
|
||||||
Returns the Peer ID as a printable string without the `Qm` prefix.
|
Returns the Peer ID as a printable string without the `Qm` prefix.
|
||||||
|
12
package.json
12
package.json
@ -1,8 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "peer-id",
|
"name": "peer-id",
|
||||||
"version": "0.13.2",
|
"version": "0.13.4",
|
||||||
"description": "IPFS Peer Id implementation in Node.js",
|
"description": "IPFS Peer Id implementation in Node.js",
|
||||||
"leadMaintainer": "Pedro Teixeira <i@pgte.me>",
|
"leadMaintainer": "Vasco Santos <santos.vasco10@gmail.com>",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"bin": "src/bin.js",
|
"bin": "src/bin.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -34,12 +34,13 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/libp2p/js-peer-id",
|
"homepage": "https://github.com/libp2p/js-peer-id",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^19.0.5",
|
"aegir": "^20.0.0",
|
||||||
"bundlesize": "~0.18.0",
|
"bundlesize": "~0.18.0",
|
||||||
"chai": "^4.2.0",
|
"chai": "^4.2.0",
|
||||||
"dirty-chai": "^2.0.1"
|
"dirty-chai": "^2.0.1"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"cids": "~0.7.1",
|
||||||
"class-is": "^1.1.0",
|
"class-is": "^1.1.0",
|
||||||
"libp2p-crypto": "~0.17.0",
|
"libp2p-crypto": "~0.17.0",
|
||||||
"multihashes": "~0.4.15",
|
"multihashes": "~0.4.15",
|
||||||
@ -58,14 +59,17 @@
|
|||||||
"Hugo Dias <hugomrdias@gmail.com>",
|
"Hugo Dias <hugomrdias@gmail.com>",
|
||||||
"Jacob Heun <jacobheun@gmail.com>",
|
"Jacob Heun <jacobheun@gmail.com>",
|
||||||
"Maciej Krüger <mkg20001@gmail.com>",
|
"Maciej Krüger <mkg20001@gmail.com>",
|
||||||
|
"Marcin Rataj <lidel@lidel.org>",
|
||||||
"Michael Garvin <gar+gh@danger.computer>",
|
"Michael Garvin <gar+gh@danger.computer>",
|
||||||
|
"Nate Foss <npfoss@gmail.com>",
|
||||||
"Pedro Teixeira <i@pgte.me>",
|
"Pedro Teixeira <i@pgte.me>",
|
||||||
"Prashanth Chandra <coolshanth94@gmail.com>",
|
"Prashanth Chandra <coolshanth94@gmail.com>",
|
||||||
"Richard Littauer <richard.littauer@gmail.com>",
|
"Richard Littauer <richard.littauer@gmail.com>",
|
||||||
"Richard Schneider <makaretu@gmail.com>",
|
"Richard Schneider <makaretu@gmail.com>",
|
||||||
"Stephen Whitmore <stephen.whitmore@gmail.com>",
|
"Stephen Whitmore <stephen.whitmore@gmail.com>",
|
||||||
"Vasco Santos <vasco.santos@ua.pt>",
|
"Topper Bowers <topper@quorumcontrol.com>",
|
||||||
"Vasco Santos <vasco.santos@moxy.studio>",
|
"Vasco Santos <vasco.santos@moxy.studio>",
|
||||||
|
"Vasco Santos <vasco.santos@ua.pt>",
|
||||||
"Yahya <ya7yaz@gmail.com>",
|
"Yahya <ya7yaz@gmail.com>",
|
||||||
"greenkeeperio-bot <support@greenkeeper.io>",
|
"greenkeeperio-bot <support@greenkeeper.io>",
|
||||||
"nginnever <ginneversource@gmail.com>",
|
"nginnever <ginneversource@gmail.com>",
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#!/usr/local/bin/node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
39
src/index.js
39
src/index.js
@ -5,6 +5,7 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const mh = require('multihashes')
|
const mh = require('multihashes')
|
||||||
|
const CID = require('cids')
|
||||||
const cryptoKeys = require('libp2p-crypto/src/keys')
|
const cryptoKeys = require('libp2p-crypto/src/keys')
|
||||||
const assert = require('assert')
|
const assert = require('assert')
|
||||||
const withIs = require('class-is')
|
const withIs = require('class-is')
|
||||||
@ -48,6 +49,13 @@ class PeerId {
|
|||||||
if (this._privKey) {
|
if (this._privKey) {
|
||||||
return this._privKey.public
|
return this._privKey.public
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const decoded = mh.decode(this.id)
|
||||||
|
|
||||||
|
if (decoded.name === 'identity') {
|
||||||
|
this._pubKey = cryptoKeys.unmarshalPublicKey(decoded.digest)
|
||||||
|
return this._pubKey
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
set pubKey (pubKey) {
|
set pubKey (pubKey) {
|
||||||
@ -115,6 +123,16 @@ class PeerId {
|
|||||||
return this._idB58String
|
return this._idB58String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return self-describing String representation
|
||||||
|
// in default format from RFC 0001: https://github.com/libp2p/specs/pull/209
|
||||||
|
toString () {
|
||||||
|
if (!this._idCIDString) {
|
||||||
|
const cid = new CID(1, 'libp2p-key', this.id, 'base32')
|
||||||
|
this._idCIDString = cid.toBaseEncodedString('base32')
|
||||||
|
}
|
||||||
|
return this._idCIDString
|
||||||
|
}
|
||||||
|
|
||||||
isEqual (id) {
|
isEqual (id) {
|
||||||
if (Buffer.isBuffer(id)) {
|
if (Buffer.isBuffer(id)) {
|
||||||
return this.id.equals(id)
|
return this.id.equals(id)
|
||||||
@ -177,7 +195,18 @@ exports.createFromBytes = (buf) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromB58String = (str) => {
|
exports.createFromB58String = (str) => {
|
||||||
return new PeerIdWithIs(mh.fromB58String(str))
|
return exports.createFromCID(str) // B58String is CIDv0
|
||||||
|
}
|
||||||
|
|
||||||
|
const validMulticodec = (cid) => {
|
||||||
|
// supported: 'libp2p-key' (CIDv1) and 'dag-pb' (CIDv0 converted to CIDv1)
|
||||||
|
return cid.codec === 'libp2p-key' || cid.codec === 'dag-pb'
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.createFromCID = (cid) => {
|
||||||
|
cid = CID.isCID(cid) ? cid : new CID(cid)
|
||||||
|
if (!validMulticodec(cid)) throw new Error('Supplied PeerID CID has invalid multicodec: ' + cid.codec)
|
||||||
|
return new PeerIdWithIs(cid.multihash)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Public Key input will be a buffer
|
// Public Key input will be a buffer
|
||||||
@ -213,10 +242,10 @@ exports.createFromPrivKey = async (key) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromJSON = async (obj) => {
|
exports.createFromJSON = async (obj) => {
|
||||||
let id = mh.fromB58String(obj.id)
|
const id = mh.fromB58String(obj.id)
|
||||||
let rawPrivKey = obj.privKey && Buffer.from(obj.privKey, 'base64')
|
const rawPrivKey = obj.privKey && Buffer.from(obj.privKey, 'base64')
|
||||||
let rawPubKey = obj.pubKey && Buffer.from(obj.pubKey, 'base64')
|
const rawPubKey = obj.pubKey && Buffer.from(obj.pubKey, 'base64')
|
||||||
let pub = rawPubKey && await cryptoKeys.unmarshalPublicKey(rawPubKey)
|
const pub = rawPubKey && await cryptoKeys.unmarshalPublicKey(rawPubKey)
|
||||||
|
|
||||||
if (!rawPrivKey) {
|
if (!rawPrivKey) {
|
||||||
return new PeerIdWithIs(id, null, pub)
|
return new PeerIdWithIs(id, null, pub)
|
||||||
|
@ -8,6 +8,7 @@ chai.use(dirtyChai)
|
|||||||
const expect = chai.expect
|
const expect = chai.expect
|
||||||
const crypto = require('libp2p-crypto')
|
const crypto = require('libp2p-crypto')
|
||||||
const mh = require('multihashes')
|
const mh = require('multihashes')
|
||||||
|
const CID = require('cids')
|
||||||
|
|
||||||
const PeerId = require('../src')
|
const PeerId = require('../src')
|
||||||
|
|
||||||
@ -17,6 +18,8 @@ const testId = require('./fixtures/sample-id')
|
|||||||
const testIdHex = testId.id
|
const testIdHex = testId.id
|
||||||
const testIdBytes = mh.fromHexString(testId.id)
|
const testIdBytes = mh.fromHexString(testId.id)
|
||||||
const testIdB58String = mh.toB58String(testIdBytes)
|
const testIdB58String = mh.toB58String(testIdBytes)
|
||||||
|
const testIdCID = new CID(1, 'libp2p-key', testIdBytes)
|
||||||
|
const testIdCIDString = testIdCID.toBaseEncodedString('base32')
|
||||||
|
|
||||||
const goId = require('./fixtures/go-private-key')
|
const goId = require('./fixtures/go-private-key')
|
||||||
|
|
||||||
@ -42,6 +45,12 @@ describe('PeerId', () => {
|
|||||||
expect(id.toB58String()).to.equal(expB58)
|
expect(id.toB58String()).to.equal(expB58)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('can get the public key from a Secp256k1 key', async () => {
|
||||||
|
const original = await PeerId.create({ keyType: 'secp256k1', bits: 256 })
|
||||||
|
const newId = PeerId.createFromB58String(original.toB58String())
|
||||||
|
expect(original.pubKey.bytes).to.eql(newId.pubKey.bytes)
|
||||||
|
})
|
||||||
|
|
||||||
it('isPeerId', async () => {
|
it('isPeerId', async () => {
|
||||||
const id = await PeerId.create(testOpts)
|
const id = await PeerId.create(testOpts)
|
||||||
expect(PeerId.isPeerId(id)).to.equal(true)
|
expect(PeerId.isPeerId(id)).to.equal(true)
|
||||||
@ -57,27 +66,96 @@ describe('PeerId', () => {
|
|||||||
}).to.throw(/immutable/)
|
}).to.throw(/immutable/)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('recreate an Id from Hex string', () => {
|
it('recreate from Hex string', () => {
|
||||||
const id = PeerId.createFromHexString(testIdHex)
|
const id = PeerId.createFromHexString(testIdHex)
|
||||||
expect(testIdBytes).to.deep.equal(id.id)
|
expect(testIdBytes).to.deep.equal(id.toBytes())
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Recreate an Id from a Buffer', () => {
|
it('recreate from a Buffer', () => {
|
||||||
const id = PeerId.createFromBytes(testIdBytes)
|
const id = PeerId.createFromBytes(testIdBytes)
|
||||||
expect(testId.id).to.equal(id.toHexString())
|
expect(testId.id).to.equal(id.toHexString())
|
||||||
|
expect(testIdBytes).to.deep.equal(id.toBytes())
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Recreate a B58 String', () => {
|
it('recreate from a B58 String', () => {
|
||||||
const id = PeerId.createFromB58String(testIdB58String)
|
const id = PeerId.createFromB58String(testIdB58String)
|
||||||
expect(testIdB58String).to.equal(id.toB58String())
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
|
expect(testIdBytes).to.deep.equal(id.toBytes())
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Recreate from a Public Key', async () => {
|
it('recreate from CID object', () => {
|
||||||
|
const id = PeerId.createFromCID(testIdCID)
|
||||||
|
expect(testIdCIDString).to.equal(id.toString())
|
||||||
|
expect(testIdBytes).to.deep.equal(id.toBytes())
|
||||||
|
})
|
||||||
|
|
||||||
|
it('recreate from Base58 String (CIDv0))', () => {
|
||||||
|
const id = PeerId.createFromCID(testIdB58String)
|
||||||
|
expect(testIdCIDString).to.equal(id.toString())
|
||||||
|
expect(testIdBytes).to.deep.equal(id.toBytes())
|
||||||
|
})
|
||||||
|
|
||||||
|
it('recreate from CIDv1 Base32 (libp2p-key multicodec)', () => {
|
||||||
|
const cid = new CID(1, 'libp2p-key', testIdBytes)
|
||||||
|
const cidString = cid.toBaseEncodedString('base32')
|
||||||
|
const id = PeerId.createFromCID(cidString)
|
||||||
|
expect(cidString).to.equal(id.toString())
|
||||||
|
expect(testIdBytes).to.deep.equal(id.toBytes())
|
||||||
|
})
|
||||||
|
|
||||||
|
it('recreate from CIDv1 Base32 (dag-pb multicodec)', () => {
|
||||||
|
const cid = new CID(1, 'dag-pb', testIdBytes)
|
||||||
|
const cidString = cid.toBaseEncodedString('base32')
|
||||||
|
const id = PeerId.createFromCID(cidString)
|
||||||
|
// toString should return CID with multicodec set to libp2p-key
|
||||||
|
expect(new CID(id.toString()).codec).to.equal('libp2p-key')
|
||||||
|
expect(testIdBytes).to.deep.equal(id.toBytes())
|
||||||
|
})
|
||||||
|
|
||||||
|
it('recreate from CID Buffer', () => {
|
||||||
|
const id = PeerId.createFromCID(testIdCID.buffer)
|
||||||
|
expect(testIdCIDString).to.equal(id.toString())
|
||||||
|
expect(testIdBytes).to.deep.equal(id.toBytes())
|
||||||
|
})
|
||||||
|
|
||||||
|
it('throws on invalid CID multicodec', () => {
|
||||||
|
// only libp2p and dag-pb are supported
|
||||||
|
const invalidCID = new CID(1, 'raw', testIdBytes).toBaseEncodedString('base32')
|
||||||
|
expect(() => {
|
||||||
|
PeerId.createFromCID(invalidCID)
|
||||||
|
}).to.throw(/Supplied PeerID CID has invalid multicodec: raw/)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('throws on invalid CID value', () => {
|
||||||
|
// using function code that does not represent valid hash function
|
||||||
|
// https://github.com/multiformats/js-multihash/blob/b85999d5768bf06f1b0f16b926ef2cb6d9c14265/src/constants.js#L345
|
||||||
|
const invalidCID = 'QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L'
|
||||||
|
expect(() => {
|
||||||
|
PeerId.createFromCID(invalidCID)
|
||||||
|
}).to.throw(/multihash unknown function code: 0x50/)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('throws on invalid CID object', () => {
|
||||||
|
const invalidCID = {}
|
||||||
|
expect(() => {
|
||||||
|
PeerId.createFromCID(invalidCID)
|
||||||
|
}).to.throw(/Invalid version, must be a number equal to 1 or 0/)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('throws on invalid CID object', () => {
|
||||||
|
const invalidCID = {}
|
||||||
|
expect(() => {
|
||||||
|
PeerId.createFromCID(invalidCID)
|
||||||
|
}).to.throw(/Invalid version, must be a number equal to 1 or 0/)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('recreate from a Public Key', async () => {
|
||||||
const id = await PeerId.createFromPubKey(testId.pubKey)
|
const id = await PeerId.createFromPubKey(testId.pubKey)
|
||||||
expect(testIdB58String).to.equal(id.toB58String())
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
|
expect(testIdBytes).to.deep.equal(id.toBytes())
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Recreate from a Private Key', async () => {
|
it('recreate from a Private Key', async () => {
|
||||||
const id = await PeerId.createFromPrivKey(testId.privKey)
|
const id = await PeerId.createFromPrivKey(testId.privKey)
|
||||||
expect(testIdB58String).to.equal(id.toB58String())
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
const encoded = Buffer.from(testId.privKey, 'base64')
|
const encoded = Buffer.from(testId.privKey, 'base64')
|
||||||
@ -86,7 +164,7 @@ describe('PeerId', () => {
|
|||||||
expect(id.marshalPubKey()).to.deep.equal(id2.marshalPubKey())
|
expect(id.marshalPubKey()).to.deep.equal(id2.marshalPubKey())
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Recreate from Protobuf', async () => {
|
it('recreate from Protobuf', async () => {
|
||||||
const id = await PeerId.createFromProtobuf(testId.marshaled)
|
const id = await PeerId.createFromProtobuf(testId.marshaled)
|
||||||
expect(testIdB58String).to.equal(id.toB58String())
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
const encoded = Buffer.from(testId.privKey, 'base64')
|
const encoded = Buffer.from(testId.privKey, 'base64')
|
||||||
|
Reference in New Issue
Block a user