Skip to content

Commit

Permalink
fix: allow passing cid encoded ipns keys (#117)
Browse files Browse the repository at this point in the history
* fix: allow passing cid encoded ipns keys

* fix: handle cid encoded peerid in parse-url-string

* chore: remove unused import

---------

Co-authored-by: Daniel N <[email protected]>
Co-authored-by: Russell Dempsey <[email protected]>
  • Loading branch information
3 people authored Oct 23, 2024
1 parent aede17f commit 1836e40
Show file tree
Hide file tree
Showing 2 changed files with 31 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 { CID } from 'multiformats/cid'
import { TLRU } from './tlru.js'
import type { RequestFormatShorthand } from '../types.js'
Expand Down Expand Up @@ -177,7 +177,13 @@ export async function parseUrlString ({ urlString, ipns, logger }: ParseUrlStrin
let peerId: PeerId | undefined
try {
// try resolving as an IPNS name
peerId = peerIdFromString(cidOrPeerIdOrDnsLink)

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
25 changes: 23 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 @@ -4,6 +4,7 @@ import { defaultLogger } from '@libp2p/logger'
import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { type Answer } from '@multiformats/dns'
import { expect } from 'aegir/chai'
import { base36 } from 'multiformats/bases/base36'
import { CID } from 'multiformats/cid'
import { match } from 'sinon'
import { stubInterface } from 'sinon-ts'
Expand Down Expand Up @@ -76,7 +77,7 @@ describe('parseUrlString', () => {
ipns,
logger
})
).to.eventually.be.rejected.with.property('message', 'Could not parse PeerId in ipns url "mydomain.com", Please pass a multibase decoder for strings that do not start with "1" or "Q"')
).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 @@ -161,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", Please pass a multibase decoder for strings that do not start with "1" or "Q"'),
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 Expand Up @@ -459,10 +460,13 @@ describe('parseUrlString', () => {

describe('ipns://<peerId> URLs', () => {
let testPeerId: PeerId
let base36CidPeerId: string

beforeEach(async () => {
const key = await generateKeyPair('Ed25519')
testPeerId = peerIdFromPrivateKey(key)

base36CidPeerId = key.publicKey.toCID().toString(base36)
})

it('handles invalid PeerIds', async () => {
Expand Down Expand Up @@ -504,6 +508,23 @@ describe('parseUrlString', () => {
)
})

it('can parse a base36 PeerId CID', async () => {
ipns.resolve.withArgs(matchPeerId(testPeerId)).resolves({
cid: CID.parse('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr'),
path: '',
record: ipnsRecordStub({ peerId: testPeerId })
})

await assertMatchUrl(
`ipns://${base36CidPeerId}`, {
protocol: 'ipns',
cid: 'QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr',
path: '',
query: {}
}
)
})

it('can parse a URL with PeerId+path', async () => {
ipns.resolve.withArgs(matchPeerId(testPeerId)).resolves({
cid: CID.parse('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr'),
Expand Down

0 comments on commit 1836e40

Please sign in to comment.