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

Commit

Permalink
feat: support CIDs in /ipns/ content paths
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
lidel committed Nov 4, 2019
1 parent b289a19 commit 86bb92b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/core/ipns/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions test/core/name.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit 86bb92b

Please sign in to comment.