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 (#2566)
Browse files Browse the repository at this point in the history
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 <lidel@lidel.org>
  • Loading branch information
lidel authored and Alan Shaw committed Nov 6, 2019
1 parent 607b531 commit 4fa39fb
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@
"execa": "^3.0.0",
"form-data": "^2.5.1",
"hat": "0.0.3",
"interface-ipfs-core": "^0.117.2",
"interface-ipfs-core": "^0.118.0",
"ipfs-interop": "^0.1.1",
"ipfsd-ctl": "^0.47.2",
"libp2p-websocket-star": "~0.10.2",
Expand Down
4 changes: 2 additions & 2 deletions src/core/components/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const human = require('human-to-milliseconds')
const crypto = require('libp2p-crypto')
const errcode = require('err-code')
const mergeOptions = require('merge-options')
const mh = require('multihashes')
const CID = require('cids')
const isDomain = require('is-domain-name')
const promisify = require('promisify-es6')

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

const [namespace, hash, ...remainder] = name.slice(1).split('/')
try {
mh.fromB58String(hash)
new CID(hash) // eslint-disable-line no-new
} catch (err) {
// lets check if we have a domain ex. /ipns/ipfs.io and resolve with dns
if (isDomain(hash)) {
Expand Down
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 in Base32 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
27 changes: 27 additions & 0 deletions test/http-api/inject/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* eslint-env mocha */
'use strict'

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

Expand Down Expand Up @@ -45,5 +46,31 @@ module.exports = (http) => {
expect(res).to.exist()
expect(res.result.Path).to.satisfy(checkAll([`/ipfs/${cid}`]))
})

it('should publish and resolve a record with explicit CIDv1 in Base32', async function () {
this.timeout(160 * 1000)

// ensure PeerID is represented as CIDv1 in Base32
const { id } = await http.api._ipfs.id()
let cidv1 = new CID(id)
if (cidv1.version === 0) cidv1 = cidv1.toV1() // future-proofing
const peerIdAsCidv1b32 = cidv1.toString('base32')

let res = await api.inject({
method: 'GET',
url: `/api/v0/name/publish?arg=${cid}&resolve=false`
})

expect(res).to.exist()
expect(res.result.Value).to.equal(`/ipfs/${cid}`)

res = await api.inject({
method: 'GET',
url: `/api/v0/name/resolve?arg=${peerIdAsCidv1b32}`
})

expect(res).to.exist()
expect(res.result.Path).to.satisfy(checkAll([`/ipfs/${cid}`]))
})
})
}

0 comments on commit 4fa39fb

Please sign in to comment.