From ceeff130fad916a421500b68d7123f19b137082e Mon Sep 17 00:00:00 2001 From: Topper Bowers Date: Wed, 25 Sep 2019 16:03:01 +0200 Subject: [PATCH] feat: inline public key handling for the 0.12.x line (#102) --- .travis.yml | 1 - src/index.js | 7 +++++++ test/peer-id.spec.js | 10 ++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 78bd310..9550f44 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,7 +22,6 @@ jobs: - stage: check script: - - npx aegir commitlint --travis - npx aegir dep-check - npm run lint diff --git a/src/index.js b/src/index.js index e4a7dd8..7a00bc5 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 5f7f068..f12ce46 100644 --- a/test/peer-id.spec.js +++ b/test/peer-id.spec.js @@ -50,6 +50,16 @@ describe('PeerId', () => { }) }) + it('can get the public key from a Secp256k1 key', (done) => { + PeerId.create({ keyType: 'secp256k1', bits: 256 }, (err, original) => { + expect(err).to.not.exist() + + const newId = PeerId.createFromB58String(original.toB58String()) + expect(original.pubKey.bytes).to.eql(newId.pubKey.bytes) + done() + }) + }) + it('isPeerId', (done) => { PeerId.create(testOpts, (err, id) => { expect(err).to.not.exist()