From 7c6c672c659a6007b6ee4ca25a1538feb627dd5d Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Mon, 22 Jun 2020 17:57:37 +0100 Subject: [PATCH] fix: key encoding (#48) The boolean argument to `new Key` specifies whether to try to clean the key up - remove trailing slashes, etc. Here we're putting potentially non-printable characters into the key so we don't want to do any such operation. fixes ipfs/js-ipfs#2930 --- src/index.js | 4 ++-- test/index.spec.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index dd42697..812f1a8 100644 --- a/src/index.js +++ b/src/index.js @@ -231,9 +231,9 @@ const getIdKeys = (pid) => { const ipnsBuffer = Buffer.from('/ipns/') return { - routingPubKey: new Key(Buffer.concat([pkBuffer, pid])), // Added on https://github.com/ipfs/js-ipns/pull/8#issue-213857876 (pkKey will be deprecated in a future release) + routingPubKey: new Key(Buffer.concat([pkBuffer, pid]), false), // Added on https://github.com/ipfs/js-ipns/pull/8#issue-213857876 (pkKey will be deprecated in a future release) pkKey: new Key(rawStdEncoding(Buffer.concat([pkBuffer, pid]))), - routingKey: new Key(Buffer.concat([ipnsBuffer, pid])), // Added on https://github.com/ipfs/js-ipns/pull/6#issue-213631461 (ipnsKey will be deprecated in a future release) + routingKey: new Key(Buffer.concat([ipnsBuffer, pid]), false), // Added on https://github.com/ipfs/js-ipns/pull/6#issue-213631461 (ipnsKey will be deprecated in a future release) ipnsKey: new Key(rawStdEncoding(Buffer.concat([ipnsBuffer, pid]))) } } diff --git a/test/index.spec.js b/test/index.spec.js index 38957ce..eed414f 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -10,6 +10,7 @@ const expect = chai.expect chai.use(dirtyChai) chai.use(chaiBytes) chai.use(chaiString) +const { toB58String } = require('multihashes') const ipfs = require('ipfs') const ipfsHttpClient = require('ipfs-http-client') @@ -157,6 +158,20 @@ describe('ipns', function () { expect(idKeys.routingKey).to.not.startsWith('/ipns/') }) + it('should be able to turn routing key back into id', () => { + const keys = [ + 'QmQd5Enz5tzP8u5wHur8ADuJMbcNhEf86CkWkqRzoWUhst', + 'QmW6mcoqDKJRch2oph2FmvZhPLJn6wPU648Vv9iMyMtmtG' + ] + + keys.forEach(key => { + const { routingKey } = ipns.getIdKeys(fromB58String(key)) + const id = toB58String(routingKey.toBuffer().slice(ipns.namespaceLength)) + + expect(id).to.equal(key) + }) + }) + it('should be able to embed a public key in an ipns record', async () => { const sequence = 0 const validity = 1000000