Skip to content
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
6 changes: 5 additions & 1 deletion packages/ipns/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ import { resolveDNSLink } from './dnslink.js'
import { InvalidValueError, RecordsFailedValidationError, UnsupportedMultibasePrefixError, UnsupportedMultihashCodecError } from './errors.js'
import { helia } from './routing/helia.js'
import { localStore, type LocalStore } from './routing/local-store.js'
import { isCodec, IDENTITY_CODEC, SHA2_256_CODEC } from './utils.js'
import { isCodec, IDENTITY_CODEC, SHA2_256_CODEC, IPNS_STRING_PREFIX } from './utils.js'
import type { IPNSRouting, IPNSRoutingEvents } from './routing/index.js'
import type { Routing } from '@helia/interface'
import type { AbortOptions, ComponentLogger, Logger, PrivateKey, PublicKey } from '@libp2p/interface'
Expand Down Expand Up @@ -756,6 +756,10 @@ class DefaultIPNS implements IPNS {
if (mh == null) {
// if no public key is embedded in the record, use the key that was passed in
if (typeof key === 'string') {
if (key.startsWith(IPNS_STRING_PREFIX)) {
// remove the /ipns/ prefix from the key
key = key.slice(IPNS_STRING_PREFIX.length)
}
// Convert string key to MultihashDigest
try {
mh = peerIdFromString(key).toMultihash()
Expand Down
2 changes: 2 additions & 0 deletions packages/ipns/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { MultihashDigest } from 'multiformats/hashes/interface'
export const IDENTITY_CODEC = 0x0
export const SHA2_256_CODEC = 0x12

export const IPNS_STRING_PREFIX = '/ipns/'

export function isCodec <T extends number> (digest: MultihashDigest, codec: T): digest is MultihashDigest<T> {
return digest.code === codec
}
8 changes: 8 additions & 0 deletions packages/ipns/test/republish.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { base36 } from 'multiformats/bases/base36'
import { CID } from 'multiformats/cid'
import { stubInterface } from 'sinon-ts'
import { ipns } from '../src/index.js'
import { IPNS_STRING_PREFIX } from '../src/utils.js'
import type { IPNS, IPNSRouting } from '../src/index.js'
import type { Routing } from '@helia/interface'
import type { DNS } from '@multiformats/dns'
Expand Down Expand Up @@ -83,6 +84,13 @@ describe('republishRecord', () => {
await expect(name.republishRecord(keyString, ed25519Record)).to.not.be.rejected
})

it('should republish a record using a string key (base36 encoded CID) prefixed with /ipns/', async () => {
const ed25519Key = await generateKeyPair('Ed25519')
const ed25519Record = await createIPNSRecord(ed25519Key, testCid, 1n, 24 * 60 * 60 * 1000)
const keyString = `${IPNS_STRING_PREFIX}${ed25519Key.publicKey.toCID().toString(base36)}`
await expect(name.republishRecord(keyString, ed25519Record)).to.not.be.rejected
})

it('should emit progress events on error', async () => {
const ed25519Key = await generateKeyPair('Ed25519')
const otherEd25519Key = await generateKeyPair('Ed25519')
Expand Down