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.
- package/.github/workflows/publish-npmjs.yaml +22 -22
- package/.gitignore +15 -15
- package/LICENSE +21 -21
- package/README-SDK.md +758 -754
- package/README.md +165 -150
- package/SPEC.md +3845 -3843
- package/config.d.ts +50 -50
- package/config.js +115 -115
- package/examples/AllSolidityTypes.sol +184 -184
- package/examples/SimpleIERC20.sol +74 -74
- package/examples/events.js +41 -35
- package/examples/events.ts +35 -0
- package/examples/example-generator-sdk-js.js +100 -95
- package/examples/example-generator-sdk-js.ts +77 -0
- package/examples/example-generator-sdk-ts.js +100 -95
- package/examples/example-generator-sdk-ts.ts +77 -0
- package/examples/example.js +72 -61
- package/examples/example.ts +61 -0
- package/examples/offline-signing.js +79 -73
- package/examples/offline-signing.ts +66 -0
- package/examples/package-lock.json +596 -57
- package/examples/package.json +32 -16
- package/examples/read-operations.js +32 -27
- package/examples/read-operations.ts +31 -0
- package/examples/sdk-generator-erc20.inline.json +251 -251
- package/examples/solidity-types.ts +43 -43
- package/examples/wallet-offline.js +35 -29
- package/examples/wallet-offline.ts +34 -0
- package/generate-sdk.js +1824 -1490
- package/index.js +12 -12
- package/package.json +95 -75
- package/scripts/copy-declarations.js +31 -0
- package/scripts/run-all-one-by-one.js +151 -0
- package/src/abi/fragments.d.ts +42 -42
- package/src/abi/fragments.js +63 -63
- package/src/abi/index.d.ts +13 -13
- package/src/abi/index.js +9 -9
- package/src/abi/interface.d.ts +128 -132
- package/src/abi/interface.js +590 -590
- package/src/abi/js-abi-coder.d.ts +8 -0
- package/src/abi/js-abi-coder.js +474 -474
- package/src/constants.d.ts +66 -61
- package/src/constants.js +101 -94
- package/src/contract/contract-factory.d.ts +28 -28
- package/src/contract/contract-factory.js +105 -105
- package/src/contract/contract.d.ts +113 -114
- package/src/contract/contract.js +354 -354
- package/src/contract/index.d.ts +9 -9
- package/src/contract/index.js +9 -9
- package/src/errors/index.d.ts +92 -92
- package/src/errors/index.js +188 -188
- package/src/generator/index.d.ts +74 -0
- package/src/generator/index.js +1404 -1404
- package/src/index.d.ts +125 -127
- package/src/index.js +41 -41
- package/src/internal/hex.d.ts +61 -61
- package/src/internal/hex.js +144 -144
- package/src/providers/extra-providers.d.ts +139 -128
- package/src/providers/extra-providers.js +600 -575
- package/src/providers/index.d.ts +17 -16
- package/src/providers/index.js +10 -10
- package/src/providers/json-rpc-provider.d.ts +12 -12
- package/src/providers/json-rpc-provider.js +79 -79
- package/src/providers/provider.d.ts +208 -203
- package/src/providers/provider.js +393 -371
- package/src/types/index.d.ts +214 -462
- package/src/types/index.js +9 -9
- package/src/utils/address.d.ts +72 -72
- package/src/utils/address.js +181 -182
- package/src/utils/encoding.d.ts +120 -120
- package/src/utils/encoding.js +306 -306
- package/src/utils/hashing.d.ts +82 -76
- package/src/utils/hashing.js +313 -298
- package/src/utils/index.d.ts +65 -55
- package/src/utils/index.js +13 -13
- package/src/utils/result.d.ts +57 -57
- package/src/utils/result.js +128 -128
- package/src/utils/rlp.d.ts +12 -12
- package/src/utils/rlp.js +200 -200
- package/src/utils/units.d.ts +29 -29
- package/src/utils/units.js +107 -107
- package/src/wallet/index.d.ts +10 -10
- package/src/wallet/index.js +8 -8
- package/src/wallet/wallet.d.ts +168 -160
- package/src/wallet/wallet.js +500 -489
- package/test/e2e/all-solidity-types.dynamic.test.js +207 -200
- package/test/e2e/all-solidity-types.dynamic.test.ts +191 -0
- package/test/e2e/all-solidity-types.fixtures.js +231 -231
- package/test/e2e/all-solidity-types.generated-sdks.e2e.test.js +387 -368
- package/test/e2e/all-solidity-types.generated-sdks.e2e.test.ts +350 -0
- package/test/e2e/helpers.js +59 -47
- package/test/e2e/signing-context-and-fee.e2e.test.js +141 -0
- package/test/e2e/signing-context-and-fee.e2e.test.ts +128 -0
- package/test/e2e/simple-erc20.generated-sdks.e2e.test.js +168 -151
- package/test/e2e/simple-erc20.generated-sdks.e2e.test.ts +141 -0
- package/test/e2e/transactional.test.js +245 -191
- package/test/e2e/transactional.test.ts +208 -0
- package/test/e2e/typed-generator.e2e.test.js +407 -404
- package/test/e2e/typed-generator.e2e.test.ts +337 -0
- package/test/fixtures/ConstructorParam.sol +23 -23
- package/test/fixtures/MultiContracts.sol +37 -37
- package/test/fixtures/SimpleStorage.sol +18 -18
- package/test/fixtures/StakingContract.abi.json +1 -1
- package/test/integration/ipc-provider.test.js +49 -44
- package/test/integration/ipc-provider.test.ts +44 -0
- package/test/integration/provider.test.js +88 -72
- package/test/integration/provider.test.ts +85 -0
- package/test/integration/ws-provider.test.js +41 -33
- package/test/integration/ws-provider.test.ts +38 -0
- package/test/security/malformed-input.test.js +37 -31
- package/test/security/malformed-input.test.ts +35 -0
- package/test/unit/_encrypted-output.txt +6 -0
- package/test/unit/_log-encrypted-jsons.js +45 -0
- package/test/unit/_write-keystore-fixture.js +16 -0
- package/test/unit/abi-interface.test.js +103 -98
- package/test/unit/abi-interface.test.ts +102 -0
- package/test/unit/address-wallet.test.js +392 -257
- package/test/unit/address-wallet.test.ts +379 -0
- package/test/unit/browser-provider.test.js +85 -82
- package/test/unit/browser-provider.test.ts +79 -0
- package/test/unit/contract.test.js +85 -82
- package/test/unit/contract.test.ts +83 -0
- package/test/unit/encoding-units-rlp.test.js +92 -89
- package/test/unit/encoding-units-rlp.test.ts +91 -0
- package/test/unit/errors.test.js +77 -74
- package/test/unit/errors.test.ts +76 -0
- package/test/unit/filter-by-blockhash.test.js +55 -52
- package/test/unit/filter-by-blockhash.test.ts +54 -0
- package/test/unit/fixtures/encrypted-keystores-48-32-36.js +9 -0
- package/test/unit/generate-contract-cli.test.js +42 -39
- package/test/unit/generate-contract-cli.test.ts +41 -0
- package/test/unit/generate-sdk-artifacts-json.test.js +113 -110
- package/test/unit/generate-sdk-artifacts-json.test.ts +110 -0
- package/test/unit/generator.test.js +102 -99
- package/test/unit/generator.test.ts +101 -0
- package/test/unit/hashing.test.js +68 -54
- package/test/unit/hashing.test.ts +67 -0
- package/test/unit/init.test.js +39 -36
- package/test/unit/init.test.ts +38 -0
- package/test/unit/interface.test.js +56 -53
- package/test/unit/interface.test.ts +54 -0
- package/test/unit/internal-hex.test.js +50 -47
- package/test/unit/internal-hex.test.ts +49 -0
- package/test/unit/populate-transaction.test.js +65 -62
- package/test/unit/populate-transaction.test.ts +64 -0
- package/test/unit/providers.test.js +200 -144
- package/test/unit/providers.test.ts +196 -0
- package/test/unit/result.test.js +80 -77
- package/test/unit/result.test.ts +79 -0
- package/test/unit/solidity-types.test.js +49 -46
- package/test/unit/solidity-types.test.ts +39 -0
- package/test/unit/utils.test.js +57 -54
- package/test/unit/utils.test.ts +56 -0
- package/test/verbose-logger.js +74 -0
- package/tsconfig.build.json +14 -0
package/src/utils/hashing.js
CHANGED
|
@@ -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 {
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
block
|
|
118
|
-
|
|
119
|
-
block
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
*
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
*
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
*
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
*
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
*
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
return
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
*
|
|
227
|
-
* @param {string
|
|
228
|
-
* @param {string|Uint8Array}
|
|
229
|
-
* @param {
|
|
230
|
-
* @
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
const
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
*
|
|
244
|
-
* @param {
|
|
245
|
-
* @param {string
|
|
246
|
-
* @
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
*
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
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
|
+
|