@waku/rln 0.1.8-e224c05.0 → 0.1.8-e800af3.0

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 (187) hide show
  1. package/bundle/_virtual/utils.js +2 -2
  2. package/bundle/_virtual/utils2.js +2 -2
  3. package/bundle/index.js +6 -1
  4. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/_sha2.js +1 -1
  5. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/hmac.js +1 -1
  6. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/pbkdf2.js +1 -1
  7. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/scrypt.js +1 -1
  8. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/sha256.js +1 -1
  9. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/sha512.js +1 -1
  10. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/utils.js +1 -1
  11. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/random.js +1 -1
  12. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/utils.js +2 -2
  13. package/bundle/node_modules/@chainsafe/is-ip/lib/is-ip.js +12 -0
  14. package/bundle/node_modules/@chainsafe/is-ip/lib/parse.js +26 -0
  15. package/bundle/node_modules/@chainsafe/is-ip/lib/parser.js +202 -0
  16. package/bundle/node_modules/@multiformats/multiaddr/dist/src/constants.js +43 -0
  17. package/bundle/node_modules/@multiformats/multiaddr/dist/src/errors.js +17 -0
  18. package/bundle/node_modules/@multiformats/multiaddr/dist/src/registry.js +245 -0
  19. package/bundle/node_modules/@multiformats/multiaddr/dist/src/utils.js +191 -0
  20. package/bundle/node_modules/@multiformats/multiaddr/dist/src/validation.js +30 -0
  21. package/bundle/node_modules/@noble/hashes/esm/hmac.js +88 -0
  22. package/bundle/node_modules/@noble/hashes/esm/sha3.js +1 -1
  23. package/bundle/node_modules/@noble/hashes/esm/utils.js +8 -1
  24. package/bundle/node_modules/@waku/zerokit-rln-wasm/rln_wasm.js +517 -255
  25. package/bundle/node_modules/it-length-prefixed/dist/src/decode.js +6 -0
  26. package/bundle/node_modules/multiformats/dist/src/bases/base10.js +3 -1
  27. package/bundle/node_modules/multiformats/dist/src/bases/base16.js +4 -2
  28. package/bundle/node_modules/multiformats/dist/src/bases/base2.js +3 -1
  29. package/bundle/node_modules/multiformats/dist/src/bases/base256emoji.js +3 -1
  30. package/bundle/node_modules/multiformats/dist/src/bases/base32.js +11 -9
  31. package/bundle/node_modules/multiformats/dist/src/bases/base36.js +4 -2
  32. package/bundle/node_modules/multiformats/dist/src/bases/base58.js +4 -2
  33. package/bundle/node_modules/multiformats/dist/src/bases/base64.js +6 -4
  34. package/bundle/node_modules/multiformats/dist/src/bases/base8.js +3 -1
  35. package/bundle/node_modules/multiformats/dist/src/bases/identity.js +3 -1
  36. package/bundle/node_modules/multiformats/dist/src/basics.js +15 -0
  37. package/bundle/node_modules/multiformats/dist/src/bytes.js +15 -1
  38. package/bundle/node_modules/multiformats/dist/src/cid.js +371 -0
  39. package/bundle/node_modules/multiformats/dist/src/hashes/digest.js +62 -0
  40. package/bundle/node_modules/multiformats/dist/src/varint.js +15 -0
  41. package/bundle/node_modules/multiformats/dist/src/vendor/varint.js +78 -0
  42. package/bundle/node_modules/protons-runtime/dist/src/codec.js +20 -0
  43. package/bundle/node_modules/protons-runtime/dist/src/codecs/enum.js +24 -0
  44. package/bundle/node_modules/protons-runtime/dist/src/codecs/message.js +7 -0
  45. package/bundle/node_modules/protons-runtime/dist/src/decode.js +8 -0
  46. package/bundle/node_modules/protons-runtime/dist/src/encode.js +11 -0
  47. package/bundle/node_modules/protons-runtime/dist/src/index.js +30 -0
  48. package/bundle/node_modules/protons-runtime/dist/src/utils/float.js +54 -0
  49. package/bundle/node_modules/protons-runtime/dist/src/utils/longbits.js +175 -0
  50. package/bundle/node_modules/protons-runtime/dist/src/utils/pool.js +28 -0
  51. package/bundle/node_modules/protons-runtime/dist/src/utils/reader.js +367 -0
  52. package/bundle/node_modules/protons-runtime/dist/src/utils/utf8.js +99 -0
  53. package/bundle/node_modules/protons-runtime/dist/src/utils/writer.js +438 -0
  54. package/bundle/node_modules/uint8-varint/dist/src/index.js +124 -0
  55. package/bundle/node_modules/uint8arrays/dist/src/alloc.js +17 -0
  56. package/bundle/node_modules/uint8arrays/dist/src/concat.js +20 -0
  57. package/bundle/node_modules/uint8arrays/dist/src/from-string.js +19 -0
  58. package/bundle/node_modules/uint8arrays/dist/src/to-string.js +19 -0
  59. package/bundle/node_modules/uint8arrays/dist/src/util/as-uint8array.js +9 -0
  60. package/bundle/node_modules/uint8arrays/dist/src/util/bases.js +49 -0
  61. package/bundle/packages/core/dist/lib/connection_manager/connection_limiter.js +18 -0
  62. package/bundle/packages/core/dist/lib/connection_manager/connection_manager.js +24 -0
  63. package/bundle/packages/core/dist/lib/connection_manager/dialer.js +14 -0
  64. package/bundle/packages/core/dist/lib/connection_manager/discovery_dialer.js +14 -0
  65. package/bundle/packages/core/dist/lib/connection_manager/keep_alive_manager.js +15 -0
  66. package/bundle/packages/core/dist/lib/connection_manager/shard_reader.js +14 -0
  67. package/bundle/packages/core/dist/lib/filter/filter.js +28 -0
  68. package/bundle/packages/core/dist/lib/light_push/light_push.js +28 -0
  69. package/bundle/packages/core/dist/lib/message/version_0.js +172 -0
  70. package/bundle/packages/core/dist/lib/metadata/metadata.js +28 -0
  71. package/bundle/packages/core/dist/lib/store/store.js +24 -0
  72. package/bundle/packages/interfaces/dist/connection_manager.js +9 -0
  73. package/bundle/packages/interfaces/dist/health_status.js +17 -0
  74. package/bundle/packages/interfaces/dist/protocols.js +92 -0
  75. package/bundle/packages/interfaces/dist/waku.js +7 -0
  76. package/bundle/packages/proto/dist/generated/filter.js +447 -0
  77. package/bundle/packages/proto/dist/generated/filter_v2.js +426 -0
  78. package/bundle/packages/proto/dist/generated/light_push.js +550 -0
  79. package/bundle/packages/proto/dist/generated/message.js +215 -0
  80. package/bundle/packages/proto/dist/generated/metadata.js +132 -0
  81. package/bundle/packages/proto/dist/generated/peer_exchange.js +211 -0
  82. package/bundle/packages/proto/dist/generated/sds_message.js +172 -0
  83. package/bundle/packages/proto/dist/generated/store_v3.js +492 -0
  84. package/bundle/packages/proto/dist/generated/topic_only_message.js +63 -0
  85. package/bundle/packages/rln/dist/codec.js +92 -0
  86. package/bundle/packages/rln/dist/contract/constants.js +14 -7
  87. package/bundle/packages/rln/dist/contract/rln_base_contract.js +2 -1
  88. package/bundle/packages/rln/dist/contract/rln_contract.js +109 -0
  89. package/bundle/packages/rln/dist/credentials_manager.js +45 -4
  90. package/bundle/packages/rln/dist/identity.js +2 -1
  91. package/bundle/packages/rln/dist/keystore/keystore.js +30 -9
  92. package/bundle/packages/rln/dist/message.js +59 -0
  93. package/bundle/packages/rln/dist/proof.js +54 -0
  94. package/bundle/packages/rln/dist/resources/verification_key.js +112 -0
  95. package/bundle/packages/rln/dist/resources/witness_calculator.js +1 -1
  96. package/bundle/packages/rln/dist/rln.js +36 -4
  97. package/bundle/packages/rln/dist/root_tracker.js +76 -0
  98. package/bundle/packages/rln/dist/utils/bytes.js +70 -31
  99. package/bundle/packages/rln/dist/utils/epoch.js +23 -1
  100. package/bundle/packages/rln/dist/utils/hash.js +10 -0
  101. package/bundle/packages/rln/dist/zerokit.js +99 -2
  102. package/bundle/packages/utils/dist/bytes/index.js +31 -0
  103. package/bundle/resources/rln.wasm +0 -0
  104. package/bundle/resources/rln_final.zkey +0 -0
  105. package/bundle/resources/verification_key.d.ts +13 -0
  106. package/bundle/resources/verification_key.js +112 -0
  107. package/bundle/resources/witness_calculator.d.ts +7 -21
  108. package/bundle/resources/witness_calculator.js +1 -1
  109. package/dist/.tsbuildinfo +1 -1
  110. package/dist/codec.d.ts +40 -0
  111. package/dist/codec.js +79 -0
  112. package/dist/codec.js.map +1 -0
  113. package/dist/codec.test-utils.d.ts +37 -0
  114. package/dist/codec.test-utils.js +61 -0
  115. package/dist/codec.test-utils.js.map +1 -0
  116. package/dist/contract/constants.d.ts +10 -3
  117. package/dist/contract/constants.js +13 -6
  118. package/dist/contract/constants.js.map +1 -1
  119. package/dist/contract/index.d.ts +1 -0
  120. package/dist/contract/index.js +1 -0
  121. package/dist/contract/index.js.map +1 -1
  122. package/dist/contract/rln_base_contract.js +2 -1
  123. package/dist/contract/rln_base_contract.js.map +1 -1
  124. package/dist/contract/rln_contract.d.ts +17 -0
  125. package/dist/contract/rln_contract.js +107 -0
  126. package/dist/contract/rln_contract.js.map +1 -0
  127. package/dist/contract/test_setup.d.ts +26 -0
  128. package/dist/contract/test_setup.js +56 -0
  129. package/dist/contract/test_setup.js.map +1 -0
  130. package/dist/contract/test_utils.d.ts +39 -0
  131. package/dist/contract/test_utils.js +118 -0
  132. package/dist/contract/test_utils.js.map +1 -0
  133. package/dist/credentials_manager.d.ts +14 -2
  134. package/dist/credentials_manager.js +45 -4
  135. package/dist/credentials_manager.js.map +1 -1
  136. package/dist/identity.js +2 -1
  137. package/dist/identity.js.map +1 -1
  138. package/dist/index.d.ts +6 -2
  139. package/dist/index.js +6 -2
  140. package/dist/index.js.map +1 -1
  141. package/dist/keystore/keystore.js +30 -9
  142. package/dist/keystore/keystore.js.map +1 -1
  143. package/dist/message.d.ts +19 -0
  144. package/dist/message.js +51 -0
  145. package/dist/message.js.map +1 -0
  146. package/dist/proof.d.ts +21 -0
  147. package/dist/proof.js +50 -0
  148. package/dist/proof.js.map +1 -0
  149. package/dist/resources/rln.wasm +0 -0
  150. package/dist/resources/rln_final.zkey +0 -0
  151. package/dist/resources/verification_key.d.ts +13 -0
  152. package/dist/resources/verification_key.js +112 -0
  153. package/dist/resources/witness_calculator.d.ts +7 -21
  154. package/dist/resources/witness_calculator.js +1 -1
  155. package/dist/rln.d.ts +9 -0
  156. package/dist/rln.js +32 -4
  157. package/dist/rln.js.map +1 -1
  158. package/dist/root_tracker.d.ts +10 -0
  159. package/dist/root_tracker.js +75 -0
  160. package/dist/root_tracker.js.map +1 -0
  161. package/dist/utils/bytes.d.ts +31 -9
  162. package/dist/utils/bytes.js +70 -31
  163. package/dist/utils/bytes.js.map +1 -1
  164. package/dist/zerokit.d.ts +11 -0
  165. package/dist/zerokit.js +97 -1
  166. package/dist/zerokit.js.map +1 -1
  167. package/package.json +1 -1
  168. package/src/codec.test-utils.ts +88 -0
  169. package/src/codec.ts +138 -0
  170. package/src/contract/constants.ts +16 -6
  171. package/src/contract/index.ts +1 -0
  172. package/src/contract/rln_base_contract.ts +2 -1
  173. package/src/contract/rln_contract.ts +147 -0
  174. package/src/contract/test_setup.ts +86 -0
  175. package/src/contract/test_utils.ts +179 -0
  176. package/src/credentials_manager.ts +72 -8
  177. package/src/identity.ts +2 -1
  178. package/src/index.ts +11 -1
  179. package/src/keystore/keystore.ts +32 -9
  180. package/src/message.ts +73 -0
  181. package/src/proof.ts +69 -0
  182. package/src/resources/verification_key.d.ts +13 -0
  183. package/src/resources/witness_calculator.d.ts +7 -21
  184. package/src/rln.ts +65 -5
  185. package/src/root_tracker.ts +92 -0
  186. package/src/utils/bytes.ts +73 -36
  187. package/src/zerokit.ts +217 -1
