@helia/interop 3.0.1 → 4.0.0-e554493

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. package/.aegir.js +46 -0
  2. package/README.md +11 -0
  3. package/dist/src/bin.d.ts +3 -0
  4. package/dist/src/bin.d.ts.map +1 -0
  5. package/dist/src/bin.js +20 -0
  6. package/dist/src/bin.js.map +1 -0
  7. package/dist/src/car.spec.d.ts +2 -0
  8. package/dist/src/car.spec.d.ts.map +1 -0
  9. package/dist/src/car.spec.js +77 -0
  10. package/dist/src/car.spec.js.map +1 -0
  11. package/dist/src/dag-cbor.spec.d.ts +2 -0
  12. package/dist/src/dag-cbor.spec.d.ts.map +1 -0
  13. package/dist/src/dag-cbor.spec.js +49 -0
  14. package/dist/src/dag-cbor.spec.js.map +1 -0
  15. package/dist/src/dag-json.spec.d.ts +2 -0
  16. package/dist/src/dag-json.spec.d.ts.map +1 -0
  17. package/dist/src/dag-json.spec.js +49 -0
  18. package/dist/src/dag-json.spec.js.map +1 -0
  19. package/dist/src/fixtures/connect.d.ts +7 -0
  20. package/dist/src/fixtures/connect.d.ts.map +1 -0
  21. package/dist/src/fixtures/connect.js +17 -0
  22. package/dist/src/fixtures/connect.js.map +1 -0
  23. package/dist/src/fixtures/create-helia-http.d.ts +4 -0
  24. package/dist/src/fixtures/create-helia-http.d.ts.map +1 -0
  25. package/dist/src/fixtures/create-helia-http.js +7 -0
  26. package/dist/src/fixtures/create-helia-http.js.map +1 -0
  27. package/dist/src/fixtures/create-helia.browser.d.ts +6 -0
  28. package/dist/src/fixtures/create-helia.browser.d.ts.map +1 -0
  29. package/dist/src/fixtures/create-helia.browser.js +56 -0
  30. package/dist/src/fixtures/create-helia.browser.js.map +1 -0
  31. package/dist/src/fixtures/create-helia.d.ts +6 -0
  32. package/dist/src/fixtures/create-helia.d.ts.map +1 -0
  33. package/dist/src/fixtures/create-helia.js +46 -0
  34. package/dist/src/fixtures/create-helia.js.map +1 -0
  35. package/dist/src/fixtures/create-kubo.browser.d.ts +3 -0
  36. package/dist/src/fixtures/create-kubo.browser.d.ts.map +1 -0
  37. package/dist/src/fixtures/create-kubo.browser.js +30 -0
  38. package/dist/src/fixtures/create-kubo.browser.js.map +1 -0
  39. package/dist/src/fixtures/create-kubo.d.ts +3 -0
  40. package/dist/src/fixtures/create-kubo.d.ts.map +1 -0
  41. package/dist/src/fixtures/create-kubo.js +32 -0
  42. package/dist/src/fixtures/create-kubo.js.map +1 -0
  43. package/dist/src/fixtures/create-peer-ids.d.ts +14 -0
  44. package/dist/src/fixtures/create-peer-ids.d.ts.map +1 -0
  45. package/dist/src/fixtures/create-peer-ids.js +37 -0
  46. package/dist/src/fixtures/create-peer-ids.js.map +1 -0
  47. package/dist/src/fixtures/key-types.d.ts +3 -0
  48. package/dist/src/fixtures/key-types.d.ts.map +1 -0
  49. package/dist/src/fixtures/key-types.js +6 -0
  50. package/dist/src/fixtures/key-types.js.map +1 -0
  51. package/dist/src/fixtures/memory-car.d.ts +7 -0
  52. package/dist/src/fixtures/memory-car.d.ts.map +1 -0
  53. package/dist/src/fixtures/memory-car.js +26 -0
  54. package/dist/src/fixtures/memory-car.js.map +1 -0
  55. package/dist/src/fixtures/wait-for.d.ts +7 -0
  56. package/dist/src/fixtures/wait-for.d.ts.map +1 -0
  57. package/dist/src/fixtures/wait-for.js +17 -0
  58. package/dist/src/fixtures/wait-for.js.map +1 -0
  59. package/dist/src/helia-blockstore.spec.d.ts +2 -0
  60. package/dist/src/helia-blockstore.spec.d.ts.map +1 -0
  61. package/dist/src/helia-blockstore.spec.js +48 -0
  62. package/dist/src/helia-blockstore.spec.js.map +1 -0
  63. package/dist/src/helia-hashes.spec.d.ts +2 -0
  64. package/dist/src/helia-hashes.spec.d.ts.map +1 -0
  65. package/dist/src/helia-hashes.spec.js +50 -0
  66. package/dist/src/helia-hashes.spec.js.map +1 -0
  67. package/dist/src/helia-pins.spec.d.ts +2 -0
  68. package/dist/src/helia-pins.spec.d.ts.map +1 -0
  69. package/dist/src/helia-pins.spec.js +48 -0
  70. package/dist/src/helia-pins.spec.js.map +1 -0
  71. package/dist/src/index.d.ts +12 -0
  72. package/dist/src/index.d.ts.map +1 -1
  73. package/dist/src/index.js +12 -0
  74. package/dist/src/index.js.map +1 -1
  75. package/dist/src/ipns-http.spec.d.ts +2 -0
  76. package/dist/src/ipns-http.spec.d.ts.map +1 -0
  77. package/dist/src/ipns-http.spec.js +55 -0
  78. package/dist/src/ipns-http.spec.js.map +1 -0
  79. package/dist/src/ipns-pubsub.spec.d.ts +2 -0
  80. package/dist/src/ipns-pubsub.spec.d.ts.map +1 -0
  81. package/dist/src/ipns-pubsub.spec.js +151 -0
  82. package/dist/src/ipns-pubsub.spec.js.map +1 -0
  83. package/dist/src/ipns.spec.d.ts +2 -0
  84. package/dist/src/ipns.spec.d.ts.map +1 -0
  85. package/dist/src/ipns.spec.js +146 -0
  86. package/dist/src/ipns.spec.js.map +1 -0
  87. package/dist/src/json.spec.d.ts +2 -0
  88. package/dist/src/json.spec.d.ts.map +1 -0
  89. package/dist/src/json.spec.js +49 -0
  90. package/dist/src/json.spec.js.map +1 -0
  91. package/dist/src/mfs.spec.d.ts +2 -0
  92. package/dist/src/mfs.spec.d.ts.map +1 -0
  93. package/dist/src/mfs.spec.js +85 -0
  94. package/dist/src/mfs.spec.js.map +1 -0
  95. package/dist/src/strings.spec.d.ts +2 -0
  96. package/dist/src/strings.spec.d.ts.map +1 -0
  97. package/dist/src/strings.spec.js +51 -0
  98. package/dist/src/strings.spec.js.map +1 -0
  99. package/dist/src/unixfs-bitswap.spec.d.ts +2 -0
  100. package/dist/src/unixfs-bitswap.spec.d.ts.map +1 -0
  101. package/dist/src/unixfs-bitswap.spec.js +65 -0
  102. package/dist/src/unixfs-bitswap.spec.js.map +1 -0
  103. package/dist/src/unixfs-files.spec.d.ts +2 -0
  104. package/dist/src/unixfs-files.spec.d.ts.map +1 -0
  105. package/dist/src/unixfs-files.spec.js +69 -0
  106. package/dist/src/unixfs-files.spec.js.map +1 -0
  107. package/package.json +33 -20
  108. package/src/bin.ts +25 -0
  109. package/src/car.spec.ts +102 -0
  110. package/src/dag-cbor.spec.ts +65 -0
  111. package/src/dag-json.spec.ts +65 -0
  112. package/src/fixtures/connect.ts +19 -0
  113. package/src/fixtures/create-helia-http.ts +8 -0
  114. package/src/fixtures/create-helia.browser.ts +68 -0
  115. package/src/fixtures/create-helia.ts +55 -0
  116. package/src/fixtures/create-kubo.browser.ts +30 -0
  117. package/src/fixtures/create-kubo.ts +32 -0
  118. package/src/fixtures/create-peer-ids.ts +46 -0
  119. package/src/fixtures/key-types.ts +7 -0
  120. package/src/fixtures/memory-car.ts +33 -0
  121. package/src/fixtures/wait-for.ts +26 -0
  122. package/src/helia-blockstore.spec.ts +59 -0
  123. package/src/helia-hashes.spec.ts +61 -0
  124. package/src/helia-pins.spec.ts +64 -0
  125. package/src/index.ts +13 -0
  126. package/src/ipns-http.spec.ts +68 -0
  127. package/src/ipns-pubsub.spec.ts +187 -0
  128. package/src/ipns.spec.ts +183 -0
  129. package/src/json.spec.ts +65 -0
  130. package/src/mfs.spec.ts +105 -0
  131. package/src/strings.spec.ts +67 -0
  132. package/src/unixfs-bitswap.spec.ts +86 -0
  133. package/src/unixfs-files.spec.ts +89 -0
