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

feat: js-ipfs support of CIDs in /ipns/ content paths #2566

Merged
merged 3 commits into from
Nov 6, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
4 changes: 2 additions & 2 deletions src/core/components/name.js
Original file line number Diff line number Diff line change
@@ -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')

@@ -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)) {
3 changes: 2 additions & 1 deletion src/core/ipns/resolver.js
Original file line number Diff line number Diff line change
@@ -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

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

@@ -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}`]))
})
})
}