quantumcoin 7.0.12 → 7.0.14

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 (214) hide show
  1. package/README-SDK.md +828 -823
  2. package/README.md +4 -0
  3. package/config.d.ts +50 -50
  4. package/examples/example-generated-sdk-js/README.md +65 -0
  5. package/examples/example-generated-sdk-js/examples/_test-wallet.js +17 -0
  6. package/examples/example-generated-sdk-js/examples/deploy.js +41 -0
  7. package/examples/example-generated-sdk-js/examples/events.js +36 -0
  8. package/examples/example-generated-sdk-js/examples/read-operations.js +46 -0
  9. package/examples/example-generated-sdk-js/examples/write-operations.js +44 -0
  10. package/examples/example-generated-sdk-js/index.d.ts +1 -0
  11. package/examples/example-generated-sdk-js/index.js +15 -0
  12. package/examples/example-generated-sdk-js/package-lock.json +59 -0
  13. package/examples/example-generated-sdk-js/package.json +22 -0
  14. package/examples/example-generated-sdk-js/src/SimpleERC20.d.ts +19 -0
  15. package/examples/example-generated-sdk-js/src/SimpleERC20.js +353 -0
  16. package/examples/example-generated-sdk-js/src/SimpleERC20__factory.d.ts +10 -0
  17. package/examples/example-generated-sdk-js/src/SimpleERC20__factory.js +29 -0
  18. package/examples/example-generated-sdk-js/src/index.d.ts +4 -0
  19. package/examples/example-generated-sdk-js/src/index.js +5 -0
  20. package/examples/example-generated-sdk-js/src/quantumcoin-shims.d.ts +23 -0
  21. package/examples/example-generated-sdk-js/src/types.d.ts +3 -0
  22. package/examples/example-generated-sdk-js/src/types.js +3 -0
  23. package/examples/example-generated-sdk-js/test/e2e/SimpleERC20.e2e.test.js +78 -0
  24. package/examples/example-generated-sdk-ts/README.md +65 -0
  25. package/examples/example-generated-sdk-ts/examples/_test-wallet.js +17 -0
  26. package/examples/example-generated-sdk-ts/examples/deploy.js +41 -0
  27. package/examples/example-generated-sdk-ts/examples/events.js +36 -0
  28. package/examples/example-generated-sdk-ts/examples/read-operations.js +46 -0
  29. package/examples/example-generated-sdk-ts/examples/write-operations.js +44 -0
  30. package/examples/example-generated-sdk-ts/index.d.ts +1 -0
  31. package/examples/example-generated-sdk-ts/index.js +15 -0
  32. package/examples/example-generated-sdk-ts/package-lock.json +59 -0
  33. package/examples/example-generated-sdk-ts/package.json +23 -0
  34. package/examples/example-generated-sdk-ts/src/SimpleERC20.ts +334 -0
  35. package/examples/example-generated-sdk-ts/src/SimpleERC20__factory.ts +28 -0
  36. package/examples/example-generated-sdk-ts/src/index.ts +4 -0
  37. package/examples/example-generated-sdk-ts/src/quantumcoin-shims.d.ts +23 -0
  38. package/examples/example-generated-sdk-ts/src/types.ts +4 -0
  39. package/examples/example-generated-sdk-ts/test/e2e/SimpleERC20.e2e.test.js +78 -0
  40. package/examples/example-generated-sdk-ts/tsconfig.json +14 -0
  41. package/examples/node_modules/.package-lock.json +5 -5
  42. package/examples/node_modules/quantum-coin-js-sdk/README.md +5 -5
  43. package/examples/node_modules/quantum-coin-js-sdk/example/package-lock.json +1 -1
  44. package/examples/node_modules/quantum-coin-js-sdk/index.d.ts +1031 -1031
  45. package/examples/node_modules/quantum-coin-js-sdk/index.js +15 -15
  46. package/examples/node_modules/quantum-coin-js-sdk/package.json +1 -1
  47. package/examples/node_modules/quantum-coin-js-sdk/wasmBase64.js +2 -2
  48. package/examples/package-lock.json +6 -6
  49. package/examples/package.json +1 -1
  50. package/examples/sdk-generator-erc20.inline.json +251 -251
  51. package/generate-sdk.js +1845 -1822
  52. package/package.json +2 -2
  53. package/src/abi/fragments.d.ts +42 -42
  54. package/src/abi/index.d.ts +13 -13
  55. package/src/abi/interface.js +11 -2
  56. package/src/abi/js-abi-coder.js +61 -2
  57. package/src/contract/contract.js +53 -5
  58. package/src/contract/index.d.ts +9 -9
  59. package/src/errors/index.d.ts +92 -92
  60. package/src/generator/index.d.ts +11 -4
  61. package/src/generator/index.js +185 -18
  62. package/src/internal/hex.d.ts +68 -61
  63. package/src/internal/hex.js +36 -0
  64. package/src/providers/json-rpc-provider.d.ts +12 -12
  65. package/src/providers/provider.js +141 -8
  66. package/src/utils/address.d.ts +58 -58
  67. package/src/utils/encoding.d.ts +120 -120
  68. package/src/utils/hashing.js +298 -298
  69. package/src/utils/index.d.ts +63 -63
  70. package/src/utils/index.js +14 -14
  71. package/src/utils/result.d.ts +57 -57
  72. package/src/utils/rlp.d.ts +12 -12
  73. package/src/utils/rlp.js +13 -1
  74. package/src/utils/units.d.ts +29 -29
  75. package/src/wallet/index.d.ts +10 -10
  76. package/src/wallet/wallet.d.ts +192 -192
  77. package/src/wallet/wallet.js +713 -630
  78. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/README.md +83 -0
  79. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/artifacts/AllSolidityTypes.abi.json +12544 -0
  80. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/artifacts/AllSolidityTypes.bin +1 -0
  81. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/examples/_test-wallet.js +17 -0
  82. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/examples/_test-wallet.ts +10 -0
  83. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/examples/deploy.js +41 -0
  84. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/examples/deploy.ts +41 -0
  85. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/examples/events.js +36 -0
  86. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/examples/events.ts +36 -0
  87. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/examples/offline-signing.js +82 -0
  88. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/examples/offline-signing.ts +80 -0
  89. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/examples/read-operations.js +46 -0
  90. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/examples/read-operations.ts +44 -0
  91. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/examples/write-operations.js +44 -0
  92. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/examples/write-operations.ts +44 -0
  93. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/index.d.ts +1 -0
  94. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/index.js +21 -0
  95. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/package-lock.json +597 -0
  96. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/package.json +25 -0
  97. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/src/AllSolidityTypes.d.ts +1280 -0
  98. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/src/AllSolidityTypes.js +14021 -0
  99. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/src/AllSolidityTypes__factory.d.ts +11 -0
  100. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/src/AllSolidityTypes__factory.js +31 -0
  101. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/src/index.d.ts +4 -0
  102. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/src/index.js +5 -0
  103. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/src/quantumcoin-shims.d.ts +25 -0
  104. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/src/types.d.ts +3 -0
  105. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/src/types.js +3 -0
  106. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/test/e2e/AllSolidityTypes.e2e.test.js +77 -0
  107. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/test/e2e/AllSolidityTypes.extra.test.js +195 -0
  108. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/README.md +83 -0
  109. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/artifacts/AllSolidityTypes.abi.json +12544 -0
  110. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/artifacts/AllSolidityTypes.bin +1 -0
  111. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/examples/_test-wallet.js +17 -0
  112. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/examples/_test-wallet.ts +10 -0
  113. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/examples/deploy.js +41 -0
  114. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/examples/deploy.ts +41 -0
  115. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/examples/events.js +36 -0
  116. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/examples/events.ts +36 -0
  117. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/examples/offline-signing.js +82 -0
  118. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/examples/offline-signing.ts +80 -0
  119. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/examples/read-operations.js +46 -0
  120. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/examples/read-operations.ts +44 -0
  121. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/examples/write-operations.js +44 -0
  122. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/examples/write-operations.ts +44 -0
  123. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/index.d.ts +1 -0
  124. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/index.js +21 -0
  125. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/package-lock.json +597 -0
  126. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/package.json +26 -0
  127. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/src/AllSolidityTypes.ts +13940 -0
  128. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/src/AllSolidityTypes__factory.ts +31 -0
  129. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/src/index.ts +4 -0
  130. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/src/quantumcoin-shims.d.ts +25 -0
  131. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/src/types.ts +4 -0
  132. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/test/e2e/AllSolidityTypes.e2e.test.js +77 -0
  133. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/test/e2e/AllSolidityTypes.extra.test.js +195 -0
  134. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/tsconfig.json +18 -0
  135. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/README.md +74 -0
  136. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/artifacts/SimpleERC20.abi.json +245 -0
  137. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/artifacts/SimpleERC20.bin +1 -0
  138. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/examples/_test-wallet.js +17 -0
  139. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/examples/_test-wallet.ts +10 -0
  140. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/examples/deploy.js +41 -0
  141. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/examples/deploy.ts +41 -0
  142. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/examples/events.js +36 -0
  143. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/examples/events.ts +36 -0
  144. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/examples/offline-signing.js +82 -0
  145. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/examples/offline-signing.ts +80 -0
  146. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/examples/read-operations.js +46 -0
  147. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/examples/read-operations.ts +44 -0
  148. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/examples/write-operations.js +44 -0
  149. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/examples/write-operations.ts +44 -0
  150. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/index.d.ts +1 -0
  151. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/index.js +16 -0
  152. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/package-lock.json +597 -0
  153. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/package.json +25 -0
  154. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/src/SimpleERC20.d.ts +24 -0
  155. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/src/SimpleERC20.js +378 -0
  156. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/src/SimpleERC20__factory.d.ts +10 -0
  157. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/src/SimpleERC20__factory.js +31 -0
  158. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/src/index.d.ts +4 -0
  159. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/src/index.js +5 -0
  160. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/src/quantumcoin-shims.d.ts +25 -0
  161. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/src/types.d.ts +3 -0
  162. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/src/types.js +3 -0
  163. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/test/e2e/SimpleERC20.e2e.test.js +90 -0
  164. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/README.md +74 -0
  165. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/artifacts/SimpleERC20.abi.json +245 -0
  166. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/artifacts/SimpleERC20.bin +1 -0
  167. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/examples/_test-wallet.js +17 -0
  168. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/examples/_test-wallet.ts +10 -0
  169. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/examples/deploy.js +41 -0
  170. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/examples/deploy.ts +41 -0
  171. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/examples/events.js +36 -0
  172. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/examples/events.ts +36 -0
  173. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/examples/offline-signing.js +82 -0
  174. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/examples/offline-signing.ts +80 -0
  175. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/examples/read-operations.js +46 -0
  176. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/examples/read-operations.ts +44 -0
  177. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/examples/write-operations.js +44 -0
  178. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/examples/write-operations.ts +44 -0
  179. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/index.d.ts +1 -0
  180. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/index.js +16 -0
  181. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/package-lock.json +597 -0
  182. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/package.json +26 -0
  183. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/src/SimpleERC20.ts +361 -0
  184. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/src/SimpleERC20__factory.ts +30 -0
  185. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/src/index.ts +4 -0
  186. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/src/quantumcoin-shims.d.ts +25 -0
  187. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/src/types.ts +4 -0
  188. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/test/e2e/SimpleERC20.e2e.test.js +90 -0
  189. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/tsconfig.json +18 -0
  190. package/test/e2e/generator-interface.e2e.test.js +165 -0
  191. package/test/e2e/generator-interface.e2e.test.ts +160 -0
  192. package/test/e2e/signing-context-and-fee.e2e.test.js +141 -141
  193. package/test/e2e/signing-context-and-fee.e2e.test.ts +128 -128
  194. package/test/integration/provider.test.js +88 -88
  195. package/test/security/abi-decoder-bounds.test.js +122 -0
  196. package/test/security/contract-overrides.test.js +112 -0
  197. package/test/security/generator-injection.test.js +195 -0
  198. package/test/security/malformed-input.test.js +26 -27
  199. package/test/security/rpc-numeric-bounds.test.js +81 -0
  200. package/test/security/rpc-trust.test.js +202 -0
  201. package/test/unit/abi-interface.test.js +12 -5
  202. package/test/unit/abi-interface.test.ts +8 -1
  203. package/test/unit/address-wallet.test.js +923 -892
  204. package/test/unit/address-wallet.test.ts +877 -877
  205. package/test/unit/encoding-units-rlp.test.js +35 -0
  206. package/test/unit/generator.test.js +48 -1
  207. package/test/unit/generator.test.ts +48 -1
  208. package/test/unit/hashing.test.js +64 -64
  209. package/test/unit/hashing.test.ts +63 -63
  210. package/test/unit/internal-hex.test.js +32 -1
  211. package/test/unit/internal-hex.test.ts +32 -1
  212. package/test/unit/populate-transaction.test.js +33 -0
  213. package/test/unit/providers.test.js +51 -1
  214. package/test/unit/providers.test.ts +53 -0