@@ -0,0 +1,26 @@
1
+ export interface WaitForOptions {
2
+ timeout: number
3
+ delay?: number
4
+ message?: string
5
+ }
6
+
7
+ export async function waitFor (fn: () => Promise<boolean>, options: WaitForOptions): Promise<void> {
8
+ const delay = options.delay ?? 100
9
+ const timeoutAt = Date.now() + options.timeout
10
+
11
+ while (true) {
12
+ const result = await fn()
13
+
14
+ if (result) {
15
+ return
16
+ }
17
+
18
+ await new Promise<void>((resolve) => {
19
+ setTimeout(() => { resolve() }, delay)
20
+ })
21
+
22
+ if (Date.now() > timeoutAt) {
23
+ throw new Error(options.message ?? 'WaitFor timed out')
24
+ }
25
+ }
26
+ }
@@ -0,0 +1,59 @@
1
+ /* eslint-env mocha */
2
+
3
+ import { peerIdFromString } from '@libp2p/peer-id'
4
+ import { expect } from 'aegir/chai'
5
+ import toBuffer from 'it-to-buffer'
6
+ import { CID } from 'multiformats/cid'
7
+ import * as raw from 'multiformats/codecs/raw'
8
+ import { sha256 } from 'multiformats/hashes/sha2'
9
+ import { createHeliaNode } from './fixtures/create-helia.js'
10
+ import { createKuboNode } from './fixtures/create-kubo.js'
11
+ import type { HeliaLibp2p } from 'helia'
12
+ import type { Controller } from 'ipfsd-ctl'
13
+
14
+ describe('helia - blockstore', () => {
15
+ let helia: HeliaLibp2p
16
+ let kubo: Controller
17
+
18
+ beforeEach(async () => {
19
+ helia = await createHeliaNode()
20
+ kubo = await createKuboNode()
21
+
22
+ // connect the two nodes
23
+ await helia.libp2p.peerStore.merge(peerIdFromString(kubo.peer.id.toString()), {
24
+ multiaddrs: kubo.peer.addresses
25
+ })
26
+ await helia.libp2p.dial(peerIdFromString(kubo.peer.id.toString()))
27
+ })
28
+
29
+ afterEach(async () => {
30
+ if (helia != null) {
31
+ await helia.stop()
32
+ }
33
+
34
+ if (kubo != null) {
35
+ await kubo.stop()
36
+ }
37
+ })
38
+
39
+ it('should be able to send a block', async () => {
40
+ const input = Uint8Array.from([0, 1, 2, 3, 4])
41
+ const digest = await sha256.digest(input)
42
+ const cid = CID.createV1(raw.code, digest)
43
+ await helia.blockstore.put(cid, input)
44
+ const output = await toBuffer(kubo.api.cat(cid))
45
+
46
+ expect(output).to.equalBytes(input)
47
+ })
48
+
49
+ it('should be able to receive a block', async () => {
50
+ const input = Uint8Array.from([0, 1, 2, 3, 4])
51
+ const { cid } = await kubo.api.add({ content: input }, {
52
+ cidVersion: 1,
53
+ rawLeaves: true
54
+ })
55
+ const output = await helia.blockstore.get(CID.parse(cid.toString()))
56
+
57
+ expect(output).to.equalBytes(input)
58
+ })
59
+ })
@@ -0,0 +1,61 @@
1
+ /* eslint-env mocha */
2
+
3
+ import { peerIdFromString } from '@libp2p/peer-id'
4
+ import { sha3512 } from '@multiformats/sha3'
5
+ import { expect } from 'aegir/chai'
6
+ import toBuffer from 'it-to-buffer'
7
+ import { CID } from 'multiformats/cid'
8
+ import * as raw from 'multiformats/codecs/raw'
9
+ import { createHeliaNode } from './fixtures/create-helia.js'
10
+ import { createKuboNode } from './fixtures/create-kubo.js'
11
+ import type { HeliaLibp2p } from 'helia'
12
+ import type { Controller } from 'ipfsd-ctl'
13
+
14
+ describe('helia - hashes', () => {
15
+ let helia: HeliaLibp2p
16
+ let kubo: Controller
17
+
18
+ beforeEach(async () => {
19
+ helia = await createHeliaNode()
20
+ kubo = await createKuboNode()
21
+
22
+ // connect the two nodes
23
+ await helia.libp2p.peerStore.merge(peerIdFromString(kubo.peer.id.toString()), {
24
+ multiaddrs: kubo.peer.addresses
25
+ })
26
+ await helia.libp2p.dial(peerIdFromString(kubo.peer.id.toString()))
27
+ })
28
+
29
+ afterEach(async () => {
30
+ if (helia != null) {
31
+ await helia.stop()
32
+ }
33
+
34
+ if (kubo != null) {
35
+ await kubo.stop()
36
+ }
37
+ })
38
+
39
+ it('should be able to send a block with a non-default hash', async () => {
40
+ const input = Uint8Array.from([0, 1, 2, 3, 4])
41
+ const digest = await sha3512.digest(input)
42
+ const cid = CID.createV1(raw.code, digest)
43
+ await helia.blockstore.put(cid, input)
44
+ const output = await toBuffer(kubo.api.cat(cid))
45
+
46
+ expect(output).to.equalBytes(input)
47
+ })
48
+
49
+ it('should be able to receive a block with a non-default hash', async () => {
50
+ const input = Uint8Array.from([0, 1, 2, 3, 4])
51
+ const { cid } = await kubo.api.add({ content: input }, {
52
+ cidVersion: 1,
53
+ rawLeaves: true,
54
+ hashAlg: 'sha3-512'
55
+ })
56
+ expect(cid.multihash.code).to.equal(sha3512.code)
57
+ const output = await helia.blockstore.get(CID.parse(cid.toString()))
58
+
59
+ expect(output).to.equalBytes(input)
60
+ })
61
+ })
@@ -0,0 +1,64 @@
1
+ /* eslint-env mocha */
2
+
3
+ import { expect } from 'aegir/chai'
4
+ import all from 'it-all'
5
+ import drain from 'it-drain'
6
+ import { CID } from 'multiformats/cid'
7
+ import * as raw from 'multiformats/codecs/raw'
8
+ import { sha256 } from 'multiformats/hashes/sha2'
9
+ import { createHeliaNode } from './fixtures/create-helia.js'
10
+ import { createKuboNode } from './fixtures/create-kubo.js'
11
+ import type { HeliaLibp2p } from 'helia'
12
+ import type { Controller } from 'ipfsd-ctl'
13
+
14
+ describe('helia - pins', () => {
15
+ let helia: HeliaLibp2p
16
+ let kubo: Controller
17
+
18
+ beforeEach(async () => {
19
+ helia = await createHeliaNode()
20
+ kubo = await createKuboNode()
21
+
22
+ // connect the two nodes
23
+ await helia.libp2p.dial(kubo.peer.addresses)
24
+ })
25
+
26
+ afterEach(async () => {
27
+ if (helia != null) {
28
+ await helia.stop()
29
+ }
30
+
31
+ if (kubo != null) {
32
+ await kubo.stop()
33
+ }
34
+ })
35
+
36
+ it('pinning on kubo should pull from helia', async () => {
37
+ const input = Uint8Array.from([0, 1, 2, 3, 4])
38
+ const digest = await sha256.digest(input)
39
+ const cid = CID.createV1(raw.code, digest)
40
+
41
+ expect((await all(kubo.api.refs.local())).map(r => r.ref)).to.not.include(cid.toString())
42
+
43
+ await helia.blockstore.put(cid, input)
44
+
45
+ const pinned = await kubo.api.pin.add(cid)
46
+ expect(pinned.toString()).to.equal(cid.toString())
47
+
48
+ expect((await all(kubo.api.refs.local())).map(r => r.ref)).to.include(cid.toString())
49
+ })
50
+
51
+ it('pinning on helia should pull from kubo', async () => {
52
+ const input = Uint8Array.from([0, 1, 2, 3, 4])
53
+ const { cid } = await kubo.api.add({ content: input }, {
54
+ cidVersion: 1,
55
+ rawLeaves: true
56
+ })
57
+
58
+ await expect(helia.blockstore.has(CID.parse(cid.toString()))).to.eventually.be.false()
59
+
60
+ await drain(helia.pins.add(CID.parse(cid.toString())))
61
+
62
+ await expect(helia.blockstore.has(CID.parse(cid.toString()))).to.eventually.be.true()
63
+ })
64
+ })
package/src/index.ts CHANGED
@@ -1 +1,14 @@
1
+ /**
2
+ * @packageDocumentation
3
+ *
4
+ * Runs interop tests between Helia and Kubo.
5
+ *
6
+ * @example Testing a new Kubo release
7
+ *
8
+ * ```console
9
+ * $ npm i @helia/interop
10
+ * $ KUBO_BINARY=/path/to/kubo helia-interop
11
+ * ```
12
+ */
13
+
1
14
  export {}
