mirror of
https://github.com/fluencelabs/js-libp2p-crypto
synced 2025-03-15 08:41:05 +00:00
feat: add id()
method to Secp256k1PrivateKey
Feature parity with ed25519 and rsa
This commit is contained in:
parent
3a8bab9f44
commit
f4dbd62e49
@ -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",
|
||||
|
20
src/index.js
20
src/index.js
@ -1,5 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const bs58 = require('bs58')
|
||||
const multihashing = require('multihashing-async')
|
||||
|
||||
module.exports = (keysProtobuf, randomBytes, crypto) => {
|
||||
@ -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) {
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user