Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow passing cid encoded ipns keys #117

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading