quantumcoin 7.0.3 → 7.0.5

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 (155) hide show
  1. package/.github/workflows/publish-npmjs.yaml +22 -22
  2. package/.gitignore +15 -15
  3. package/LICENSE +21 -21
  4. package/README-SDK.md +758 -754
  5. package/README.md +165 -150
  6. package/SPEC.md +3845 -3843
  7. package/config.d.ts +50 -50
  8. package/config.js +115 -115
  9. package/examples/AllSolidityTypes.sol +184 -184
  10. package/examples/SimpleIERC20.sol +74 -74
  11. package/examples/events.js +41 -35
  12. package/examples/events.ts +35 -0
  13. package/examples/example-generator-sdk-js.js +100 -95
  14. package/examples/example-generator-sdk-js.ts +77 -0
  15. package/examples/example-generator-sdk-ts.js +100 -95
  16. package/examples/example-generator-sdk-ts.ts +77 -0
  17. package/examples/example.js +72 -61
  18. package/examples/example.ts +61 -0
  19. package/examples/offline-signing.js +79 -73
  20. package/examples/offline-signing.ts +66 -0
  21. package/examples/package-lock.json +596 -57
  22. package/examples/package.json +32 -16
  23. package/examples/read-operations.js +32 -27
  24. package/examples/read-operations.ts +31 -0
  25. package/examples/sdk-generator-erc20.inline.json +251 -251
  26. package/examples/solidity-types.ts +43 -43
  27. package/examples/wallet-offline.js +35 -29
  28. package/examples/wallet-offline.ts +34 -0
  29. package/generate-sdk.js +1824 -1490
  30. package/index.js +12 -12
  31. package/package.json +95 -75
  32. package/scripts/copy-declarations.js +31 -0
  33. package/scripts/run-all-one-by-one.js +151 -0
  34. package/src/abi/fragments.d.ts +42 -42
  35. package/src/abi/fragments.js +63 -63
  36. package/src/abi/index.d.ts +13 -13
  37. package/src/abi/index.js +9 -9
  38. package/src/abi/interface.d.ts +128 -132
  39. package/src/abi/interface.js +590 -590
  40. package/src/abi/js-abi-coder.d.ts +8 -0
  41. package/src/abi/js-abi-coder.js +474 -474
  42. package/src/constants.d.ts +66 -61
  43. package/src/constants.js +101 -94
  44. package/src/contract/contract-factory.d.ts +28 -28
  45. package/src/contract/contract-factory.js +105 -105
  46. package/src/contract/contract.d.ts +113 -114
  47. package/src/contract/contract.js +354 -354
  48. package/src/contract/index.d.ts +9 -9
  49. package/src/contract/index.js +9 -9
  50. package/src/errors/index.d.ts +92 -92
  51. package/src/errors/index.js +188 -188
  52. package/src/generator/index.d.ts +74 -0
  53. package/src/generator/index.js +1404 -1404
  54. package/src/index.d.ts +125 -127
  55. package/src/index.js +41 -41
  56. package/src/internal/hex.d.ts +61 -61
  57. package/src/internal/hex.js +144 -144
  58. package/src/providers/extra-providers.d.ts +139 -128
  59. package/src/providers/extra-providers.js +600 -575
  60. package/src/providers/index.d.ts +17 -16
  61. package/src/providers/index.js +10 -10
  62. package/src/providers/json-rpc-provider.d.ts +12 -12
  63. package/src/providers/json-rpc-provider.js +79 -79
  64. package/src/providers/provider.d.ts +208 -203
  65. package/src/providers/provider.js +393 -371
  66. package/src/types/index.d.ts +214 -462
  67. package/src/types/index.js +9 -9
  68. package/src/utils/address.d.ts +72 -72
  69. package/src/utils/address.js +181 -182
  70. package/src/utils/encoding.d.ts +120 -120
  71. package/src/utils/encoding.js +306 -306
  72. package/src/utils/hashing.d.ts +82 -76
  73. package/src/utils/hashing.js +313 -298
  74. package/src/utils/index.d.ts +65 -55
  75. package/src/utils/index.js +13 -13
  76. package/src/utils/result.d.ts +57 -57
  77. package/src/utils/result.js +128 -128
  78. package/src/utils/rlp.d.ts +12 -12
  79. package/src/utils/rlp.js +200 -200
  80. package/src/utils/units.d.ts +29 -29
  81. package/src/utils/units.js +107 -107
  82. package/src/wallet/index.d.ts +10 -10
  83. package/src/wallet/index.js +8 -8
  84. package/src/wallet/wallet.d.ts +168 -160
  85. package/src/wallet/wallet.js +500 -489
  86. package/test/e2e/all-solidity-types.dynamic.test.js +207 -200
  87. package/test/e2e/all-solidity-types.dynamic.test.ts +191 -0
  88. package/test/e2e/all-solidity-types.fixtures.js +231 -231
  89. package/test/e2e/all-solidity-types.generated-sdks.e2e.test.js +387 -368
  90. package/test/e2e/all-solidity-types.generated-sdks.e2e.test.ts +350 -0
  91. package/test/e2e/helpers.js +59 -47
  92. package/test/e2e/signing-context-and-fee.e2e.test.js +141 -0
  93. package/test/e2e/signing-context-and-fee.e2e.test.ts +128 -0
  94. package/test/e2e/simple-erc20.generated-sdks.e2e.test.js +168 -151
  95. package/test/e2e/simple-erc20.generated-sdks.e2e.test.ts +141 -0
  96. package/test/e2e/transactional.test.js +245 -191
  97. package/test/e2e/transactional.test.ts +208 -0
  98. package/test/e2e/typed-generator.e2e.test.js +407 -404
  99. package/test/e2e/typed-generator.e2e.test.ts +337 -0
  100. package/test/fixtures/ConstructorParam.sol +23 -23
  101. package/test/fixtures/MultiContracts.sol +37 -37
  102. package/test/fixtures/SimpleStorage.sol +18 -18
  103. package/test/fixtures/StakingContract.abi.json +1 -1
  104. package/test/integration/ipc-provider.test.js +49 -44
  105. package/test/integration/ipc-provider.test.ts +44 -0
  106. package/test/integration/provider.test.js +88 -72
  107. package/test/integration/provider.test.ts +85 -0
  108. package/test/integration/ws-provider.test.js +41 -33
  109. package/test/integration/ws-provider.test.ts +38 -0
  110. package/test/security/malformed-input.test.js +37 -31
  111. package/test/security/malformed-input.test.ts +35 -0
  112. package/test/unit/_encrypted-output.txt +6 -0
  113. package/test/unit/_log-encrypted-jsons.js +45 -0
  114. package/test/unit/_write-keystore-fixture.js +16 -0
  115. package/test/unit/abi-interface.test.js +103 -98
  116. package/test/unit/abi-interface.test.ts +102 -0
  117. package/test/unit/address-wallet.test.js +392 -257
  118. package/test/unit/address-wallet.test.ts +379 -0
  119. package/test/unit/browser-provider.test.js +85 -82
  120. package/test/unit/browser-provider.test.ts +79 -0
  121. package/test/unit/contract.test.js +85 -82
  122. package/test/unit/contract.test.ts +83 -0
  123. package/test/unit/encoding-units-rlp.test.js +92 -89
  124. package/test/unit/encoding-units-rlp.test.ts +91 -0
  125. package/test/unit/errors.test.js +77 -74
  126. package/test/unit/errors.test.ts +76 -0
  127. package/test/unit/filter-by-blockhash.test.js +55 -52
  128. package/test/unit/filter-by-blockhash.test.ts +54 -0
  129. package/test/unit/fixtures/encrypted-keystores-48-32-36.js +9 -0
  130. package/test/unit/generate-contract-cli.test.js +42 -39
  131. package/test/unit/generate-contract-cli.test.ts +41 -0
  132. package/test/unit/generate-sdk-artifacts-json.test.js +113 -110
  133. package/test/unit/generate-sdk-artifacts-json.test.ts +110 -0
  134. package/test/unit/generator.test.js +102 -99
  135. package/test/unit/generator.test.ts +101 -0
  136. package/test/unit/hashing.test.js +68 -54
  137. package/test/unit/hashing.test.ts +67 -0
  138. package/test/unit/init.test.js +39 -36
  139. package/test/unit/init.test.ts +38 -0
  140. package/test/unit/interface.test.js +56 -53
  141. package/test/unit/interface.test.ts +54 -0
  142. package/test/unit/internal-hex.test.js +50 -47
  143. package/test/unit/internal-hex.test.ts +49 -0
  144. package/test/unit/populate-transaction.test.js +65 -62
  145. package/test/unit/populate-transaction.test.ts +64 -0
  146. package/test/unit/providers.test.js +200 -144
  147. package/test/unit/providers.test.ts +196 -0
  148. package/test/unit/result.test.js +80 -77
  149. package/test/unit/result.test.ts +79 -0
  150. package/test/unit/solidity-types.test.js +49 -46
  151. package/test/unit/solidity-types.test.ts +39 -0
  152. package/test/unit/utils.test.js +57 -54
  153. package/test/unit/utils.test.ts +56 -0
  154. package/test/verbose-logger.js +74 -0
  155. package/tsconfig.build.json +14 -0
