From f39fb24321da9ba45a1fd57e10d9d43764c82f92 Mon Sep 17 00:00:00 2001 From: Topper Bowers Date: Wed, 25 Sep 2019 15:39:24 +0200 Subject: [PATCH] feat: allow nested PeerIds to support pubKey function when using identity encoding (#101) --- src/index.js | 7 +++++++ test/peer-id.spec.js | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/src/index.js b/src/index.js index 1e08220..94a1980 100644 --- a/src/index.js +++ b/src/index.js @@ -48,6 +48,13 @@ class PeerId { if (this._privKey) { 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) { diff --git a/test/peer-id.spec.js b/test/peer-id.spec.js index a608f26..685866c 100644 --- a/test/peer-id.spec.js +++ b/test/peer-id.spec.js @@ -42,6 +42,12 @@ describe('PeerId', () => { 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 () => { const id = await PeerId.create(testOpts) expect(PeerId.isPeerId(id)).to.equal(true)