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

Commit

Permalink
fix: ed25519 key ID generation
Browse files Browse the repository at this point in the history
As discussed here: ipfs/js-ipfs#3591

Satisfy linter

test: actually verify ids
  • Loading branch information
nadimkobeissi authored and jacobheun committed Mar 15, 2021
1 parent b07978d commit bc33769
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/keys/ed25519-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

const sha = require('multihashing-async/src/sha')
const protobuf = require('protons')
const multibase = require('multibase')
const errcode = require('err-code')
const uint8ArrayEquals = require('uint8arrays/equals')
const uint8ArrayToString = require('uint8arrays/to-string')

const crypto = require('./ed25519')
const pbm = protobuf(require('./keys.proto'))
Expand Down Expand Up @@ -85,7 +85,7 @@ class Ed25519PrivateKey {
*/
async id () {
const hash = await this.public.hash()
return multibase.encode('base58btc', hash).toString().slice(1)
return uint8ArrayToString(hash, 'base58btc')
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/keys/ed25519.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ describe('ed25519', function () {
})

it('key id', async () => {
const key = await crypto.keys.unmarshalPrivateKey(fixtures.verify.privateKey)
const id = await key.id()
expect(id).to.exist()
expect(id).to.be.a('string')
expect(id).to.eql('QmUawW9qzMrBBiMrWQQMvoatadKZ9F4EwZCAMKnt3STLtz')
})

it('should export a password encrypted libp2p-key', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/keys/rsa.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ describe('RSA', function () {
})

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

describe('key equals', () => {
Expand Down
8 changes: 4 additions & 4 deletions test/keys/secp256k1.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const keysPBM = crypto.keys.keysPBM
const randomBytes = crypto.randomBytes
const secp256k1Crypto = require('../../src/keys/secp256k1')(randomBytes)
const uint8ArrayFromString = require('uint8arrays/from-string')
const fixtures = require('../fixtures/go-key-secp256k1')

describe('secp256k1 keys', () => {
let key
Expand Down Expand Up @@ -58,9 +59,10 @@ describe('secp256k1 keys', () => {
})

it('key id', async () => {
const decoded = keysPBM.PrivateKey.decode(fixtures.privateKey)
const key = await secp256k1.unmarshalSecp256k1PrivateKey(decoded.Data)
const id = await key.id()
expect(id).to.exist()
expect(id).to.be.a('string')
expect(id).to.eql('QmPCyMBGEyifPtx5aa6k6wkY9N1eBf9vHK1eKfNc35q9uq')
})

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

describe('go interop', () => {
const fixtures = require('../fixtures/go-key-secp256k1')

it('loads a private key marshaled by go-libp2p-crypto', async () => {
// we need to first extract the key data from the protobuf, which is
// normally handled by js-libp2p-crypto
Expand Down

0 comments on commit bc33769

Please sign in to comment.