Skip to content

Commit 0699fb7

Browse files
2colorachingbrain
andauthored
feat: support parsing base32 and base36 libp2p-key CIDs in peerIdFromString (#3042)
* feat: add utility function to parse peer ids * fix: linting error * fix: use prefix to detect cid encoded peers * fix: linting errors * chore: split base32/base36 tests --------- Co-authored-by: Daniel N <[email protected]> Co-authored-by: achingbrain <[email protected]>
1 parent 600d0a5 commit 0699fb7

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

packages/peer-id/src/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import { publicKeyFromMultihash } from '@libp2p/crypto/keys'
1818
import { InvalidCIDError, InvalidMultihashError, InvalidParametersError, UnsupportedKeyTypeError } from '@libp2p/interface'
1919
import { base58btc } from 'multiformats/bases/base58'
20-
import { type CID, type MultibaseDecoder } from 'multiformats/cid'
20+
import { CID, type MultibaseDecoder } from 'multiformats/cid'
2121
import * as Digest from 'multiformats/hashes/digest'
2222
import { identity } from 'multiformats/hashes/identity'
2323
import { sha256 } from 'multiformats/hashes/sha2'
@@ -37,6 +37,9 @@ export function peerIdFromString (str: string, decoder?: MultibaseDecoder<any>):
3737
// identity hash ed25519/secp256k1 key or sha2-256 hash of
3838
// rsa public key - base58btc encoded either way
3939
multihash = Digest.decode(base58btc.decode(`z${str}`))
40+
} else if (str.startsWith('k51qzi5uqu5') || str.startsWith('kzwfwjn5ji4') || str.startsWith('k2k4r8') || str.startsWith('bafz')) {
41+
// base36 encoded CIDv1 with libp2p-key and identity hash (for ed25519/secp256k1/rsa) or base32 encoded CIDv1 with libp2p-key and identity hash (for ed25519/secp256k1/rsa)
42+
return peerIdFromCID(CID.parse(str))
4043
} else {
4144
if (decoder == null) {
4245
throw new InvalidParametersError('Please pass a multibase decoder for strings that do not start with "1" or "Q"')

packages/peer-id/test/index.spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* eslint-env mocha */
22
import { generateKeyPair } from '@libp2p/crypto/keys'
33
import { expect } from 'aegir/chai'
4+
import { base32 } from 'multiformats/bases/base32'
5+
import { base36 } from 'multiformats/bases/base36'
46
import { base58btc } from 'multiformats/bases/base58'
57
import { CID } from 'multiformats/cid'
68
import { identity } from 'multiformats/hashes/identity'
@@ -56,6 +58,20 @@ describe('PeerId', () => {
5658
expect(id.toCID().toString()).to.equal(peerId.toCID().toString())
5759
})
5860

61+
it('should return the correct peer id from cid encoded peer id in base36', async () => {
62+
const id = peerIdFromString(peerId.toCID().toString(base36))
63+
expect(id.type).to.equal(type)
64+
expect(id.toString()).to.equal(peerId.toString())
65+
expect(id.toCID().toString()).to.equal(peerId.toCID().toString())
66+
})
67+
68+
it('should return the correct peer id from cid encoded peer id in base32', async () => {
69+
const id = peerIdFromString(peerId.toCID().toString(base32))
70+
expect(id.type).to.equal(type)
71+
expect(id.toString()).to.equal(peerId.toString())
72+
expect(id.toCID().toString()).to.equal(peerId.toCID().toString())
73+
})
74+
5975
it('should default to base58btc when stringifying', async () => {
6076
expect(base58btc.decode(`z${peerId.toString()}`)).to.be.ok()
6177
})

0 commit comments

Comments
 (0)