Skip to content

Commit

Permalink
fix: handle cid encoded peerid in parse-url-string
Browse files Browse the repository at this point in the history
  • Loading branch information
2color committed Oct 23, 2024
1 parent efb81f3 commit fd40890
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 8 additions & 2 deletions packages/verified-fetch/src/utils/parse-url-string.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { peerIdFromString } from '@libp2p/peer-id'
import { peerIdFromCID, peerIdFromString } from '@libp2p/peer-id'
import { base36 } from 'multiformats/bases/base36'
import { CID } from 'multiformats/cid'
import { TLRU } from './tlru.js'
Expand Down Expand Up @@ -178,7 +178,13 @@ export async function parseUrlString ({ urlString, ipns, logger }: ParseUrlStrin
let peerId: PeerId | undefined
try {
// try resolving as an IPNS name
peerId = peerIdFromString(cidOrPeerIdOrDnsLink, base36.decoder)

if (cidOrPeerIdOrDnsLink.charAt(0) === '1' || cidOrPeerIdOrDnsLink.charAt(0) === 'Q') {
peerId = peerIdFromString(cidOrPeerIdOrDnsLink)
} else {
// try resolving as a base36 CID
peerId = peerIdFromCID(CID.parse(cidOrPeerIdOrDnsLink))
}
if (peerId.publicKey == null) {
throw new TypeError('cidOrPeerIdOrDnsLink contains no public key')
}
Expand Down
4 changes: 2 additions & 2 deletions packages/verified-fetch/test/utils/parse-url-string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('parseUrlString', () => {
ipns,
logger
})
).to.eventually.be.rejected.with.property('message', 'Could not parse PeerId in ipns url "mydomain.com", Unable to decode multibase string "mydomain.com", base36 decoder only supports inputs prefixed with k')
).to.eventually.be.rejected.with.property('message', 'Could not parse PeerId in ipns url "mydomain.com", To parse non base32, base36 or base58btc encoded CID multibase decoder must be provided')
})
})

Expand Down Expand Up @@ -162,7 +162,7 @@ describe('parseUrlString', () => {

await expect(parseUrlString({ urlString: 'ipns://mydomain.com', ipns, logger })).to.eventually.be.rejected
.with.property('errors').that.deep.equals([
new TypeError('Could not parse PeerId in ipns url "mydomain.com", Unable to decode multibase string "mydomain.com", base36 decoder only supports inputs prefixed with k'),
new TypeError('Could not parse PeerId in ipns url "mydomain.com", To parse non base32, base36 or base58btc encoded CID multibase decoder must be provided'),
new Error('Unexpected failure from ipns dns query')
])
})
Expand Down

0 comments on commit fd40890

Please sign in to comment.