From 86bb92bfc69e119ae880716f7b072bed494b609f Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Sat, 26 Oct 2019 00:26:28 +0200 Subject: [PATCH] feat: support CIDs in /ipns/ content paths This adds support for PeerIDs as CIDs in /ipns/ paths. See https://github.com/libp2p/specs/issues/216 for full context. License: MIT Signed-off-by: Marcin Rataj --- src/core/ipns/resolver.js | 3 ++- test/core/name.spec.js | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/core/ipns/resolver.js b/src/core/ipns/resolver.js index 0970eecdfc..d830517a35 100644 --- a/src/core/ipns/resolver.js +++ b/src/core/ipns/resolver.js @@ -4,6 +4,7 @@ const ipns = require('ipns') const crypto = require('libp2p-crypto') const PeerId = require('peer-id') const errcode = require('err-code') +const CID = require('cids') const debug = require('debug') const log = debug('ipfs:ipns:resolver') @@ -74,7 +75,7 @@ class IpnsResolver { // resolve ipns entries from the provided routing async _resolveName (name) { - const peerId = PeerId.createFromB58String(name) + const peerId = PeerId.createFromBytes(new CID(name).multihash) // TODO: change to `PeerId.createFromCID` when https://github.com/libp2p/js-peer-id/pull/105 lands and js-ipfs switched to async peer-id lib const { routingKey } = ipns.getIdKeys(peerId.toBytes()) let record diff --git a/test/core/name.spec.js b/test/core/name.spec.js index edad61d17e..36fb5bcbb8 100644 --- a/test/core/name.spec.js +++ b/test/core/name.spec.js @@ -13,6 +13,7 @@ const ipnsRouting = require('../../src/core/ipns/routing/config') const OfflineDatastore = require('../../src/core/ipns/routing/offline-datastore') const PubsubDatastore = require('../../src/core/ipns/routing/pubsub-datastore') const { Key, Errors } = require('interface-datastore') +const CID = require('cids') const DaemonFactory = require('ipfsd-ctl') const df = DaemonFactory.create({ @@ -372,6 +373,16 @@ describe('name', function () { expect(value).to.exist() }) + + it('should resolve an ipns path with PeerID as CIDv1 correctly', async function () { + const res = await node.add(fixture) + await node.name.publish(`/ipfs/${res[0].hash}`) + let peerCid = new CID(nodeId) + if (peerCid.version === 0) peerCid = peerCid.toV1() // future-proofing + const value = await ipnsPath.resolvePath(node, `/ipns/${peerCid.toString('base32')}`) + + expect(value).to.exist() + }) }) describe('ipns.routing', function () {