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