quantum-coin-js-sdk 2.0.1 → 2.1.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 (232) hide show
  1. package/README.md +13 -0
  2. package/index.d.ts +9 -0
  3. package/index.js +37 -4
  4. package/package.json +12 -4
  5. package/wasmBase64.js +3 -2
  6. package/.idea/.name +0 -1
  7. package/.idea/modules.xml +0 -8
  8. package/.idea/quantum-coin-js-sdk.iml +0 -12
  9. package/.idea/vcs.xml +0 -6
  10. package/.vs/ProjectSettings.json +0 -3
  11. package/.vs/VSWorkspaceState.json +0 -8
  12. package/.vs/quantum-coin-js-sdk/FileContentIndex/a9d6de82-cfc3-4c22-ac38-06d706ff3673.vsidx +0 -0
  13. package/.vs/quantum-coin-js-sdk/FileContentIndex/fdc816df-1898-4153-99f2-c69ef4c2283b.vsidx +0 -0
  14. package/.vs/quantum-coin-js-sdk/config/applicationhost.config +0 -1011
  15. package/.vs/quantum-coin-js-sdk/v17/.wsuo +0 -0
  16. package/.vs/quantum-coin-js-sdk/v17/DocumentLayout.backup.json +0 -39
  17. package/.vs/quantum-coin-js-sdk/v17/DocumentLayout.json +0 -57
  18. package/.vs/slnx.sqlite +0 -0
  19. package/example/README.md +0 -14
  20. package/example/conversion-example.js +0 -19
  21. package/example/example-create-contract.js +0 -396
  22. package/example/example-encode-decode-rlp.js +0 -225
  23. package/example/example-event-pack-unpack.js +0 -391
  24. package/example/example-misc.js +0 -96
  25. package/example/example-token-pack-unpack.js +0 -960
  26. package/example/example-wallet-version4.js +0 -34
  27. package/example/example-wallet.js +0 -39
  28. package/example/example.js +0 -115
  29. package/example/node_modules/.bin/sdkgen +0 -16
  30. package/example/node_modules/.bin/sdkgen.cmd +0 -17
  31. package/example/node_modules/.bin/sdkgen.ps1 +0 -28
  32. package/example/node_modules/.package-lock.json +0 -51
  33. package/example/node_modules/quantumcoin/.github/workflows/publish-npmjs.yaml +0 -22
  34. package/example/node_modules/quantumcoin/LICENSE +0 -21
  35. package/example/node_modules/quantumcoin/README-SDK.md +0 -828
  36. package/example/node_modules/quantumcoin/README.md +0 -197
  37. package/example/node_modules/quantumcoin/config.d.ts +0 -50
  38. package/example/node_modules/quantumcoin/config.js +0 -123
  39. package/example/node_modules/quantumcoin/examples/AllSolidityTypes.sol +0 -184
  40. package/example/node_modules/quantumcoin/examples/SimpleIERC20.sol +0 -74
  41. package/example/node_modules/quantumcoin/examples/events.js +0 -41
  42. package/example/node_modules/quantumcoin/examples/events.ts +0 -35
  43. package/example/node_modules/quantumcoin/examples/example-generator-sdk-js.js +0 -100
  44. package/example/node_modules/quantumcoin/examples/example-generator-sdk-js.ts +0 -77
  45. package/example/node_modules/quantumcoin/examples/example-generator-sdk-ts.js +0 -100
  46. package/example/node_modules/quantumcoin/examples/example-generator-sdk-ts.ts +0 -77
  47. package/example/node_modules/quantumcoin/examples/example.js +0 -67
  48. package/example/node_modules/quantumcoin/examples/example.ts +0 -56
  49. package/example/node_modules/quantumcoin/examples/offline-signing.js +0 -77
  50. package/example/node_modules/quantumcoin/examples/offline-signing.ts +0 -65
  51. package/example/node_modules/quantumcoin/examples/package-lock.json +0 -596
  52. package/example/node_modules/quantumcoin/examples/package.json +0 -32
  53. package/example/node_modules/quantumcoin/examples/read-operations.js +0 -32
  54. package/example/node_modules/quantumcoin/examples/read-operations.ts +0 -31
  55. package/example/node_modules/quantumcoin/examples/sdk-generator-erc20.inline.json +0 -251
  56. package/example/node_modules/quantumcoin/examples/solidity-types.ts +0 -43
  57. package/example/node_modules/quantumcoin/examples/wallet-offline.js +0 -36
  58. package/example/node_modules/quantumcoin/examples/wallet-offline.ts +0 -26
  59. package/example/node_modules/quantumcoin/generate-sdk.js +0 -1846
  60. package/example/node_modules/quantumcoin/index.js +0 -12
  61. package/example/node_modules/quantumcoin/package.json +0 -95
  62. package/example/node_modules/quantumcoin/scripts/copy-declarations.js +0 -31
  63. package/example/node_modules/quantumcoin/scripts/run-all-one-by-one.js +0 -151
  64. package/example/node_modules/quantumcoin/src/abi/fragments.d.ts +0 -42
  65. package/example/node_modules/quantumcoin/src/abi/fragments.js +0 -63
  66. package/example/node_modules/quantumcoin/src/abi/index.d.ts +0 -13
  67. package/example/node_modules/quantumcoin/src/abi/index.js +0 -9
  68. package/example/node_modules/quantumcoin/src/abi/interface.d.ts +0 -128
  69. package/example/node_modules/quantumcoin/src/abi/interface.js +0 -605
  70. package/example/node_modules/quantumcoin/src/abi/js-abi-coder.d.ts +0 -8
  71. package/example/node_modules/quantumcoin/src/abi/js-abi-coder.js +0 -538
  72. package/example/node_modules/quantumcoin/src/constants.d.ts +0 -61
  73. package/example/node_modules/quantumcoin/src/constants.js +0 -94
  74. package/example/node_modules/quantumcoin/src/contract/contract-factory.d.ts +0 -28
  75. package/example/node_modules/quantumcoin/src/contract/contract-factory.js +0 -111
  76. package/example/node_modules/quantumcoin/src/contract/contract.d.ts +0 -113
  77. package/example/node_modules/quantumcoin/src/contract/contract.js +0 -408
  78. package/example/node_modules/quantumcoin/src/contract/index.d.ts +0 -9
  79. package/example/node_modules/quantumcoin/src/contract/index.js +0 -9
  80. package/example/node_modules/quantumcoin/src/errors/index.d.ts +0 -92
  81. package/example/node_modules/quantumcoin/src/errors/index.js +0 -200
  82. package/example/node_modules/quantumcoin/src/generator/index.d.ts +0 -81
  83. package/example/node_modules/quantumcoin/src/generator/index.js +0 -1571
  84. package/example/node_modules/quantumcoin/src/index.d.ts +0 -122
  85. package/example/node_modules/quantumcoin/src/index.js +0 -41
  86. package/example/node_modules/quantumcoin/src/internal/hex.d.ts +0 -68
  87. package/example/node_modules/quantumcoin/src/internal/hex.js +0 -180
  88. package/example/node_modules/quantumcoin/src/providers/extra-providers.d.ts +0 -139
  89. package/example/node_modules/quantumcoin/src/providers/extra-providers.js +0 -614
  90. package/example/node_modules/quantumcoin/src/providers/index.d.ts +0 -17
  91. package/example/node_modules/quantumcoin/src/providers/index.js +0 -10
  92. package/example/node_modules/quantumcoin/src/providers/json-rpc-provider.d.ts +0 -12
  93. package/example/node_modules/quantumcoin/src/providers/json-rpc-provider.js +0 -89
  94. package/example/node_modules/quantumcoin/src/providers/provider.d.ts +0 -206
  95. package/example/node_modules/quantumcoin/src/providers/provider.js +0 -524
  96. package/example/node_modules/quantumcoin/src/types/index.d.ts +0 -214
  97. package/example/node_modules/quantumcoin/src/types/index.js +0 -9
  98. package/example/node_modules/quantumcoin/src/utils/address.d.ts +0 -58
  99. package/example/node_modules/quantumcoin/src/utils/address.js +0 -144
  100. package/example/node_modules/quantumcoin/src/utils/encoding.d.ts +0 -120
  101. package/example/node_modules/quantumcoin/src/utils/encoding.js +0 -306
  102. package/example/node_modules/quantumcoin/src/utils/fixednumber.d.ts +0 -57
  103. package/example/node_modules/quantumcoin/src/utils/fixednumber.js +0 -366
  104. package/example/node_modules/quantumcoin/src/utils/hashing.d.ts +0 -76
  105. package/example/node_modules/quantumcoin/src/utils/hashing.js +0 -298
  106. package/example/node_modules/quantumcoin/src/utils/index.d.ts +0 -63
  107. package/example/node_modules/quantumcoin/src/utils/index.js +0 -14
  108. package/example/node_modules/quantumcoin/src/utils/result.d.ts +0 -57
  109. package/example/node_modules/quantumcoin/src/utils/result.js +0 -128
  110. package/example/node_modules/quantumcoin/src/utils/rlp.d.ts +0 -12
  111. package/example/node_modules/quantumcoin/src/utils/rlp.js +0 -215
  112. package/example/node_modules/quantumcoin/src/utils/units.d.ts +0 -29
  113. package/example/node_modules/quantumcoin/src/utils/units.js +0 -107
  114. package/example/node_modules/quantumcoin/src/wallet/index.d.ts +0 -10
  115. package/example/node_modules/quantumcoin/src/wallet/index.js +0 -8
  116. package/example/node_modules/quantumcoin/src/wallet/wallet.d.ts +0 -192
  117. package/example/node_modules/quantumcoin/src/wallet/wallet.js +0 -713
  118. package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.dynamic.test.js +0 -207
  119. package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.dynamic.test.ts +0 -191
  120. package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.fixtures.js +0 -231
  121. package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.generated-sdks.e2e.test.js +0 -387
  122. package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.generated-sdks.e2e.test.ts +0 -350
  123. package/example/node_modules/quantumcoin/test/e2e/generator-interface.e2e.test.js +0 -165
  124. package/example/node_modules/quantumcoin/test/e2e/generator-interface.e2e.test.ts +0 -160
  125. package/example/node_modules/quantumcoin/test/e2e/helpers.js +0 -59
  126. package/example/node_modules/quantumcoin/test/e2e/signing-context-and-fee.e2e.test.js +0 -141
  127. package/example/node_modules/quantumcoin/test/e2e/signing-context-and-fee.e2e.test.ts +0 -128
  128. package/example/node_modules/quantumcoin/test/e2e/simple-erc20.generated-sdks.e2e.test.js +0 -168
  129. package/example/node_modules/quantumcoin/test/e2e/simple-erc20.generated-sdks.e2e.test.ts +0 -141
  130. package/example/node_modules/quantumcoin/test/e2e/transactional.test.js +0 -245
  131. package/example/node_modules/quantumcoin/test/e2e/transactional.test.ts +0 -208
  132. package/example/node_modules/quantumcoin/test/e2e/typed-generator.e2e.test.js +0 -407
  133. package/example/node_modules/quantumcoin/test/e2e/typed-generator.e2e.test.ts +0 -337
  134. package/example/node_modules/quantumcoin/test/fixtures/ConstructorParam.sol +0 -23
  135. package/example/node_modules/quantumcoin/test/fixtures/MultiContracts.sol +0 -37
  136. package/example/node_modules/quantumcoin/test/fixtures/SimpleStorage.sol +0 -18
  137. package/example/node_modules/quantumcoin/test/fixtures/StakingContract.abi.json +0 -1
  138. package/example/node_modules/quantumcoin/test/integration/ipc-provider.test.js +0 -49
  139. package/example/node_modules/quantumcoin/test/integration/ipc-provider.test.ts +0 -44
  140. package/example/node_modules/quantumcoin/test/integration/provider.test.js +0 -88
  141. package/example/node_modules/quantumcoin/test/integration/provider.test.ts +0 -85
  142. package/example/node_modules/quantumcoin/test/integration/ws-provider.test.js +0 -41
  143. package/example/node_modules/quantumcoin/test/integration/ws-provider.test.ts +0 -38
  144. package/example/node_modules/quantumcoin/test/security/abi-decoder-bounds.test.js +0 -122
  145. package/example/node_modules/quantumcoin/test/security/contract-overrides.test.js +0 -112
  146. package/example/node_modules/quantumcoin/test/security/generator-injection.test.js +0 -195
  147. package/example/node_modules/quantumcoin/test/security/malformed-input.test.js +0 -330
  148. package/example/node_modules/quantumcoin/test/security/malformed-input.test.ts +0 -35
  149. package/example/node_modules/quantumcoin/test/security/rpc-numeric-bounds.test.js +0 -81
  150. package/example/node_modules/quantumcoin/test/security/rpc-trust.test.js +0 -202
  151. package/example/node_modules/quantumcoin/test/unit/_encrypted-output.txt +0 -6
  152. package/example/node_modules/quantumcoin/test/unit/_log-encrypted-jsons.js +0 -45
  153. package/example/node_modules/quantumcoin/test/unit/_write-keystore-fixture.js +0 -16
  154. package/example/node_modules/quantumcoin/test/unit/abi-interface.test.js +0 -110
  155. package/example/node_modules/quantumcoin/test/unit/abi-interface.test.ts +0 -109
  156. package/example/node_modules/quantumcoin/test/unit/address-wallet.test.js +0 -923
  157. package/example/node_modules/quantumcoin/test/unit/address-wallet.test.ts +0 -877
  158. package/example/node_modules/quantumcoin/test/unit/browser-provider.test.js +0 -85
  159. package/example/node_modules/quantumcoin/test/unit/browser-provider.test.ts +0 -79
  160. package/example/node_modules/quantumcoin/test/unit/contract.test.js +0 -85
  161. package/example/node_modules/quantumcoin/test/unit/contract.test.ts +0 -83
  162. package/example/node_modules/quantumcoin/test/unit/encoding-units-rlp.test.js +0 -127
  163. package/example/node_modules/quantumcoin/test/unit/encoding-units-rlp.test.ts +0 -91
  164. package/example/node_modules/quantumcoin/test/unit/errors.test.js +0 -77
  165. package/example/node_modules/quantumcoin/test/unit/errors.test.ts +0 -76
  166. package/example/node_modules/quantumcoin/test/unit/filter-by-blockhash.test.js +0 -55
  167. package/example/node_modules/quantumcoin/test/unit/filter-by-blockhash.test.ts +0 -54
  168. package/example/node_modules/quantumcoin/test/unit/fixednumber.test.js +0 -656
  169. package/example/node_modules/quantumcoin/test/unit/fixednumber.test.ts +0 -660
  170. package/example/node_modules/quantumcoin/test/unit/fixtures/encrypted-keystores-48-32-36.js +0 -9
  171. package/example/node_modules/quantumcoin/test/unit/generate-contract-cli.test.js +0 -42
  172. package/example/node_modules/quantumcoin/test/unit/generate-contract-cli.test.ts +0 -41
  173. package/example/node_modules/quantumcoin/test/unit/generate-sdk-artifacts-json.test.js +0 -113
  174. package/example/node_modules/quantumcoin/test/unit/generate-sdk-artifacts-json.test.ts +0 -110
  175. package/example/node_modules/quantumcoin/test/unit/generator.test.js +0 -149
  176. package/example/node_modules/quantumcoin/test/unit/generator.test.ts +0 -148
  177. package/example/node_modules/quantumcoin/test/unit/hashing.test.js +0 -64
  178. package/example/node_modules/quantumcoin/test/unit/hashing.test.ts +0 -63
  179. package/example/node_modules/quantumcoin/test/unit/init.test.js +0 -39
  180. package/example/node_modules/quantumcoin/test/unit/init.test.ts +0 -38
  181. package/example/node_modules/quantumcoin/test/unit/interface.test.js +0 -56
  182. package/example/node_modules/quantumcoin/test/unit/interface.test.ts +0 -54
  183. package/example/node_modules/quantumcoin/test/unit/internal-hex.test.js +0 -81
  184. package/example/node_modules/quantumcoin/test/unit/internal-hex.test.ts +0 -80
  185. package/example/node_modules/quantumcoin/test/unit/populate-transaction.test.js +0 -98
  186. package/example/node_modules/quantumcoin/test/unit/populate-transaction.test.ts +0 -64
  187. package/example/node_modules/quantumcoin/test/unit/providers.test.js +0 -252
  188. package/example/node_modules/quantumcoin/test/unit/providers.test.ts +0 -251
  189. package/example/node_modules/quantumcoin/test/unit/result.test.js +0 -80
  190. package/example/node_modules/quantumcoin/test/unit/result.test.ts +0 -79
  191. package/example/node_modules/quantumcoin/test/unit/solidity-types.test.js +0 -49
  192. package/example/node_modules/quantumcoin/test/unit/solidity-types.test.ts +0 -39
  193. package/example/node_modules/quantumcoin/test/unit/utils.test.js +0 -57
  194. package/example/node_modules/quantumcoin/test/unit/utils.test.ts +0 -56
  195. package/example/node_modules/quantumcoin/test/verbose-logger.js +0 -74
  196. package/example/node_modules/quantumcoin/tsconfig.build.json +0 -14
  197. package/example/node_modules/seed-words/.github/workflows/publish-npmjs.yaml +0 -22
  198. package/example/node_modules/seed-words/BUILD.md +0 -7
  199. package/example/node_modules/seed-words/LICENSE +0 -121
  200. package/example/node_modules/seed-words/README.md +0 -67
  201. package/example/node_modules/seed-words/dist/seedwords.d.ts +0 -39
  202. package/example/node_modules/seed-words/package.json +0 -27
  203. package/example/node_modules/seed-words/seedwords.js +0 -315
  204. package/example/node_modules/seed-words/seedwords.txt +0 -65536
  205. package/example/node_modules/seed-words/tsconfig.json +0 -21
  206. package/example/package-lock.json +0 -60
  207. package/example/package.json +0 -15
  208. package/scripts/build-browser-tests.js +0 -29
  209. package/scripts/build-wasm.js +0 -85
  210. package/tests/browser/bundle.js +0 -97643
  211. package/tests/browser/entry.js +0 -57
  212. package/tests/browser/harness.html +0 -20
  213. package/tests/browser/run-browser.js +0 -50
  214. package/tests/encrypted-32.json +0 -1
  215. package/tests/encrypted-36.json +0 -1
  216. package/tests/encrypted-48.json +0 -1
  217. package/tests/generate-verify-vectors.js +0 -88
  218. package/tests/get-gas-price.test.js +0 -59
  219. package/tests/hashing.test.js +0 -34
  220. package/tests/non-transactional.preinit.test.js +0 -33
  221. package/tests/non-transactional.test.js +0 -1318
  222. package/tests/sign-raw-keytype5-context-null.test.js +0 -105
  223. package/tests/sign-raw-transaction.test.js +0 -194
  224. package/tests/sign-verify.test.js +0 -308
  225. package/tests/specs/assert.js +0 -74
  226. package/tests/specs/crypto.spec.js +0 -104
  227. package/tests/specs/hex.js +0 -31
  228. package/tests/specs/preinit.spec.js +0 -19
  229. package/tests/specs/sign.spec.js +0 -90
  230. package/tests/specs/wallet.spec.js +0 -98
  231. package/tests/transactional.rpc.test.js +0 -102
  232. package/tests/verify-vectors.json +0 -95035
