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

Commit bc1a355

Browse files
committed
feat: support CIDs in /ipns/ content paths
This adds support for resolving PeerIDs as CIDs in /ipns/ paths. See libp2p/specs#216 for full context. License: MIT Signed-off-by: Marcin Rataj <[email protected]>
1 parent b289a19 commit bc1a355

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

src/core/components/name.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const human = require('human-to-milliseconds')
66
const crypto = require('libp2p-crypto')
77
const errcode = require('err-code')
88
const mergeOptions = require('merge-options')
9-
const mh = require('multihashes')
9+
const CID = require('cids')
1010
const isDomain = require('is-domain-name')
1111
const promisify = require('promisify-es6')
1212

@@ -155,7 +155,7 @@ module.exports = function name (self) {
155155

156156
const [namespace, hash, ...remainder] = name.slice(1).split('/')
157157
try {
158-
mh.fromB58String(hash)
158+
new CID(hash) // eslint-disable-line no-new
159159
} catch (err) {
160160
// lets check if we have a domain ex. /ipns/ipfs.io and resolve with dns
161161
if (isDomain(hash)) {

src/core/ipns/resolver.js

+2-1
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

+11
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 in Base32 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 () {

test/http-api/inject/name.js

+27
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* eslint-env mocha */
33
'use strict'
44

5+
const CID = require('cids')
56
const { expect } = require('interface-ipfs-core/src/utils/mocha')
67
const checkAll = (bits) => string => bits.every(bit => string.includes(bit))
78

@@ -45,5 +46,31 @@ module.exports = (http) => {
4546
expect(res).to.exist()
4647
expect(res.result.Path).to.satisfy(checkAll([`/ipfs/${cid}`]))
4748
})
49+
50+
it('should publish and resolve a record with explicit CIDv1 in Base32', async function () {
51+
this.timeout(160 * 1000)
52+
53+
// ensure PeerID is represented as CIDv1 in Base32
54+
const { id } = await http.api._ipfs.id()
55+
let cidv1 = new CID(id)
56+
if (cidv1.version === 0) cidv1 = cidv1.toV1() // future-proofing
57+
const peerIdAsCidv1b32 = cidv1.toString('base32')
58+
59+
let res = await api.inject({
60+
method: 'GET',
61+
url: `/api/v0/name/publish?arg=${cid}&resolve=false`
62+
})
63+
64+
expect(res).to.exist()
65+
expect(res.result.Value).to.equal(`/ipfs/${cid}`)
66+
67+
res = await api.inject({
68+
method: 'GET',
69+
url: `/api/v0/name/resolve?arg=${peerIdAsCidv1b32}`
70+
})
71+
72+
expect(res).to.exist()
73+
expect(res.result.Path).to.satisfy(checkAll([`/ipfs/${cid}`]))
74+
})
4875
})
4976
}

0 commit comments

Comments
 (0)