Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
feat: add id() method to Secp256k1PrivateKey
Browse files Browse the repository at this point in the history
Feature parity with ed25519 and rsa
  • Loading branch information
AlbertoElias authored and dignifiedquire committed Feb 20, 2019
1 parent 3a8bab9 commit f4dbd62
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"license": "MIT",
"dependencies": {
"async": "^2.6.1",
"bs58": "^4.0.1",
"multihashing-async": "~0.5.1",
"nodeify": "^1.0.1",
"safe-buffer": "^5.1.2",
Expand Down
20 changes: 20 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const bs58 = require('bs58')
const multihashing = require('multihashing-async')

module.exports = (keysProtobuf, randomBytes, crypto) => {
Expand Down Expand Up @@ -73,6 +74,25 @@ module.exports = (keysProtobuf, randomBytes, crypto) => {
ensure(callback)
multihashing(this.bytes, 'sha2-256', callback)
}

/**
* Gets the ID of the key.
*
* The key id is the base58 encoding of the SHA-256 multihash of its public key.
* The public key is a protobuf encoding containing a type and the DER encoding
* of the PKCS SubjectPublicKeyInfo.
*
* @param {function(Error, id)} callback
* @returns {undefined}
*/
id (callback) {
this.public.hash((err, hash) => {
if (err) {
return callback(err)
}
callback(null, bs58.encode(hash))
})
}
}

function unmarshalSecp256k1PrivateKey (bytes, callback) {
Expand Down
9 changes: 9 additions & 0 deletions test/secp256k1.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ describe('secp256k1 keys', () => {
})
})

it('key id', (done) => {
key.id((err, id) => {
expect(err).to.not.exist()
expect(id).to.exist()
expect(id).to.be.a('string')
done()
})
})

describe('key equals', () => {
it('equals itself', () => {
expect(key.equals(key)).to.eql(true)
Expand Down

0 comments on commit f4dbd62

Please sign in to comment.