Skip to content

Commit f4eb7d2

Browse files
authored
fix: handle multihashes in dns link results (#1072)
Fixes regression caused when removing peer ids
1 parent 257989d commit f4eb7d2

3 files changed

Lines changed: 45 additions & 14 deletions

File tree

packages/dnslink/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@
5757
},
5858
"devDependencies": {
5959
"@libp2p/logger": "^6.2.8",
60-
"@libp2p/peer-id": "^6.0.10",
61-
"@types/dns-packet": "^5.6.5",
6260
"aegir": "^48.0.11",
6361
"delay": "^7.0.0",
6462
"sinon-ts": "^2.0.0"

packages/dnslink/src/namespaces/ipns.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { base58btc } from 'multiformats/bases/base58'
12
import { CID } from 'multiformats/cid'
3+
import * as Digest from 'multiformats/hashes/digest'
24
import { InvalidNamespaceError } from '../errors.ts'
35
import type { DNSLinkParser, DNSLinkIPNSResult, DNSLinkDNSLinkResult } from '../index.ts'
46
import type { Answer } from '@multiformats/dns'
7+
import type { MultihashDigest } from 'multiformats/cid'
58

69
export const ipns: DNSLinkParser<DNSLinkIPNSResult | DNSLinkDNSLinkResult> = (value: string, answer: Answer): DNSLinkIPNSResult | DNSLinkDNSLinkResult => {
710
const [, protocol, peerId, ...rest] = value.split('/')
@@ -11,10 +14,11 @@ export const ipns: DNSLinkParser<DNSLinkIPNSResult | DNSLinkDNSLinkResult> = (va
1114
}
1215

1316
try {
14-
// if the result parses as a PeerId, we've reached the end of the recursion
17+
// if the result parses as a base58btc encoded multihash or a CID, we've
18+
// reached the end of the recursion
1519
return {
1620
namespace: 'ipns',
17-
value: CID.parse(peerId).multihash,
21+
value: decode(peerId),
1822
path: rest.length > 0 ? `/${rest.join('/')}` : '',
1923
answer
2024
}
@@ -28,3 +32,14 @@ export const ipns: DNSLinkParser<DNSLinkIPNSResult | DNSLinkDNSLinkResult> = (va
2832
}
2933
}
3034
}
35+
36+
/**
37+
* Attempt to decode the passed string as a base58btc encoded multihash or a CID
38+
*/
39+
function decode (str: string): MultihashDigest {
40+
try {
41+
return Digest.decode(base58btc.baseDecode(str))
42+
} catch {
43+
return CID.parse(str).multihash
44+
}
45+
}

packages/dnslink/test/index.spec.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { NotFoundError } from '@libp2p/interface'
22
import { defaultLogger } from '@libp2p/logger'
3-
import { peerIdFromString } from '@libp2p/peer-id'
43
import { RecordType } from '@multiformats/dns'
54
import { expect } from 'aegir/chai'
65
import delay from 'delay'
7-
import { base36 } from 'multiformats/bases/base36'
86
import { base58btc } from 'multiformats/bases/base58'
97
import { CID } from 'multiformats/cid'
108
import * as Digest from 'multiformats/hashes/digest'
@@ -214,14 +212,14 @@ describe('dnslink', () => {
214212
expect(result).to.have.nested.property('[0].path', '/foobar/path/123')
215213
})
216214

217-
it('should resolve recursive dnslink -> <peerId>/<path>', async () => {
218-
const peerId = peerIdFromString('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
215+
it('should resolve recursive dnslink -> <Qm_PeerId>/<path>', async () => {
216+
const multihash = 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
219217

220218
dns.query.withArgs('_dnslink.foobar.baz').resolves(dnsResponse([{
221219
name: 'foobar.baz.',
222220
TTL: 60,
223221
type: RecordType.TXT,
224-
data: `dnslink=/ipns/${peerId.toString()}/foobar/path/123`
222+
data: `dnslink=/ipns/${multihash}/foobar/path/123`
225223
}]))
226224

227225
const result = await name.resolve('foobar.baz')
@@ -230,18 +228,38 @@ describe('dnslink', () => {
230228
throw new Error('Did not resolve entry')
231229
}
232230

233-
expect(result).to.have.deep.nested.property('[0].value', Digest.decode(base58btc.decode('zQmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')))
231+
expect(result).to.have.deep.nested.property('[0].value', Digest.decode(base58btc.baseDecode(multihash)))
232+
expect(result).to.have.nested.property('[0].path', '/foobar/path/123')
233+
})
234+
235+
it('should resolve recursive dnslink -> <12D3Koo_PeerId>/<path>', async () => {
236+
const multihash = '12D3KooWDUyFJTGJADckWJdfh2haqj6ckaXFWi1DReJ4RP8KsHg5'
237+
238+
dns.query.withArgs('_dnslink.foobar.baz').resolves(dnsResponse([{
239+
name: 'foobar.baz.',
240+
TTL: 60,
241+
type: RecordType.TXT,
242+
data: `dnslink=/ipns/${multihash}/foobar/path/123`
243+
}]))
244+
245+
const result = await name.resolve('foobar.baz')
246+
247+
if (result == null) {
248+
throw new Error('Did not resolve entry')
249+
}
250+
251+
expect(result).to.have.deep.nested.property('[0].value', Digest.decode(base58btc.baseDecode(multihash)))
234252
expect(result).to.have.nested.property('[0].path', '/foobar/path/123')
235253
})
236254

237255
it('should resolve recursive dnslink -> <IPNS_base36_CID>/<path>', async () => {
238-
const peerId = peerIdFromString('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
239-
const peerIdBase36CID = peerId.toCID().toString(base36)
256+
const cid = 'k51qzi5uqu5dhjghbwdvbo6mi40htrq6e2z4pwgp15pgv3ho1azvidttzh8yy2'
257+
240258
dns.query.withArgs('_dnslink.foobar.baz').resolves(dnsResponse([{
241259
name: 'foobar.baz.',
242260
TTL: 60,
243261
type: RecordType.TXT,
244-
data: `dnslink=/ipns/${peerIdBase36CID}/foobar/path/123`
262+
data: `dnslink=/ipns/${cid}/foobar/path/123`
245263
}]))
246264

247265
const result = await name.resolve('foobar.baz')
@@ -250,7 +268,7 @@ describe('dnslink', () => {
250268
throw new Error('Did not resolve entry')
251269
}
252270

253-
expect(result).to.have.deep.nested.property('[0].value', Digest.decode(base58btc.decode('zQmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')))
271+
expect(result).to.have.deep.nested.property('[0].value', CID.parse(cid).multihash)
254272
expect(result).to.have.nested.property('[0].path', '/foobar/path/123')
255273
})
256274

0 commit comments

Comments
 (0)