@@ -1,298 +1,313 @@
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 = arrayify(key);
220
- const d = 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 = arrayify(password);
236
- const s = 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 = arrayify(password);
254
- const sa = 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 = arrayify(password);
281
- const sa = arrayify(salt);
282
- const out = crypto.scryptSync(Buffer.from(pw), Buffer.from(sa), dkLen, { N, r, p, maxmem: 128 * 1024 * 1024 });
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 { MessagePrefix } = require("../constants");
11
+ const { arrayify, bytesToHex, utf8ToBytes } = require("../internal/hex");
12
+ const { concat } = require("./encoding");
13
+
14
+ const _MASK64 = (1n << 64n) - 1n;
15
+
16
+ function _rotl64(x, n) {
17
+ const nn = BigInt(n);
18
+ return ((x << nn) | (x >> (64n - nn))) & _MASK64;
19
+ }
20
+
21
+ function _readU64LE(bytes, off) {
22
+ let x = 0n;
23
+ for (let i = 0; i < 8; i++) {
24
+ x |= BigInt(bytes[off + i]) << (8n * BigInt(i));
25
+ }
26
+ return x;
27
+ }
28
+
29
+ function _writeU64LE(out, off, x) {
30
+ let v = x & _MASK64;
31
+ for (let i = 0; i < 8; i++) {
32
+ out[off + i] = Number((v >> (8n * BigInt(i))) & 0xffn);
33
+ }
34
+ }
35
+
36
+ // Keccak-f[1600] round constants
37
+ const _RC = [
38
+ 0x0000000000000001n, 0x0000000000008082n, 0x800000000000808an, 0x8000000080008000n,
39
+ 0x000000000000808bn, 0x0000000080000001n, 0x8000000080008081n, 0x8000000000008009n,
40
+ 0x000000000000008an, 0x0000000000000088n, 0x0000000080008009n, 0x000000008000000an,
41
+ 0x000000008000808bn, 0x800000000000008bn, 0x8000000000008089n, 0x8000000000008003n,
42
+ 0x8000000000008002n, 0x8000000000000080n, 0x000000000000800an, 0x800000008000000an,
43
+ 0x8000000080008081n, 0x8000000000008080n, 0x0000000080000001n, 0x8000000080008008n,
44
+ ];
45
+
46
+ // Rotation offsets (rho step)
47
+ const _R = [
48
+ // Indexed by lane index (x + 5*y) where x,y in [0..4]
49
+ // (This is the standard Keccak rho offsets table, flattened in y-major order.)
50
+ 0, 1, 62, 28, 27,
51
+ 36, 44, 6, 55, 20,
52
+ 3, 10, 43, 25, 39,
53
+ 41, 45, 15, 21, 8,
54
+ 18, 2, 61, 56, 14,
55
+ ];
56
+
57
+ // Pi step mapping: index -> newIndex
58
+ const _PI = [
59
+ 0, 10, 20, 5, 15,
60
+ 16, 1, 11, 21, 6,
61
+ 7, 17, 2, 12, 22,
62
+ 23, 8, 18, 3, 13,
63
+ 14, 24, 9, 19, 4,
64
+ ];
65
+
66
+ function _keccakF1600(state) {
67
+ const b = new Array(25);
68
+ const c = new Array(5);
69
+ const d = new Array(5);
70
+
71
+ for (let round = 0; round < 24; round++) {
72
+ // Theta
73
+ for (let x = 0; x < 5; x++) {
74
+ c[x] = state[x] ^ state[x + 5] ^ state[x + 10] ^ state[x + 15] ^ state[x + 20];
75
+ }
76
+ for (let x = 0; x < 5; x++) {
77
+ d[x] = c[(x + 4) % 5] ^ _rotl64(c[(x + 1) % 5], 1);
78
+ }
79
+ for (let i = 0; i < 25; i++) {
80
+ state[i] = (state[i] ^ d[i % 5]) & _MASK64;
81
+ }
82
+
83
+ // Rho + Pi
84
+ for (let i = 0; i < 25; i++) {
85
+ b[_PI[i]] = _rotl64(state[i], _R[i]);
86
+ }
87
+
88
+ // Chi
89
+ for (let y = 0; y < 5; y++) {
90
+ const row = y * 5;
91
+ for (let x = 0; x < 5; x++) {
92
+ state[row + x] = (b[row + x] ^ ((~b[row + ((x + 1) % 5)]) & b[row + ((x + 2) % 5)])) & _MASK64;
93
+ }
94
+ }
95
+
96
+ // Iota
97
+ state[0] = (state[0] ^ _RC[round]) & _MASK64;
98
+ }
99
+ }
100
+
101
+ function _keccak256Bytes(bytes) {
102
+ // Keccak-256: rate=1088 bits => 136 bytes
103
+ const rate = 136;
104
+ const outLen = 32;
105
+ const state = new Array(25).fill(0n);
106
+
107
+ let offset = 0;
108
+ while (offset + rate <= bytes.length) {
109
+ for (let i = 0; i < rate / 8; i++) {
110
+ state[i] = (state[i] ^ _readU64LE(bytes, offset + i * 8)) & _MASK64;
111
+ }
112
+ _keccakF1600(state);
113
+ offset += rate;
114
+ }
115
+
116
+ // Final block + padding (keccak pad10*1 with domain 0x01)
117
+ const block = new Uint8Array(rate);
118
+ const remaining = bytes.length - offset;
119
+ block.set(bytes.slice(offset), 0);
120
+ block[remaining] = 0x01;
121
+ block[rate - 1] |= 0x80;
122
+
123
+ for (let i = 0; i < rate / 8; i++) {
124
+ state[i] = (state[i] ^ _readU64LE(block, i * 8)) & _MASK64;
125
+ }
126
+ _keccakF1600(state);
127
+
128
+ const out = new Uint8Array(outLen);
129
+ let outOff = 0;
130
+ let lane = 0;
131
+ while (outOff < outLen) {
132
+ const tmp = new Uint8Array(8);
133
+ _writeU64LE(tmp, 0, state[lane]);
134
+ const take = Math.min(8, outLen - outOff);
135
+ out.set(tmp.slice(0, take), outOff);
136
+ outOff += take;
137
+ lane++;
138
+ }
139
+ return out;
140
+ }
141
+
142
+ function _hash(alg, data) {
143
+ const bytes = arrayify(data);
144
+ return bytesToHex(crypto.createHash(alg).update(Buffer.from(bytes)).digest());
145
+ }
146
+
147
+ /**
148
+ * keccak256 hash of BytesLike.
149
+ * @param {string|Uint8Array} data
150
+ * @returns {string}
151
+ */
152
+ function keccak256(data) {
153
+ const bytes = arrayify(data);
154
+ const hashes = crypto.getHashes();
155
+
156
+ // Prefer native keccak if available (varies by Node/OpenSSL build).
157
+ if (hashes.includes("keccak256")) {
158
+ return bytesToHex(crypto.createHash("keccak256").update(Buffer.from(bytes)).digest());
159
+ }
160
+ if (hashes.includes("keccak-256")) {
161
+ return bytesToHex(crypto.createHash("keccak-256").update(Buffer.from(bytes)).digest());
162
+ }
163
+
164
+ // Fallback: pure JS Keccak-256
165
+ return bytesToHex(_keccak256Bytes(bytes));
166
+ }
167
+
168
+ /**
169
+ * sha256 hash of BytesLike.
170
+ * @param {string|Uint8Array} data
171
+ * @returns {string}
172
+ */
173
+ function sha256(data) {
174
+ return _hash("sha256", data);
175
+ }
176
+
177
+ /**
178
+ * sha512 hash of BytesLike.
179
+ * @param {string|Uint8Array} data
180
+ * @returns {string}
181
+ */
182
+ function sha512(data) {
183
+ return _hash("sha512", data);
184
+ }
185
+
186
+ /**
187
+ * ripemd160 hash of BytesLike.
188
+ * @param {string|Uint8Array} data
189
+ * @returns {string}
190
+ */
191
+ function ripemd160(data) {
192
+ return _hash("ripemd160", data);
193
+ }
194
+
195
+ /**
196
+ * EIP-191 personal-sign message digest (ethers.js hashMessage pattern).
197
+ * Prefixes message with MessagePrefix and decimal length, then keccak256.
198
+ * If message is a string, it is converted to UTF-8 bytes first.
199
+ * @param {string|Uint8Array} message
200
+ * @returns {string} Hex digest
201
+ */
202
+ function hashMessage(message) {
203
+ const msgBytes = typeof message === "string" ? utf8ToBytes(message) : arrayify(message);
204
+ return keccak256(concat([utf8ToBytes(MessagePrefix), utf8ToBytes(String(msgBytes.length)), msgBytes]));
205
+ }
206
+
207
+ /**
208
+ * ethers-style id(text) => keccak256(utf8Bytes(text))
209
+ * @param {string} text
210
+ * @returns {string}
211
+ */
212
+ function id(text) {
213
+ return keccak256(utf8ToBytes(text));
214
+ }
215
+
216
+ /**
217
+ * Generate cryptographically strong random bytes.
218
+ * @param {number} length
219
+ * @returns {Uint8Array}
220
+ */
221
+ function randomBytes(length) {
222
+ return new Uint8Array(crypto.randomBytes(length));
223
+ }
224
+
225
+ /**
226
+ * Compute HMAC over data.
227
+ * @param {string} algorithm
228
+ * @param {string|Uint8Array} key
229
+ * @param {string|Uint8Array} data
230
+ * @returns {string}
231
+ */
232
+ function computeHmac(algorithm, key, data) {
233
+ const k = arrayify(key);
234
+ const d = arrayify(data);
235
+ const h = crypto.createHmac(algorithm, Buffer.from(k)).update(Buffer.from(d)).digest();
236
+ return bytesToHex(new Uint8Array(h));
237
+ }
238
+
239
+ /**
240
+ * PBKDF2 (sync) helper returning hex string.
241
+ * @param {string|Uint8Array} password
242
+ * @param {string|Uint8Array} salt
243
+ * @param {number} iterations
244
+ * @param {number} keylen
245
+ * @param {string=} algorithm
246
+ * @returns {string}
247
+ */
248
+ function pbkdf2(password, salt, iterations, keylen, algorithm) {
249
+ const p = arrayify(password);
250
+ const s = arrayify(salt);
251
+ const a = algorithm || "sha256";
252
+ const out = crypto.pbkdf2Sync(Buffer.from(p), Buffer.from(s), iterations, keylen, a);
253
+ return bytesToHex(new Uint8Array(out));
254
+ }
255
+
256
+ /**
257
+ * scrypt (async) helper returning hex string.
258
+ * @param {string|Uint8Array} password
259
+ * @param {string|Uint8Array} salt
260
+ * @param {number} N
261
+ * @param {number} r
262
+ * @param {number} p
263
+ * @param {number} dkLen
264
+ * @returns {Promise<string>}
265
+ */
266
+ function scrypt(password, salt, N, r, p, dkLen) {
267
+ const pw = arrayify(password);
268
+ const sa = arrayify(salt);
269
+ return new Promise((resolve, reject) => {
270
+ crypto.scrypt(
271
+ Buffer.from(pw),
272
+ Buffer.from(sa),
273
+ dkLen,
274
+ { N, r, p, maxmem: 128 * 1024 * 1024 },
275
+ (err, derived) => {
276
+ if (err) return reject(err);
277
+ resolve(bytesToHex(new Uint8Array(derived)));
278
+ },
279
+ );
280
+ });
281
+ }
282
+
283
+ /**
284
+ * scrypt (sync) helper returning hex string.
285
+ * @param {string|Uint8Array} password
286
+ * @param {string|Uint8Array} salt
287
+ * @param {number} N
288
+ * @param {number} r
289
+ * @param {number} p
290
+ * @param {number} dkLen
291
+ * @returns {string}
292
+ */
293
+ function scryptSync(password, salt, N, r, p, dkLen) {
294
+ const pw = arrayify(password);
295
+ const sa = arrayify(salt);
296
+ const out = crypto.scryptSync(Buffer.from(pw), Buffer.from(sa), dkLen, { N, r, p, maxmem: 128 * 1024 * 1024 });
297
+ return bytesToHex(new Uint8Array(out));
298
+ }
299
+
300
+ module.exports = {
301
+ keccak256,
302
+ hashMessage,
303
+ sha256,
304
+ sha512,
305
+ ripemd160,
306
+ id,
307
+ randomBytes,
308
+ computeHmac,
309
+ pbkdf2,
310
+ scrypt,
311
+ scryptSync,
312
+ };
313
+