feat: add id() method to Secp256k1PrivateKey

Feature parity with ed25519 and rsa
This commit is contained in:
Alberto Elias 2019-02-20 13:23:05 +01:00 committed by Friedel Ziegelmayer
parent 3a8bab9f44
commit f4dbd62e49
3 changed files with 30 additions and 0 deletions

View File

@ -28,6 +28,7 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"async": "^2.6.1", "async": "^2.6.1",
"bs58": "^4.0.1",
"multihashing-async": "~0.5.1", "multihashing-async": "~0.5.1",
"nodeify": "^1.0.1", "nodeify": "^1.0.1",
"safe-buffer": "^5.1.2", "safe-buffer": "^5.1.2",

View File

@ -1,5 +1,6 @@
'use strict' 'use strict'
const bs58 = require('bs58')
const multihashing = require('multihashing-async') const multihashing = require('multihashing-async')
module.exports = (keysProtobuf, randomBytes, crypto) => { module.exports = (keysProtobuf, randomBytes, crypto) => {
@ -73,6 +74,25 @@ module.exports = (keysProtobuf, randomBytes, crypto) => {
ensure(callback) ensure(callback)
multihashing(this.bytes, 'sha2-256', 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) { function unmarshalSecp256k1PrivateKey (bytes, callback) {

View File

@ -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', () => { describe('key equals', () => {
it('equals itself', () => { it('equals itself', () => {
expect(key.equals(key)).to.eql(true) expect(key.equals(key)).to.eql(true)