Skip to content

Commit

Permalink
fix: handle dnslink pointing to cid peer id
Browse files Browse the repository at this point in the history
fixes #721
  • Loading branch information
2color committed Jan 22, 2025
1 parent c7024de commit 6f9076c
Showing 1 changed file with 11 additions and 3 deletions.
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))
}

Check warning on line 77 in packages/ipns/src/dnslink.ts

View check run for this annotation

Codecov / codecov/patch

packages/ipns/src/dnslink.ts#L75-L77

Added lines #L75 - L77 were not covered by tests

// if the result is a PeerId, we've reached the end of the recursion
return {
Expand Down

0 comments on commit 6f9076c

Please sign in to comment.