diff --git a/packages/verified-fetch/src/utils/parse-url-string.ts b/packages/verified-fetch/src/utils/parse-url-string.ts index 443bd8d..0bdc75b 100644 --- a/packages/verified-fetch/src/utils/parse-url-string.ts +++ b/packages/verified-fetch/src/utils/parse-url-string.ts @@ -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' @@ -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') } diff --git a/packages/verified-fetch/test/utils/parse-url-string.spec.ts b/packages/verified-fetch/test/utils/parse-url-string.spec.ts index 12f7944..5e7d588 100644 --- a/packages/verified-fetch/test/utils/parse-url-string.spec.ts +++ b/packages/verified-fetch/test/utils/parse-url-string.spec.ts @@ -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') }) }) @@ -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') ]) })