|
| 1 | +import { peerIdFromString } from '@libp2p/peer-id' |
| 2 | +import { expect } from 'aegir/chai' |
| 3 | +import { CID } from 'multiformats/cid' |
| 4 | +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' |
| 5 | +import { normalizeValue, peerIdFromRoutingKey, peerIdToRoutingKey } from '../src/utils.js' |
| 6 | +import type { PeerId } from '@libp2p/interface' |
| 7 | + |
| 8 | +describe('utils', () => { |
| 9 | + describe('normalizeValue', () => { |
| 10 | + const cases: Record<string, { input: any, output: string }> = { |
| 11 | + // CID input |
| 12 | + 'v0 CID': { |
| 13 | + input: CID.parse('QmWEekX7EZLUd9VXRNMRXW3LXe4F6x7mB8oPxY5XLptrBq'), |
| 14 | + output: '/ipfs/bafybeidvkqhl6dwsdzx5km7tupo33ywt7czkl5topwogxx6lybko2d7pua' |
| 15 | + }, |
| 16 | + 'v1 CID': { |
| 17 | + input: CID.parse('bafybeidvkqhl6dwsdzx5km7tupo33ywt7czkl5topwogxx6lybko2d7pua'), |
| 18 | + output: '/ipfs/bafybeidvkqhl6dwsdzx5km7tupo33ywt7czkl5topwogxx6lybko2d7pua' |
| 19 | + }, |
| 20 | + 'v1 Libp2p Key CID': { |
| 21 | + input: CID.parse('bafzaajqaeqeaceralaazlm56u23dyhpm7ztoo5x4dcus2ghpqwedhoezk4h6yijbl6rq'), |
| 22 | + output: '/ipns/k73ap3wtp70r7cd9ofyhwgogv1j96huvtvfnsof5spyfaaopkxmonumi4fckgguqr' |
| 23 | + }, |
| 24 | + |
| 25 | + // peer id input |
| 26 | + 'Ed25519 PeerId': { |
| 27 | + input: peerIdFromString('12D3KooWKBpVwnRACfEsk6QME7dA5CZnFYVHQ7Zc927BEzuUekQe'), |
| 28 | + output: '/ipns/k51qzi5uqu5djni72pr40dt64kxlh0zb8baat8h7dtdvkov66euc2lho0oidr3' |
| 29 | + }, |
| 30 | + 'secp256k1 PeerId': { |
| 31 | + input: peerIdFromString('16Uiu2HAkyBsAs6fPyJYVNq3pUDFxyFnUPTQYL2JpLMEViMUwEnp2'), |
| 32 | + output: '/ipns/kzwfwjn5ji4pul2d7gonydo4rtncuequd647001hm1afmxxvhfs1pz9ckfrc1c3' |
| 33 | + }, |
| 34 | + 'RSA PeerId': { |
| 35 | + input: peerIdFromString('QmPofjNRgPN3ndH5RbcSr3X5EekvpCRsUw1E8ji8kJaQJa'), |
| 36 | + output: '/ipns/k2k4r8jyk192oxg2e40x4av8re5e6frptftrwrhu6k1ia6whqsew13f3' |
| 37 | + }, |
| 38 | + |
| 39 | + // string input |
| 40 | + 'string path': { |
| 41 | + input: '/hello', |
| 42 | + output: '/hello' |
| 43 | + }, |
| 44 | + |
| 45 | + // Uint8Array input |
| 46 | + 'v0 CID bytes': { |
| 47 | + input: CID.parse('QmWEekX7EZLUd9VXRNMRXW3LXe4F6x7mB8oPxY5XLptrBq').bytes, |
| 48 | + output: '/ipfs/bafybeidvkqhl6dwsdzx5km7tupo33ywt7czkl5topwogxx6lybko2d7pua' |
| 49 | + }, |
| 50 | + 'v1 CID bytes': { |
| 51 | + input: CID.parse('bafybeidvkqhl6dwsdzx5km7tupo33ywt7czkl5topwogxx6lybko2d7pua').bytes, |
| 52 | + output: '/ipfs/bafybeidvkqhl6dwsdzx5km7tupo33ywt7czkl5topwogxx6lybko2d7pua' |
| 53 | + }, |
| 54 | + 'v1 Libp2p Key CID bytes': { |
| 55 | + input: CID.parse('bafzaajqaeqeaceralaazlm56u23dyhpm7ztoo5x4dcus2ghpqwedhoezk4h6yijbl6rq').bytes, |
| 56 | + output: '/ipfs/bafzaajqaeqeaceralaazlm56u23dyhpm7ztoo5x4dcus2ghpqwedhoezk4h6yijbl6rq' |
| 57 | + }, |
| 58 | + 'string path Uint8Array': { |
| 59 | + input: uint8ArrayFromString('/hello'), |
| 60 | + output: '/hello' |
| 61 | + }, |
| 62 | + 'IPFS path v0 CID Uint8Array': { |
| 63 | + input: uint8ArrayFromString('/ipfs/QmWEekX7EZLUd9VXRNMRXW3LXe4F6x7mB8oPxY5XLptrBq'), |
| 64 | + output: '/ipfs/QmWEekX7EZLUd9VXRNMRXW3LXe4F6x7mB8oPxY5XLptrBq' |
| 65 | + }, |
| 66 | + 'IPFS path v1 CID Uint8Array': { |
| 67 | + input: uint8ArrayFromString('/ipfs/bafybeidvkqhl6dwsdzx5km7tupo33ywt7czkl5topwogxx6lybko2d7pua'), |
| 68 | + output: '/ipfs/bafybeidvkqhl6dwsdzx5km7tupo33ywt7czkl5topwogxx6lybko2d7pua' |
| 69 | + }, |
| 70 | + 'IPFS path v1 Libp2p Key CID Uint8Array': { |
| 71 | + input: uint8ArrayFromString('/ipfs/bafzaajqaeqeaceralaazlm56u23dyhpm7ztoo5x4dcus2ghpqwedhoezk4h6yijbl6rq'), |
| 72 | + output: '/ipfs/bafzaajqaeqeaceralaazlm56u23dyhpm7ztoo5x4dcus2ghpqwedhoezk4h6yijbl6rq' |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + Object.entries(cases).forEach(([name, { input, output }]) => { |
| 77 | + it(`should normalize a ${name}`, async () => { |
| 78 | + expect(normalizeValue(await input)).to.equal(output) |
| 79 | + }) |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + describe('routing keys', () => { |
| 84 | + const cases: Record<string, PeerId> = { |
| 85 | + Ed25519: peerIdFromString('12D3KooWKBpVwnRACfEsk6QME7dA5CZnFYVHQ7Zc927BEzuUekQe'), |
| 86 | + secp256k1: peerIdFromString('16Uiu2HAkyBsAs6fPyJYVNq3pUDFxyFnUPTQYL2JpLMEViMUwEnp2'), |
| 87 | + RSA: peerIdFromString('QmPofjNRgPN3ndH5RbcSr3X5EekvpCRsUw1E8ji8kJaQJa') |
| 88 | + } |
| 89 | + |
| 90 | + Object.entries(cases).forEach(([name, input]) => { |
| 91 | + it(`should round trip a ${name} key`, async () => { |
| 92 | + const key = peerIdToRoutingKey(input) |
| 93 | + const output = peerIdFromRoutingKey(key) |
| 94 | + |
| 95 | + expect(input.equals(output)).to.be.true() |
| 96 | + }) |
| 97 | + }) |
| 98 | + }) |
| 99 | +}) |
0 commit comments