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

Resolving fqdn with "ipfs resolve" and "ipfs name resolve" #1970

Closed
wants to merge 2 commits into from
Closed
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
43 changes: 24 additions & 19 deletions src/core/components/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,21 @@ module.exports = (self) => {
return setImmediate(() => cb(new Error('invalid argument')))
}

// TODO remove this and update subsequent code when IPNS is implemented
if (!isIpfs.ipfsPath(name)) {
return setImmediate(() => cb(new Error('resolve non-IPFS names is not implemented')))
}

const split = name.split('/') // ['', 'ipfs', 'hash', ...path]
const cid = new CID(split[2])
const split = name.split('/') // ['', 'ipfs', 'hash or domain', ...path]
const hashOrDomain = split[2]
const path = split.slice(3).join('/')

if (split.length === 3) {
return setImmediate(() => cb(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}`))
if (isIpfs.cid(hashOrDomain)) {
return resolveCID(hashOrDomain, path, opts, cb)
}

const path = split.slice(3).join('/')

resolve(cid, path, (err, res) => {
if (err) return cb(err)
const { cid, remainderPath } = res
cb(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}${remainderPath ? '/' + remainderPath : ''}`)
})
// if its not a cid then its probably a domain name to resolve
return resolveDomain(hashOrDomain, path, opts, cb)
})

// Resolve the given CID + path to a CID.
function resolve (cid, path, callback) {
// Resolve the given CID + path to a CID (recursive).
function resolveCID (hash, path, opts, callback) {
let cid = new CID(hash)
let value, remainderPath
doUntil(
(cb) => {
Expand Down Expand Up @@ -82,8 +74,21 @@ module.exports = (self) => {
},
(err) => {
if (err) return callback(err)
callback(null, { cid, remainderPath: path })
callback(null, `/ipfs/${cidToString(cid, { base: opts.cidBase })}${remainderPath ? '/' + remainderPath : ''}`)
}
)
}

function resolveDomain (domain, path, opts, callback) {
const recursive = opts.recursive && opts.recursive.toString() === 'true'
return self.dns(domain, (err, result) => {
if (err) return callback(err)
const hash = result.split('/')[2]
const remainderPath = path
if (recursive) {
return resolveCID(hash, remainderPath, opts, callback)
}
callback(null, `/ipfs/${cidToString(hash, { base: opts.cidBase })}${remainderPath ? '/' + remainderPath : ''}`)
})
}
}