essential-eth 0.4.11 → 0.5.4-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/classes/Contract.d.ts +3 -2
- package/lib/cjs/classes/Contract.js +3 -2
- package/lib/cjs/classes/utils/clean-block.d.ts +3 -3
- package/lib/cjs/classes/utils/clean-block.js +3 -2
- package/lib/cjs/classes/utils/clean-transaction.d.ts +3 -3
- package/lib/cjs/classes/utils/clean-transaction.js +10 -6
- package/lib/cjs/classes/utils/encode-decode-transaction.d.ts +1 -0
- package/lib/cjs/classes/utils/encode-decode-transaction.js +4 -4
- package/lib/cjs/classes/utils/fetchers.d.ts +1 -1
- package/lib/cjs/index.d.ts +6 -3
- package/lib/cjs/index.js +19 -1
- 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/BaseProvider.d.ts +261 -0
- package/lib/cjs/providers/BaseProvider.js +340 -0
- package/lib/cjs/providers/FallthroughProvider.d.ts +25 -0
- package/lib/cjs/providers/FallthroughProvider.js +65 -0
- package/lib/cjs/providers/JsonRpcProvider.d.ts +7 -33
- package/lib/cjs/providers/JsonRpcProvider.js +11 -93
- package/lib/cjs/providers/test/rpc-urls.d.ts +1 -0
- package/lib/cjs/providers/test/rpc-urls.js +1 -0
- package/lib/cjs/providers/utils/chains-info.d.ts +14 -0
- package/lib/cjs/providers/utils/chains-info.js +42 -0
- package/lib/cjs/shared/tiny-big/tiny-big.d.ts +9 -2
- package/lib/cjs/shared/tiny-big/tiny-big.js +26 -2
- package/lib/cjs/types/Block.types.d.ts +8 -19
- package/lib/cjs/types/Transaction.types.d.ts +23 -14
- package/lib/cjs/utils/bytes.d.ts +171 -0
- package/lib/cjs/utils/bytes.js +564 -0
- package/lib/cjs/utils/solidity-keccak256.d.ts +30 -0
- package/lib/cjs/utils/solidity-keccak256.js +125 -0
- package/lib/esm/classes/Contract.js +1 -1
- package/lib/esm/classes/utils/clean-block.d.ts +2 -2
- package/lib/esm/classes/utils/clean-block.js +2 -1
- package/lib/esm/classes/utils/clean-transaction.d.ts +2 -2
- package/lib/esm/classes/utils/clean-transaction.js +10 -6
- package/lib/esm/classes/utils/encode-decode-transaction.d.ts +1 -0
- package/lib/esm/classes/utils/encode-decode-transaction.js +2 -2
- package/lib/esm/classes/utils/fetchers.d.ts +1 -1
- package/lib/esm/index.d.ts +6 -3
- package/lib/esm/index.js +4 -1
- 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/BaseProvider.d.ts +17 -0
- package/lib/esm/providers/BaseProvider.js +88 -0
- package/lib/esm/providers/FallthroughProvider.d.ts +12 -0
- package/lib/esm/providers/FallthroughProvider.js +41 -0
- package/lib/esm/providers/JsonRpcProvider.d.ts +4 -9
- package/lib/esm/providers/JsonRpcProvider.js +8 -67
- package/lib/esm/providers/test/rpc-urls.d.ts +1 -0
- package/lib/esm/providers/test/rpc-urls.js +1 -0
- package/lib/esm/providers/utils/chains-info.d.ts +14 -0
- package/lib/esm/providers/utils/chains-info.js +42 -0
- package/lib/esm/shared/tiny-big/tiny-big.d.ts +2 -0
- package/lib/esm/shared/tiny-big/tiny-big.js +19 -0
- package/lib/esm/types/Block.types.d.ts +7 -19
- package/lib/esm/types/Transaction.types.d.ts +22 -14
- 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 +19 -19
- package/readme.md +251 -61
|
@@ -0,0 +1,564 @@
|
|
|
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
|
+
function isHexable(value) {
|
|
7
|
+
return !!value.toHexString;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Returns true if and only if value is a valid [Bytes](#bytes) or DataHexString
|
|
11
|
+
* * Same as [`ethers.utils.isBytesLike`](https://docs.ethers.io/v5/api/utils/bytes/#utils-isBytesLike)
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```js
|
|
15
|
+
* isBytesLike([1,2,3]);
|
|
16
|
+
* // true
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```js
|
|
21
|
+
* isBytesLike(false);
|
|
22
|
+
* // false
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```js
|
|
27
|
+
* isBytesLike(new Uint8Array(1));
|
|
28
|
+
* // true
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
function isBytesLike(value) {
|
|
32
|
+
return (isHexString(value) && !(value.length % 2)) || isBytes(value);
|
|
33
|
+
}
|
|
34
|
+
exports.isBytesLike = isBytesLike;
|
|
35
|
+
function isInteger(value) {
|
|
36
|
+
return typeof value === 'number' && value == value && value % 1 === 0;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Returns true if and only if value is a valid [Bytes](#bytes)
|
|
40
|
+
* * Same as [`ethers.utils.isBytes`](https://docs.ethers.io/v5/api/utils/bytes/#utils-isBytes)
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```js
|
|
44
|
+
* isBytes([1,2,3]);
|
|
45
|
+
* // true
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```js
|
|
50
|
+
* isBytes(false);
|
|
51
|
+
* // false
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```js
|
|
56
|
+
* isBytes(new Uint8Array(1));
|
|
57
|
+
* // true
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
function isBytes(value) {
|
|
61
|
+
if (value == null) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
if (value.constructor === Uint8Array) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
if (typeof value === 'string') {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
if (!isInteger(value.length) || value.length < 0) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
for (let i = 0; i < value.length; i++) {
|
|
74
|
+
const v = value[i];
|
|
75
|
+
if (!isInteger(v) || v < 0 || v >= 256) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
exports.isBytes = isBytes;
|
|
82
|
+
/**
|
|
83
|
+
* Converts DataHexStringOrArrayish to a Uint8Array
|
|
84
|
+
* * Same as [`ethers.utils.arrayify`](https://docs.ethers.io/v5/api/utils/bytes/#utils-arrayify)
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```js
|
|
88
|
+
* arrayify(1);
|
|
89
|
+
* // Uint8Array(1) [ 1 ]
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```js
|
|
94
|
+
* arrayify(0x1234);
|
|
95
|
+
* // Uint8Array(2) [ 18, 52 ]
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```js
|
|
100
|
+
* arrayify('0x1', { hexPad: 'right' });
|
|
101
|
+
* // Uint8Array(1) [ 16 ]
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
function arrayify(value, options) {
|
|
105
|
+
if (!options) {
|
|
106
|
+
options = {};
|
|
107
|
+
}
|
|
108
|
+
if (typeof value === 'number') {
|
|
109
|
+
logger_1.logger.checkSafeUint53(value, 'invalid arrayify value');
|
|
110
|
+
const result = [];
|
|
111
|
+
while (value) {
|
|
112
|
+
result.unshift(value & 0xff);
|
|
113
|
+
value = parseInt(String(value / 256));
|
|
114
|
+
}
|
|
115
|
+
if (result.length === 0) {
|
|
116
|
+
result.push(0);
|
|
117
|
+
}
|
|
118
|
+
return new Uint8Array(result);
|
|
119
|
+
}
|
|
120
|
+
if (options.allowMissingPrefix &&
|
|
121
|
+
typeof value === 'string' &&
|
|
122
|
+
value.substring(0, 2) !== '0x') {
|
|
123
|
+
value = '0x' + value;
|
|
124
|
+
}
|
|
125
|
+
if (isHexable(value)) {
|
|
126
|
+
value = value.toHexString();
|
|
127
|
+
}
|
|
128
|
+
if (isHexString(value)) {
|
|
129
|
+
let hex = value.substring(2);
|
|
130
|
+
if (hex.length % 2) {
|
|
131
|
+
if (options.hexPad === 'left') {
|
|
132
|
+
hex = '0' + hex;
|
|
133
|
+
}
|
|
134
|
+
else if (options.hexPad === 'right') {
|
|
135
|
+
hex += '0';
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
logger_1.logger.throwArgumentError('hex data is odd-length', 'value', value);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const result = [];
|
|
142
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
143
|
+
result.push(parseInt(hex.substring(i, i + 2), 16));
|
|
144
|
+
}
|
|
145
|
+
return new Uint8Array(result);
|
|
146
|
+
}
|
|
147
|
+
if (isBytes(value)) {
|
|
148
|
+
return new Uint8Array(value);
|
|
149
|
+
}
|
|
150
|
+
return logger_1.logger.throwArgumentError('invalid arrayify value', 'value', value);
|
|
151
|
+
}
|
|
152
|
+
exports.arrayify = arrayify;
|
|
153
|
+
/**
|
|
154
|
+
* Concatenates all the BytesLike in arrayOfBytesLike into a single Uint8Array.
|
|
155
|
+
* * Same as [`ethers.utils.concat`](https://docs.ethers.io/v5/api/utils/bytes/#utils-concat)
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```js
|
|
159
|
+
* concat([0, 1]);
|
|
160
|
+
* // Uint8Array(2) [ 0, 1 ]
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
function concat(arrayOfBytesLike) {
|
|
164
|
+
const objects = arrayOfBytesLike.map((item) => arrayify(item));
|
|
165
|
+
const length = objects.reduce((accum, item) => accum + item.length, 0);
|
|
166
|
+
const result = new Uint8Array(length);
|
|
167
|
+
objects.reduce((offset, object) => {
|
|
168
|
+
result.set(object, offset);
|
|
169
|
+
return offset + object.length;
|
|
170
|
+
}, 0);
|
|
171
|
+
return result;
|
|
172
|
+
}
|
|
173
|
+
exports.concat = concat;
|
|
174
|
+
function stripZeros(value) {
|
|
175
|
+
let result = arrayify(value);
|
|
176
|
+
if (result.length === 0) {
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
// Find the first non-zero entry
|
|
180
|
+
let start = 0;
|
|
181
|
+
while (start < result.length && result[start] === 0) {
|
|
182
|
+
start++;
|
|
183
|
+
}
|
|
184
|
+
// If we started with zeros, strip them
|
|
185
|
+
if (start) {
|
|
186
|
+
result = result.slice(start);
|
|
187
|
+
}
|
|
188
|
+
return result;
|
|
189
|
+
}
|
|
190
|
+
exports.stripZeros = stripZeros;
|
|
191
|
+
function zeroPad(value, length) {
|
|
192
|
+
value = arrayify(value);
|
|
193
|
+
if (value.length > length) {
|
|
194
|
+
logger_1.logger.throwArgumentError('value out of range', 'value', value);
|
|
195
|
+
}
|
|
196
|
+
const result = new Uint8Array(length);
|
|
197
|
+
result.set(value, length - value.length);
|
|
198
|
+
return result;
|
|
199
|
+
}
|
|
200
|
+
exports.zeroPad = zeroPad;
|
|
201
|
+
/**
|
|
202
|
+
* Returns true if and only if object is a valid hex string.
|
|
203
|
+
* If length is specified and object is not a valid DataHexString of length bytes, an InvalidArgument error is thrown.
|
|
204
|
+
* * Same as [`ethers.utils.isHexString`](https://docs.ethers.io/v5/api/utils/bytes/#utils-isHexString)
|
|
205
|
+
*/
|
|
206
|
+
function isHexString(value, length) {
|
|
207
|
+
if (typeof value !== 'string' || !value.match(/^0x[0-9A-Fa-f]*$/)) {
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
if (length && value.length !== 2 + 2 * length) {
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
exports.isHexString = isHexString;
|
|
216
|
+
const HexCharacters = '0123456789abcdef';
|
|
217
|
+
/**
|
|
218
|
+
* @example
|
|
219
|
+
* ```js
|
|
220
|
+
* hexlify(4);
|
|
221
|
+
* // '0x04'
|
|
222
|
+
*
|
|
223
|
+
* hexlify(14);
|
|
224
|
+
* // '0x0e'
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
227
|
+
function hexlify(value, options) {
|
|
228
|
+
if (!options) {
|
|
229
|
+
options = {};
|
|
230
|
+
}
|
|
231
|
+
if (typeof value === 'number') {
|
|
232
|
+
logger_1.logger.checkSafeUint53(value, 'invalid hexlify value');
|
|
233
|
+
let hex = '';
|
|
234
|
+
while (value) {
|
|
235
|
+
hex = HexCharacters[value & 0xf] + hex;
|
|
236
|
+
value = Math.floor(value / 16);
|
|
237
|
+
}
|
|
238
|
+
if (hex.length) {
|
|
239
|
+
if (hex.length % 2) {
|
|
240
|
+
hex = '0' + hex;
|
|
241
|
+
}
|
|
242
|
+
return '0x' + hex;
|
|
243
|
+
}
|
|
244
|
+
return '0x00';
|
|
245
|
+
}
|
|
246
|
+
if (typeof value === 'bigint') {
|
|
247
|
+
value = value.toString(16);
|
|
248
|
+
if (value.length % 2) {
|
|
249
|
+
return '0x0' + value;
|
|
250
|
+
}
|
|
251
|
+
return '0x' + value;
|
|
252
|
+
}
|
|
253
|
+
if (options.allowMissingPrefix &&
|
|
254
|
+
typeof value === 'string' &&
|
|
255
|
+
value.substring(0, 2) !== '0x') {
|
|
256
|
+
value = '0x' + value;
|
|
257
|
+
}
|
|
258
|
+
if (isHexable(value)) {
|
|
259
|
+
return value.toHexString();
|
|
260
|
+
}
|
|
261
|
+
if (isHexString(value)) {
|
|
262
|
+
if (value.length % 2) {
|
|
263
|
+
if (options.hexPad === 'left') {
|
|
264
|
+
value = '0x0' + value.substring(2);
|
|
265
|
+
}
|
|
266
|
+
else if (options.hexPad === 'right') {
|
|
267
|
+
value += '0';
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
logger_1.logger.throwArgumentError('hex data is odd-length', 'value', value);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return value.toLowerCase();
|
|
274
|
+
}
|
|
275
|
+
if (isBytes(value)) {
|
|
276
|
+
let result = '0x';
|
|
277
|
+
for (let i = 0; i < value.length; i++) {
|
|
278
|
+
const v = value[i];
|
|
279
|
+
result += HexCharacters[(v & 0xf0) >> 4] + HexCharacters[v & 0x0f];
|
|
280
|
+
}
|
|
281
|
+
return result;
|
|
282
|
+
}
|
|
283
|
+
return logger_1.logger.throwArgumentError('invalid hexlify value', 'value', value);
|
|
284
|
+
}
|
|
285
|
+
exports.hexlify = hexlify;
|
|
286
|
+
function hexDataLength(data) {
|
|
287
|
+
if (typeof data !== 'string') {
|
|
288
|
+
data = hexlify(data);
|
|
289
|
+
}
|
|
290
|
+
else if (!isHexString(data) || data.length % 2) {
|
|
291
|
+
return null;
|
|
292
|
+
}
|
|
293
|
+
return (data.length - 2) / 2;
|
|
294
|
+
}
|
|
295
|
+
exports.hexDataLength = hexDataLength;
|
|
296
|
+
function hexDataSlice(data, offset, endOffset) {
|
|
297
|
+
if (typeof data !== 'string') {
|
|
298
|
+
data = hexlify(data);
|
|
299
|
+
}
|
|
300
|
+
else if (!isHexString(data) || data.length % 2) {
|
|
301
|
+
logger_1.logger.throwArgumentError('invalid hexData', 'value', data);
|
|
302
|
+
}
|
|
303
|
+
offset = 2 + 2 * offset;
|
|
304
|
+
if (endOffset != null) {
|
|
305
|
+
return '0x' + data.substring(offset, 2 + 2 * endOffset);
|
|
306
|
+
}
|
|
307
|
+
return '0x' + data.substring(offset);
|
|
308
|
+
}
|
|
309
|
+
exports.hexDataSlice = hexDataSlice;
|
|
310
|
+
function hexConcat(items) {
|
|
311
|
+
let result = '0x';
|
|
312
|
+
items.forEach((item) => {
|
|
313
|
+
result += hexlify(item).substring(2);
|
|
314
|
+
});
|
|
315
|
+
return result;
|
|
316
|
+
}
|
|
317
|
+
exports.hexConcat = hexConcat;
|
|
318
|
+
function hexValue(value) {
|
|
319
|
+
const trimmed = hexStripZeros(hexlify(value, { hexPad: 'left' }));
|
|
320
|
+
if (trimmed === '0x') {
|
|
321
|
+
return '0x0';
|
|
322
|
+
}
|
|
323
|
+
return trimmed;
|
|
324
|
+
}
|
|
325
|
+
exports.hexValue = hexValue;
|
|
326
|
+
function hexStripZeros(value) {
|
|
327
|
+
if (typeof value !== 'string') {
|
|
328
|
+
value = hexlify(value);
|
|
329
|
+
}
|
|
330
|
+
if (!isHexString(value)) {
|
|
331
|
+
logger_1.logger.throwArgumentError('invalid hex string', 'value', value);
|
|
332
|
+
}
|
|
333
|
+
value = value.substring(2);
|
|
334
|
+
let offset = 0;
|
|
335
|
+
while (offset < value.length && value[offset] === '0') {
|
|
336
|
+
offset++;
|
|
337
|
+
}
|
|
338
|
+
return '0x' + value.substring(offset);
|
|
339
|
+
}
|
|
340
|
+
exports.hexStripZeros = hexStripZeros;
|
|
341
|
+
/**
|
|
342
|
+
* Returns a hex string padded to a specified length of bytes.
|
|
343
|
+
*
|
|
344
|
+
* Similar to ["hexZeroPad" in ethers.js](https://docs.ethers.io/v5/api/utils/bytes/#utils-hexZeroPad)
|
|
345
|
+
*
|
|
346
|
+
* 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.
|
|
347
|
+
*
|
|
348
|
+
* @param hexValue - A hex-string, hex-number, or decimal number (auto-converts to base-16) to be padded
|
|
349
|
+
* @param length - The final length in bytes
|
|
350
|
+
*
|
|
351
|
+
* @throws - If the value is not a hex string or number
|
|
352
|
+
* @throws - If the value is longer than the length
|
|
353
|
+
*
|
|
354
|
+
* @example
|
|
355
|
+
* ```javascript
|
|
356
|
+
* hexZeroPad('0x60', 2);
|
|
357
|
+
* // '0x0060'
|
|
358
|
+
* ```
|
|
359
|
+
*
|
|
360
|
+
* @example
|
|
361
|
+
* ```javascript
|
|
362
|
+
* hexZeroPad(0x60, 3);
|
|
363
|
+
* // '0x000060'
|
|
364
|
+
* ```
|
|
365
|
+
*
|
|
366
|
+
* @example
|
|
367
|
+
* ```javascript
|
|
368
|
+
* hexZeroPad('12345', 1);
|
|
369
|
+
* // Throws
|
|
370
|
+
* ```
|
|
371
|
+
*/
|
|
372
|
+
function hexZeroPad(value, length) {
|
|
373
|
+
if (typeof value !== 'string') {
|
|
374
|
+
value = hexlify(value);
|
|
375
|
+
}
|
|
376
|
+
else if (!isHexString(value)) {
|
|
377
|
+
logger_1.logger.throwArgumentError('invalid hex string', 'value', value);
|
|
378
|
+
}
|
|
379
|
+
if (value.length > 2 * length + 2) {
|
|
380
|
+
logger_1.logger.throwError('value out of range', { value, length });
|
|
381
|
+
}
|
|
382
|
+
while (value.length < 2 * length + 2) {
|
|
383
|
+
value = '0x0' + value.substring(2);
|
|
384
|
+
}
|
|
385
|
+
return value;
|
|
386
|
+
}
|
|
387
|
+
exports.hexZeroPad = hexZeroPad;
|
|
388
|
+
// export function splitSignature(signature: SignatureLike): Signature {
|
|
389
|
+
// const result: Signature = {
|
|
390
|
+
// r: '0x',
|
|
391
|
+
// s: '0x',
|
|
392
|
+
// _vs: '0x',
|
|
393
|
+
// recoveryParam: 0,
|
|
394
|
+
// v: 0,
|
|
395
|
+
// yParityAndS: '0x',
|
|
396
|
+
// compact: '0x',
|
|
397
|
+
// };
|
|
398
|
+
// if (isBytesLike(signature)) {
|
|
399
|
+
// const bytes: Uint8Array = arrayify(signature);
|
|
400
|
+
// // Get the r, s and v
|
|
401
|
+
// if (bytes.length === 64) {
|
|
402
|
+
// // EIP-2098; pull the v from the top bit of s and clear it
|
|
403
|
+
// result.v = 27 + (bytes[32] >> 7);
|
|
404
|
+
// bytes[32] &= 0x7f;
|
|
405
|
+
// result.r = hexlify(bytes.slice(0, 32));
|
|
406
|
+
// result.s = hexlify(bytes.slice(32, 64));
|
|
407
|
+
// } else if (bytes.length === 65) {
|
|
408
|
+
// result.r = hexlify(bytes.slice(0, 32));
|
|
409
|
+
// result.s = hexlify(bytes.slice(32, 64));
|
|
410
|
+
// result.v = bytes[64];
|
|
411
|
+
// } else {
|
|
412
|
+
// logger.throwArgumentError(
|
|
413
|
+
// 'invalid signature string',
|
|
414
|
+
// 'signature',
|
|
415
|
+
// signature,
|
|
416
|
+
// );
|
|
417
|
+
// }
|
|
418
|
+
// // Allow a recid to be used as the v
|
|
419
|
+
// if (result.v < 27) {
|
|
420
|
+
// if (result.v === 0 || result.v === 1) {
|
|
421
|
+
// result.v += 27;
|
|
422
|
+
// } else {
|
|
423
|
+
// logger.throwArgumentError(
|
|
424
|
+
// 'signature invalid v byte',
|
|
425
|
+
// 'signature',
|
|
426
|
+
// signature,
|
|
427
|
+
// );
|
|
428
|
+
// }
|
|
429
|
+
// }
|
|
430
|
+
// // Compute recoveryParam from v
|
|
431
|
+
// result.recoveryParam = 1 - (result.v % 2);
|
|
432
|
+
// // Compute _vs from recoveryParam and s
|
|
433
|
+
// if (result.recoveryParam) {
|
|
434
|
+
// bytes[32] |= 0x80;
|
|
435
|
+
// }
|
|
436
|
+
// result._vs = hexlify(bytes.slice(32, 64));
|
|
437
|
+
// } else {
|
|
438
|
+
// result.r = signature.r;
|
|
439
|
+
// result.s = signature.s;
|
|
440
|
+
// result.v = signature.v;
|
|
441
|
+
// result.recoveryParam = signature.recoveryParam;
|
|
442
|
+
// result._vs = signature._vs;
|
|
443
|
+
// // If the _vs is available, use it to populate missing s, v and recoveryParam
|
|
444
|
+
// // and verify non-missing s, v and recoveryParam
|
|
445
|
+
// if (result._vs != null) {
|
|
446
|
+
// const vs = zeroPad(arrayify(result._vs), 32);
|
|
447
|
+
// result._vs = hexlify(vs);
|
|
448
|
+
// // Set or check the recid
|
|
449
|
+
// const recoveryParam = vs[0] >= 128 ? 1 : 0;
|
|
450
|
+
// if (result.recoveryParam == null) {
|
|
451
|
+
// result.recoveryParam = recoveryParam;
|
|
452
|
+
// } else if (result.recoveryParam !== recoveryParam) {
|
|
453
|
+
// logger.throwArgumentError(
|
|
454
|
+
// 'signature recoveryParam mismatch _vs',
|
|
455
|
+
// 'signature',
|
|
456
|
+
// signature,
|
|
457
|
+
// );
|
|
458
|
+
// }
|
|
459
|
+
// // Set or check the s
|
|
460
|
+
// vs[0] &= 0x7f;
|
|
461
|
+
// const s = hexlify(vs);
|
|
462
|
+
// if (result.s == null) {
|
|
463
|
+
// result.s = s;
|
|
464
|
+
// } else if (result.s !== s) {
|
|
465
|
+
// logger.throwArgumentError(
|
|
466
|
+
// 'signature v mismatch _vs',
|
|
467
|
+
// 'signature',
|
|
468
|
+
// signature,
|
|
469
|
+
// );
|
|
470
|
+
// }
|
|
471
|
+
// }
|
|
472
|
+
// // Use recid and v to populate each other
|
|
473
|
+
// if (result.recoveryParam == null) {
|
|
474
|
+
// if (result.v == null) {
|
|
475
|
+
// logger.throwArgumentError(
|
|
476
|
+
// 'signature missing v and recoveryParam',
|
|
477
|
+
// 'signature',
|
|
478
|
+
// signature,
|
|
479
|
+
// );
|
|
480
|
+
// } else if (result.v === 0 || result.v === 1) {
|
|
481
|
+
// result.recoveryParam = result.v;
|
|
482
|
+
// } else {
|
|
483
|
+
// result.recoveryParam = 1 - (result.v % 2);
|
|
484
|
+
// }
|
|
485
|
+
// } else {
|
|
486
|
+
// if (result.v == null) {
|
|
487
|
+
// result.v = 27 + result.recoveryParam;
|
|
488
|
+
// } else {
|
|
489
|
+
// const recId =
|
|
490
|
+
// result.v === 0 || result.v === 1 ? result.v : 1 - (result.v % 2);
|
|
491
|
+
// if (result.recoveryParam !== recId) {
|
|
492
|
+
// logger.throwArgumentError(
|
|
493
|
+
// 'signature recoveryParam mismatch v',
|
|
494
|
+
// 'signature',
|
|
495
|
+
// signature,
|
|
496
|
+
// );
|
|
497
|
+
// }
|
|
498
|
+
// }
|
|
499
|
+
// }
|
|
500
|
+
// if (result.r == null || !isHexString(result.r)) {
|
|
501
|
+
// logger.throwArgumentError(
|
|
502
|
+
// 'signature missing or invalid r',
|
|
503
|
+
// 'signature',
|
|
504
|
+
// signature,
|
|
505
|
+
// );
|
|
506
|
+
// } else {
|
|
507
|
+
// result.r = hexZeroPad(result.r, 32);
|
|
508
|
+
// }
|
|
509
|
+
// if (result.s == null || !isHexString(result.s)) {
|
|
510
|
+
// logger.throwArgumentError(
|
|
511
|
+
// 'signature missing or invalid s',
|
|
512
|
+
// 'signature',
|
|
513
|
+
// signature,
|
|
514
|
+
// );
|
|
515
|
+
// } else {
|
|
516
|
+
// result.s = hexZeroPad(result.s, 32);
|
|
517
|
+
// }
|
|
518
|
+
// const vs = arrayify(result.s);
|
|
519
|
+
// if (vs[0] >= 128) {
|
|
520
|
+
// logger.throwArgumentError(
|
|
521
|
+
// 'signature s out of range',
|
|
522
|
+
// 'signature',
|
|
523
|
+
// signature,
|
|
524
|
+
// );
|
|
525
|
+
// }
|
|
526
|
+
// if (result.recoveryParam) {
|
|
527
|
+
// vs[0] |= 0x80;
|
|
528
|
+
// }
|
|
529
|
+
// const _vs = hexlify(vs);
|
|
530
|
+
// if (result._vs) {
|
|
531
|
+
// if (!isHexString(result._vs)) {
|
|
532
|
+
// logger.throwArgumentError(
|
|
533
|
+
// 'signature invalid _vs',
|
|
534
|
+
// 'signature',
|
|
535
|
+
// signature,
|
|
536
|
+
// );
|
|
537
|
+
// }
|
|
538
|
+
// result._vs = hexZeroPad(result._vs, 32);
|
|
539
|
+
// }
|
|
540
|
+
// // Set or check the _vs
|
|
541
|
+
// if (result._vs == null) {
|
|
542
|
+
// result._vs = _vs;
|
|
543
|
+
// } else if (result._vs !== _vs) {
|
|
544
|
+
// logger.throwArgumentError(
|
|
545
|
+
// 'signature _vs mismatch v and s',
|
|
546
|
+
// 'signature',
|
|
547
|
+
// signature,
|
|
548
|
+
// );
|
|
549
|
+
// }
|
|
550
|
+
// }
|
|
551
|
+
// result.yParityAndS = result._vs;
|
|
552
|
+
// result.compact = result.r + result.yParityAndS.substring(2);
|
|
553
|
+
// return result;
|
|
554
|
+
// }
|
|
555
|
+
// export function joinSignature(signature: SignatureLike): string {
|
|
556
|
+
// signature = splitSignature(signature);
|
|
557
|
+
// return hexlify(
|
|
558
|
+
// concat([
|
|
559
|
+
// signature.r,
|
|
560
|
+
// signature.s,
|
|
561
|
+
// signature.recoveryParam ? '0x1c' : '0x1b',
|
|
562
|
+
// ]),
|
|
563
|
+
// );
|
|
564
|
+
// }
|
|
@@ -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;
|