Skip to content
Merged
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion packages/dnslink/src/dnslink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ export class DNSLink <Namespaces extends Record<string, DNSLinkParser<DNSLinkRes

if (protocol === 'dnslink') {
// if the result was another DNSLink domain, try to follow it
return await this.recursiveResolveDomain(domainOrCID, depth - 1, options)
output.push(...await this.recursiveResolveDomain(domainOrCID, depth - 1, options))
continue
}

const parser = this.namespaces[protocol]
Expand All @@ -169,6 +170,14 @@ export class DNSLink <Namespaces extends Record<string, DNSLinkParser<DNSLinkRes
continue
}

const record = parser(result, answer)

if (record.namespace === 'dnslink') {
// if the result was another DNSLink domain, try to follow it
output.push(...await this.recursiveResolveDomain(record.value, depth - 1, options))
continue
}

output.push(parser(result, answer))
} catch (err: any) {
this.log.error('could not parse DNS link record for domain %s, %s - %e', domain, answer.data, err)
Expand Down
22 changes: 22 additions & 0 deletions packages/dnslink/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,28 @@ export interface DNSLinkIPNSResult {
path: string
}

export interface DNSLinkDNSLinkResult {
/**
* The resolved record
*/
answer: Answer

/**
* The IPNS namespace
*/
namespace: 'dnslink'

/**
* The resolved value
*/
value: string

/**
* If the resolved value is an IPFS path, it will be present here
*/
path: string
}

export interface DNSLinkResolveResult {
/**
* The resolved record
Expand Down
26 changes: 18 additions & 8 deletions packages/dnslink/src/namespaces/ipns.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import { peerIdFromString } from '@libp2p/peer-id'
import { InvalidNamespaceError } from '../errors.ts'
import type { DNSLinkParser, DNSLinkIPNSResult } from '../index.js'
import type { DNSLinkParser, DNSLinkIPNSResult, DNSLinkDNSLinkResult } from '../index.js'
import type { Answer } from '@multiformats/dns'

export const ipns: DNSLinkParser<DNSLinkIPNSResult> = (value: string, answer: Answer): DNSLinkIPNSResult => {
export const ipns: DNSLinkParser<DNSLinkIPNSResult | DNSLinkDNSLinkResult> = (value: string, answer: Answer): DNSLinkIPNSResult | DNSLinkDNSLinkResult => {
const [, protocol, peerId, ...rest] = value.split('/')

if (protocol !== 'ipns') {
throw new InvalidNamespaceError(`Namespace ${protocol} was not "ipns"`)
}

// if the result is a CID, we've reached the end of the recursion
return {
namespace: 'ipns',
peerId: peerIdFromString(peerId),
path: rest.length > 0 ? `/${rest.join('/')}` : '',
answer
try {
// if the result parses as a PeerId, we've reached the end of the recursion
return {
namespace: 'ipns',
peerId: peerIdFromString(peerId),
path: rest.length > 0 ? `/${rest.join('/')}` : '',
answer
}
} catch {
// if the value did not parse as a PeerId, it's probably another DNSLink
return {
namespace: 'dnslink',
value: peerId,
path: rest.length > 0 ? `/${rest.join('/')}` : '',
answer
}
}
}
47 changes: 47 additions & 0 deletions packages/dnslink/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,51 @@ describe('dnslink', () => {
expect(result).to.have.nested.property('[0].namespace', 'hello')
expect(result).to.have.nested.property('[0].value', 'world')
})

it('should resolve recursive DNSLink names', async () => {
dns.query.withArgs('_dnslink.foobar.baz').resolves(dnsResponse([{
name: '_dnslink.foobar.baz.',
TTL: 60,
type: RecordType.TXT,
data: 'dnslink=/ipns/qux.quux'
}]))

dns.query.withArgs('_dnslink.qux.quux').resolves(dnsResponse([{
name: '_dnslink.qux.quux.',
TTL: 60,
type: RecordType.TXT,
data: 'dnslink=/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
}]))

const result = await name.resolve('foobar.baz')

expect(result).to.have.deep.nested.property('[0].cid', CID.parse('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'))
})

it('should resolve multiple recursive DNSLink names', async () => {
dns.query.withArgs('_dnslink.foobar.baz').resolves(dnsResponse([{
name: '_dnslink.foobar.baz.',
TTL: 60,
type: RecordType.TXT,
data: 'dnslink=/ipns/qux.quux'
}, {
name: '_dnslink.foobar.baz.',
TTL: 60,
type: RecordType.TXT,
// spellchecker:disable-next-line
data: 'dnslink=/ipfs/bafybeifcaqowoyito3qvsmbwbiugsu4umlxn4ehu223hvtubbfvwyuxjoe'
}]))

dns.query.withArgs('_dnslink.qux.quux').resolves(dnsResponse([{
name: '_dnslink.qux.quux.',
TTL: 60,
type: RecordType.TXT,
data: 'dnslink=/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
}]))

const result = await name.resolve('foobar.baz')

expect(result).to.have.deep.nested.property('[0].cid', CID.parse('bafybeifcaqowoyito3qvsmbwbiugsu4umlxn4ehu223hvtubbfvwyuxjoe'))
expect(result).to.have.deep.nested.property('[1].cid', CID.parse('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'))
})
})