Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 86bb92b

Browse files
committed
feat: support CIDs in /ipns/ content paths
This adds support for PeerIDs as CIDs in /ipns/ paths. See libp2p/specs#216 for full context. License: MIT Signed-off-by: Marcin Rataj <lidel@lidel.org>
1 parent b289a19 commit 86bb92b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/core/ipns/resolver.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const ipns = require('ipns')
44
const crypto = require('libp2p-crypto')
55
const PeerId = require('peer-id')
66
const errcode = require('err-code')
7+
const CID = require('cids')
78

89
const debug = require('debug')
910
const log = debug('ipfs:ipns:resolver')
@@ -74,7 +75,7 @@ class IpnsResolver {
7475

7576
// resolve ipns entries from the provided routing
7677
async _resolveName (name) {
77-
const peerId = PeerId.createFromB58String(name)
78+
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
7879
const { routingKey } = ipns.getIdKeys(peerId.toBytes())
7980
let record
8081

test/core/name.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const ipnsRouting = require('../../src/core/ipns/routing/config')
1313
const OfflineDatastore = require('../../src/core/ipns/routing/offline-datastore')
1414
const PubsubDatastore = require('../../src/core/ipns/routing/pubsub-datastore')
1515
const { Key, Errors } = require('interface-datastore')
16+
const CID = require('cids')
1617

1718
const DaemonFactory = require('ipfsd-ctl')
1819
const df = DaemonFactory.create({
@@ -372,6 +373,16 @@ describe('name', function () {
372373

373374
expect(value).to.exist()
374375
})
376+
377+
it('should resolve an ipns path with PeerID as CIDv1 correctly', async function () {
378+
const res = await node.add(fixture)
379+
await node.name.publish(`/ipfs/${res[0].hash}`)
380+
let peerCid = new CID(nodeId)
381+
if (peerCid.version === 0) peerCid = peerCid.toV1() // future-proofing
382+
const value = await ipnsPath.resolvePath(node, `/ipns/${peerCid.toString('base32')}`)
383+
384+
expect(value).to.exist()
385+
})
375386
})
376387

377388
describe('ipns.routing', function () {

0 commit comments

Comments
 (0)