@@ -0,0 +1,6 @@
1
+ /* eslint max-depth: ["error", 6] */
2
+ var ReadMode;
3
+ (function (ReadMode) {
4
+ ReadMode[ReadMode["LENGTH"] = 0] = "LENGTH";
5
+ ReadMode[ReadMode["DATA"] = 1] = "DATA";
6
+ })(ReadMode || (ReadMode = {}));
@@ -1,7 +1,9 @@
1
1
  import { baseX } from './base.js';
2
2
 
3
- baseX({
3
+ const base10 = baseX({
4
4
  prefix: '9',
5
5
  name: 'base10',
6
6
  alphabet: '0123456789'
7
7
  });
8
+
9
+ export { base10 };
@@ -1,14 +1,16 @@
1
1
  import { rfc4648 } from './base.js';
2
2
 
3
- rfc4648({
3
+ const base16 = rfc4648({
4
4
  prefix: 'f',
5
5
  name: 'base16',
6
6
  alphabet: '0123456789abcdef',
7
7
  bitsPerChar: 4
8
8
  });
9
- rfc4648({
9
+ const base16upper = rfc4648({
10
10
  prefix: 'F',
11
11
  name: 'base16upper',
12
12
  alphabet: '0123456789ABCDEF',
13
13
  bitsPerChar: 4
14
14
  });
15
+
16
+ export { base16, base16upper };
@@ -1,8 +1,10 @@
1
1
  import { rfc4648 } from './base.js';
2
2
 
3
- rfc4648({
3
+ const base2 = rfc4648({
4
4
  prefix: '0',
5
5
  name: 'base2',
6
6
  alphabet: '01',
7
7
  bitsPerChar: 1
8
8
  });
9
+
10
+ export { base2 };
@@ -31,9 +31,11 @@ function decode(str) {
31
31
  }
32
32
  return new Uint8Array(byts);
33
33
  }
34
- from({
34
+ const base256emoji = from({
35
35
  prefix: '🚀',
36
36
  name: 'base256emoji',
37
37
  encode,
38
38
  decode
39
39
  });
40
+
41
+ export { base256emoji };
@@ -1,56 +1,58 @@
1
1
  import { rfc4648 } from './base.js';
2
2
 
3
- rfc4648({
3
+ const base32 = rfc4648({
4
4
  prefix: 'b',
5
5
  name: 'base32',
6
6
  alphabet: 'abcdefghijklmnopqrstuvwxyz234567',
7
7
  bitsPerChar: 5
8
8
  });
9
- rfc4648({
9
+ const base32upper = rfc4648({
10
10
  prefix: 'B',
11
11
  name: 'base32upper',
12
12
  alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
13
13
  bitsPerChar: 5
14
14
  });
15
- rfc4648({
15
+ const base32pad = rfc4648({
16
16
  prefix: 'c',
17
17
  name: 'base32pad',
18
18
  alphabet: 'abcdefghijklmnopqrstuvwxyz234567=',
19
19
  bitsPerChar: 5
20
20
  });
21
- rfc4648({
21
+ const base32padupper = rfc4648({
22
22
  prefix: 'C',
23
23
  name: 'base32padupper',
24
24
  alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=',
25
25
  bitsPerChar: 5
26
26
  });
27
- rfc4648({
27
+ const base32hex = rfc4648({
28
28
  prefix: 'v',
29
29
  name: 'base32hex',
30
30
  alphabet: '0123456789abcdefghijklmnopqrstuv',
31
31
  bitsPerChar: 5
32
32
  });
33
- rfc4648({
33
+ const base32hexupper = rfc4648({
34
34
  prefix: 'V',
35
35
  name: 'base32hexupper',
36
36
  alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUV',
37
37
  bitsPerChar: 5
38
38
  });
39
- rfc4648({
39
+ const base32hexpad = rfc4648({
40
40
  prefix: 't',
41
41
  name: 'base32hexpad',
42
42
  alphabet: '0123456789abcdefghijklmnopqrstuv=',
43
43
  bitsPerChar: 5
44
44
  });
45
- rfc4648({
45
+ const base32hexpadupper = rfc4648({
46
46
  prefix: 'T',
47
47
  name: 'base32hexpadupper',
48
48
  alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUV=',
49
49
  bitsPerChar: 5
50
50
  });
51
- rfc4648({
51
+ const base32z = rfc4648({
52
52
  prefix: 'h',
53
53
  name: 'base32z',
54
54
  alphabet: 'ybndrfg8ejkmcpqxot1uwisza345h769',
55
55
  bitsPerChar: 5
56
56
  });
57
+
58
+ export { base32, base32hex, base32hexpad, base32hexpadupper, base32hexupper, base32pad, base32padupper, base32upper, base32z };
@@ -1,12 +1,14 @@
1
1
  import { baseX } from './base.js';
2
2
 
3
- baseX({
3
+ const base36 = baseX({
4
4
  prefix: 'k',
5
5
  name: 'base36',
6
6
  alphabet: '0123456789abcdefghijklmnopqrstuvwxyz'
7
7
  });
8
- baseX({
8
+ const base36upper = baseX({
9
9
  prefix: 'K',
10
10
  name: 'base36upper',
11
11
  alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
12
12
  });
13
+
14
+ export { base36, base36upper };
@@ -1,12 +1,14 @@
1
1
  import { baseX } from './base.js';
2
2
 
3
- baseX({
3
+ const base58btc = baseX({
4
4
  name: 'base58btc',
5
5
  prefix: 'z',
6
6
  alphabet: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
7
7
  });
8
- baseX({
8
+ const base58flickr = baseX({
9
9
  name: 'base58flickr',
10
10
  prefix: 'Z',
11
11
  alphabet: '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
12
12
  });
13
+
14
+ export { base58btc, base58flickr };
@@ -1,26 +1,28 @@
1
1
  import { rfc4648 } from './base.js';
2
2
 
3
- rfc4648({
3
+ const base64 = rfc4648({
4
4
  prefix: 'm',
5
5
  name: 'base64',
6
6
  alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
7
7
  bitsPerChar: 6
8
8
  });
9
- rfc4648({
9
+ const base64pad = rfc4648({
10
10
  prefix: 'M',
11
11
  name: 'base64pad',
12
12
  alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
13
13
  bitsPerChar: 6
14
14
  });
15
- rfc4648({
15
+ const base64url = rfc4648({
16
16
  prefix: 'u',
17
17
  name: 'base64url',
18
18
  alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
19
19
  bitsPerChar: 6
20
20
  });
21
- rfc4648({
21
+ const base64urlpad = rfc4648({
22
22
  prefix: 'U',
23
23
  name: 'base64urlpad',
24
24
  alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=',
25
25
  bitsPerChar: 6
26
26
  });
27
+
28
+ export { base64, base64pad, base64url, base64urlpad };
@@ -1,8 +1,10 @@
1
1
  import { rfc4648 } from './base.js';
2
2
 
3
- rfc4648({
3
+ const base8 = rfc4648({
4
4
  prefix: '7',
5
5
  name: 'base8',
6
6
  alphabet: '01234567',
7
7
  bitsPerChar: 3
8
8
  });
9
+
10
+ export { base8 };
@@ -1,9 +1,11 @@
1
1
  import { toString, fromString } from '../bytes.js';
2
2
  import { from } from './base.js';
3
3
 
4
- from({
4
+ const identity = from({
5
5
  prefix: '\x00',
6
6
  name: 'identity',
7
7
  encode: (buf) => toString(buf),
8
8
  decode: (str) => fromString(str)
9
9
  });
10
+
11
+ export { identity };
@@ -0,0 +1,15 @@
1
+ import * as base10 from './bases/base10.js';
2
+ import * as base16 from './bases/base16.js';
3
+ import * as base2 from './bases/base2.js';
4
+ import * as base256emoji from './bases/base256emoji.js';
5
+ import * as base32 from './bases/base32.js';
6
+ import * as base36 from './bases/base36.js';
7
+ import * as base58 from './bases/base58.js';
8
+ import * as base64 from './bases/base64.js';
9
+ import * as base8 from './bases/base8.js';
10
+ import * as identity from './bases/identity.js';
11
+ import './codecs/json.js';
12
+
13
+ const bases = { ...identity, ...base2, ...base8, ...base10, ...base16, ...base32, ...base36, ...base58, ...base64, ...base256emoji };
14
+
15
+ export { bases };
@@ -1,3 +1,17 @@
1
+ function equals(aa, bb) {
2
+ if (aa === bb) {
3
+ return true;
4
+ }
5
+ if (aa.byteLength !== bb.byteLength) {
6
+ return false;
7
+ }
8
+ for (let ii = 0; ii < aa.byteLength; ii++) {
9
+ if (aa[ii] !== bb[ii]) {
10
+ return false;
11
+ }
12
+ }
13
+ return true;
14
+ }
1
15
  function coerce(o) {
2
16
  if (o instanceof Uint8Array && o.constructor.name === 'Uint8Array') {
3
17
  return o;
@@ -17,4 +31,4 @@ function toString(b) {
17
31
  return new TextDecoder().decode(b);
18
32
  }
19
33
 
20
- export { coerce, fromString, toString };
34
+ export { coerce, equals, fromString, toString };
@@ -0,0 +1,371 @@
1
+ import { base32 } from './bases/base32.js';
2
+ import { base36 } from './bases/base36.js';
3
+ import { base58btc } from './bases/base58.js';
4
+ import { coerce } from './bytes.js';
5
+ import { create, equals, decode, Digest } from './hashes/digest.js';
6
+ import { decode as decode$1, encodeTo, encodingLength } from './varint.js';
7
+
8
+ function format(link, base) {
9
+ const { bytes, version } = link;
10
+ switch (version) {
11
+ case 0:
12
+ return toStringV0(bytes, baseCache(link), base ?? base58btc.encoder);
13
+ default:
14
+ return toStringV1(bytes, baseCache(link), (base ?? base32.encoder));
15
+ }
16
+ }
17
+ const cache = new WeakMap();
18
+ function baseCache(cid) {
19
+ const baseCache = cache.get(cid);
20
+ if (baseCache == null) {
21
+ const baseCache = new Map();
22
+ cache.set(cid, baseCache);
23
+ return baseCache;
24
+ }
25
+ return baseCache;
26
+ }
27
+ class CID {
28
+ code;
29
+ version;
30
+ multihash;
31
+ bytes;
32
+ '/';
33
+ /**
34
+ * @param version - Version of the CID
35
+ * @param code - Code of the codec content is encoded in, see https://github.com/multiformats/multicodec/blob/master/table.csv
36
+ * @param multihash - (Multi)hash of the of the content.
37
+ */
38
+ constructor(version, code, multihash, bytes) {
39
+ this.code = code;
40
+ this.version = version;
41
+ this.multihash = multihash;
42
+ this.bytes = bytes;
43
+ // flag to serializers that this is a CID and
44
+ // should be treated specially
45
+ this['/'] = bytes;
46
+ }
47
+ /**
48
+ * Signalling `cid.asCID === cid` has been replaced with `cid['/'] === cid.bytes`
49
+ * please either use `CID.asCID(cid)` or switch to new signalling mechanism
50
+ *
51
+ * @deprecated
52
+ */
53
+ get asCID() {
54
+ return this;
55
+ }
56
+ // ArrayBufferView
57
+ get byteOffset() {
58
+ return this.bytes.byteOffset;
59
+ }
60
+ // ArrayBufferView
61
+ get byteLength() {
62
+ return this.bytes.byteLength;
63
+ }
64
+ toV0() {
65
+ switch (this.version) {
66
+ case 0: {
67
+ return this;
68
+ }
69
+ case 1: {
70
+ const { code, multihash } = this;
71
+ if (code !== DAG_PB_CODE) {
72
+ throw new Error('Cannot convert a non dag-pb CID to CIDv0');
73
+ }
74
+ // sha2-256
75
+ if (multihash.code !== SHA_256_CODE) {
76
+ throw new Error('Cannot convert non sha2-256 multihash CID to CIDv0');
77
+ }
78
+ return (CID.createV0(multihash));
79
+ }
80
+ default: {
81
+ throw Error(`Can not convert CID version ${this.version} to version 0. This is a bug please report`);
82
+ }
83
+ }
84
+ }
85
+ toV1() {
86
+ switch (this.version) {
87
+ case 0: {
88
+ const { code, digest } = this.multihash;
89
+ const multihash = create(code, digest);
90
+ return (CID.createV1(this.code, multihash));
91
+ }
92
+ case 1: {
93
+ return this;
94
+ }
95
+ default: {
96
+ throw Error(`Can not convert CID version ${this.version} to version 1. This is a bug please report`);
97
+ }
98
+ }
99
+ }
100
+ equals(other) {
101
+ return CID.equals(this, other);
102
+ }
103
+ static equals(self, other) {
104
+ const unknown = other;
105
+ return (unknown != null &&
106
+ self.code === unknown.code &&
107
+ self.version === unknown.version &&
108
+ equals(self.multihash, unknown.multihash));
109
+ }
110
+ toString(base) {
111
+ return format(this, base);
112
+ }
113
+ toJSON() {
114
+ return { '/': format(this) };
115
+ }
116
+ link() {
117
+ return this;
118
+ }
119
+ [Symbol.toStringTag] = 'CID';
120
+ // Legacy
121
+ [Symbol.for('nodejs.util.inspect.custom')]() {
122
+ return `CID(${this.toString()})`;
123
+ }
124
+ /**
125
+ * Takes any input `value` and returns a `CID` instance if it was
126
+ * a `CID` otherwise returns `null`. If `value` is instanceof `CID`
127
+ * it will return value back. If `value` is not instance of this CID
128
+ * class, but is compatible CID it will return new instance of this
129
+ * `CID` class. Otherwise returns null.
130
+ *
131
+ * This allows two different incompatible versions of CID library to
132
+ * co-exist and interop as long as binary interface is compatible.
133
+ */
134
+ static asCID(input) {
135
+ if (input == null) {
136
+ return null;
137
+ }
138
+ const value = input;
139
+ if (value instanceof CID) {
140
+ // If value is instance of CID then we're all set.
141
+ return value;
142
+ }
143
+ else if ((value['/'] != null && value['/'] === value.bytes) || value.asCID === value) {
144
+ // If value isn't instance of this CID class but `this.asCID === this` or
145
+ // `value['/'] === value.bytes` is true it is CID instance coming from a
146
+ // different implementation (diff version or duplicate). In that case we
147
+ // rebase it to this `CID` implementation so caller is guaranteed to get
148
+ // instance with expected API.
149
+ const { version, code, multihash, bytes } = value;
150
+ return new CID(version, code, multihash, bytes ?? encodeCID(version, code, multihash.bytes));
151
+ }
152
+ else if (value[cidSymbol] === true) {
153
+ // If value is a CID from older implementation that used to be tagged via
154
+ // symbol we still rebase it to the this `CID` implementation by
155
+ // delegating that to a constructor.
156
+ const { version, multihash, code } = value;
157
+ const digest = decode(multihash);
158
+ return CID.create(version, code, digest);
159
+ }
160
+ else {
161
+ // Otherwise value is not a CID (or an incompatible version of it) in
162
+ // which case we return `null`.
163
+ return null;
164
+ }
165
+ }
166
+ /**
167
+ * @param version - Version of the CID
168
+ * @param code - Code of the codec content is encoded in, see https://github.com/multiformats/multicodec/blob/master/table.csv
169
+ * @param digest - (Multi)hash of the of the content.
170
+ */
171
+ static create(version, code, digest) {
172
+ if (typeof code !== 'number') {
173
+ throw new Error('String codecs are no longer supported');
174
+ }
175
+ if (!(digest.bytes instanceof Uint8Array)) {
176
+ throw new Error('Invalid digest');
177
+ }
178
+ switch (version) {
179
+ case 0: {
180
+ if (code !== DAG_PB_CODE) {
181
+ throw new Error(`Version 0 CID must use dag-pb (code: ${DAG_PB_CODE}) block encoding`);
182
+ }
183
+ else {
184
+ return new CID(version, code, digest, digest.bytes);
185
+ }
186
+ }
187
+ case 1: {
188
+ const bytes = encodeCID(version, code, digest.bytes);
189
+ return new CID(version, code, digest, bytes);
190
+ }
191
+ default: {
192
+ throw new Error('Invalid version');
193
+ }
194
+ }
195
+ }
196
+ /**
197
+ * Simplified version of `create` for CIDv0.
198
+ */
199
+ static createV0(digest) {
200
+ return CID.create(0, DAG_PB_CODE, digest);
201
+ }
202
+ /**
203
+ * Simplified version of `create` for CIDv1.
204
+ *
205
+ * @param code - Content encoding format code.
206
+ * @param digest - Multihash of the content.
207
+ */
208
+ static createV1(code, digest) {
209
+ return CID.create(1, code, digest);
210
+ }
211
+ /**
212
+ * Decoded a CID from its binary representation. The byte array must contain
213
+ * only the CID with no additional bytes.
214
+ *
215
+ * An error will be thrown if the bytes provided do not contain a valid
216
+ * binary representation of a CID.
217
+ */
218
+ static decode(bytes) {
219
+ const [cid, remainder] = CID.decodeFirst(bytes);
220
+ if (remainder.length !== 0) {
221
+ throw new Error('Incorrect length');
222
+ }
223
+ return cid;
224
+ }
225
+ /**
226
+ * Decoded a CID from its binary representation at the beginning of a byte
227
+ * array.
228
+ *
229
+ * Returns an array with the first element containing the CID and the second
230
+ * element containing the remainder of the original byte array. The remainder
231
+ * will be a zero-length byte array if the provided bytes only contained a
232
+ * binary CID representation.
233
+ */
234
+ static decodeFirst(bytes) {
235
+ const specs = CID.inspectBytes(bytes);
236
+ const prefixSize = specs.size - specs.multihashSize;
237
+ const multihashBytes = coerce(bytes.subarray(prefixSize, prefixSize + specs.multihashSize));
238
+ if (multihashBytes.byteLength !== specs.multihashSize) {
239
+ throw new Error('Incorrect length');
240
+ }
241
+ const digestBytes = multihashBytes.subarray(specs.multihashSize - specs.digestSize);
242
+ const digest = new Digest(specs.multihashCode, specs.digestSize, digestBytes, multihashBytes);
243
+ const cid = specs.version === 0
244
+ ? CID.createV0(digest)
245
+ : CID.createV1(specs.codec, digest);
246
+ return [cid, bytes.subarray(specs.size)];
247
+ }
248
+ /**
249
+ * Inspect the initial bytes of a CID to determine its properties.
250
+ *
251
+ * Involves decoding up to 4 varints. Typically this will require only 4 to 6
252
+ * bytes but for larger multicodec code values and larger multihash digest
253
+ * lengths these varints can be quite large. It is recommended that at least
254
+ * 10 bytes be made available in the `initialBytes` argument for a complete
255
+ * inspection.
256
+ */
257
+ static inspectBytes(initialBytes) {
258
+ let offset = 0;
259
+ const next = () => {
260
+ const [i, length] = decode$1(initialBytes.subarray(offset));
261
+ offset += length;
262
+ return i;
263
+ };
264
+ let version = next();
265
+ let codec = DAG_PB_CODE;
266
+ if (version === 18) {
267
+ // CIDv0
268
+ version = 0;
269
+ offset = 0;
270
+ }
271
+ else {
272
+ codec = next();
273
+ }
274
+ if (version !== 0 && version !== 1) {
275
+ throw new RangeError(`Invalid CID version ${version}`);
276
+ }
277
+ const prefixSize = offset;
278
+ const multihashCode = next(); // multihash code
279
+ const digestSize = next(); // multihash length
280
+ const size = offset + digestSize;
281
+ const multihashSize = size - prefixSize;
282
+ return { version, codec, multihashCode, digestSize, multihashSize, size };
283
+ }
284
+ /**
285
+ * Takes cid in a string representation and creates an instance. If `base`
286
+ * decoder is not provided will use a default from the configuration. It will
287
+ * throw an error if encoding of the CID is not compatible with supplied (or
288
+ * a default decoder).
289
+ */
290
+ static parse(source, base) {
291
+ const [prefix, bytes] = parseCIDtoBytes(source, base);
292
+ const cid = CID.decode(bytes);
293
+ if (cid.version === 0 && source[0] !== 'Q') {
294
+ throw Error('Version 0 CID string must not include multibase prefix');
295
+ }
296
+ // Cache string representation to avoid computing it on `this.toString()`
297
+ baseCache(cid).set(prefix, source);
298
+ return cid;
299
+ }
300
+ }
301
+ function parseCIDtoBytes(source, base) {
302
+ switch (source[0]) {
303
+ // CIDv0 is parsed differently
304
+ case 'Q': {
305
+ const decoder = base ?? base58btc;
306
+ return [
307
+ base58btc.prefix,
308
+ decoder.decode(`${base58btc.prefix}${source}`)
309
+ ];
310
+ }
311
+ case base58btc.prefix: {
312
+ const decoder = base ?? base58btc;
313
+ return [base58btc.prefix, decoder.decode(source)];
314
+ }
315
+ case base32.prefix: {
316
+ const decoder = base ?? base32;
317
+ return [base32.prefix, decoder.decode(source)];
318
+ }
319
+ case base36.prefix: {
320
+ const decoder = base ?? base36;
321
+ return [base36.prefix, decoder.decode(source)];
322
+ }
323
+ default: {
324
+ if (base == null) {
325
+ throw Error('To parse non base32, base36 or base58btc encoded CID multibase decoder must be provided');
326
+ }
327
+ return [source[0], base.decode(source)];
328
+ }
329
+ }
330
+ }
331
+ function toStringV0(bytes, cache, base) {
332
+ const { prefix } = base;
333
+ if (prefix !== base58btc.prefix) {
334
+ throw Error(`Cannot string encode V0 in ${base.name} encoding`);
335
+ }
336
+ const cid = cache.get(prefix);
337
+ if (cid == null) {
338
+ const cid = base.encode(bytes).slice(1);
339
+ cache.set(prefix, cid);
340
+ return cid;
341
+ }
342
+ else {
343
+ return cid;
344
+ }
345
+ }
346
+ function toStringV1(bytes, cache, base) {
347
+ const { prefix } = base;
348
+ const cid = cache.get(prefix);
349
+ if (cid == null) {
350
+ const cid = base.encode(bytes);
351
+ cache.set(prefix, cid);
352
+ return cid;
353
+ }
354
+ else {
355
+ return cid;
356
+ }
357
+ }
358
+ const DAG_PB_CODE = 0x70;
359
+ const SHA_256_CODE = 0x12;
360
+ function encodeCID(version, code, multihash) {
361
+ const codeOffset = encodingLength(version);
362
+ const hashOffset = codeOffset + encodingLength(code);
363
+ const bytes = new Uint8Array(hashOffset + multihash.byteLength);
364
+ encodeTo(version, bytes, 0);
365
+ encodeTo(code, bytes, codeOffset);
366
+ bytes.set(multihash, hashOffset);
367
+ return bytes;
368
+ }
369
+ const cidSymbol = Symbol.for('@ipld/js-cid/CID');
370
+
371
+ export { CID, format };