From 1c16dd3dec8a641f55187bd9fbb6c03ba5fafdaa Mon Sep 17 00:00:00 2001 From: Nadim Kobeissi Date: Wed, 17 Mar 2021 19:01:53 +0100 Subject: [PATCH] fix: ed25519 PeerID generation (#186) * Fix Ed25519 PeerID generation This commit pushes further fixes to the generation of Ed25519 peer IDs, building upon the discussion in ipfs/js-ipfs#3591 and the subsequent pull request libp2p/js-libp2p-crypto#185. The purpose of this new pull request is to harmonize the encoding of PeerIDs for Ed25519 keys such that the same new format is used everywhere: peer IDs when assigned upon key generation, peer IDs when shown via key listing, as well as the peer IDs displayed as IPNS names when the key is used as the basis for an IPNS record. Concretely, this changes the peer ID representation of Ed25519 keys from the `Qm...` format to the newer `1...` format. The accompanying test has been modified accordingly. * Satisfy linter --- package.json | 1 + src/keys/ed25519-class.js | 7 +++---- test/keys/ed25519.spec.js | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 17c2959..05b2306 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "keypair": "^1.0.1", "multibase": "^3.0.0", "multicodec": "^2.0.0", + "multihashes": "^4.0.2", "multihashing-async": "^2.0.1", "node-forge": "^0.10.0", "pem-jwk": "^2.0.0", diff --git a/src/keys/ed25519-class.js b/src/keys/ed25519-class.js index ec57209..b6b549e 100644 --- a/src/keys/ed25519-class.js +++ b/src/keys/ed25519-class.js @@ -4,8 +4,7 @@ const sha = require('multihashing-async/src/sha') const protobuf = require('protons') const errcode = require('err-code') const uint8ArrayEquals = require('uint8arrays/equals') -const uint8ArrayToString = require('uint8arrays/to-string') - +const mh = require('multihashes') const crypto = require('./ed25519') const pbm = protobuf(require('./keys.proto')) const exporter = require('./exporter') @@ -84,8 +83,8 @@ class Ed25519PrivateKey { * @returns {Promise} */ async id () { - const hash = await this.public.hash() - return uint8ArrayToString(hash, 'base58btc') + const encoding = mh.encode(this.public.bytes, 'identity') + return await mh.toB58String(encoding) } /** diff --git a/test/keys/ed25519.spec.js b/test/keys/ed25519.spec.js index e457b33..a8d3589 100644 --- a/test/keys/ed25519.spec.js +++ b/test/keys/ed25519.spec.js @@ -82,7 +82,7 @@ describe('ed25519', function () { it('key id', async () => { const key = await crypto.keys.unmarshalPrivateKey(fixtures.verify.privateKey) const id = await key.id() - expect(id).to.eql('QmUawW9qzMrBBiMrWQQMvoatadKZ9F4EwZCAMKnt3STLtz') + expect(id).to.eql('12D3KooWLqLxEfJ9nDdEe8Kh8PFvNPQRYDQBwyL7CMM7HhVd5LsX') }) it('should export a password encrypted libp2p-key', async () => {