@@ -1,298 +1,298 @@
1
- /**
2
- * @fileoverview Hash utilities (ethers.js v6 compatible names).
3
- *
4
- * Where possible, we use Node's built-in crypto module. For keccak256,
5
- * we prefer a native OpenSSL implementation if available, otherwise we
6
- * fall back to a small pure-JS Keccak-256 implementation.
7
- */
8
-
9
- const crypto = require("crypto");
10
- const { arrayify, bytesToHex, utf8ToBytes } = require("../internal/hex");
11
-
12
- const _MASK64 = (1n << 64n) - 1n;
13
-
14
- function _rotl64(x, n) {
15
- const nn = BigInt(n);
16
- return ((x << nn) | (x >> (64n - nn))) & _MASK64;
17
- }
18
-
19
- function _readU64LE(bytes, off) {
20
- let x = 0n;
21
- for (let i = 0; i < 8; i++) {
22
- x |= BigInt(bytes[off + i]) << (8n * BigInt(i));
23
- }
24
- return x;
25
- }
26
-
27
- function _writeU64LE(out, off, x) {
28
- let v = x & _MASK64;
29
- for (let i = 0; i < 8; i++) {
30
- out[off + i] = Number((v >> (8n * BigInt(i))) & 0xffn);
31
- }
32
- }
33
-
34
- // Keccak-f[1600] round constants
35
- const _RC = [
36
- 0x0000000000000001n, 0x0000000000008082n, 0x800000000000808an, 0x8000000080008000n,
37
- 0x000000000000808bn, 0x0000000080000001n, 0x8000000080008081n, 0x8000000000008009n,
38
- 0x000000000000008an, 0x0000000000000088n, 0x0000000080008009n, 0x000000008000000an,
39
- 0x000000008000808bn, 0x800000000000008bn, 0x8000000000008089n, 0x8000000000008003n,
40
- 0x8000000000008002n, 0x8000000000000080n, 0x000000000000800an, 0x800000008000000an,
41
- 0x8000000080008081n, 0x8000000000008080n, 0x0000000080000001n, 0x8000000080008008n,
42
- ];
43
-
44
- // Rotation offsets (rho step)
45
- const _R = [
46
- // Indexed by lane index (x + 5*y) where x,y in [0..4]
47
- // (This is the standard Keccak rho offsets table, flattened in y-major order.)
48
- 0, 1, 62, 28, 27,
49
- 36, 44, 6, 55, 20,
50
- 3, 10, 43, 25, 39,
51
- 41, 45, 15, 21, 8,
52
- 18, 2, 61, 56, 14,
53
- ];
54
-
55
- // Pi step mapping: index -> newIndex
56
- const _PI = [
57
- 0, 10, 20, 5, 15,
58
- 16, 1, 11, 21, 6,
59
- 7, 17, 2, 12, 22,
60
- 23, 8, 18, 3, 13,
61
- 14, 24, 9, 19, 4,
62
- ];
63
-
64
- function _keccakF1600(state) {
65
- const b = new Array(25);
66
- const c = new Array(5);
67
- const d = new Array(5);
68
-
69
- for (let round = 0; round < 24; round++) {
70
- // Theta
71
- for (let x = 0; x < 5; x++) {
72
- c[x] = state[x] ^ state[x + 5] ^ state[x + 10] ^ state[x + 15] ^ state[x + 20];
73
- }
74
- for (let x = 0; x < 5; x++) {
75
- d[x] = c[(x + 4) % 5] ^ _rotl64(c[(x + 1) % 5], 1);
76
- }
77
- for (let i = 0; i < 25; i++) {
78
- state[i] = (state[i] ^ d[i % 5]) & _MASK64;
79
- }
80
-
81
- // Rho + Pi
82
- for (let i = 0; i < 25; i++) {
83
- b[_PI[i]] = _rotl64(state[i], _R[i]);
84
- }
85
-
86
- // Chi
87
- for (let y = 0; y < 5; y++) {
88
- const row = y * 5;
89
- for (let x = 0; x < 5; x++) {
90
- state[row + x] = (b[row + x] ^ ((~b[row + ((x + 1) % 5)]) & b[row + ((x + 2) % 5)])) & _MASK64;
91
- }
92
- }
93
-
94
- // Iota
95
- state[0] = (state[0] ^ _RC[round]) & _MASK64;
96
- }
97
- }
98
-
99
- function _keccak256Bytes(bytes) {
100
- // Keccak-256: rate=1088 bits => 136 bytes
101
- const rate = 136;
102
- const outLen = 32;
103
- const state = new Array(25).fill(0n);
104
-
105
- let offset = 0;
106
- while (offset + rate <= bytes.length) {
107
- for (let i = 0; i < rate / 8; i++) {
108
- state[i] = (state[i] ^ _readU64LE(bytes, offset + i * 8)) & _MASK64;
109
- }
110
- _keccakF1600(state);
111
- offset += rate;
112
- }
113
-
114
- // Final block + padding (keccak pad10*1 with domain 0x01)
115
- const block = new Uint8Array(rate);
116
- const remaining = bytes.length - offset;
117
- block.set(bytes.slice(offset), 0);
118
- block[remaining] = 0x01;
119
- block[rate - 1] |= 0x80;
120
-
121
- for (let i = 0; i < rate / 8; i++) {
122
- state[i] = (state[i] ^ _readU64LE(block, i * 8)) & _MASK64;
123
- }
124
- _keccakF1600(state);
125
-
126
- const out = new Uint8Array(outLen);
127
- let outOff = 0;
128
- let lane = 0;
129
- while (outOff < outLen) {
130
- const tmp = new Uint8Array(8);
131
- _writeU64LE(tmp, 0, state[lane]);
132
- const take = Math.min(8, outLen - outOff);
133
- out.set(tmp.slice(0, take), outOff);
134
- outOff += take;
135
- lane++;
136
- }
137
- return out;
138
- }
139
-
140
- function _hash(alg, data) {
141
- const bytes = arrayify(data);
142
- return bytesToHex(crypto.createHash(alg).update(Buffer.from(bytes)).digest());
143
- }
144
-
145
- /**
146
- * keccak256 hash of BytesLike.
147
- * @param {string|Uint8Array} data
148
- * @returns {string}
149
- */
150
- function keccak256(data) {
151
- const bytes = arrayify(data);
152
- const hashes = crypto.getHashes();
153
-
154
- // Prefer native keccak if available (varies by Node/OpenSSL build).
155
- if (hashes.includes("keccak256")) {
156
- return bytesToHex(crypto.createHash("keccak256").update(Buffer.from(bytes)).digest());
157
- }
158
- if (hashes.includes("keccak-256")) {
159
- return bytesToHex(crypto.createHash("keccak-256").update(Buffer.from(bytes)).digest());
160
- }
161
-
162
- // Fallback: pure JS Keccak-256
163
- return bytesToHex(_keccak256Bytes(bytes));
164
- }
165
-
166
- /**
167
- * sha256 hash of BytesLike.
168
- * @param {string|Uint8Array} data
169
- * @returns {string}
170
- */
171
- function sha256(data) {
172
- return _hash("sha256", data);
173
- }
174
-
175
- /**
176
- * sha512 hash of BytesLike.
177
- * @param {string|Uint8Array} data
178
- * @returns {string}
179
- */
180
- function sha512(data) {
181
- return _hash("sha512", data);
182
- }
183
-
184
- /**
185
- * ripemd160 hash of BytesLike.
186
- * @param {string|Uint8Array} data
187
- * @returns {string}
188
- */
189
- function ripemd160(data) {
190
- return _hash("ripemd160", data);
191
- }
192
-
193
- /**
194
- * ethers-style id(text) => keccak256(utf8Bytes(text))
195
- * @param {string} text
196
- * @returns {string}
197
- */
198
- function id(text) {
199
- return keccak256(utf8ToBytes(text));
200
- }
201
-
202
- /**
203
- * Generate cryptographically strong random bytes.
204
- * @param {number} length
205
- * @returns {Uint8Array}
206
- */
207
- function randomBytes(length) {
208
- return new Uint8Array(crypto.randomBytes(length));
209
- }
210
-
211
- /**
212
- * Compute HMAC over data.
213
- * @param {string} algorithm
214
- * @param {string|Uint8Array} key
215
- * @param {string|Uint8Array} data
216
- * @returns {string}
217
- */
218
- function computeHmac(algorithm, key, data) {
219
- const k = typeof key === "string" ? utf8ToBytes(key) : arrayify(key);
220
- const d = typeof data === "string" ? utf8ToBytes(data) : arrayify(data);
221
- const h = crypto.createHmac(algorithm, Buffer.from(k)).update(Buffer.from(d)).digest();
222
- return bytesToHex(new Uint8Array(h));
223
- }
224
-
225
- /**
226
- * PBKDF2 (sync) helper returning hex string.
227
- * @param {string|Uint8Array} password
228
- * @param {string|Uint8Array} salt
229
- * @param {number} iterations
230
- * @param {number} keylen
231
- * @param {string=} algorithm
232
- * @returns {string}
233
- */
234
- function pbkdf2(password, salt, iterations, keylen, algorithm) {
235
- const p = typeof password === "string" ? utf8ToBytes(password) : arrayify(password);
236
- const s = typeof salt === "string" ? utf8ToBytes(salt) : arrayify(salt);
237
- const a = algorithm || "sha256";
238
- const out = crypto.pbkdf2Sync(Buffer.from(p), Buffer.from(s), iterations, keylen, a);
239
- return bytesToHex(new Uint8Array(out));
240
- }
241
-
242
- /**
243
- * scrypt (async) helper returning hex string.
244
- * @param {string|Uint8Array} password
245
- * @param {string|Uint8Array} salt
246
- * @param {number} N
247
- * @param {number} r
248
- * @param {number} p
249
- * @param {number} dkLen
250
- * @returns {Promise<string>}
251
- */
252
- function scrypt(password, salt, N, r, p, dkLen) {
253
- const pw = typeof password === "string" ? utf8ToBytes(password) : arrayify(password);
254
- const sa = typeof salt === "string" ? utf8ToBytes(salt) : arrayify(salt);
255
- return new Promise((resolve, reject) => {
256
- crypto.scrypt(
257
- Buffer.from(pw),
258
- Buffer.from(sa),
259
- dkLen,
260
- { N, r, p, maxmem: 128 * 1024 * 1024 },
261
- (err, derived) => {
262
- if (err) return reject(err);
263
- resolve(bytesToHex(new Uint8Array(derived)));
264
- },
265
- );
266
- });
267
- }
268
-
269
- /**
270
- * scrypt (sync) helper returning hex string.
271
- * @param {string|Uint8Array} password
272
- * @param {string|Uint8Array} salt
273
- * @param {number} N
274
- * @param {number} r
275
- * @param {number} p
276
- * @param {number} dkLen
277
- * @returns {string}
278
- */
279
- function scryptSync(password, salt, N, r, p, dkLen) {
280
- const pw = typeof password === "string" ? utf8ToBytes(password) : arrayify(password);
281
- const sa = typeof salt === "string" ? utf8ToBytes(salt) : arrayify(salt);
282
- const out = crypto.scryptSync(Buffer.from(pw), Buffer.from(sa), dkLen, { N, r, p, maxmem: 257 * 1024 * 1024 }); //257 instead of 256 for buffer for compat for N=262144, r=8, p=1
283
- return bytesToHex(new Uint8Array(out));
284
- }
285
-
286
- module.exports = {
287
- keccak256,
288
- sha256,
289
- sha512,
290
- ripemd160,
291
- id,
292
- randomBytes,
293
- computeHmac,
294
- pbkdf2,
295
- scrypt,
296
- scryptSync,
297
- };
298
-
1
+ /**
2
+ * @fileoverview Hash utilities (ethers.js v6 compatible names).
3
+ *
4
+ * Where possible, we use Node's built-in crypto module. For keccak256,
5
+ * we prefer a native OpenSSL implementation if available, otherwise we
6
+ * fall back to a small pure-JS Keccak-256 implementation.
7
+ */
8
+
9
+ const crypto = require("crypto");
10
+ const { arrayify, bytesToHex, utf8ToBytes } = require("../internal/hex");
11
+
12
+ const _MASK64 = (1n << 64n) - 1n;
13
+
14
+ function _rotl64(x, n) {
15
+ const nn = BigInt(n);
16
+ return ((x << nn) | (x >> (64n - nn))) & _MASK64;
17
+ }
18
+
19
+ function _readU64LE(bytes, off) {
20
+ let x = 0n;
21
+ for (let i = 0; i < 8; i++) {
22
+ x |= BigInt(bytes[off + i]) << (8n * BigInt(i));
23
+ }
24
+ return x;
25
+ }
26
+
27
+ function _writeU64LE(out, off, x) {
28
+ let v = x & _MASK64;
29
+ for (let i = 0; i < 8; i++) {
30
+ out[off + i] = Number((v >> (8n * BigInt(i))) & 0xffn);
31
+ }
32
+ }
33
+
34
+ // Keccak-f[1600] round constants
35
+ const _RC = [
36
+ 0x0000000000000001n, 0x0000000000008082n, 0x800000000000808an, 0x8000000080008000n,
37
+ 0x000000000000808bn, 0x0000000080000001n, 0x8000000080008081n, 0x8000000000008009n,
38
+ 0x000000000000008an, 0x0000000000000088n, 0x0000000080008009n, 0x000000008000000an,
39
+ 0x000000008000808bn, 0x800000000000008bn, 0x8000000000008089n, 0x8000000000008003n,
40
+ 0x8000000000008002n, 0x8000000000000080n, 0x000000000000800an, 0x800000008000000an,
41
+ 0x8000000080008081n, 0x8000000000008080n, 0x0000000080000001n, 0x8000000080008008n,
42
+ ];
43
+
44
+ // Rotation offsets (rho step)
45
+ const _R = [
46
+ // Indexed by lane index (x + 5*y) where x,y in [0..4]
47
+ // (This is the standard Keccak rho offsets table, flattened in y-major order.)
48
+ 0, 1, 62, 28, 27,
49
+ 36, 44, 6, 55, 20,
50
+ 3, 10, 43, 25, 39,
51
+ 41, 45, 15, 21, 8,
52
+ 18, 2, 61, 56, 14,
53
+ ];
54
+
55
+ // Pi step mapping: index -> newIndex
56
+ const _PI = [
57
+ 0, 10, 20, 5, 15,
58
+ 16, 1, 11, 21, 6,
59
+ 7, 17, 2, 12, 22,
60
+ 23, 8, 18, 3, 13,
61
+ 14, 24, 9, 19, 4,
62
+ ];
63
+
64
+ function _keccakF1600(state) {
65
+ const b = new Array(25);
66
+ const c = new Array(5);
67
+ const d = new Array(5);
68
+
69
+ for (let round = 0; round < 24; round++) {
70
+ // Theta
71
+ for (let x = 0; x < 5; x++) {
72
+ c[x] = state[x] ^ state[x + 5] ^ state[x + 10] ^ state[x + 15] ^ state[x + 20];
73
+ }
74
+ for (let x = 0; x < 5; x++) {
75
+ d[x] = c[(x + 4) % 5] ^ _rotl64(c[(x + 1) % 5], 1);
76
+ }
77
+ for (let i = 0; i < 25; i++) {
78
+ state[i] = (state[i] ^ d[i % 5]) & _MASK64;
79
+ }
80
+
81
+ // Rho + Pi
82
+ for (let i = 0; i < 25; i++) {
83
+ b[_PI[i]] = _rotl64(state[i], _R[i]);
84
+ }
85
+
86
+ // Chi
87
+ for (let y = 0; y < 5; y++) {
88
+ const row = y * 5;
89
+ for (let x = 0; x < 5; x++) {
90
+ state[row + x] = (b[row + x] ^ ((~b[row + ((x + 1) % 5)]) & b[row + ((x + 2) % 5)])) & _MASK64;
91
+ }
92
+ }
93
+
94
+ // Iota
95
+ state[0] = (state[0] ^ _RC[round]) & _MASK64;
96
+ }
97
+ }
98
+
99
+ function _keccak256Bytes(bytes) {
100
+ // Keccak-256: rate=1088 bits => 136 bytes
101
+ const rate = 136;
102
+ const outLen = 32;
103
+ const state = new Array(25).fill(0n);
104
+
105
+ let offset = 0;
106
+ while (offset + rate <= bytes.length) {
107
+ for (let i = 0; i < rate / 8; i++) {
108
+ state[i] = (state[i] ^ _readU64LE(bytes, offset + i * 8)) & _MASK64;
109
+ }
110
+ _keccakF1600(state);
111
+ offset += rate;
112
+ }
113
+
114
+ // Final block + padding (keccak pad10*1 with domain 0x01)
115
+ const block = new Uint8Array(rate);
116
+ const remaining = bytes.length - offset;
117
+ block.set(bytes.slice(offset), 0);
118
+ block[remaining] = 0x01;
119
+ block[rate - 1] |= 0x80;
120
+
121
+ for (let i = 0; i < rate / 8; i++) {
122
+ state[i] = (state[i] ^ _readU64LE(block, i * 8)) & _MASK64;
123
+ }
124
+ _keccakF1600(state);
125
+
126
+ const out = new Uint8Array(outLen);
127
+ let outOff = 0;
128
+ let lane = 0;
129
+ while (outOff < outLen) {
130
+ const tmp = new Uint8Array(8);
131
+ _writeU64LE(tmp, 0, state[lane]);
132
+ const take = Math.min(8, outLen - outOff);
133
+ out.set(tmp.slice(0, take), outOff);
134
+ outOff += take;
135
+ lane++;
136
+ }
137
+ return out;
138
+ }
139
+
140
+ function _hash(alg, data) {
141
+ const bytes = arrayify(data);
142
+ return bytesToHex(crypto.createHash(alg).update(Buffer.from(bytes)).digest());
143
+ }
144
+
145
+ /**
146
+ * keccak256 hash of BytesLike.
147
+ * @param {string|Uint8Array} data
148
+ * @returns {string}
149
+ */
150
+ function keccak256(data) {
151
+ const bytes = arrayify(data);
152
+ const hashes = crypto.getHashes();
153
+
154
+ // Prefer native keccak if available (varies by Node/OpenSSL build).
155
+ if (hashes.includes("keccak256")) {
156
+ return bytesToHex(crypto.createHash("keccak256").update(Buffer.from(bytes)).digest());
157
+ }
158
+ if (hashes.includes("keccak-256")) {
159
+ return bytesToHex(crypto.createHash("keccak-256").update(Buffer.from(bytes)).digest());
160
+ }
161
+
162
+ // Fallback: pure JS Keccak-256
163
+ return bytesToHex(_keccak256Bytes(bytes));
164
+ }
165
+
166
+ /**
167
+ * sha256 hash of BytesLike.
168
+ * @param {string|Uint8Array} data
169
+ * @returns {string}
170
+ */
171
+ function sha256(data) {
172
+ return _hash("sha256", data);
173
+ }
174
+
175
+ /**
176
+ * sha512 hash of BytesLike.
177
+ * @param {string|Uint8Array} data
178
+ * @returns {string}
179
+ */
180
+ function sha512(data) {
181
+ return _hash("sha512", data);
182
+ }
183
+
184
+ /**
185
+ * ripemd160 hash of BytesLike.
186
+ * @param {string|Uint8Array} data
187
+ * @returns {string}
188
+ */
189
+ function ripemd160(data) {
190
+ return _hash("ripemd160", data);
191
+ }
192
+
193
+ /**
194
+ * ethers-style id(text) => keccak256(utf8Bytes(text))
195
+ * @param {string} text
196
+ * @returns {string}
197
+ */
198
+ function id(text) {
199
+ return keccak256(utf8ToBytes(text));
200
+ }
201
+
202
+ /**
203
+ * Generate cryptographically strong random bytes.
204
+ * @param {number} length
205
+ * @returns {Uint8Array}
206
+ */
207
+ function randomBytes(length) {
208
+ return new Uint8Array(crypto.randomBytes(length));
209
+ }
210
+
211
+ /**
212
+ * Compute HMAC over data.
213
+ * @param {string} algorithm
214
+ * @param {string|Uint8Array} key
215
+ * @param {string|Uint8Array} data
216
+ * @returns {string}
217
+ */
218
+ function computeHmac(algorithm, key, data) {
219
+ const k = typeof key === "string" ? utf8ToBytes(key) : arrayify(key);
220
+ const d = typeof data === "string" ? utf8ToBytes(data) : arrayify(data);
221
+ const h = crypto.createHmac(algorithm, Buffer.from(k)).update(Buffer.from(d)).digest();
222
+ return bytesToHex(new Uint8Array(h));
223
+ }
224
+
225
+ /**
226
+ * PBKDF2 (sync) helper returning hex string.
227
+ * @param {string|Uint8Array} password
228
+ * @param {string|Uint8Array} salt
229
+ * @param {number} iterations
230
+ * @param {number} keylen
231
+ * @param {string=} algorithm
232
+ * @returns {string}
233
+ */
234
+ function pbkdf2(password, salt, iterations, keylen, algorithm) {
235
+ const p = typeof password === "string" ? utf8ToBytes(password) : arrayify(password);
236
+ const s = typeof salt === "string" ? utf8ToBytes(salt) : arrayify(salt);
237
+ const a = algorithm || "sha256";
238
+ const out = crypto.pbkdf2Sync(Buffer.from(p), Buffer.from(s), iterations, keylen, a);
239
+ return bytesToHex(new Uint8Array(out));
240
+ }
241
+
242
+ /**
243
+ * scrypt (async) helper returning hex string.
244
+ * @param {string|Uint8Array} password
245
+ * @param {string|Uint8Array} salt
246
+ * @param {number} N
247
+ * @param {number} r
248
+ * @param {number} p
249
+ * @param {number} dkLen
250
+ * @returns {Promise<string>}
251
+ */
252
+ function scrypt(password, salt, N, r, p, dkLen) {
253
+ const pw = typeof password === "string" ? utf8ToBytes(password) : arrayify(password);
254
+ const sa = typeof salt === "string" ? utf8ToBytes(salt) : arrayify(salt);
255
+ return new Promise((resolve, reject) => {
256
+ crypto.scrypt(
257
+ Buffer.from(pw),
258
+ Buffer.from(sa),
259
+ dkLen,
260
+ { N, r, p, maxmem: 128 * 1024 * 1024 },
261
+ (err, derived) => {
262
+ if (err) return reject(err);
263
+ resolve(bytesToHex(new Uint8Array(derived)));
264
+ },
265
+ );
266
+ });
267
+ }
268
+
269
+ /**
270
+ * scrypt (sync) helper returning hex string.
271
+ * @param {string|Uint8Array} password
272
+ * @param {string|Uint8Array} salt
273
+ * @param {number} N
274
+ * @param {number} r
275
+ * @param {number} p
276
+ * @param {number} dkLen
277
+ * @returns {string}
278
+ */
279
+ function scryptSync(password, salt, N, r, p, dkLen) {
280
+ const pw = typeof password === "string" ? utf8ToBytes(password) : arrayify(password);
281
+ const sa = typeof salt === "string" ? utf8ToBytes(salt) : arrayify(salt);
282
+ const out = crypto.scryptSync(Buffer.from(pw), Buffer.from(sa), dkLen, { N, r, p, maxmem: 257 * 1024 * 1024 }); //257 instead of 256 for buffer for compat for N=262144, r=8, p=1
283
+ return bytesToHex(new Uint8Array(out));
284
+ }
285
+
286
+ module.exports = {
287
+ keccak256,
288
+ sha256,
289
+ sha512,
290
+ ripemd160,
291
+ id,
292
+ randomBytes,
293
+ computeHmac,
294
+ pbkdf2,
295
+ scrypt,
296
+ scryptSync,
297
+ };
298
+