essential-eth 0.9.1-next.0 → 0.9.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/dist/cjs/classes/Contract.js +66 -311
- package/dist/cjs/classes/test/Contract/crv-abi.js +256 -252
- package/dist/cjs/classes/test/Contract/ens-abi.js +212 -497
- package/dist/cjs/classes/test/Contract/fei-abi.js +284 -605
- package/dist/cjs/classes/test/Contract/foo-abi.js +21 -44
- package/dist/cjs/classes/test/Contract/uniswap-abi.js +68 -64
- package/dist/cjs/classes/utils/clean-block.js +30 -52
- package/dist/cjs/classes/utils/clean-log.js +20 -42
- package/dist/cjs/classes/utils/clean-transaction-receipt.js +30 -51
- package/dist/cjs/classes/utils/clean-transaction.js +30 -52
- package/dist/cjs/classes/utils/encode-decode-transaction.js +107 -148
- package/dist/cjs/classes/utils/fetchers.js +44 -166
- package/dist/cjs/classes/utils/hex-to-decimal.js +6 -1
- package/dist/cjs/classes/utils/prepare-transaction.js +36 -59
- package/dist/cjs/index.js +56 -25
- package/dist/cjs/index.umd.js +1 -1
- package/dist/cjs/index.umd.js.map +1 -1
- package/dist/cjs/logger/logger.js +29 -123
- package/dist/cjs/logger/package-version.d.ts +1 -1
- package/dist/cjs/logger/package-version.js +4 -1
- package/dist/cjs/providers/AlchemyProvider.js +9 -82
- package/dist/cjs/providers/BaseProvider.js +520 -958
- package/dist/cjs/providers/FallthroughProvider.js +47 -156
- package/dist/cjs/providers/JsonRpcProvider.js +28 -114
- package/dist/cjs/providers/test/json-rpc-provider/get-logs/mocks.js +104 -101
- package/dist/cjs/providers/test/mock-of.js +6 -3
- package/dist/cjs/providers/test/rpc-urls.js +23 -15
- package/dist/cjs/providers/utils/chains-info.js +3 -1
- package/dist/cjs/shared/tiny-big/helpers.js +42 -93
- package/dist/cjs/shared/tiny-big/tiny-big.js +81 -210
- package/dist/cjs/shared/validate-type.js +7 -7
- package/dist/cjs/types/Block.types.js +2 -1
- package/dist/cjs/types/Contract.types.js +2 -1
- package/dist/cjs/types/FeeData.types.js +2 -1
- package/dist/cjs/types/Filter.types.js +2 -1
- package/dist/cjs/types/Network.types.js +2 -3
- package/dist/cjs/types/Transaction.types.js +2 -1
- package/dist/cjs/utils/bytes.js +318 -286
- package/dist/cjs/utils/compute-address.js +15 -8
- package/dist/cjs/utils/compute-public-key.js +10 -5
- package/dist/cjs/utils/ether-to-gwei.js +11 -10
- package/dist/cjs/utils/ether-to-wei.js +11 -10
- package/dist/cjs/utils/gwei-to-ether.js +11 -10
- package/dist/cjs/utils/hash-message.js +16 -11
- package/dist/cjs/utils/is-address.js +12 -8
- package/dist/cjs/utils/keccak256.js +14 -8
- package/dist/cjs/utils/solidity-keccak256.js +60 -49
- package/dist/cjs/utils/split-signature.js +73 -55
- package/dist/cjs/utils/to-checksum-address.js +20 -15
- package/dist/cjs/utils/to-utf8-bytes.js +6 -1
- package/dist/cjs/utils/wei-to-ether.js +15 -13
- package/dist/esm/logger/package-version.d.ts +1 -1
- package/dist/esm/logger/package-version.js +1 -1
- package/package.json +2 -2
- package/readme.md +1 -1
- package/dist/cjs/classes/test/Contract/crv.test.js +0 -497
- package/dist/cjs/classes/test/Contract/ens.test.js +0 -160
- package/dist/cjs/classes/test/Contract/fei.test.js +0 -224
- package/dist/cjs/classes/test/Contract/foo.test.js +0 -33
- package/dist/cjs/classes/test/Contract/uni.test.js +0 -289
- package/dist/cjs/providers/test/fallthrough-provider/get-gas-price.test.js +0 -228
- package/dist/cjs/providers/test/get-transaction-count.test.js +0 -239
- package/dist/cjs/providers/test/json-rpc-provider/call.test.js +0 -375
- package/dist/cjs/providers/test/json-rpc-provider/estimate-gas.test.js +0 -217
- package/dist/cjs/providers/test/json-rpc-provider/get-balance.test.js +0 -223
- package/dist/cjs/providers/test/json-rpc-provider/get-block-number.test.js +0 -163
- package/dist/cjs/providers/test/json-rpc-provider/get-block.test.js +0 -414
- package/dist/cjs/providers/test/json-rpc-provider/get-code.test.js +0 -282
- package/dist/cjs/providers/test/json-rpc-provider/get-fee-data.test.js +0 -187
- package/dist/cjs/providers/test/json-rpc-provider/get-gas-price.test.js +0 -167
- package/dist/cjs/providers/test/json-rpc-provider/get-logs/get-logs.test.js +0 -219
- package/dist/cjs/providers/test/json-rpc-provider/get-network.test.js +0 -232
- package/dist/cjs/providers/test/json-rpc-provider/get-transaction-receipt.test.js +0 -280
- package/dist/cjs/providers/test/json-rpc-provider/get-transaction.test.js +0 -266
- package/dist/cjs/providers/test/test-alchemy-provider.test.js +0 -151
- package/dist/cjs/shared/tiny-big/helpers.test.js +0 -29
- package/dist/cjs/shared/tiny-big/tiny-big.test.js +0 -34
- package/dist/cjs/utils/tests/bytes/arrayify.test.js +0 -64
- package/dist/cjs/utils/tests/bytes/concat.test.js +0 -17
- package/dist/cjs/utils/tests/bytes/hex-concat.test.js +0 -65
- package/dist/cjs/utils/tests/bytes/hex-data-length.test.js +0 -66
- package/dist/cjs/utils/tests/bytes/hex-data-slice.test.js +0 -51
- package/dist/cjs/utils/tests/bytes/hex-strip-zeros.test.js +0 -75
- package/dist/cjs/utils/tests/bytes/hex-value.test.js +0 -141
- package/dist/cjs/utils/tests/bytes/hex-zero-pad.test.js +0 -100
- package/dist/cjs/utils/tests/bytes/hexlify.test.js +0 -78
- package/dist/cjs/utils/tests/bytes/is-bytes-like.test.js +0 -68
- package/dist/cjs/utils/tests/bytes/is-bytes.test.js +0 -76
- package/dist/cjs/utils/tests/bytes/is-hex-string.test.js +0 -97
- package/dist/cjs/utils/tests/bytes/strip-zeros.test.js +0 -106
- package/dist/cjs/utils/tests/bytes/zero-pad.test.js +0 -144
- package/dist/cjs/utils/tests/compute-address.test.js +0 -49
- package/dist/cjs/utils/tests/compute-public-key.test.js +0 -26
- package/dist/cjs/utils/tests/ether-to-gwei.test.js +0 -26
- package/dist/cjs/utils/tests/ether-to-wei.test.js +0 -36
- package/dist/cjs/utils/tests/gwei-to-ether.test.js +0 -28
- package/dist/cjs/utils/tests/hash-message.test.js +0 -32
- package/dist/cjs/utils/tests/is-address.test.js +0 -48
- package/dist/cjs/utils/tests/keccak256.test.js +0 -121
- package/dist/cjs/utils/tests/solidity-keccak256.test.js +0 -311
- package/dist/cjs/utils/tests/split-signature.test.js +0 -46
- package/dist/cjs/utils/tests/to-checksum-address.test.js +0 -39
- package/dist/cjs/utils/tests/to-utf8-bytes.test.js +0 -34
- package/dist/cjs/utils/tests/wei-to-ether.test.js +0 -41
package/dist/cjs/utils/bytes.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
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;
|
|
1
4
|
// primarily duplicate code from https://github.com/ethers-io/ethers.js/blob/f599d6f23dad0d0acaa3828d6b7acaab2d5e455b/packages/bytes/src.ts/index.ts
|
|
2
|
-
|
|
3
|
-
"@swc/helpers - typeof";
|
|
4
|
-
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
5
|
-
}
|
|
6
|
-
import { logger } from "../logger/logger";
|
|
5
|
+
const logger_1 = require("../logger/logger");
|
|
7
6
|
/**
|
|
8
7
|
* Check if a value can be converted to a hex string
|
|
9
8
|
*
|
|
@@ -15,7 +14,8 @@ import { logger } from "../logger/logger";
|
|
|
15
14
|
* isHexable(val);
|
|
16
15
|
* // true
|
|
17
16
|
* ```
|
|
18
|
-
*/
|
|
17
|
+
*/
|
|
18
|
+
function isHexable(value) {
|
|
19
19
|
return !!value.toHexString;
|
|
20
20
|
}
|
|
21
21
|
/**
|
|
@@ -39,9 +39,11 @@ import { logger } from "../logger/logger";
|
|
|
39
39
|
* isBytesLike(new Uint8Array(1));
|
|
40
40
|
* // true
|
|
41
41
|
* ```
|
|
42
|
-
*/
|
|
43
|
-
|
|
42
|
+
*/
|
|
43
|
+
function isBytesLike(value) {
|
|
44
|
+
return (isHexString(value) && !(value.length % 2)) || isBytes(value);
|
|
44
45
|
}
|
|
46
|
+
exports.isBytesLike = isBytesLike;
|
|
45
47
|
/**
|
|
46
48
|
* Checks if a value is an integer
|
|
47
49
|
*
|
|
@@ -57,8 +59,9 @@ import { logger } from "../logger/logger";
|
|
|
57
59
|
* isInteger(6.2)
|
|
58
60
|
* // false
|
|
59
61
|
* ```
|
|
60
|
-
*/
|
|
61
|
-
|
|
62
|
+
*/
|
|
63
|
+
function isInteger(value) {
|
|
64
|
+
return typeof value === 'number' && value == value && value % 1 === 0;
|
|
62
65
|
}
|
|
63
66
|
/**
|
|
64
67
|
* Returns true if and only if value is a valid [Bytes](#bytes)
|
|
@@ -81,27 +84,29 @@ import { logger } from "../logger/logger";
|
|
|
81
84
|
* isBytes(new Uint8Array(1));
|
|
82
85
|
* // true
|
|
83
86
|
* ```
|
|
84
|
-
*/
|
|
87
|
+
*/
|
|
88
|
+
function isBytes(value) {
|
|
85
89
|
if (value == null) {
|
|
86
90
|
return false;
|
|
87
91
|
}
|
|
88
92
|
if (value.constructor === Uint8Array) {
|
|
89
93
|
return true;
|
|
90
94
|
}
|
|
91
|
-
if (typeof value ===
|
|
95
|
+
if (typeof value === 'string') {
|
|
92
96
|
return false;
|
|
93
97
|
}
|
|
94
98
|
if (!isInteger(value.length) || value.length < 0) {
|
|
95
99
|
return false;
|
|
96
100
|
}
|
|
97
|
-
for(
|
|
98
|
-
|
|
101
|
+
for (let i = 0; i < value.length; i++) {
|
|
102
|
+
const v = value[i];
|
|
99
103
|
if (!isInteger(v) || v < 0 || v >= 256) {
|
|
100
104
|
return false;
|
|
101
105
|
}
|
|
102
106
|
}
|
|
103
107
|
return true;
|
|
104
108
|
}
|
|
109
|
+
exports.isBytes = isBytes;
|
|
105
110
|
/**
|
|
106
111
|
* Converts DataHexStringOrArrayish to a Uint8Array
|
|
107
112
|
* Same as [`ethers.utils.arrayify`](https://docs.ethers.io/v5/api/utils/bytes/#utils-arrayify)
|
|
@@ -124,14 +129,15 @@ import { logger } from "../logger/logger";
|
|
|
124
129
|
* arrayify('0x1', { hexPad: 'right' });
|
|
125
130
|
* // Uint8Array(1) [ 16 ]
|
|
126
131
|
* ```
|
|
127
|
-
*/
|
|
132
|
+
*/
|
|
133
|
+
function arrayify(value, options) {
|
|
128
134
|
if (!options) {
|
|
129
135
|
options = {};
|
|
130
136
|
}
|
|
131
|
-
if (typeof value ===
|
|
132
|
-
logger.checkSafeUint53(value,
|
|
133
|
-
|
|
134
|
-
while(value){
|
|
137
|
+
if (typeof value === 'number') {
|
|
138
|
+
logger_1.logger.checkSafeUint53(value, 'invalid arrayify value');
|
|
139
|
+
const result = [];
|
|
140
|
+
while (value) {
|
|
135
141
|
result.unshift(value & 0xff);
|
|
136
142
|
value = parseInt(String(value / 256));
|
|
137
143
|
}
|
|
@@ -140,34 +146,39 @@ import { logger } from "../logger/logger";
|
|
|
140
146
|
}
|
|
141
147
|
return new Uint8Array(result);
|
|
142
148
|
}
|
|
143
|
-
if (options.allowMissingPrefix &&
|
|
144
|
-
value
|
|
149
|
+
if (options.allowMissingPrefix &&
|
|
150
|
+
typeof value === 'string' &&
|
|
151
|
+
value.substring(0, 2) !== '0x') {
|
|
152
|
+
value = '0x' + value;
|
|
145
153
|
}
|
|
146
154
|
if (isHexable(value)) {
|
|
147
155
|
value = value.toHexString();
|
|
148
156
|
}
|
|
149
157
|
if (isHexString(value)) {
|
|
150
|
-
|
|
158
|
+
let hex = value.substring(2);
|
|
151
159
|
if (hex.length % 2) {
|
|
152
|
-
if (options.hexPad ===
|
|
153
|
-
hex =
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
160
|
+
if (options.hexPad === 'left') {
|
|
161
|
+
hex = '0' + hex;
|
|
162
|
+
}
|
|
163
|
+
else if (options.hexPad === 'right') {
|
|
164
|
+
hex += '0';
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
logger_1.logger.throwArgumentError('hex data is odd-length', 'value', value);
|
|
158
168
|
}
|
|
159
169
|
}
|
|
160
|
-
|
|
161
|
-
for(
|
|
162
|
-
|
|
170
|
+
const result = [];
|
|
171
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
172
|
+
result.push(parseInt(hex.substring(i, i + 2), 16));
|
|
163
173
|
}
|
|
164
|
-
return new Uint8Array(
|
|
174
|
+
return new Uint8Array(result);
|
|
165
175
|
}
|
|
166
176
|
if (isBytes(value)) {
|
|
167
177
|
return new Uint8Array(value);
|
|
168
178
|
}
|
|
169
|
-
return logger.throwArgumentError(
|
|
179
|
+
return logger_1.logger.throwArgumentError('invalid arrayify value', 'value', value);
|
|
170
180
|
}
|
|
181
|
+
exports.arrayify = arrayify;
|
|
171
182
|
/**
|
|
172
183
|
* Concatenates all the BytesLike in arrayOfBytesLike into a single Uint8Array.
|
|
173
184
|
* Same as [`ethers.utils.concat`](https://docs.ethers.io/v5/api/utils/bytes/#utils-concat)
|
|
@@ -179,20 +190,18 @@ import { logger } from "../logger/logger";
|
|
|
179
190
|
* concat([0, 1]);
|
|
180
191
|
* // Uint8Array(2) [ 0, 1 ]
|
|
181
192
|
* ```
|
|
182
|
-
*/
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}, 0);
|
|
189
|
-
var result = new Uint8Array(length);
|
|
190
|
-
objects.reduce(function(offset, object) {
|
|
193
|
+
*/
|
|
194
|
+
function concat(arrayOfBytesLike) {
|
|
195
|
+
const objects = arrayOfBytesLike.map((item) => arrayify(item));
|
|
196
|
+
const length = objects.reduce((accum, item) => accum + item.length, 0);
|
|
197
|
+
const result = new Uint8Array(length);
|
|
198
|
+
objects.reduce((offset, object) => {
|
|
191
199
|
result.set(object, offset);
|
|
192
200
|
return offset + object.length;
|
|
193
201
|
}, 0);
|
|
194
202
|
return result;
|
|
195
203
|
}
|
|
204
|
+
exports.concat = concat;
|
|
196
205
|
/**
|
|
197
206
|
* Strips leading zeros from a BytesLike object
|
|
198
207
|
*
|
|
@@ -204,14 +213,15 @@ import { logger } from "../logger/logger";
|
|
|
204
213
|
* // Uint8Array { [Iterator] 0: 40, 1: 52 }
|
|
205
214
|
* // Equivalent to '0x2834'
|
|
206
215
|
* ```
|
|
207
|
-
*/
|
|
208
|
-
|
|
216
|
+
*/
|
|
217
|
+
function stripZeros(value) {
|
|
218
|
+
let result = arrayify(value);
|
|
209
219
|
if (result.length === 0) {
|
|
210
220
|
return result;
|
|
211
221
|
}
|
|
212
222
|
// Find the first non-zero entry
|
|
213
|
-
|
|
214
|
-
while(start < result.length && result[start] === 0){
|
|
223
|
+
let start = 0;
|
|
224
|
+
while (start < result.length && result[start] === 0) {
|
|
215
225
|
start++;
|
|
216
226
|
}
|
|
217
227
|
// If we started with zeros, strip them
|
|
@@ -220,6 +230,7 @@ import { logger } from "../logger/logger";
|
|
|
220
230
|
}
|
|
221
231
|
return result;
|
|
222
232
|
}
|
|
233
|
+
exports.stripZeros = stripZeros;
|
|
223
234
|
/**
|
|
224
235
|
* Pads the beginning of a {@link BytesLike} with zeros so it's the specified length as a Uint8Array
|
|
225
236
|
*
|
|
@@ -237,15 +248,17 @@ import { logger } from "../logger/logger";
|
|
|
237
248
|
* zeroPad([39, 25, 103, 45], 5);
|
|
238
249
|
* // Uint8Array { [Iterator] 0: 0, 1: 39, 2: 25, 3: 103, 4: 45 }
|
|
239
250
|
* ```
|
|
240
|
-
*/
|
|
251
|
+
*/
|
|
252
|
+
function zeroPad(value, length) {
|
|
241
253
|
value = arrayify(value);
|
|
242
254
|
if (value.length > length) {
|
|
243
|
-
logger.throwArgumentError(
|
|
255
|
+
logger_1.logger.throwArgumentError('value out of range', 'value', value);
|
|
244
256
|
}
|
|
245
|
-
|
|
257
|
+
const result = new Uint8Array(length);
|
|
246
258
|
result.set(value, length - value.length);
|
|
247
259
|
return result;
|
|
248
260
|
}
|
|
261
|
+
exports.zeroPad = zeroPad;
|
|
249
262
|
/**
|
|
250
263
|
* Returns true if and only if object is a valid hex string.
|
|
251
264
|
* If length is specified and object is not a valid DataHexString of length bytes, an InvalidArgument error is thrown.
|
|
@@ -265,8 +278,9 @@ import { logger } from "../logger/logger";
|
|
|
265
278
|
* // false
|
|
266
279
|
* // length of 4 in bytes would mean a hex string with 8 characters
|
|
267
280
|
* ```
|
|
268
|
-
*/
|
|
269
|
-
|
|
281
|
+
*/
|
|
282
|
+
function isHexString(value, length) {
|
|
283
|
+
if (typeof value !== 'string' || !value.match(/^0x[0-9A-Fa-f]*$/)) {
|
|
270
284
|
return false;
|
|
271
285
|
}
|
|
272
286
|
if (length && value.length !== 2 + 2 * length) {
|
|
@@ -274,7 +288,8 @@ import { logger } from "../logger/logger";
|
|
|
274
288
|
}
|
|
275
289
|
return true;
|
|
276
290
|
}
|
|
277
|
-
|
|
291
|
+
exports.isHexString = isHexString;
|
|
292
|
+
const HexCharacters = '0123456789abcdef';
|
|
278
293
|
/**
|
|
279
294
|
* Converts a value into a hex string
|
|
280
295
|
*
|
|
@@ -291,60 +306,66 @@ var HexCharacters = "0123456789abcdef";
|
|
|
291
306
|
* hexlify(14);
|
|
292
307
|
* // '0x0e'
|
|
293
308
|
* ```
|
|
294
|
-
*/
|
|
309
|
+
*/
|
|
310
|
+
function hexlify(value, options) {
|
|
295
311
|
if (!options) {
|
|
296
312
|
options = {};
|
|
297
313
|
}
|
|
298
|
-
if (typeof value ===
|
|
299
|
-
logger.checkSafeUint53(value,
|
|
300
|
-
|
|
301
|
-
while(value){
|
|
314
|
+
if (typeof value === 'number') {
|
|
315
|
+
logger_1.logger.checkSafeUint53(value, 'invalid hexlify value');
|
|
316
|
+
let hex = '';
|
|
317
|
+
while (value) {
|
|
302
318
|
hex = HexCharacters[value & 0xf] + hex;
|
|
303
319
|
value = Math.floor(value / 16);
|
|
304
320
|
}
|
|
305
321
|
if (hex.length) {
|
|
306
322
|
if (hex.length % 2) {
|
|
307
|
-
hex =
|
|
323
|
+
hex = '0' + hex;
|
|
308
324
|
}
|
|
309
|
-
return
|
|
325
|
+
return '0x' + hex;
|
|
310
326
|
}
|
|
311
|
-
return
|
|
327
|
+
return '0x00';
|
|
312
328
|
}
|
|
313
|
-
if (
|
|
329
|
+
if (typeof value === 'bigint') {
|
|
314
330
|
value = value.toString(16);
|
|
315
331
|
if (value.length % 2) {
|
|
316
|
-
return
|
|
332
|
+
return '0x0' + value;
|
|
317
333
|
}
|
|
318
|
-
return
|
|
334
|
+
return '0x' + value;
|
|
319
335
|
}
|
|
320
|
-
if (options.allowMissingPrefix &&
|
|
321
|
-
value
|
|
336
|
+
if (options.allowMissingPrefix &&
|
|
337
|
+
typeof value === 'string' &&
|
|
338
|
+
value.substring(0, 2) !== '0x') {
|
|
339
|
+
value = '0x' + value;
|
|
322
340
|
}
|
|
323
341
|
if (isHexable(value)) {
|
|
324
342
|
return value.toHexString();
|
|
325
343
|
}
|
|
326
344
|
if (isHexString(value)) {
|
|
327
345
|
if (value.length % 2) {
|
|
328
|
-
if (options.hexPad ===
|
|
329
|
-
value =
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
346
|
+
if (options.hexPad === 'left') {
|
|
347
|
+
value = '0x0' + value.substring(2);
|
|
348
|
+
}
|
|
349
|
+
else if (options.hexPad === 'right') {
|
|
350
|
+
value += '0';
|
|
351
|
+
}
|
|
352
|
+
else {
|
|
353
|
+
logger_1.logger.throwArgumentError('hex data is odd-length', 'value', value);
|
|
334
354
|
}
|
|
335
355
|
}
|
|
336
356
|
return value.toLowerCase();
|
|
337
357
|
}
|
|
338
358
|
if (isBytes(value)) {
|
|
339
|
-
|
|
340
|
-
for(
|
|
341
|
-
|
|
359
|
+
let result = '0x';
|
|
360
|
+
for (let i = 0; i < value.length; i++) {
|
|
361
|
+
const v = value[i];
|
|
342
362
|
result += HexCharacters[(v & 0xf0) >> 4] + HexCharacters[v & 0x0f];
|
|
343
363
|
}
|
|
344
364
|
return result;
|
|
345
365
|
}
|
|
346
|
-
return logger.throwArgumentError(
|
|
366
|
+
return logger_1.logger.throwArgumentError('invalid hexlify value', 'value', value);
|
|
347
367
|
}
|
|
368
|
+
exports.hexlify = hexlify;
|
|
348
369
|
/**
|
|
349
370
|
* Gets the length of data represented as a hex string
|
|
350
371
|
*
|
|
@@ -360,14 +381,17 @@ var HexCharacters = "0123456789abcdef";
|
|
|
360
381
|
* hexDataLength('0x3925');
|
|
361
382
|
* // 2
|
|
362
383
|
* ```
|
|
363
|
-
*/
|
|
364
|
-
|
|
384
|
+
*/
|
|
385
|
+
function hexDataLength(data) {
|
|
386
|
+
if (typeof data !== 'string') {
|
|
365
387
|
data = hexlify(data);
|
|
366
|
-
}
|
|
388
|
+
}
|
|
389
|
+
else if (!isHexString(data) || data.length % 2) {
|
|
367
390
|
return null;
|
|
368
391
|
}
|
|
369
392
|
return (data.length - 2) / 2;
|
|
370
393
|
}
|
|
394
|
+
exports.hexDataLength = hexDataLength;
|
|
371
395
|
/**
|
|
372
396
|
* Slices a {@link BytesLike} to extract a certain part of the input
|
|
373
397
|
*
|
|
@@ -380,18 +404,21 @@ var HexCharacters = "0123456789abcdef";
|
|
|
380
404
|
* hexDataSlice([20, 6, 48], 0, 2);
|
|
381
405
|
* // '0x1406'
|
|
382
406
|
* ```
|
|
383
|
-
*/
|
|
384
|
-
|
|
407
|
+
*/
|
|
408
|
+
function hexDataSlice(data, offset, endOffset) {
|
|
409
|
+
if (typeof data !== 'string') {
|
|
385
410
|
data = hexlify(data);
|
|
386
|
-
}
|
|
387
|
-
|
|
411
|
+
}
|
|
412
|
+
else if (!isHexString(data) || data.length % 2) {
|
|
413
|
+
logger_1.logger.throwArgumentError('invalid hexData', 'value', data);
|
|
388
414
|
}
|
|
389
415
|
offset = 2 + 2 * offset;
|
|
390
416
|
if (endOffset != null) {
|
|
391
|
-
return
|
|
417
|
+
return '0x' + data.substring(offset, 2 + 2 * endOffset);
|
|
392
418
|
}
|
|
393
|
-
return
|
|
419
|
+
return '0x' + data.substring(offset);
|
|
394
420
|
}
|
|
421
|
+
exports.hexDataSlice = hexDataSlice;
|
|
395
422
|
/**
|
|
396
423
|
* Concatenates values together into one hex string
|
|
397
424
|
*
|
|
@@ -402,13 +429,15 @@ var HexCharacters = "0123456789abcdef";
|
|
|
402
429
|
* hexConcat([[2, 4, 0, 1], 9, '0x2934', '0x3947']);
|
|
403
430
|
* // '0x020400010929343947'
|
|
404
431
|
* ```
|
|
405
|
-
*/
|
|
406
|
-
|
|
407
|
-
|
|
432
|
+
*/
|
|
433
|
+
function hexConcat(items) {
|
|
434
|
+
let result = '0x';
|
|
435
|
+
items.forEach((item) => {
|
|
408
436
|
result += hexlify(item).substring(2);
|
|
409
437
|
});
|
|
410
438
|
return result;
|
|
411
439
|
}
|
|
440
|
+
exports.hexConcat = hexConcat;
|
|
412
441
|
/**
|
|
413
442
|
* Converts a number of different types into a hex string
|
|
414
443
|
*
|
|
@@ -424,15 +453,15 @@ var HexCharacters = "0123456789abcdef";
|
|
|
424
453
|
* hexValue([9, 4, 19, 4]);
|
|
425
454
|
* // '0x9041304'
|
|
426
455
|
* ```
|
|
427
|
-
*/
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
return "0x0";
|
|
456
|
+
*/
|
|
457
|
+
function hexValue(value) {
|
|
458
|
+
const trimmed = hexStripZeros(hexlify(value, { hexPad: 'left' }));
|
|
459
|
+
if (trimmed === '0x') {
|
|
460
|
+
return '0x0';
|
|
433
461
|
}
|
|
434
462
|
return trimmed;
|
|
435
463
|
}
|
|
464
|
+
exports.hexValue = hexValue;
|
|
436
465
|
/**
|
|
437
466
|
* Strips the leading zeros from a value and returns it as a hex string
|
|
438
467
|
*
|
|
@@ -443,20 +472,22 @@ var HexCharacters = "0123456789abcdef";
|
|
|
443
472
|
* hexStripZeros([0,0,0,48]);
|
|
444
473
|
* // '0x30'
|
|
445
474
|
* ```
|
|
446
|
-
*/
|
|
447
|
-
|
|
475
|
+
*/
|
|
476
|
+
function hexStripZeros(value) {
|
|
477
|
+
if (typeof value !== 'string') {
|
|
448
478
|
value = hexlify(value);
|
|
449
479
|
}
|
|
450
480
|
if (!isHexString(value)) {
|
|
451
|
-
logger.throwArgumentError(
|
|
481
|
+
logger_1.logger.throwArgumentError('invalid hex string', 'value', value);
|
|
452
482
|
}
|
|
453
483
|
value = value.substring(2);
|
|
454
|
-
|
|
455
|
-
while(offset < value.length && value[offset] ===
|
|
484
|
+
let offset = 0;
|
|
485
|
+
while (offset < value.length && value[offset] === '0') {
|
|
456
486
|
offset++;
|
|
457
487
|
}
|
|
458
|
-
return
|
|
488
|
+
return '0x' + value.substring(offset);
|
|
459
489
|
}
|
|
490
|
+
exports.hexStripZeros = hexStripZeros;
|
|
460
491
|
/**
|
|
461
492
|
* Returns a hex string padded to a specified length of bytes.
|
|
462
493
|
*
|
|
@@ -484,196 +515,197 @@ var HexCharacters = "0123456789abcdef";
|
|
|
484
515
|
* hexZeroPad('12345', 1);
|
|
485
516
|
* // Throws
|
|
486
517
|
* ```
|
|
487
|
-
*/
|
|
488
|
-
|
|
518
|
+
*/
|
|
519
|
+
function hexZeroPad(value, length) {
|
|
520
|
+
if (typeof value !== 'string') {
|
|
489
521
|
value = hexlify(value);
|
|
490
|
-
}
|
|
491
|
-
|
|
522
|
+
}
|
|
523
|
+
else if (!isHexString(value)) {
|
|
524
|
+
logger_1.logger.throwArgumentError('invalid hex string', 'value', value);
|
|
492
525
|
}
|
|
493
526
|
if (value.length > 2 * length + 2) {
|
|
494
|
-
logger.throwError(
|
|
495
|
-
value: value,
|
|
496
|
-
length: length
|
|
497
|
-
});
|
|
527
|
+
logger_1.logger.throwError('value out of range', { value, length });
|
|
498
528
|
}
|
|
499
|
-
while(value.length < 2 * length + 2){
|
|
500
|
-
value =
|
|
529
|
+
while (value.length < 2 * length + 2) {
|
|
530
|
+
value = '0x0' + value.substring(2);
|
|
501
531
|
}
|
|
502
532
|
return value;
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
533
|
+
}
|
|
534
|
+
exports.hexZeroPad = hexZeroPad;
|
|
535
|
+
// export function splitSignature(signature: SignatureLike): Signature {
|
|
536
|
+
// const result: Signature = {
|
|
537
|
+
// r: '0x',
|
|
538
|
+
// s: '0x',
|
|
539
|
+
// _vs: '0x',
|
|
540
|
+
// recoveryParam: 0,
|
|
541
|
+
// v: 0,
|
|
542
|
+
// yParityAndS: '0x',
|
|
543
|
+
// compact: '0x',
|
|
544
|
+
// };
|
|
545
|
+
// if (isBytesLike(signature)) {
|
|
546
|
+
// const bytes: Uint8Array = arrayify(signature);
|
|
547
|
+
// // Get the r, s and v
|
|
548
|
+
// if (bytes.length === 64) {
|
|
549
|
+
// // EIP-2098; pull the v from the top bit of s and clear it
|
|
550
|
+
// result.v = 27 + (bytes[32] >> 7);
|
|
551
|
+
// bytes[32] &= 0x7f;
|
|
552
|
+
// result.r = hexlify(bytes.slice(0, 32));
|
|
553
|
+
// result.s = hexlify(bytes.slice(32, 64));
|
|
554
|
+
// } else if (bytes.length === 65) {
|
|
555
|
+
// result.r = hexlify(bytes.slice(0, 32));
|
|
556
|
+
// result.s = hexlify(bytes.slice(32, 64));
|
|
557
|
+
// result.v = bytes[64];
|
|
558
|
+
// } else {
|
|
559
|
+
// logger.throwArgumentError(
|
|
560
|
+
// 'invalid signature string',
|
|
561
|
+
// 'signature',
|
|
562
|
+
// signature,
|
|
563
|
+
// );
|
|
564
|
+
// }
|
|
565
|
+
// // Allow a recid to be used as the v
|
|
566
|
+
// if (result.v < 27) {
|
|
567
|
+
// if (result.v === 0 || result.v === 1) {
|
|
568
|
+
// result.v += 27;
|
|
569
|
+
// } else {
|
|
570
|
+
// logger.throwArgumentError(
|
|
571
|
+
// 'signature invalid v byte',
|
|
572
|
+
// 'signature',
|
|
573
|
+
// signature,
|
|
574
|
+
// );
|
|
575
|
+
// }
|
|
576
|
+
// }
|
|
577
|
+
// // Compute recoveryParam from v
|
|
578
|
+
// result.recoveryParam = 1 - (result.v % 2);
|
|
579
|
+
// // Compute _vs from recoveryParam and s
|
|
580
|
+
// if (result.recoveryParam) {
|
|
581
|
+
// bytes[32] |= 0x80;
|
|
582
|
+
// }
|
|
583
|
+
// result._vs = hexlify(bytes.slice(32, 64));
|
|
584
|
+
// } else {
|
|
585
|
+
// result.r = signature.r;
|
|
586
|
+
// result.s = signature.s;
|
|
587
|
+
// result.v = signature.v;
|
|
588
|
+
// result.recoveryParam = signature.recoveryParam;
|
|
589
|
+
// result._vs = signature._vs;
|
|
590
|
+
// // If the _vs is available, use it to populate missing s, v and recoveryParam
|
|
591
|
+
// // and verify non-missing s, v and recoveryParam
|
|
592
|
+
// if (result._vs != null) {
|
|
593
|
+
// const vs = zeroPad(arrayify(result._vs), 32);
|
|
594
|
+
// result._vs = hexlify(vs);
|
|
595
|
+
// // Set or check the recid
|
|
596
|
+
// const recoveryParam = vs[0] >= 128 ? 1 : 0;
|
|
597
|
+
// if (result.recoveryParam == null) {
|
|
598
|
+
// result.recoveryParam = recoveryParam;
|
|
599
|
+
// } else if (result.recoveryParam !== recoveryParam) {
|
|
600
|
+
// logger.throwArgumentError(
|
|
601
|
+
// 'signature recoveryParam mismatch _vs',
|
|
602
|
+
// 'signature',
|
|
603
|
+
// signature,
|
|
604
|
+
// );
|
|
605
|
+
// }
|
|
606
|
+
// // Set or check the s
|
|
607
|
+
// vs[0] &= 0x7f;
|
|
608
|
+
// const s = hexlify(vs);
|
|
609
|
+
// if (result.s == null) {
|
|
610
|
+
// result.s = s;
|
|
611
|
+
// } else if (result.s !== s) {
|
|
612
|
+
// logger.throwArgumentError(
|
|
613
|
+
// 'signature v mismatch _vs',
|
|
614
|
+
// 'signature',
|
|
615
|
+
// signature,
|
|
616
|
+
// );
|
|
617
|
+
// }
|
|
618
|
+
// }
|
|
619
|
+
// // Use recid and v to populate each other
|
|
620
|
+
// if (result.recoveryParam == null) {
|
|
621
|
+
// if (result.v == null) {
|
|
622
|
+
// logger.throwArgumentError(
|
|
623
|
+
// 'signature missing v and recoveryParam',
|
|
624
|
+
// 'signature',
|
|
625
|
+
// signature,
|
|
626
|
+
// );
|
|
627
|
+
// } else if (result.v === 0 || result.v === 1) {
|
|
628
|
+
// result.recoveryParam = result.v;
|
|
629
|
+
// } else {
|
|
630
|
+
// result.recoveryParam = 1 - (result.v % 2);
|
|
631
|
+
// }
|
|
632
|
+
// } else {
|
|
633
|
+
// if (result.v == null) {
|
|
634
|
+
// result.v = 27 + result.recoveryParam;
|
|
635
|
+
// } else {
|
|
636
|
+
// const recId =
|
|
637
|
+
// result.v === 0 || result.v === 1 ? result.v : 1 - (result.v % 2);
|
|
638
|
+
// if (result.recoveryParam !== recId) {
|
|
639
|
+
// logger.throwArgumentError(
|
|
640
|
+
// 'signature recoveryParam mismatch v',
|
|
641
|
+
// 'signature',
|
|
642
|
+
// signature,
|
|
643
|
+
// );
|
|
644
|
+
// }
|
|
645
|
+
// }
|
|
646
|
+
// }
|
|
647
|
+
// if (result.r == null || !isHexString(result.r)) {
|
|
648
|
+
// logger.throwArgumentError(
|
|
649
|
+
// 'signature missing or invalid r',
|
|
650
|
+
// 'signature',
|
|
651
|
+
// signature,
|
|
652
|
+
// );
|
|
653
|
+
// } else {
|
|
654
|
+
// result.r = hexZeroPad(result.r, 32);
|
|
655
|
+
// }
|
|
656
|
+
// if (result.s == null || !isHexString(result.s)) {
|
|
657
|
+
// logger.throwArgumentError(
|
|
658
|
+
// 'signature missing or invalid s',
|
|
659
|
+
// 'signature',
|
|
660
|
+
// signature,
|
|
661
|
+
// );
|
|
662
|
+
// } else {
|
|
663
|
+
// result.s = hexZeroPad(result.s, 32);
|
|
664
|
+
// }
|
|
665
|
+
// const vs = arrayify(result.s);
|
|
666
|
+
// if (vs[0] >= 128) {
|
|
667
|
+
// logger.throwArgumentError(
|
|
668
|
+
// 'signature s out of range',
|
|
669
|
+
// 'signature',
|
|
670
|
+
// signature,
|
|
671
|
+
// );
|
|
672
|
+
// }
|
|
673
|
+
// if (result.recoveryParam) {
|
|
674
|
+
// vs[0] |= 0x80;
|
|
675
|
+
// }
|
|
676
|
+
// const _vs = hexlify(vs);
|
|
677
|
+
// if (result._vs) {
|
|
678
|
+
// if (!isHexString(result._vs)) {
|
|
679
|
+
// logger.throwArgumentError(
|
|
680
|
+
// 'signature invalid _vs',
|
|
681
|
+
// 'signature',
|
|
682
|
+
// signature,
|
|
683
|
+
// );
|
|
684
|
+
// }
|
|
685
|
+
// result._vs = hexZeroPad(result._vs, 32);
|
|
686
|
+
// }
|
|
687
|
+
// // Set or check the _vs
|
|
688
|
+
// if (result._vs == null) {
|
|
689
|
+
// result._vs = _vs;
|
|
690
|
+
// } else if (result._vs !== _vs) {
|
|
691
|
+
// logger.throwArgumentError(
|
|
692
|
+
// 'signature _vs mismatch v and s',
|
|
693
|
+
// 'signature',
|
|
694
|
+
// signature,
|
|
695
|
+
// );
|
|
696
|
+
// }
|
|
697
|
+
// }
|
|
698
|
+
// result.yParityAndS = result._vs;
|
|
699
|
+
// result.compact = result.r + result.yParityAndS.substring(2);
|
|
700
|
+
// return result;
|
|
701
|
+
// }
|
|
702
|
+
// export function joinSignature(signature: SignatureLike): string {
|
|
703
|
+
// signature = splitSignature(signature);
|
|
704
|
+
// return hexlify(
|
|
705
|
+
// concat([
|
|
706
|
+
// signature.r,
|
|
707
|
+
// signature.s,
|
|
708
|
+
// signature.recoveryParam ? '0x1c' : '0x1b',
|
|
709
|
+
// ]),
|
|
710
|
+
// );
|
|
711
|
+
// }
|