@@ -1,538 +0,0 @@
1
- /**
2
- * @fileoverview Pure-JS ABI encoder/decoder (Solidity ABI compatible).
3
- *
4
- * This exists as a fallback for ABI surfaces that `quantum-coin-js-sdk` does not
5
- * currently handle correctly (notably: tuples/structs and complex nested types).
6
- *
7
- * Notes:
8
- * - QuantumCoin addresses are 32 bytes (not 20).
9
- * - Integers are encoded as 32-byte words (big-endian), with range enforcement.
10
- */
11
-
12
- const { makeError } = require("../errors");
13
- const { normalizeHex, strip0x, arrayify, bytesToHex, utf8ToBytes, bytesToUtf8 } = require("../internal/hex");
14
- const { id } = require("../utils/hashing");
15
-
16
- const MAX_ABI_DEPTH = 16;
17
-
18
- function _isArrayType(type) {
19
- return typeof type === "string" && type.endsWith("]");
20
- }
21
-
22
- function _arrayInnerType(type) {
23
- return type.slice(0, type.lastIndexOf("["));
24
- }
25
-
26
- function _arrayBracket(type) {
27
- return type.slice(type.lastIndexOf("[") + 1, type.length - 1); // "" for dynamic
28
- }
29
-
30
- function _isDynamicArray(type) {
31
- return _isArrayType(type) && _arrayBracket(type) === "";
32
- }
33
-
34
- function _isFixedArray(type) {
35
- return _isArrayType(type) && _arrayBracket(type) !== "";
36
- }
37
-
38
- function _fixedArrayLength(type) {
39
- const n = Number(_arrayBracket(type));
40
- return Number.isFinite(n) ? n : 0;
41
- }
42
-
43
- function _isBytesN(type) {
44
- return /^bytes(\d+)$/.test(type);
45
- }
46
-
47
- function _bytesNLen(type) {
48
- const m = type.match(/^bytes(\d+)$/);
49
- const n = m ? Number(m[1]) : 0;
50
- return Number.isFinite(n) ? n : 0;
51
- }
52
-
53
- function _isUint(type) {
54
- return type === "uint" || /^uint(\d+)$/.test(type);
55
- }
56
-
57
- function _isInt(type) {
58
- return type === "int" || /^int(\d+)$/.test(type);
59
- }
60
-
61
- function _intBits(type) {
62
- if (type === "uint" || type === "int") return 256;
63
- const m = type.match(/^(u?int)(\d+)$/);
64
- const n = m ? Number(m[2]) : 0;
65
- return Number.isFinite(n) && n > 0 ? n : 256;
66
- }
67
-
68
- function _isDynamicType(param) {
69
- const type = String(param && param.type ? param.type : "");
70
- if (_isArrayType(type)) {
71
- const innerParam = { ...(param || {}), type: _arrayInnerType(type) };
72
- return _isDynamicArray(type) || _isDynamicType(innerParam);
73
- }
74
- if (type === "string" || type === "bytes") return true;
75
- if (type === "tuple") {
76
- const comps = Array.isArray(param.components) ? param.components : [];
77
- return comps.some(_isDynamicType);
78
- }
79
- return false;
80
- }
81
-
82
- function _staticWords(param) {
83
- const type = String(param && param.type ? param.type : "");
84
- if (_isDynamicType(param)) return 1; // head word is offset
85
- if (_isArrayType(type)) {
86
- const innerParam = { ...(param || {}), type: _arrayInnerType(type) };
87
- if (_isFixedArray(type)) return _fixedArrayLength(type) * _staticWords(innerParam);
88
- // dynamic arrays are dynamic types (handled above)
89
- }
90
- if (type === "tuple") {
91
- const comps = Array.isArray(param.components) ? param.components : [];
92
- return comps.reduce((a, c) => a + _staticWords(c), 0);
93
- }
94
- // elementary static types => 1 word
95
- return 1;
96
- }
97
-
98
- function _padRightTo32(bytes) {
99
- const pad = (32 - (bytes.length % 32)) % 32;
100
- if (pad === 0) return bytes;
101
- const out = new Uint8Array(bytes.length + pad);
102
- out.set(bytes, 0);
103
- return out;
104
- }
105
-
106
- function _concat(parts) {
107
- const len = parts.reduce((a, b) => a + b.length, 0);
108
- const out = new Uint8Array(len);
109
- let off = 0;
110
- for (const p of parts) {
111
- out.set(p, off);
112
- off += p.length;
113
- }
114
- return out;
115
- }
116
-
117
- function _u256ToWord(bi) {
118
- let x = bi;
119
- if (x < 0n) throw new Error("uint must be non-negative");
120
- if (x >= (1n << 256n)) throw makeError("value exceeds uint256", "INVALID_ARGUMENT", { value: x.toString() });
121
- let hex = x.toString(16);
122
- hex = hex.padStart(64, "0");
123
- return arrayify("0x" + hex);
124
- }
125
-
126
- function _checkUintRange(bi, bits) {
127
- const b = BigInt(bits);
128
- const max = 1n << b;
129
- if (bi < 0n || bi >= max) {
130
- throw makeError("uint value out of range", "INVALID_ARGUMENT", { value: bi.toString(), bits });
131
- }
132
- }
133
-
134
- function _checkIntRange(bi, bits) {
135
- const b = BigInt(bits);
136
- const min = -(1n << (b - 1n));
137
- const max = (1n << (b - 1n)) - 1n;
138
- if (bi < min || bi > max) {
139
- throw makeError("int value out of range", "INVALID_ARGUMENT", { value: bi.toString(), bits });
140
- }
141
- }
142
-
143
- function _encodeIntWord(bi, bits) {
144
- // Solidity ABI sign-extends int<M> to 256 bits.
145
- _checkIntRange(bi, bits);
146
- const mod256 = 1n << 256n;
147
- const x = bi < 0n ? mod256 + bi : bi;
148
- return _u256ToWord(x);
149
- }
150
-
151
- function _asBigInt(value) {
152
- if (typeof value === "bigint") return value;
153
- if (typeof value === "number") {
154
- if (!Number.isFinite(value)) throw makeError("invalid number", "INVALID_ARGUMENT", { value });
155
- return BigInt(Math.trunc(value));
156
- }
157
- if (typeof value === "string") {
158
- const s = value.trim();
159
- if (/^0x[0-9a-fA-F]+$/.test(s)) return BigInt(s);
160
- if (/^-?\d+$/.test(s)) return BigInt(s);
161
- }
162
- throw makeError("invalid bigint-ish value", "INVALID_ARGUMENT", { value });
163
- }
164
-
165
- function _encodeAddress(value) {
166
- const h = normalizeHex(String(value));
167
- const b = arrayify(h);
168
- if (b.length > 32) throw makeError("address too long", "INVALID_ARGUMENT", { value });
169
- // Left-pad to 32 bytes.
170
- const out = new Uint8Array(32);
171
- out.set(b, 32 - b.length);
172
- return out;
173
- }
174
-
175
- function _encodeFixedBytes(type, value) {
176
- const n = _bytesNLen(type);
177
- const b = arrayify(value);
178
- if (b.length !== n) throw makeError("invalid fixed bytes length", "INVALID_ARGUMENT", { type, length: b.length });
179
- const out = new Uint8Array(32);
180
- out.set(b, 0);
181
- return out;
182
- }
183
-
184
- function _encodeBytesDynamic(value) {
185
- const b = arrayify(value);
186
- const lenWord = _u256ToWord(BigInt(b.length));
187
- return _concat([lenWord, _padRightTo32(b)]);
188
- }
189
-
190
- function _encodeString(value) {
191
- const bytes = utf8ToBytes(String(value));
192
- const lenWord = _u256ToWord(BigInt(bytes.length));
193
- return _concat([lenWord, _padRightTo32(bytes)]);
194
- }
195
-
196
- function _tupleValues(components, value) {
197
- if (Array.isArray(value)) return value;
198
- if (value && typeof value === "object") {
199
- return components.map((c, i) => {
200
- const name = c && typeof c.name === "string" && c.name ? c.name : String(i);
201
- if (Object.prototype.hasOwnProperty.call(value, name)) return value[name];
202
- if (Object.prototype.hasOwnProperty.call(value, String(i))) return value[String(i)];
203
- return undefined;
204
- });
205
- }
206
- return [];
207
- }
208
-
209
- function encodeTupleLike(params, values, depth) {
210
- if (depth == null) depth = 0;
211
- const ps = Array.isArray(params) ? params : [];
212
- const vs = Array.isArray(values) ? values : [];
213
-
214
- const headWords = ps.reduce((a, p) => a + (_isDynamicType(p) ? 1 : _staticWords(p)), 0);
215
- const headSize = headWords * 32;
216
-
217
- /** @type {Uint8Array[]} */
218
- const headParts = [];
219
- /** @type {Uint8Array[]} */
220
- const tailParts = [];
221
- let tailOffset = 0;
222
-
223
- for (let i = 0; i < ps.length; i++) {
224
- const p = ps[i];
225
- const v = vs[i];
226
- if (_isDynamicType(p)) {
227
- const encTail = encodeParam(p, v, depth);
228
- headParts.push(_u256ToWord(BigInt(headSize + tailOffset)));
229
- tailParts.push(encTail);
230
- tailOffset += encTail.length;
231
- } else {
232
- headParts.push(encodeParam(p, v, depth));
233
- }
234
- }
235
-
236
- return _concat([...headParts, ...tailParts]);
237
- }
238
-
239
- function encodeParam(param, value, depth) {
240
- if (depth == null) depth = 0;
241
- if (depth > MAX_ABI_DEPTH) throw makeError("ABI encoding: maximum nesting depth exceeded", "INVALID_ARGUMENT", {});
242
- const type = String(param && param.type ? param.type : "");
243
-
244
- if (_isArrayType(type)) {
245
- const innerType = _arrayInnerType(type);
246
- const innerParam = { ...(param || {}), type: innerType };
247
- const arr = Array.isArray(value) ? value : [];
248
-
249
- if (_isDynamicArray(type)) {
250
- const lenWord = _u256ToWord(BigInt(arr.length));
251
- const elems = encodeTupleLike(Array.from({ length: arr.length }).map(() => innerParam), arr, depth + 1);
252
- return _concat([lenWord, elems]);
253
- }
254
-
255
- const n = _fixedArrayLength(type);
256
- const elems = encodeTupleLike(Array.from({ length: n }).map(() => innerParam), arr.slice(0, n), depth + 1);
257
- return elems;
258
- }
259
-
260
- if (type === "tuple") {
261
- const comps = Array.isArray(param.components) ? param.components : [];
262
- const vals = _tupleValues(comps, value);
263
- return encodeTupleLike(comps, vals, depth + 1);
264
- }
265
-
266
- if (_isUint(type)) {
267
- const bits = _intBits(type);
268
- const bi = _asBigInt(value);
269
- _checkUintRange(bi, bits);
270
- return _u256ToWord(bi);
271
- }
272
- if (_isInt(type)) return _encodeIntWord(_asBigInt(value), _intBits(type));
273
- if (type === "bool") return _u256ToWord(value ? 1n : 0n);
274
- if (type === "address") return _encodeAddress(value);
275
- if (type === "string") return _encodeString(value);
276
- if (type === "bytes") return _encodeBytesDynamic(value);
277
- if (_isBytesN(type)) return _encodeFixedBytes(type, value);
278
-
279
- throw makeError("unsupported ABI type", "NOT_IMPLEMENTED", { type });
280
- }
281
-
282
- function canonicalType(param) {
283
- const type = String(param && param.type ? param.type : "");
284
- if (_isArrayType(type)) {
285
- const innerType = _arrayInnerType(type);
286
- const suffix = type.slice(type.lastIndexOf("["));
287
- return canonicalType({ ...(param || {}), type: innerType }) + suffix;
288
- }
289
- if (type === "tuple") {
290
- const comps = Array.isArray(param.components) ? param.components : [];
291
- return `(${comps.map((c) => canonicalType(c)).join(",")})`;
292
- }
293
- return type;
294
- }
295
-
296
- function functionSelectorHex(name, inputs) {
297
- const sig = `${name}(${(inputs || []).map((i) => canonicalType(i)).join(",")})`;
298
- return normalizeHex(id(sig)).slice(0, 10);
299
- }
300
-
301
- function encodeFunctionData(name, inputs, values) {
302
- const selector = functionSelectorHex(name, inputs);
303
- const enc = encodeTupleLike(inputs, values || []);
304
- return normalizeHex(selector + strip0x(bytesToHex(enc)));
305
- }
306
-
307
- // A 32-byte word must lie fully within the data. Without this check
308
- // `data.slice(offset, offset + 32)` silently returns a short/empty array, so an
309
- // out-of-bounds read decodes to `0n` / the zero address — letting a malicious RPC
310
- // response corrupt static return values (e.g. a balance silently reads as 0).
311
- function _assertWordInBounds(data, offset) {
312
- if (!Number.isInteger(offset) || offset < 0 || offset + 32 > data.length) {
313
- throw makeError("ABI decoding: read past end of data", "INVALID_ARGUMENT", {
314
- offset,
315
- length: data.length,
316
- });
317
- }
318
- }
319
-
320
- function _readWordAsBigInt(data, offset) {
321
- _assertWordInBounds(data, offset);
322
- const chunk = data.slice(offset, offset + 32);
323
- const hex = bytesToHex(chunk);
324
- return BigInt(hex);
325
- }
326
-
327
- function _readWordAsNumber(data, offset) {
328
- const bi = _readWordAsBigInt(data, offset);
329
- if (bi > BigInt(Number.MAX_SAFE_INTEGER)) throw makeError("offset too large", "INVALID_ARGUMENT", { offset: bi.toString() });
330
- return Number(bi);
331
- }
332
-
333
- function _decodeUint(type, data, offset) {
334
- void type;
335
- return _readWordAsBigInt(data, offset);
336
- }
337
-
338
- function _decodeInt(type, data, offset) {
339
- const bits = _intBits(type);
340
- const bi = _readWordAsBigInt(data, offset);
341
- const mod = 1n << BigInt(bits);
342
- const mask = mod - 1n;
343
- const trunc = bi & mask; // int<M> is sign-extended; recover the low M bits
344
- const signBit = 1n << BigInt(bits - 1);
345
- const signed = trunc & signBit ? trunc - mod : trunc;
346
- return signed;
347
- }
348
-
349
- function _decodeBool(data, offset) {
350
- const bi = _readWordAsBigInt(data, offset);
351
- return bi !== 0n;
352
- }
353
-
354
- function _decodeAddress(data, offset) {
355
- _assertWordInBounds(data, offset);
356
- const chunk = data.slice(offset, offset + 32);
357
- return normalizeHex(bytesToHex(chunk));
358
- }
359
-
360
- function _decodeFixedBytes(type, data, offset) {
361
- _assertWordInBounds(data, offset);
362
- const n = _bytesNLen(type);
363
- const chunk = data.slice(offset, offset + 32);
364
- return normalizeHex(bytesToHex(chunk.slice(0, n)));
365
- }
366
-
367
- function _decodeBytesDynamic(data, baseOffset) {
368
- const len = _readWordAsNumber(data, baseOffset);
369
- const start = baseOffset + 32;
370
- // The declared length must fit within the available data; otherwise a
371
- // malicious response could declare a huge length and cause silent truncation
372
- // or oversized allocation.
373
- if (start + len > data.length) {
374
- throw makeError("ABI decoding: bytes length exceeds available data", "INVALID_ARGUMENT", { length: len });
375
- }
376
- const out = data.slice(start, start + len);
377
- return normalizeHex(bytesToHex(out));
378
- }
379
-
380
- function _decodeString(data, baseOffset) {
381
- const len = _readWordAsNumber(data, baseOffset);
382
- const start = baseOffset + 32;
383
- // The declared length must fit within the available data.
384
- if (start + len > data.length) {
385
- throw makeError("ABI decoding: string length exceeds available data", "INVALID_ARGUMENT", { length: len });
386
- }
387
- const out = data.slice(start, start + len);
388
- return bytesToUtf8(out);
389
- }
390
-
391
- function decodeTupleLike(params, data, baseOffset, depth) {
392
- if (depth == null) depth = 0;
393
- const ps = Array.isArray(params) ? params : [];
394
-
395
- const headWords = ps.reduce((a, p) => a + (_isDynamicType(p) ? 1 : _staticWords(p)), 0);
396
- const headSize = headWords * 32;
397
-
398
- /** @type {any[]} */
399
- const values = [];
400
- let headOff = 0;
401
-
402
- for (let i = 0; i < ps.length; i++) {
403
- const p = ps[i];
404
- if (_isDynamicType(p)) {
405
- const rel = _readWordAsNumber(data, baseOffset + headOff);
406
- // The dynamic offset is attacker-controlled. In canonical ABI encoding
407
- // the tail always starts at or after the head, so reject offsets that point
408
- // back into the head region (aliasing) and offsets that fall outside the
409
- // available data before following the pointer.
410
- if (rel < headSize) {
411
- throw makeError("ABI decoding: dynamic offset points into head region", "INVALID_ARGUMENT", {
412
- offset: rel,
413
- headSize,
414
- });
415
- }
416
- if (baseOffset + rel + 32 > data.length) {
417
- throw makeError("ABI decoding: dynamic offset out of bounds", "INVALID_ARGUMENT", {
418
- offset: rel,
419
- length: data.length,
420
- });
421
- }
422
- values.push(decodeParam(p, data, baseOffset + rel, depth));
423
- headOff += 32;
424
- } else {
425
- values.push(decodeParam(p, data, baseOffset + headOff, depth));
426
- headOff += _staticWords(p) * 32;
427
- }
428
- }
429
-
430
- return values;
431
- }
432
-
433
- function decodeParam(param, data, offset, depth) {
434
- if (depth == null) depth = 0;
435
- if (depth > MAX_ABI_DEPTH) throw makeError("ABI decoding: maximum nesting depth exceeded", "INVALID_ARGUMENT", {});
436
- const type = String(param && param.type ? param.type : "");
437
-
438
- if (_isArrayType(type)) {
439
- const innerType = _arrayInnerType(type);
440
- const innerParam = { ...(param || {}), type: innerType };
441
-
442
- if (_isDynamicArray(type)) {
443
- const len = _readWordAsNumber(data, offset);
444
- // Each element consumes at least one 32-byte head word. Reject lengths
445
- // that cannot possibly fit in the remaining calldata before allocating an
446
- // array of `len` entries (prevents unbounded allocation / OOM from a
447
- // hostile ABI response declaring an enormous length).
448
- const elemsStart = offset + 32;
449
- if (len * 32 > data.length - elemsStart) {
450
- throw makeError("ABI decoding: array length exceeds available data", "INVALID_ARGUMENT", { length: len });
451
- }
452
- const elems = decodeTupleLike(Array.from({ length: len }).map(() => innerParam), data, elemsStart, depth + 1);
453
- return elems;
454
- }
455
-
456
- const n = _fixedArrayLength(type);
457
- // A fixed-array dimension comes from the (attacker-controllable) type
458
- // string. Each element occupies at least one 32-byte head word, so reject a
459
- // length that cannot possibly fit in the remaining data before allocating an
460
- // array of `n` entries (prevents unbounded allocation / OOM from e.g.
461
- // `uint256[1000000000]`).
462
- if (n * 32 > data.length - offset) {
463
- throw makeError("ABI decoding: fixed array length exceeds available data", "INVALID_ARGUMENT", {
464
- length: n,
465
- });
466
- }
467
- return decodeTupleLike(Array.from({ length: n }).map(() => innerParam), data, offset, depth + 1);
468
- }
469
-
470
- if (type === "tuple") {
471
- const comps = Array.isArray(param.components) ? param.components : [];
472
- const vals = decodeTupleLike(comps, data, offset, depth + 1);
473
- const out = {};
474
- for (let i = 0; i < comps.length; i++) {
475
- const c = comps[i];
476
- const name = c && typeof c.name === "string" && c.name ? c.name : String(i);
477
- out[name] = vals[i];
478
- }
479
- return out;
480
- }
481
-
482
- if (_isUint(type)) return _decodeUint(type, data, offset);
483
- if (_isInt(type)) return _decodeInt(type, data, offset);
484
- if (type === "bool") return _decodeBool(data, offset);
485
- if (type === "address") return _decodeAddress(data, offset);
486
- if (type === "bytes") return _decodeBytesDynamic(data, offset);
487
- if (type === "string") return _decodeString(data, offset);
488
- if (_isBytesN(type)) return _decodeFixedBytes(type, data, offset);
489
-
490
- throw makeError("unsupported ABI type", "NOT_IMPLEMENTED", { type });
491
- }
492
-
493
- function decodeFunctionResult(outputs, dataHex) {
494
- const data = arrayify(dataHex);
495
- return decodeTupleLike(outputs || [], data, 0);
496
- }
497
-
498
- function hasTuple(params) {
499
- const ps = Array.isArray(params) ? params : [];
500
- for (const p of ps) {
501
- const type = String(p && p.type ? p.type : "");
502
- if (type === "tuple") return true;
503
- if (_isArrayType(type) && hasTuple([{ ...(p || {}), type: _arrayInnerType(type) }])) return true;
504
- if (Array.isArray(p && p.components) && hasTuple(p.components)) return true;
505
- }
506
- return false;
507
- }
508
-
509
- function hasNestedArrays(params) {
510
- const ps = Array.isArray(params) ? params : [];
511
- for (const p of ps) {
512
- const type = String(p && p.type ? p.type : "");
513
- if (_isArrayType(type)) {
514
- const inner = _arrayInnerType(type);
515
- if (_isArrayType(inner)) return true;
516
- if (hasNestedArrays([{ ...(p || {}), type: inner }])) return true;
517
- }
518
- if (type === "tuple" && Array.isArray(p && p.components) && hasNestedArrays(p.components)) return true;
519
- if (Array.isArray(p && p.components) && hasNestedArrays(p.components)) return true;
520
- }
521
- return false;
522
- }
523
-
524
- function needsJsAbi(params) {
525
- return hasTuple(params) || hasNestedArrays(params);
526
- }
527
-
528
- module.exports = {
529
- hasTuple,
530
- hasNestedArrays,
531
- needsJsAbi,
532
- canonicalType,
533
- functionSelectorHex,
534
- encodeFunctionData,
535
- encodeTupleLike,
536
- decodeFunctionResult,
537
- };
538
-
@@ -1,61 +0,0 @@
1
- /**
2
- * The current version of the QuantumCoin.js library.
3
- * @type {string}
4
- */
5
- export const version: string;
6
- /**
7
- * The zero address (32 bytes).
8
- * @type {string}
9
- */
10
- export const ZeroAddress: string;
11
- /**
12
- * The zero hash (32 bytes).
13
- * @type {string}
14
- */
15
- export const ZeroHash: string;
16
- /**
17
- * The maximum value for a uint256.
18
- * @type {bigint}
19
- */
20
- export const MaxUint256: bigint;
21
- /**
22
- * The maximum value for a uint160.
23
- * @type {bigint}
24
- */
25
- export const MaxUint160: bigint;
26
- /**
27
- * The minimum value for an int256.
28
- * @type {bigint}
29
- */
30
- export const MinInt256: bigint;
31
- /**
32
- * The maximum value for an int256.
33
- * @type {bigint}
34
- */
35
- export const MaxInt256: bigint;
36
- /**
37
- * Error code for numeric faults.
38
- * @type {string}
39
- */
40
- export const NumericFault: string;
41
- /**
42
- * Error code for numeric faults (alias).
43
- * @type {string}
44
- */
45
- export const NumericFaultCode: string;
46
- /**
47
- * Wei per coin (1e18).
48
- * @type {bigint}
49
- */
50
- export const WeiPerEther: bigint;
51
- /**
52
- * Currency symbol (compat with ethers.js v6).
53
- * @type {string}
54
- */
55
- export const EtherSymbol: string;
56
- /**
57
- * BigNumber constant (exported as `N` in ethers.js v6).
58
- * In QuantumCoin.js, this is a bigint constant.
59
- * @type {bigint}
60
- */
61
- export const N: bigint;
@@ -1,94 +0,0 @@
1
- /**
2
- * @fileoverview Public constants matching ethers.js v6 naming.
3
- */
4
-
5
- const pkg = require("../package.json");
6
-
7
- /**
8
- * The current version of the QuantumCoin.js library.
9
- * @type {string}
10
- */
11
- const version = pkg.version || "0.0.0";
12
-
13
- /**
14
- * The zero address (32 bytes).
15
- * @type {string}
16
- */
17
- const ZeroAddress = "0x" + "00".repeat(32);
18
-
19
- /**
20
- * The zero hash (32 bytes).
21
- * @type {string}
22
- */
23
- const ZeroHash = "0x" + "00".repeat(32);
24
-
25
- /**
26
- * The maximum value for a uint256.
27
- * @type {bigint}
28
- */
29
- const MaxUint256 = (2n ** 256n) - 1n;
30
-
31
- /**
32
- * The maximum value for a uint160.
33
- * @type {bigint}
34
- */
35
- const MaxUint160 = (2n ** 160n) - 1n;
36
-
37
- /**
38
- * The minimum value for an int256.
39
- * @type {bigint}
40
- */
41
- const MinInt256 = -(2n ** 255n);
42
-
43
- /**
44
- * The maximum value for an int256.
45
- * @type {bigint}
46
- */
47
- const MaxInt256 = (2n ** 255n) - 1n;
48
-
49
- /**
50
- * Error code for numeric faults.
51
- * @type {string}
52
- */
53
- const NumericFault = "NUMERIC_FAULT";
54
-
55
- /**
56
- * Error code for numeric faults (alias).
57
- * @type {string}
58
- */
59
- const NumericFaultCode = "NUMERIC_FAULT";
60
-
61
- /**
62
- * Wei per coin (1e18).
63
- * @type {bigint}
64
- */
65
- const WeiPerEther = 1000000000000000000n;
66
-
67
- /**
68
- * Currency symbol (compat with ethers.js v6).
69
- * @type {string}
70
- */
71
- const EtherSymbol = "Ξ";
72
-
73
- /**
74
- * BigNumber constant (exported as `N` in ethers.js v6).
75
- * In QuantumCoin.js, this is a bigint constant.
76
- * @type {bigint}
77
- */
78
- const N = 0n;
79
-
80
- module.exports = {
81
- version,
82
- ZeroAddress,
83
- ZeroHash,
84
- MaxUint256,
85
- MaxUint160,
86
- MinInt256,
87
- MaxInt256,
88
- NumericFault,
89
- NumericFaultCode,
90
- WeiPerEther,
91
- EtherSymbol,
92
- N,
93
- };
94
-