essential-eth 0.5.0 → 0.5.1
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/lib/cjs/index.d.ts +3 -2
- package/lib/cjs/index.js +17 -3
- package/lib/cjs/logger/logger.d.ts +11 -0
- package/lib/cjs/logger/logger.js +36 -0
- package/lib/cjs/logger/package-version.d.ts +1 -0
- package/lib/cjs/logger/package-version.js +5 -0
- package/lib/cjs/providers/JsonRpcProvider.d.ts +1 -1
- package/lib/cjs/providers/JsonRpcProvider.js +1 -1
- package/lib/cjs/providers/utils/chains-info.d.ts +4 -0
- package/lib/cjs/providers/utils/chains-info.js +12 -0
- package/lib/cjs/shared/tiny-big/tiny-big.d.ts +7 -0
- package/lib/cjs/shared/tiny-big/tiny-big.js +25 -7
- package/lib/cjs/utils/bytes.d.ts +90 -0
- package/lib/cjs/utils/bytes.js +484 -0
- package/lib/cjs/utils/solidity-keccak256.d.ts +30 -0
- package/lib/cjs/utils/solidity-keccak256.js +125 -0
- package/lib/esm/index.d.ts +3 -2
- package/lib/esm/index.js +3 -2
- package/lib/esm/logger/logger.d.ts +11 -0
- package/lib/esm/logger/logger.js +33 -0
- package/lib/esm/logger/package-version.d.ts +1 -0
- package/lib/esm/logger/package-version.js +1 -0
- package/lib/esm/providers/JsonRpcProvider.d.ts +1 -1
- package/lib/esm/providers/JsonRpcProvider.js +1 -1
- package/lib/esm/providers/utils/chains-info.d.ts +4 -0
- package/lib/esm/providers/utils/chains-info.js +12 -0
- package/lib/esm/shared/tiny-big/tiny-big.d.ts +2 -0
- package/lib/esm/shared/tiny-big/tiny-big.js +20 -7
- package/lib/esm/utils/bytes.d.ts +39 -0
- package/lib/esm/utils/bytes.js +245 -0
- package/lib/esm/utils/solidity-keccak256.d.ts +3 -0
- package/lib/esm/utils/solidity-keccak256.js +91 -0
- package/package.json +12 -14
- package/lib/cjs/utils/hex-zero-pad.d.ts +0 -32
- package/lib/cjs/utils/hex-zero-pad.js +0 -52
- package/lib/esm/utils/hex-zero-pad.d.ts +0 -1
- package/lib/esm/utils/hex-zero-pad.js +0 -17
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// primary duplicate code from https://github.com/ethers-io/ethers.js/blob/f599d6f23dad0d0acaa3828d6b7acaab2d5e455b/packages/bytes/src.ts/index.ts
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.hexZeroPad = exports.hexStripZeros = exports.hexValue = exports.hexConcat = exports.hexDataSlice = exports.hexDataLength = exports.hexlify = exports.isHexString = exports.zeroPad = exports.stripZeros = exports.concat = exports.arrayify = exports.isBytes = exports.isBytesLike = void 0;
|
|
5
|
+
const logger_1 = require("../logger/logger");
|
|
6
|
+
///////////////////////////////
|
|
7
|
+
function isHexable(value) {
|
|
8
|
+
return !!value.toHexString;
|
|
9
|
+
}
|
|
10
|
+
function isBytesLike(value) {
|
|
11
|
+
return (isHexString(value) && !(value.length % 2)) || isBytes(value);
|
|
12
|
+
}
|
|
13
|
+
exports.isBytesLike = isBytesLike;
|
|
14
|
+
function isInteger(value) {
|
|
15
|
+
return typeof value === 'number' && value == value && value % 1 === 0;
|
|
16
|
+
}
|
|
17
|
+
function isBytes(value) {
|
|
18
|
+
if (value == null) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
if (value.constructor === Uint8Array) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
if (typeof value === 'string') {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
if (!isInteger(value.length) || value.length < 0) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
for (let i = 0; i < value.length; i++) {
|
|
31
|
+
const v = value[i];
|
|
32
|
+
if (!isInteger(v) || v < 0 || v >= 256) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
exports.isBytes = isBytes;
|
|
39
|
+
function arrayify(value, options) {
|
|
40
|
+
if (!options) {
|
|
41
|
+
options = {};
|
|
42
|
+
}
|
|
43
|
+
if (typeof value === 'number') {
|
|
44
|
+
logger_1.logger.checkSafeUint53(value, 'invalid arrayify value');
|
|
45
|
+
const result = [];
|
|
46
|
+
while (value) {
|
|
47
|
+
result.unshift(value & 0xff);
|
|
48
|
+
value = parseInt(String(value / 256));
|
|
49
|
+
}
|
|
50
|
+
if (result.length === 0) {
|
|
51
|
+
result.push(0);
|
|
52
|
+
}
|
|
53
|
+
return new Uint8Array(result);
|
|
54
|
+
}
|
|
55
|
+
if (options.allowMissingPrefix &&
|
|
56
|
+
typeof value === 'string' &&
|
|
57
|
+
value.substring(0, 2) !== '0x') {
|
|
58
|
+
value = '0x' + value;
|
|
59
|
+
}
|
|
60
|
+
if (isHexable(value)) {
|
|
61
|
+
value = value.toHexString();
|
|
62
|
+
}
|
|
63
|
+
if (isHexString(value)) {
|
|
64
|
+
let hex = value.substring(2);
|
|
65
|
+
if (hex.length % 2) {
|
|
66
|
+
if (options.hexPad === 'left') {
|
|
67
|
+
hex = '0' + hex;
|
|
68
|
+
}
|
|
69
|
+
else if (options.hexPad === 'right') {
|
|
70
|
+
hex += '0';
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
logger_1.logger.throwArgumentError('hex data is odd-length', 'value', value);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const result = [];
|
|
77
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
78
|
+
result.push(parseInt(hex.substring(i, i + 2), 16));
|
|
79
|
+
}
|
|
80
|
+
return new Uint8Array(result);
|
|
81
|
+
}
|
|
82
|
+
if (isBytes(value)) {
|
|
83
|
+
return new Uint8Array(value);
|
|
84
|
+
}
|
|
85
|
+
return logger_1.logger.throwArgumentError('invalid arrayify value', 'value', value);
|
|
86
|
+
}
|
|
87
|
+
exports.arrayify = arrayify;
|
|
88
|
+
function concat(items) {
|
|
89
|
+
const objects = items.map((item) => arrayify(item));
|
|
90
|
+
const length = objects.reduce((accum, item) => accum + item.length, 0);
|
|
91
|
+
const result = new Uint8Array(length);
|
|
92
|
+
objects.reduce((offset, object) => {
|
|
93
|
+
result.set(object, offset);
|
|
94
|
+
return offset + object.length;
|
|
95
|
+
}, 0);
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
exports.concat = concat;
|
|
99
|
+
function stripZeros(value) {
|
|
100
|
+
let result = arrayify(value);
|
|
101
|
+
if (result.length === 0) {
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
// Find the first non-zero entry
|
|
105
|
+
let start = 0;
|
|
106
|
+
while (start < result.length && result[start] === 0) {
|
|
107
|
+
start++;
|
|
108
|
+
}
|
|
109
|
+
// If we started with zeros, strip them
|
|
110
|
+
if (start) {
|
|
111
|
+
result = result.slice(start);
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
exports.stripZeros = stripZeros;
|
|
116
|
+
function zeroPad(value, length) {
|
|
117
|
+
value = arrayify(value);
|
|
118
|
+
if (value.length > length) {
|
|
119
|
+
logger_1.logger.throwArgumentError('value out of range', 'value', value);
|
|
120
|
+
}
|
|
121
|
+
const result = new Uint8Array(length);
|
|
122
|
+
result.set(value, length - value.length);
|
|
123
|
+
return result;
|
|
124
|
+
}
|
|
125
|
+
exports.zeroPad = zeroPad;
|
|
126
|
+
function isHexString(value, length) {
|
|
127
|
+
if (typeof value !== 'string' || !value.match(/^0x[0-9A-Fa-f]*$/)) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
if (length && value.length !== 2 + 2 * length) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
exports.isHexString = isHexString;
|
|
136
|
+
const HexCharacters = '0123456789abcdef';
|
|
137
|
+
/**
|
|
138
|
+
* @example
|
|
139
|
+
* ```js
|
|
140
|
+
* hexlify(4);
|
|
141
|
+
* // '0x04'
|
|
142
|
+
*
|
|
143
|
+
* hexlify(14);
|
|
144
|
+
* // '0x0e'
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
function hexlify(value, options) {
|
|
148
|
+
if (!options) {
|
|
149
|
+
options = {};
|
|
150
|
+
}
|
|
151
|
+
if (typeof value === 'number') {
|
|
152
|
+
logger_1.logger.checkSafeUint53(value, 'invalid hexlify value');
|
|
153
|
+
let hex = '';
|
|
154
|
+
while (value) {
|
|
155
|
+
hex = HexCharacters[value & 0xf] + hex;
|
|
156
|
+
value = Math.floor(value / 16);
|
|
157
|
+
}
|
|
158
|
+
if (hex.length) {
|
|
159
|
+
if (hex.length % 2) {
|
|
160
|
+
hex = '0' + hex;
|
|
161
|
+
}
|
|
162
|
+
return '0x' + hex;
|
|
163
|
+
}
|
|
164
|
+
return '0x00';
|
|
165
|
+
}
|
|
166
|
+
if (typeof value === 'bigint') {
|
|
167
|
+
value = value.toString(16);
|
|
168
|
+
if (value.length % 2) {
|
|
169
|
+
return '0x0' + value;
|
|
170
|
+
}
|
|
171
|
+
return '0x' + value;
|
|
172
|
+
}
|
|
173
|
+
if (options.allowMissingPrefix &&
|
|
174
|
+
typeof value === 'string' &&
|
|
175
|
+
value.substring(0, 2) !== '0x') {
|
|
176
|
+
value = '0x' + value;
|
|
177
|
+
}
|
|
178
|
+
if (isHexable(value)) {
|
|
179
|
+
return value.toHexString();
|
|
180
|
+
}
|
|
181
|
+
if (isHexString(value)) {
|
|
182
|
+
if (value.length % 2) {
|
|
183
|
+
if (options.hexPad === 'left') {
|
|
184
|
+
value = '0x0' + value.substring(2);
|
|
185
|
+
}
|
|
186
|
+
else if (options.hexPad === 'right') {
|
|
187
|
+
value += '0';
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
logger_1.logger.throwArgumentError('hex data is odd-length', 'value', value);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return value.toLowerCase();
|
|
194
|
+
}
|
|
195
|
+
if (isBytes(value)) {
|
|
196
|
+
let result = '0x';
|
|
197
|
+
for (let i = 0; i < value.length; i++) {
|
|
198
|
+
const v = value[i];
|
|
199
|
+
result += HexCharacters[(v & 0xf0) >> 4] + HexCharacters[v & 0x0f];
|
|
200
|
+
}
|
|
201
|
+
return result;
|
|
202
|
+
}
|
|
203
|
+
return logger_1.logger.throwArgumentError('invalid hexlify value', 'value', value);
|
|
204
|
+
}
|
|
205
|
+
exports.hexlify = hexlify;
|
|
206
|
+
function hexDataLength(data) {
|
|
207
|
+
if (typeof data !== 'string') {
|
|
208
|
+
data = hexlify(data);
|
|
209
|
+
}
|
|
210
|
+
else if (!isHexString(data) || data.length % 2) {
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
return (data.length - 2) / 2;
|
|
214
|
+
}
|
|
215
|
+
exports.hexDataLength = hexDataLength;
|
|
216
|
+
function hexDataSlice(data, offset, endOffset) {
|
|
217
|
+
if (typeof data !== 'string') {
|
|
218
|
+
data = hexlify(data);
|
|
219
|
+
}
|
|
220
|
+
else if (!isHexString(data) || data.length % 2) {
|
|
221
|
+
logger_1.logger.throwArgumentError('invalid hexData', 'value', data);
|
|
222
|
+
}
|
|
223
|
+
offset = 2 + 2 * offset;
|
|
224
|
+
if (endOffset != null) {
|
|
225
|
+
return '0x' + data.substring(offset, 2 + 2 * endOffset);
|
|
226
|
+
}
|
|
227
|
+
return '0x' + data.substring(offset);
|
|
228
|
+
}
|
|
229
|
+
exports.hexDataSlice = hexDataSlice;
|
|
230
|
+
function hexConcat(items) {
|
|
231
|
+
let result = '0x';
|
|
232
|
+
items.forEach((item) => {
|
|
233
|
+
result += hexlify(item).substring(2);
|
|
234
|
+
});
|
|
235
|
+
return result;
|
|
236
|
+
}
|
|
237
|
+
exports.hexConcat = hexConcat;
|
|
238
|
+
function hexValue(value) {
|
|
239
|
+
const trimmed = hexStripZeros(hexlify(value, { hexPad: 'left' }));
|
|
240
|
+
if (trimmed === '0x') {
|
|
241
|
+
return '0x0';
|
|
242
|
+
}
|
|
243
|
+
return trimmed;
|
|
244
|
+
}
|
|
245
|
+
exports.hexValue = hexValue;
|
|
246
|
+
function hexStripZeros(value) {
|
|
247
|
+
if (typeof value !== 'string') {
|
|
248
|
+
value = hexlify(value);
|
|
249
|
+
}
|
|
250
|
+
if (!isHexString(value)) {
|
|
251
|
+
logger_1.logger.throwArgumentError('invalid hex string', 'value', value);
|
|
252
|
+
}
|
|
253
|
+
value = value.substring(2);
|
|
254
|
+
let offset = 0;
|
|
255
|
+
while (offset < value.length && value[offset] === '0') {
|
|
256
|
+
offset++;
|
|
257
|
+
}
|
|
258
|
+
return '0x' + value.substring(offset);
|
|
259
|
+
}
|
|
260
|
+
exports.hexStripZeros = hexStripZeros;
|
|
261
|
+
/**
|
|
262
|
+
* Returns a hex string padded to a specified length of bytes.
|
|
263
|
+
*
|
|
264
|
+
* Similar to ["hexZeroPad" in ethers.js](https://docs.ethers.io/v5/api/utils/bytes/#utils-hexZeroPad)
|
|
265
|
+
*
|
|
266
|
+
* Differs from ["padLeft" in web3.js](https://web3js.readthedocs.io/en/v1.7.1/web3-utils.html#padleft) because web3 counts by characters, not bytes.
|
|
267
|
+
*
|
|
268
|
+
* @param hexValue - A hex-string, hex-number, or decimal number (auto-converts to base-16) to be padded
|
|
269
|
+
* @param length - The final length in bytes
|
|
270
|
+
*
|
|
271
|
+
* @throws - If the value is not a hex string or number
|
|
272
|
+
* @throws - If the value is longer than the length
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* ```javascript
|
|
276
|
+
* hexZeroPad('0x60', 2);
|
|
277
|
+
* // '0x0060'
|
|
278
|
+
* ```
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* ```javascript
|
|
282
|
+
* hexZeroPad(0x60, 3);
|
|
283
|
+
* // '0x000060'
|
|
284
|
+
* ```
|
|
285
|
+
*
|
|
286
|
+
* @example
|
|
287
|
+
* ```javascript
|
|
288
|
+
* hexZeroPad('12345', 1);
|
|
289
|
+
* // Throws
|
|
290
|
+
* ```
|
|
291
|
+
*/
|
|
292
|
+
function hexZeroPad(value, length) {
|
|
293
|
+
if (typeof value !== 'string') {
|
|
294
|
+
value = hexlify(value);
|
|
295
|
+
}
|
|
296
|
+
else if (!isHexString(value)) {
|
|
297
|
+
logger_1.logger.throwArgumentError('invalid hex string', 'value', value);
|
|
298
|
+
}
|
|
299
|
+
if (value.length > 2 * length + 2) {
|
|
300
|
+
logger_1.logger.throwError('value out of range', { value, length });
|
|
301
|
+
}
|
|
302
|
+
while (value.length < 2 * length + 2) {
|
|
303
|
+
value = '0x0' + value.substring(2);
|
|
304
|
+
}
|
|
305
|
+
return value;
|
|
306
|
+
}
|
|
307
|
+
exports.hexZeroPad = hexZeroPad;
|
|
308
|
+
// export function splitSignature(signature: SignatureLike): Signature {
|
|
309
|
+
// const result: Signature = {
|
|
310
|
+
// r: '0x',
|
|
311
|
+
// s: '0x',
|
|
312
|
+
// _vs: '0x',
|
|
313
|
+
// recoveryParam: 0,
|
|
314
|
+
// v: 0,
|
|
315
|
+
// yParityAndS: '0x',
|
|
316
|
+
// compact: '0x',
|
|
317
|
+
// };
|
|
318
|
+
// if (isBytesLike(signature)) {
|
|
319
|
+
// const bytes: Uint8Array = arrayify(signature);
|
|
320
|
+
// // Get the r, s and v
|
|
321
|
+
// if (bytes.length === 64) {
|
|
322
|
+
// // EIP-2098; pull the v from the top bit of s and clear it
|
|
323
|
+
// result.v = 27 + (bytes[32] >> 7);
|
|
324
|
+
// bytes[32] &= 0x7f;
|
|
325
|
+
// result.r = hexlify(bytes.slice(0, 32));
|
|
326
|
+
// result.s = hexlify(bytes.slice(32, 64));
|
|
327
|
+
// } else if (bytes.length === 65) {
|
|
328
|
+
// result.r = hexlify(bytes.slice(0, 32));
|
|
329
|
+
// result.s = hexlify(bytes.slice(32, 64));
|
|
330
|
+
// result.v = bytes[64];
|
|
331
|
+
// } else {
|
|
332
|
+
// logger.throwArgumentError(
|
|
333
|
+
// 'invalid signature string',
|
|
334
|
+
// 'signature',
|
|
335
|
+
// signature,
|
|
336
|
+
// );
|
|
337
|
+
// }
|
|
338
|
+
// // Allow a recid to be used as the v
|
|
339
|
+
// if (result.v < 27) {
|
|
340
|
+
// if (result.v === 0 || result.v === 1) {
|
|
341
|
+
// result.v += 27;
|
|
342
|
+
// } else {
|
|
343
|
+
// logger.throwArgumentError(
|
|
344
|
+
// 'signature invalid v byte',
|
|
345
|
+
// 'signature',
|
|
346
|
+
// signature,
|
|
347
|
+
// );
|
|
348
|
+
// }
|
|
349
|
+
// }
|
|
350
|
+
// // Compute recoveryParam from v
|
|
351
|
+
// result.recoveryParam = 1 - (result.v % 2);
|
|
352
|
+
// // Compute _vs from recoveryParam and s
|
|
353
|
+
// if (result.recoveryParam) {
|
|
354
|
+
// bytes[32] |= 0x80;
|
|
355
|
+
// }
|
|
356
|
+
// result._vs = hexlify(bytes.slice(32, 64));
|
|
357
|
+
// } else {
|
|
358
|
+
// result.r = signature.r;
|
|
359
|
+
// result.s = signature.s;
|
|
360
|
+
// result.v = signature.v;
|
|
361
|
+
// result.recoveryParam = signature.recoveryParam;
|
|
362
|
+
// result._vs = signature._vs;
|
|
363
|
+
// // If the _vs is available, use it to populate missing s, v and recoveryParam
|
|
364
|
+
// // and verify non-missing s, v and recoveryParam
|
|
365
|
+
// if (result._vs != null) {
|
|
366
|
+
// const vs = zeroPad(arrayify(result._vs), 32);
|
|
367
|
+
// result._vs = hexlify(vs);
|
|
368
|
+
// // Set or check the recid
|
|
369
|
+
// const recoveryParam = vs[0] >= 128 ? 1 : 0;
|
|
370
|
+
// if (result.recoveryParam == null) {
|
|
371
|
+
// result.recoveryParam = recoveryParam;
|
|
372
|
+
// } else if (result.recoveryParam !== recoveryParam) {
|
|
373
|
+
// logger.throwArgumentError(
|
|
374
|
+
// 'signature recoveryParam mismatch _vs',
|
|
375
|
+
// 'signature',
|
|
376
|
+
// signature,
|
|
377
|
+
// );
|
|
378
|
+
// }
|
|
379
|
+
// // Set or check the s
|
|
380
|
+
// vs[0] &= 0x7f;
|
|
381
|
+
// const s = hexlify(vs);
|
|
382
|
+
// if (result.s == null) {
|
|
383
|
+
// result.s = s;
|
|
384
|
+
// } else if (result.s !== s) {
|
|
385
|
+
// logger.throwArgumentError(
|
|
386
|
+
// 'signature v mismatch _vs',
|
|
387
|
+
// 'signature',
|
|
388
|
+
// signature,
|
|
389
|
+
// );
|
|
390
|
+
// }
|
|
391
|
+
// }
|
|
392
|
+
// // Use recid and v to populate each other
|
|
393
|
+
// if (result.recoveryParam == null) {
|
|
394
|
+
// if (result.v == null) {
|
|
395
|
+
// logger.throwArgumentError(
|
|
396
|
+
// 'signature missing v and recoveryParam',
|
|
397
|
+
// 'signature',
|
|
398
|
+
// signature,
|
|
399
|
+
// );
|
|
400
|
+
// } else if (result.v === 0 || result.v === 1) {
|
|
401
|
+
// result.recoveryParam = result.v;
|
|
402
|
+
// } else {
|
|
403
|
+
// result.recoveryParam = 1 - (result.v % 2);
|
|
404
|
+
// }
|
|
405
|
+
// } else {
|
|
406
|
+
// if (result.v == null) {
|
|
407
|
+
// result.v = 27 + result.recoveryParam;
|
|
408
|
+
// } else {
|
|
409
|
+
// const recId =
|
|
410
|
+
// result.v === 0 || result.v === 1 ? result.v : 1 - (result.v % 2);
|
|
411
|
+
// if (result.recoveryParam !== recId) {
|
|
412
|
+
// logger.throwArgumentError(
|
|
413
|
+
// 'signature recoveryParam mismatch v',
|
|
414
|
+
// 'signature',
|
|
415
|
+
// signature,
|
|
416
|
+
// );
|
|
417
|
+
// }
|
|
418
|
+
// }
|
|
419
|
+
// }
|
|
420
|
+
// if (result.r == null || !isHexString(result.r)) {
|
|
421
|
+
// logger.throwArgumentError(
|
|
422
|
+
// 'signature missing or invalid r',
|
|
423
|
+
// 'signature',
|
|
424
|
+
// signature,
|
|
425
|
+
// );
|
|
426
|
+
// } else {
|
|
427
|
+
// result.r = hexZeroPad(result.r, 32);
|
|
428
|
+
// }
|
|
429
|
+
// if (result.s == null || !isHexString(result.s)) {
|
|
430
|
+
// logger.throwArgumentError(
|
|
431
|
+
// 'signature missing or invalid s',
|
|
432
|
+
// 'signature',
|
|
433
|
+
// signature,
|
|
434
|
+
// );
|
|
435
|
+
// } else {
|
|
436
|
+
// result.s = hexZeroPad(result.s, 32);
|
|
437
|
+
// }
|
|
438
|
+
// const vs = arrayify(result.s);
|
|
439
|
+
// if (vs[0] >= 128) {
|
|
440
|
+
// logger.throwArgumentError(
|
|
441
|
+
// 'signature s out of range',
|
|
442
|
+
// 'signature',
|
|
443
|
+
// signature,
|
|
444
|
+
// );
|
|
445
|
+
// }
|
|
446
|
+
// if (result.recoveryParam) {
|
|
447
|
+
// vs[0] |= 0x80;
|
|
448
|
+
// }
|
|
449
|
+
// const _vs = hexlify(vs);
|
|
450
|
+
// if (result._vs) {
|
|
451
|
+
// if (!isHexString(result._vs)) {
|
|
452
|
+
// logger.throwArgumentError(
|
|
453
|
+
// 'signature invalid _vs',
|
|
454
|
+
// 'signature',
|
|
455
|
+
// signature,
|
|
456
|
+
// );
|
|
457
|
+
// }
|
|
458
|
+
// result._vs = hexZeroPad(result._vs, 32);
|
|
459
|
+
// }
|
|
460
|
+
// // Set or check the _vs
|
|
461
|
+
// if (result._vs == null) {
|
|
462
|
+
// result._vs = _vs;
|
|
463
|
+
// } else if (result._vs !== _vs) {
|
|
464
|
+
// logger.throwArgumentError(
|
|
465
|
+
// 'signature _vs mismatch v and s',
|
|
466
|
+
// 'signature',
|
|
467
|
+
// signature,
|
|
468
|
+
// );
|
|
469
|
+
// }
|
|
470
|
+
// }
|
|
471
|
+
// result.yParityAndS = result._vs;
|
|
472
|
+
// result.compact = result.r + result.yParityAndS.substring(2);
|
|
473
|
+
// return result;
|
|
474
|
+
// }
|
|
475
|
+
// export function joinSignature(signature: SignatureLike): string {
|
|
476
|
+
// signature = splitSignature(signature);
|
|
477
|
+
// return hexlify(
|
|
478
|
+
// concat([
|
|
479
|
+
// signature.r,
|
|
480
|
+
// signature.s,
|
|
481
|
+
// signature.recoveryParam ? '0x1c' : '0x1b',
|
|
482
|
+
// ]),
|
|
483
|
+
// );
|
|
484
|
+
// }
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export declare function pack(types: ReadonlyArray<string>, values: ReadonlyArray<any>): string;
|
|
2
|
+
export declare const hashKeccak256: (data: string) => string;
|
|
3
|
+
/**
|
|
4
|
+
* Hashes data from Solidity using the Keccak256 algorithm.
|
|
5
|
+
*
|
|
6
|
+
* Similar to ["solidityKeccak256" in ethers.js](https://docs.ethers.io/v5/api/utils/hashing/#utils-solidityKeccak256)
|
|
7
|
+
*
|
|
8
|
+
* @param types - Each [Solidity type](https://docs.soliditylang.org/en/v0.8.13/types.html) corresponding to the values passed in. Helps the function parse and pack data properly.
|
|
9
|
+
*
|
|
10
|
+
* @param values - Data to be concatenated (combined) and then hashed.
|
|
11
|
+
*
|
|
12
|
+
* @returns - A Keccak256 hash (hex string) based on the values provided
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```javascript
|
|
16
|
+
* const types = ['string', 'bool', 'uint32'];
|
|
17
|
+
* const values = ['essential-eth is great', true, 14];
|
|
18
|
+
* solidityKeccak256(types, values);
|
|
19
|
+
* // '0xe4d4c8e809faac09d58f468f0aeab9474fe8965d554c6c0f868c433c3fd6acab'
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```javascript
|
|
24
|
+
* const types = ['bytes4', 'uint32[5]'];
|
|
25
|
+
* const values = [[116, 101, 115, 116], [5, 3, 4, 9, 18]];
|
|
26
|
+
* solidityKeccak256(types, values);
|
|
27
|
+
* // '0x038707a887f09355dc545412b058e7ba8f3c74047050c7c5e5e52eec608053d9'
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function solidityKeccak256(types: ReadonlyArray<string>, values: ReadonlyArray<any>): string;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.solidityKeccak256 = exports.hashKeccak256 = exports.pack = void 0;
|
|
4
|
+
const buffer_1 = require("buffer");
|
|
5
|
+
const sha3_1 = require("sha3");
|
|
6
|
+
const logger_1 = require("../logger/logger");
|
|
7
|
+
const tiny_big_1 = require("../shared/tiny-big/tiny-big");
|
|
8
|
+
const bytes_1 = require("./bytes");
|
|
9
|
+
const regexBytes = new RegExp('^bytes([0-9]+)$');
|
|
10
|
+
const regexNumber = new RegExp('^(u?int)([0-9]*)$');
|
|
11
|
+
const regexArray = new RegExp('^(.*)\\[([0-9]*)\\]$');
|
|
12
|
+
const Zeros = '0000000000000000000000000000000000000000000000000000000000000000';
|
|
13
|
+
function _pack(type, value, isArray) {
|
|
14
|
+
switch (type) {
|
|
15
|
+
case 'address':
|
|
16
|
+
if (isArray) {
|
|
17
|
+
return (0, bytes_1.zeroPad)(value, 32);
|
|
18
|
+
}
|
|
19
|
+
return (0, bytes_1.arrayify)(value);
|
|
20
|
+
case 'string':
|
|
21
|
+
return buffer_1.Buffer.from(value);
|
|
22
|
+
case 'bytes':
|
|
23
|
+
return (0, bytes_1.arrayify)(value);
|
|
24
|
+
case 'bool':
|
|
25
|
+
value = value ? '0x01' : '0x00';
|
|
26
|
+
if (isArray) {
|
|
27
|
+
return (0, bytes_1.zeroPad)(value, 32);
|
|
28
|
+
}
|
|
29
|
+
return (0, bytes_1.arrayify)(value);
|
|
30
|
+
}
|
|
31
|
+
let match = type.match(regexNumber);
|
|
32
|
+
if (match) {
|
|
33
|
+
//let signed = (match[1] === "int")
|
|
34
|
+
let size = parseInt(match[2] || '256');
|
|
35
|
+
if ((match[2] && String(size) !== match[2]) ||
|
|
36
|
+
size % 8 !== 0 ||
|
|
37
|
+
size === 0 ||
|
|
38
|
+
size > 256) {
|
|
39
|
+
logger_1.logger.throwArgumentError('invalid number type', 'type', type);
|
|
40
|
+
}
|
|
41
|
+
if (isArray) {
|
|
42
|
+
size = 256;
|
|
43
|
+
}
|
|
44
|
+
value = (0, tiny_big_1.tinyBig)(value).toTwos(size).toNumber();
|
|
45
|
+
const hexValue = (0, bytes_1.hexlify)(value);
|
|
46
|
+
return (0, bytes_1.zeroPad)(hexValue, size / 8);
|
|
47
|
+
}
|
|
48
|
+
match = type.match(regexBytes);
|
|
49
|
+
if (match) {
|
|
50
|
+
const size = parseInt(match[1]);
|
|
51
|
+
if (String(size) !== match[1] || size === 0 || size > 32) {
|
|
52
|
+
logger_1.logger.throwArgumentError('invalid bytes type', 'type', type);
|
|
53
|
+
}
|
|
54
|
+
if ((0, bytes_1.arrayify)(value).byteLength !== size) {
|
|
55
|
+
logger_1.logger.throwArgumentError(`invalid value for ${type}`, 'value', value);
|
|
56
|
+
}
|
|
57
|
+
if (isArray) {
|
|
58
|
+
return (0, bytes_1.arrayify)((value + Zeros).substring(0, 66));
|
|
59
|
+
}
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
match = type.match(regexArray);
|
|
63
|
+
if (match && Array.isArray(value)) {
|
|
64
|
+
const baseType = match[1];
|
|
65
|
+
const count = parseInt(match[2] || String(value.length));
|
|
66
|
+
if (count != value.length) {
|
|
67
|
+
logger_1.logger.throwArgumentError(`invalid array length for ${type}`, 'value', value);
|
|
68
|
+
}
|
|
69
|
+
const result = [];
|
|
70
|
+
value.forEach(function (value) {
|
|
71
|
+
result.push(_pack(baseType, value, true));
|
|
72
|
+
});
|
|
73
|
+
return (0, bytes_1.concat)(result);
|
|
74
|
+
}
|
|
75
|
+
return logger_1.logger.throwArgumentError('invalid type', 'type', type);
|
|
76
|
+
}
|
|
77
|
+
function pack(types, values) {
|
|
78
|
+
if (types.length != values.length) {
|
|
79
|
+
logger_1.logger.throwArgumentError('wrong number of values; expected ${ types.length }', 'values', values);
|
|
80
|
+
}
|
|
81
|
+
const tight = [];
|
|
82
|
+
types.forEach(function (type, index) {
|
|
83
|
+
tight.push(_pack(type, values[index]));
|
|
84
|
+
});
|
|
85
|
+
return (0, bytes_1.hexlify)((0, bytes_1.concat)(tight));
|
|
86
|
+
}
|
|
87
|
+
exports.pack = pack;
|
|
88
|
+
const hashKeccak256 = (data) => {
|
|
89
|
+
const keccak = new sha3_1.Keccak(256);
|
|
90
|
+
const bufferableData = buffer_1.Buffer.from(data.replace(/^0x/, ''), 'hex');
|
|
91
|
+
const addressHash = '0x' + keccak.update(bufferableData).digest('hex');
|
|
92
|
+
return addressHash;
|
|
93
|
+
};
|
|
94
|
+
exports.hashKeccak256 = hashKeccak256;
|
|
95
|
+
/**
|
|
96
|
+
* Hashes data from Solidity using the Keccak256 algorithm.
|
|
97
|
+
*
|
|
98
|
+
* Similar to ["solidityKeccak256" in ethers.js](https://docs.ethers.io/v5/api/utils/hashing/#utils-solidityKeccak256)
|
|
99
|
+
*
|
|
100
|
+
* @param types - Each [Solidity type](https://docs.soliditylang.org/en/v0.8.13/types.html) corresponding to the values passed in. Helps the function parse and pack data properly.
|
|
101
|
+
*
|
|
102
|
+
* @param values - Data to be concatenated (combined) and then hashed.
|
|
103
|
+
*
|
|
104
|
+
* @returns - A Keccak256 hash (hex string) based on the values provided
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```javascript
|
|
108
|
+
* const types = ['string', 'bool', 'uint32'];
|
|
109
|
+
* const values = ['essential-eth is great', true, 14];
|
|
110
|
+
* solidityKeccak256(types, values);
|
|
111
|
+
* // '0xe4d4c8e809faac09d58f468f0aeab9474fe8965d554c6c0f868c433c3fd6acab'
|
|
112
|
+
* ```
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```javascript
|
|
116
|
+
* const types = ['bytes4', 'uint32[5]'];
|
|
117
|
+
* const values = [[116, 101, 115, 116], [5, 3, 4, 9, 18]];
|
|
118
|
+
* solidityKeccak256(types, values);
|
|
119
|
+
* // '0x038707a887f09355dc545412b058e7ba8f3c74047050c7c5e5e52eec608053d9'
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
function solidityKeccak256(types, values) {
|
|
123
|
+
return (0, exports.hashKeccak256)(pack(types, values));
|
|
124
|
+
}
|
|
125
|
+
exports.solidityKeccak256 = solidityKeccak256;
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -8,8 +8,9 @@ import { TransactionResponse } from './types/Transaction.types';
|
|
|
8
8
|
import { etherToGwei } from './utils/ether-to-gwei';
|
|
9
9
|
import { etherToWei } from './utils/ether-to-wei';
|
|
10
10
|
import { gweiToEther } from './utils/gwei-to-ether';
|
|
11
|
-
import { hexZeroPad } from './utils/hex-zero-pad';
|
|
12
11
|
import { isAddress } from './utils/is-address';
|
|
13
12
|
import { toChecksumAddress } from './utils/to-checksum-address';
|
|
14
13
|
import { weiToEther } from './utils/wei-to-ether';
|
|
15
|
-
export
|
|
14
|
+
export * from './utils/bytes';
|
|
15
|
+
export * from './utils/solidity-keccak256';
|
|
16
|
+
export { etherToWei, etherToGwei, isAddress, jsonRpcProvider, JsonRpcProvider, tinyBig, toChecksumAddress, weiToEther, gweiToEther, Contract, TinyBig, BlockResponse, ContractTypes, JSONABI, JSONABIArgument, Network, TransactionResponse, };
|
package/lib/esm/index.js
CHANGED
|
@@ -4,8 +4,9 @@ import { tinyBig, TinyBig } from './shared/tiny-big/tiny-big';
|
|
|
4
4
|
import { etherToGwei } from './utils/ether-to-gwei';
|
|
5
5
|
import { etherToWei } from './utils/ether-to-wei';
|
|
6
6
|
import { gweiToEther } from './utils/gwei-to-ether';
|
|
7
|
-
import { hexZeroPad } from './utils/hex-zero-pad';
|
|
8
7
|
import { isAddress } from './utils/is-address';
|
|
9
8
|
import { toChecksumAddress } from './utils/to-checksum-address';
|
|
10
9
|
import { weiToEther } from './utils/wei-to-ether';
|
|
11
|
-
export
|
|
10
|
+
export * from './utils/bytes';
|
|
11
|
+
export * from './utils/solidity-keccak256';
|
|
12
|
+
export { etherToWei, etherToGwei, isAddress, jsonRpcProvider, JsonRpcProvider, tinyBig, toChecksumAddress, weiToEther, gweiToEther, Contract, TinyBig, };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
declare class Logger {
|
|
2
|
+
private packageVersion;
|
|
3
|
+
constructor();
|
|
4
|
+
throwError(message: string, args: {
|
|
5
|
+
[key: string]: any;
|
|
6
|
+
}): never;
|
|
7
|
+
throwArgumentError(message: string, arg: string, value: any): never;
|
|
8
|
+
checkSafeUint53(value: number, message?: string): void;
|
|
9
|
+
}
|
|
10
|
+
export declare const logger: Logger;
|
|
11
|
+
export {};
|