Skip to content

Commit

Permalink
inline public key handling for the 0.12.x line
Browse files Browse the repository at this point in the history
  • Loading branch information
tobowers committed Sep 25, 2019
1 parent 8f4027b commit 9c34d42
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions test/peer-id.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 9c34d42

Please sign in to comment.