@@ -0,0 +1,68 @@
1
+ /* eslint-env mocha */
2
+
3
+ import { ipns } from '@helia/ipns'
4
+ import { delegatedHTTPRouting } from '@helia/routers'
5
+ import { peerIdFromString } from '@libp2p/peer-id'
6
+ import { expect } from 'aegir/chai'
7
+ import { isNode } from 'wherearewe'
8
+ import { createHeliaHTTP } from './fixtures/create-helia-http.js'
9
+ import { createKuboNode } from './fixtures/create-kubo.js'
10
+ import type { Helia } from '@helia/interface'
11
+ import type { IPNS } from '@helia/ipns'
12
+ import type { Controller } from 'ipfsd-ctl'
13
+
14
+ describe('@helia/ipns - http', () => {
15
+ let helia: Helia
16
+ let kubo: Controller
17
+ let name: IPNS
18
+
19
+ /**
20
+ * Ensure that for the CID we are going to publish, the resolver has a peer ID that
21
+ * is KAD-closer to the routing key so we can predict the the resolver will receive
22
+ * the DHT record containing the IPNS record
23
+ */
24
+ beforeEach(async () => {
25
+ kubo = await createKuboNode()
26
+ helia = await createHeliaHTTP({
27
+ routers: [
28
+ delegatedHTTPRouting('http://127.0.0.1:8180')
29
+ ]
30
+ })
31
+ name = ipns(helia)
32
+ })
33
+
34
+ afterEach(async () => {
35
+ if (helia != null) {
36
+ await helia.stop()
37
+ }
38
+
39
+ if (kubo != null) {
40
+ await kubo.stop()
41
+ }
42
+ })
43
+
44
+ it('should publish on kubo and resolve on helia', async function () {
45
+ if (!isNode) {
46
+ // https://github.com/protocol/bifrost-community/issues/4#issuecomment-1898417008
47
+ return this.skip()
48
+ }
49
+
50
+ const keyName = 'my-ipns-key'
51
+ const { cid } = await kubo.api.add(Uint8Array.from([0, 1, 2, 3, 4]))
52
+
53
+ await kubo.api.key.gen(keyName, {
54
+ // @ts-expect-error the types say upper-case E, Kubo errors unless it's a
55
+ // lower case e
56
+ type: 'ed25519'
57
+ })
58
+
59
+ const res = await kubo.api.name.publish(cid, {
60
+ key: keyName
61
+ })
62
+
63
+ const key = peerIdFromString(res.name)
64
+
65
+ const resolvedCid = await name.resolve(key)
66
+ expect(resolvedCid.toString()).to.equal(cid.toString())
67
+ })
68
+ })
@@ -0,0 +1,187 @@
1
+ /* eslint-env mocha */
2
+ /* eslint max-nested-callbacks: ["error", 5] */
3
+
4
+ import { gossipsub } from '@chainsafe/libp2p-gossipsub'
5
+ import { ipns } from '@helia/ipns'
6
+ import { pubsub } from '@helia/ipns/routing'
7
+ import { peerIdFromKeys } from '@libp2p/peer-id'
8
+ import { expect } from 'aegir/chai'
9
+ import last from 'it-last'
10
+ import { base36 } from 'multiformats/bases/base36'
11
+ import { CID } from 'multiformats/cid'
12
+ import * as raw from 'multiformats/codecs/raw'
13
+ import { identity } from 'multiformats/hashes/identity'
14
+ import { sha256 } from 'multiformats/hashes/sha2'
15
+ import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
16
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
17
+ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
18
+ import { connect } from './fixtures/connect.js'
19
+ import { createHeliaNode } from './fixtures/create-helia.js'
20
+ import { createKuboNode } from './fixtures/create-kubo.js'
21
+ import { keyTypes } from './fixtures/key-types.js'
22
+ import { waitFor } from './fixtures/wait-for.js'
23
+ import type { IPNS } from '@helia/ipns'
24
+ import type { Libp2p, PubSub } from '@libp2p/interface'
25
+ import type { Keychain } from '@libp2p/keychain'
26
+ import type { HeliaLibp2p } from 'helia'
27
+ import type { Controller } from 'ipfsd-ctl'
28
+
29
+ const LIBP2P_KEY_CODEC = 0x72
30
+
31
+ // skip RSA tests because we need the DHT enabled to find the public key
32
+ // component of the keypair, but that means we can't test pubsub
33
+ // resolution because Kubo will use the DHT as well
34
+ keyTypes.filter(keyType => keyType !== 'RSA').forEach(keyType => {
35
+ describe(`@helia/ipns - pubsub routing with ${keyType} keys`, () => {
36
+ let helia: HeliaLibp2p<Libp2p<{ pubsub: PubSub, keychain: Keychain }>>
37
+ let kubo: Controller
38
+ let name: IPNS
39
+
40
+ beforeEach(async () => {
41
+ helia = await createHeliaNode({
42
+ services: {
43
+ pubsub: gossipsub()
44
+ }
45
+ })
46
+ kubo = await createKuboNode()
47
+
48
+ // connect the two nodes
49
+ await connect(helia, kubo, '/meshsub/1.1.0')
50
+
51
+ name = ipns(helia, {
52
+ routers: [
53
+ pubsub(helia)
54
+ ]
55
+ })
56
+ })
57
+
58
+ afterEach(async () => {
59
+ if (helia != null) {
60
+ await helia.stop()
61
+ }
62
+
63
+ if (kubo != null) {
64
+ await kubo.stop()
65
+ }
66
+ })
67
+
68
+ it('should publish on helia and resolve on kubo', async () => {
69
+ const input = Uint8Array.from([0, 1, 2, 3, 4])
70
+ const digest = await sha256.digest(input)
71
+ const cid = CID.createV1(raw.code, digest)
72
+
73
+ const keyName = 'my-ipns-key'
74
+ await helia.libp2p.services.keychain.createKey(keyName, keyType)
75
+ const peerId = await helia.libp2p.services.keychain.exportPeerId(keyName)
76
+
77
+ if (peerId.publicKey == null) {
78
+ throw new Error('No public key present')
79
+ }
80
+
81
+ // first publish should fail because kubo isn't subscribed to key update channel
82
+ await expect(name.publish(peerId, cid)).to.eventually.be.rejected()
83
+ .with.property('message', 'PublishError.InsufficientPeers')
84
+
85
+ // should fail to resolve the first time as kubo was not subscribed to the pubsub channel
86
+ // @ts-expect-error kubo deps are out of date
87
+ await expect(last(kubo.api.name.resolve(peerId, {
88
+ timeout: 100
89
+ }))).to.eventually.be.undefined()
90
+
91
+ // magic pubsub subscription name
92
+ const subscriptionName = `/ipns/${CID.createV1(LIBP2P_KEY_CODEC, identity.digest(peerId.publicKey)).toString(base36)}`
93
+
94
+ // wait for kubo to be subscribed to updates
95
+ await waitFor(async () => {
96
+ const subs = await kubo.api.name.pubsub.subs()
97
+
98
+ return subs.includes(subscriptionName)
99
+ }, {
100
+ timeout: 30000
101
+ })
102
+
103
+ // publish should now succeed
104
+ await name.publish(peerId, cid)
105
+
106
+ // kubo should now be able to resolve IPNS name
107
+ // @ts-expect-error kubo deps are out of date
108
+ const resolved = await last(kubo.api.name.resolve(peerId, {
109
+ timeout: 100
110
+ }))
111
+
112
+ expect(resolved).to.equal(`/ipfs/${cid.toString()}`)
113
+ })
114
+
115
+ it('should publish on kubo and resolve on helia', async function () {
116
+ if (keyType === 'secp256k1') {
117
+ // Kubo cannot generate secp256k1 keys
118
+ return this.skip()
119
+ }
120
+
121
+ const keyName = 'my-ipns-key'
122
+ const { cid } = await kubo.api.add(Uint8Array.from([0, 1, 2, 3, 4]))
123
+ const result = await kubo.api.key.gen(keyName, {
124
+ // @ts-expect-error kubo needs this in lower case
125
+ type: keyType.toLowerCase()
126
+ })
127
+
128
+ // the generated id is libp2p-key CID with the public key as an identity multihash
129
+ const peerCid = CID.parse(result.id, base36)
130
+ const peerId = await peerIdFromKeys(peerCid.multihash.digest)
131
+
132
+ // first call to pubsub resolver should fail but we should now be subscribed for updates
133
+ await expect(name.resolve(peerId)).to.eventually.be.rejected()
134
+
135
+ // actual pubsub subscription name
136
+ const subscriptionName = `/record/${uint8ArrayToString(uint8ArrayConcat([
137
+ uint8ArrayFromString('/ipns/'),
138
+ peerId.toBytes()
139
+ ]), 'base64url')}`
140
+
141
+ // wait for helia to be subscribed to the topic for record updates
142
+ await waitFor(async () => {
143
+ return helia.libp2p.services.pubsub.getTopics().includes(subscriptionName)
144
+ }, {
145
+ timeout: 30000,
146
+ message: 'Helia did not register for record updates'
147
+ })
148
+
149
+ // wait for kubo to see that helia is subscribed to the topic for record updates
150
+ await waitFor(async () => {
151
+ const peers = await kubo.api.pubsub.peers(subscriptionName)
152
+
153
+ return peers.map(p => p.toString()).includes(helia.libp2p.peerId.toString())
154
+ }, {
155
+ timeout: 30000,
156
+ message: 'Kubo did not see that Helia was registered for record updates'
157
+ })
158
+
159
+ // now publish, this should cause a pubsub message on the topic for record updates
160
+ await kubo.api.name.publish(cid, {
161
+ key: keyName
162
+ })
163
+
164
+ let resolvedCid: CID | undefined
165
+
166
+ // we should get an update eventually
167
+ await waitFor(async () => {
168
+ try {
169
+ resolvedCid = await name.resolve(peerId)
170
+
171
+ return true
172
+ } catch {
173
+ return false
174
+ }
175
+ }, {
176
+ timeout: 10000,
177
+ message: 'Helia could not resolve the IPNS record'
178
+ })
179
+
180
+ if (resolvedCid == null) {
181
+ throw new Error('Failed to resolve CID')
182
+ }
183
+
184
+ expect(resolvedCid.toString()).to.equal(cid.toString())
185
+ })
186
+ })
187
+ })
@@ -0,0 +1,183 @@
1
+ /* eslint-env mocha */
2
+
3
+ import { ipns } from '@helia/ipns'
4
+ import { peerIdFromString } from '@libp2p/peer-id'
5
+ import { createEd25519PeerId, createRSAPeerId, createSecp256k1PeerId } from '@libp2p/peer-id-factory'
6
+ import { expect } from 'aegir/chai'
7
+ import last from 'it-last'
8
+ import { CID } from 'multiformats/cid'
9
+ import * as raw from 'multiformats/codecs/raw'
10
+ import { sha256 } from 'multiformats/hashes/sha2'
11
+ import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
12
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
13
+ import { isElectronMain } from 'wherearewe'
14
+ import { connect } from './fixtures/connect.js'
15
+ import { createHeliaNode } from './fixtures/create-helia.js'
16
+ import { createKuboNode } from './fixtures/create-kubo.js'
17
+ import { sortClosestPeers } from './fixtures/create-peer-ids.js'
18
+ import { keyTypes } from './fixtures/key-types.js'
19
+ import { waitFor } from './fixtures/wait-for.js'
20
+ import type { IPNS } from '@helia/ipns'
21
+ import type { PeerId } from '@libp2p/interface'
22
+ import type { HeliaLibp2p } from 'helia'
23
+ import type { Controller } from 'ipfsd-ctl'
24
+
25
+ keyTypes.forEach(type => {
26
+ describe(`@helia/ipns - default routing with ${type} keys`, () => {
27
+ let helia: HeliaLibp2p
28
+ let kubo: Controller
29
+ let name: IPNS
30
+
31
+ // the CID we are going to publish
32
+ let value: CID
33
+
34
+ // the public key we will use to publish the value
35
+ let key: PeerId
36
+
37
+ /**
38
+ * Ensure that for the CID we are going to publish, the resolver has a peer ID that
39
+ * is KAD-closer to the routing key so we can predict the the resolver will receive
40
+ * the DHT record containing the IPNS record
41
+ */
42
+ async function createNodes (resolver: 'kubo' | 'helia'): Promise<void> {
43
+ const input = Uint8Array.from([0, 1, 2, 3, 4])
44
+ const digest = await sha256.digest(input)
45
+ value = CID.createV1(raw.code, digest)
46
+
47
+ helia = await createHeliaNode()
48
+ kubo = await createKuboNode()
49
+
50
+ // find a PeerId that is KAD-closer to the resolver than the publisher when used as an IPNS key
51
+ while (true) {
52
+ if (type === 'Ed25519') {
53
+ key = await createEd25519PeerId()
54
+ } else if (type === 'secp256k1') {
55
+ key = await createSecp256k1PeerId()
56
+ } else {
57
+ key = await createRSAPeerId()
58
+ }
59
+
60
+ const routingKey = uint8ArrayConcat([
61
+ uint8ArrayFromString('/ipns/'),
62
+ key.toBytes()
63
+ ])
64
+
65
+ const [closest] = await sortClosestPeers(routingKey, [
66
+ helia.libp2p.peerId,
67
+ peerIdFromString(kubo.peer.id.toString())
68
+ ])
69
+
70
+ if (resolver === 'kubo' && closest.equals(peerIdFromString(kubo.peer.id.toString()))) {
71
+ break
72
+ }
73
+
74
+ if (resolver === 'helia' && closest.equals(helia.libp2p.peerId)) {
75
+ break
76
+ }
77
+ }
78
+
79
+ // connect the two nodes over the KAD-DHT protocol, this should ensure
80
+ // both nodes have each other in their KAD buckets
81
+ await connect(helia, kubo, '/ipfs/lan/kad/1.0.0')
82
+
83
+ await waitFor(async () => {
84
+ let found = false
85
+
86
+ for await (const event of helia.libp2p.services.dht.findPeer(peerIdFromString(kubo.peer.id.toString()))) {
87
+ if (event.name === 'FINAL_PEER') {
88
+ found = true
89
+ }
90
+ }
91
+
92
+ return found
93
+ }, {
94
+ timeout: 30000,
95
+ delay: 1000,
96
+ message: 'Helia could not find Kubo on the DHT'
97
+ })
98
+
99
+ await waitFor(async () => {
100
+ let found = false
101
+
102
+ // @ts-expect-error kubo deps are out of date
103
+ for await (const event of kubo.api.dht.findPeer(helia.libp2p.peerId)) {
104
+ if (event.name === 'FINAL_PEER') {
105
+ found = true
106
+ }
107
+ }
108
+
109
+ return found
110
+ }, {
111
+ timeout: 30000,
112
+ delay: 1000,
113
+ message: 'Kubo could not find Helia on the DHT'
114
+ })
115
+
116
+ name = ipns(helia)
117
+ }
118
+
119
+ afterEach(async () => {
120
+ if (helia != null) {
121
+ await helia.stop()
122
+ }
123
+
124
+ if (kubo != null) {
125
+ await kubo.stop()
126
+ }
127
+ })
128
+
129
+ it(`should publish on helia and resolve on kubo using a ${type} key`, async () => {
130
+ await createNodes('kubo')
131
+
132
+ const keyName = 'my-ipns-key'
133
+ await helia.libp2p.services.keychain.importPeer(keyName, key)
134
+
135
+ await name.publish(key, value)
136
+
137
+ const resolved = await last(kubo.api.name.resolve(key.toString()))
138
+
139
+ if (resolved == null) {
140
+ throw new Error('kubo failed to resolve name')
141
+ }
142
+
143
+ expect(resolved).to.equal(`/ipfs/${value.toString()}`)
144
+ })
145
+
146
+ it('should publish on kubo and resolve on helia', async function () {
147
+ if (isElectronMain) {
148
+ // electron main does not have fetch, FormData or Blob APIs
149
+ // can revisit when kubo-rpc-client supports the key.import API
150
+ return this.skip()
151
+ }
152
+
153
+ if (type === 'secp256k1') {
154
+ // Kubo cannot import secp256k1 keys
155
+ return this.skip()
156
+ }
157
+
158
+ await createNodes('helia')
159
+
160
+ const keyName = 'my-ipns-key'
161
+ const { cid } = await kubo.api.add(Uint8Array.from([0, 1, 2, 3, 4]))
162
+
163
+ // ensure the key is in the kubo keychain so we can use it to publish the IPNS record
164
+ const body = new FormData()
165
+ body.append('key', new Blob([key.privateKey ?? new Uint8Array(0)]))
166
+
167
+ // can't use the kubo-rpc-api for this call yet
168
+ const response = await fetch(`http://${kubo.api.apiHost}:${kubo.api.apiPort}/api/v0/key/import?arg=${keyName}`, {
169
+ method: 'POST',
170
+ body
171
+ })
172
+
173
+ expect(response).to.have.property('status', 200)
174
+
175
+ await kubo.api.name.publish(cid, {
176
+ key: keyName
177
+ })
178
+
179
+ const resolvedCid = await name.resolve(key)
180
+ expect(resolvedCid.toString()).to.equal(cid.toString())
181
+ })
182
+ })
183
+ })