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

Commit bc33769

Browse files
nadimkobeissijacobheun
authored andcommitted
fix: ed25519 key ID generation
As discussed here: ipfs/js-ipfs#3591 Satisfy linter test: actually verify ids
1 parent b07978d commit bc33769

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

src/keys/ed25519-class.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
const sha = require('multihashing-async/src/sha')
44
const protobuf = require('protons')
5-
const multibase = require('multibase')
65
const errcode = require('err-code')
76
const uint8ArrayEquals = require('uint8arrays/equals')
7+
const uint8ArrayToString = require('uint8arrays/to-string')
88

99
const crypto = require('./ed25519')
1010
const pbm = protobuf(require('./keys.proto'))
@@ -85,7 +85,7 @@ class Ed25519PrivateKey {
8585
*/
8686
async id () {
8787
const hash = await this.public.hash()
88-
return multibase.encode('base58btc', hash).toString().slice(1)
88+
return uint8ArrayToString(hash, 'base58btc')
8989
}
9090

9191
/**

test/keys/ed25519.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ describe('ed25519', function () {
8080
})
8181

8282
it('key id', async () => {
83+
const key = await crypto.keys.unmarshalPrivateKey(fixtures.verify.privateKey)
8384
const id = await key.id()
84-
expect(id).to.exist()
85-
expect(id).to.be.a('string')
85+
expect(id).to.eql('QmUawW9qzMrBBiMrWQQMvoatadKZ9F4EwZCAMKnt3STLtz')
8686
})
8787

8888
it('should export a password encrypted libp2p-key', async () => {

test/keys/rsa.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ describe('RSA', function () {
5959
})
6060

6161
it('key id', async () => {
62+
const key = await crypto.keys.unmarshalPrivateKey(uint8ArrayFromString('CAASqAkwggSkAgEAAoIBAQCk0O+6oNRxhcdZe2GxEDrFBkDV4TZFZnp2ly/dL1cGMBql/8oXPZgei6h7+P5zzfDq2YCfwbjbf0IVY1AshRl6B5VGE1WS+9p1y1OZxJf5os6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CEbrPkleq33Ob7uEtfAty4aBTTHe6uEwQKBgQCB+2q8RyMSXNuADhFlzOFXGrOwJm0bEUUMTPrduRQUyt4e1qOqA3klnXe3mqGcxBpnlEe/76/JacvNom6Ikxx16a0qpYRU8OWz0KU1fR6vrrEgV98241k5t6sdL4+MGA1Bo5xyXtzLb1hdUh3vpDwVU2OrnC+To3iXus/b5EBiMQKBgEI1OaBcFiyjgLGEyFKoZbtzH1mdatTExfrAQqCjOVjQByoMpGhHTXwEaosvyYu63Pa8AJPT7juSGaiKYEJFcXO9BiNyVfmQiqSHJcYeuh+fmO9IlHRHgy5xaIIC00AHS2vC/gXwmXAdPis6BZqDJeiCuOLWJ94QXn8JBT8IgGAI', 'base64pad'))
6263
const id = await key.id()
63-
expect(id).to.exist()
64-
expect(id).to.be.a('string')
64+
expect(id).to.eql('QmQgsppVMDUpe83wcAqaemKbYvHeF127gnSFQ1xFnBodVw')
6565
})
6666

6767
describe('key equals', () => {

test/keys/secp256k1.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const keysPBM = crypto.keys.keysPBM
1111
const randomBytes = crypto.randomBytes
1212
const secp256k1Crypto = require('../../src/keys/secp256k1')(randomBytes)
1313
const uint8ArrayFromString = require('uint8arrays/from-string')
14+
const fixtures = require('../fixtures/go-key-secp256k1')
1415

1516
describe('secp256k1 keys', () => {
1617
let key
@@ -58,9 +59,10 @@ describe('secp256k1 keys', () => {
5859
})
5960

6061
it('key id', async () => {
62+
const decoded = keysPBM.PrivateKey.decode(fixtures.privateKey)
63+
const key = await secp256k1.unmarshalSecp256k1PrivateKey(decoded.Data)
6164
const id = await key.id()
62-
expect(id).to.exist()
63-
expect(id).to.be.a('string')
65+
expect(id).to.eql('QmPCyMBGEyifPtx5aa6k6wkY9N1eBf9vHK1eKfNc35q9uq')
6466
})
6567

6668
it('should export a password encrypted libp2p-key', async () => {
@@ -254,8 +256,6 @@ describe('crypto functions', () => {
254256
})
255257

256258
describe('go interop', () => {
257-
const fixtures = require('../fixtures/go-key-secp256k1')
258-
259259
it('loads a private key marshaled by go-libp2p-crypto', async () => {
260260
// we need to first extract the key data from the protobuf, which is
261261
// normally handled by js-libp2p-crypto

0 commit comments

Comments
 (0)