Skip to content

fix: handle dnslink pointing to CID with peer id (ipns name) #722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 24, 2025
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
14 changes: 11 additions & 3 deletions packages/ipns/src/dnslink.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { } from '@libp2p/interface'
import { peerIdFromString } from '@libp2p/peer-id'
import { peerIdFromCID, peerIdFromString } from '@libp2p/peer-id'
import { RecordType } from '@multiformats/dns'
import { CID } from 'multiformats/cid'
import { DNSLinkNotFoundError } from './errors.js'
import type { ResolveDNSLinkOptions } from './index.js'
import type { Logger } from '@libp2p/interface'
import type { Logger, PeerId } from '@libp2p/interface'
import type { Answer, DNS } from '@multiformats/dns'

const MAX_RECURSIVE_DEPTH = 32
Expand Down Expand Up @@ -66,7 +66,15 @@ async function recursiveResolveDnslink (domain: string, depth: number, dns: DNS,
} catch {}
} else if (protocol === 'ipns') {
try {
const peerId = peerIdFromString(domainOrCID)
let peerId: PeerId

// eslint-disable-next-line max-depth
if (domainOrCID.charAt(0) === '1' || domainOrCID.charAt(0) === 'Q') {
peerId = peerIdFromString(domainOrCID)
} else {
// try parsing as a CID
peerId = peerIdFromCID(CID.parse(domainOrCID))
}

// if the result is a PeerId, we've reached the end of the recursion
return {
Expand Down
25 changes: 25 additions & 0 deletions packages/ipns/test/resolve-dnslink.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { RecordType } from '@multiformats/dns'
import { expect } from 'aegir/chai'
import { MemoryDatastore } from 'datastore-core'
import { base36 } from 'multiformats/bases/base36'
import { CID } from 'multiformats/cid'
import { stubInterface } from 'sinon-ts'
import { ipns } from '../src/index.js'
Expand Down Expand Up @@ -172,6 +173,30 @@
expect(result.path).to.equal('foobar/path/123')
})

it('should resolve recursive dnslink -> <IPNS_base36_CID>/<path>', async () => {
const cid = CID.parse('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
const key = await generateKeyPair('Ed25519')
const peerId = peerIdFromPrivateKey(key)
const peerIdBase36CID = peerId.toCID().toString(base36)
dns.query.withArgs('_dnslink.foobar.baz').resolves(dnsResponse([{
name: 'foobar.baz.',
TTL: 60,
type: RecordType.TXT,
data: `dnslink=/ipns/${peerIdBase36CID}/foobar/path/123`
}]))

await name.publish(key, cid)

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

if (result == null) {
throw new Error('Did not resolve entry')
}

Check warning on line 194 in packages/ipns/test/resolve-dnslink.spec.ts

View check run for this annotation

Codecov / codecov/patch

packages/ipns/test/resolve-dnslink.spec.ts#L193-L194

Added lines #L193 - L194 were not covered by tests

expect(result.cid.toString()).to.equal(cid.toV1().toString())
expect(result.path).to.equal('foobar/path/123')
})

it('should follow CNAMES to delegated DNSLink domains', async () => {
const cid = CID.parse('bafybeifcaqowoyito3qvsmbwbiugsu4umlxn4ehu223hvtubbfvwyuxjoe')
const key = await generateKeyPair('Ed25519')
Expand Down
Loading