essential-eth 0.6.2 → 0.7.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.js +824 -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-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/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/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 +8 -6
- package/readme.md +34 -3
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import { utils } from "ethers";
|
|
2
|
+
import { solidityKeccak256 } from "../../index";
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param inputs
|
|
6
|
+
*/ function testSolidityKeccak256(inputs) {
|
|
7
|
+
inputs.forEach(function(input) {
|
|
8
|
+
expect(solidityKeccak256(input.types, input.values)).toBe(utils.solidityKeccak256(input.types, input.values));
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
describe("solidityKeccak256", function() {
|
|
12
|
+
it("should match ethers.js addresses", function() {
|
|
13
|
+
var inputs = [
|
|
14
|
+
{
|
|
15
|
+
types: [
|
|
16
|
+
"address"
|
|
17
|
+
],
|
|
18
|
+
values: [
|
|
19
|
+
"0x4d7F1790644Af787933c9fF0e2cff9a9B4299Abb"
|
|
20
|
+
]
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
types: [
|
|
24
|
+
"address",
|
|
25
|
+
"address"
|
|
26
|
+
],
|
|
27
|
+
values: [
|
|
28
|
+
"0xB5503a7db1A9105cd459D99153e69a76a8EF1530",
|
|
29
|
+
"0xaa0fc255b079e775f9307e5cfec472a555cebc3a"
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
];
|
|
33
|
+
testSolidityKeccak256(inputs);
|
|
34
|
+
});
|
|
35
|
+
it("should match ethers.js strings", function() {
|
|
36
|
+
var inputs = [
|
|
37
|
+
{
|
|
38
|
+
types: [
|
|
39
|
+
"string"
|
|
40
|
+
],
|
|
41
|
+
values: [
|
|
42
|
+
"essential-eth"
|
|
43
|
+
]
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
types: [
|
|
47
|
+
"string",
|
|
48
|
+
"string"
|
|
49
|
+
],
|
|
50
|
+
values: [
|
|
51
|
+
"firstText",
|
|
52
|
+
"secondString"
|
|
53
|
+
]
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
types: [
|
|
57
|
+
"string",
|
|
58
|
+
"string",
|
|
59
|
+
"string"
|
|
60
|
+
],
|
|
61
|
+
values: [
|
|
62
|
+
"example1",
|
|
63
|
+
"2934823",
|
|
64
|
+
"true"
|
|
65
|
+
]
|
|
66
|
+
}
|
|
67
|
+
];
|
|
68
|
+
testSolidityKeccak256(inputs);
|
|
69
|
+
});
|
|
70
|
+
it("should match ethers.js bytes (dynamic size) & BytesLike", function() {
|
|
71
|
+
var inputs = [
|
|
72
|
+
{
|
|
73
|
+
types: [
|
|
74
|
+
"bytes"
|
|
75
|
+
],
|
|
76
|
+
values: [
|
|
77
|
+
[
|
|
78
|
+
115,
|
|
79
|
+
101,
|
|
80
|
+
99,
|
|
81
|
+
114,
|
|
82
|
+
101,
|
|
83
|
+
116
|
|
84
|
+
]
|
|
85
|
+
]
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
types: [
|
|
89
|
+
"bytes"
|
|
90
|
+
],
|
|
91
|
+
values: [
|
|
92
|
+
"0x62797465734c696b65"
|
|
93
|
+
]
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
types: [
|
|
97
|
+
"bytes",
|
|
98
|
+
"bytes"
|
|
99
|
+
],
|
|
100
|
+
values: [
|
|
101
|
+
"0x657373656e7469616c2d657468",
|
|
102
|
+
[
|
|
103
|
+
115,
|
|
104
|
+
109,
|
|
105
|
+
97,
|
|
106
|
+
108,
|
|
107
|
+
108,
|
|
108
|
+
101,
|
|
109
|
+
115,
|
|
110
|
+
116
|
|
111
|
+
]
|
|
112
|
+
]
|
|
113
|
+
}
|
|
114
|
+
];
|
|
115
|
+
testSolidityKeccak256(inputs);
|
|
116
|
+
});
|
|
117
|
+
it("should match ethers.js bytes (static size)", function() {
|
|
118
|
+
var inputs = [
|
|
119
|
+
{
|
|
120
|
+
types: [
|
|
121
|
+
"bytes4"
|
|
122
|
+
],
|
|
123
|
+
values: [
|
|
124
|
+
[
|
|
125
|
+
116,
|
|
126
|
+
101,
|
|
127
|
+
115,
|
|
128
|
+
116
|
|
129
|
+
]
|
|
130
|
+
]
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
types: [
|
|
134
|
+
"bytes13",
|
|
135
|
+
"bytes1",
|
|
136
|
+
"bytes2",
|
|
137
|
+
"bytes5",
|
|
138
|
+
"bytes1"
|
|
139
|
+
],
|
|
140
|
+
values: [
|
|
141
|
+
[
|
|
142
|
+
101,
|
|
143
|
+
115,
|
|
144
|
+
115,
|
|
145
|
+
101,
|
|
146
|
+
110,
|
|
147
|
+
116,
|
|
148
|
+
105,
|
|
149
|
+
97,
|
|
150
|
+
108,
|
|
151
|
+
45,
|
|
152
|
+
101,
|
|
153
|
+
116,
|
|
154
|
+
104
|
|
155
|
+
],
|
|
156
|
+
[
|
|
157
|
+
32
|
|
158
|
+
],
|
|
159
|
+
[
|
|
160
|
+
105,
|
|
161
|
+
115
|
|
162
|
+
],
|
|
163
|
+
[
|
|
164
|
+
103,
|
|
165
|
+
114,
|
|
166
|
+
101,
|
|
167
|
+
97,
|
|
168
|
+
116
|
|
169
|
+
],
|
|
170
|
+
[
|
|
171
|
+
33
|
|
172
|
+
]
|
|
173
|
+
]
|
|
174
|
+
}
|
|
175
|
+
];
|
|
176
|
+
testSolidityKeccak256(inputs);
|
|
177
|
+
});
|
|
178
|
+
it('should match ethers.js booleans (specified as type "bool" to match solidity)', function() {
|
|
179
|
+
var inputs = [
|
|
180
|
+
{
|
|
181
|
+
types: [
|
|
182
|
+
"bool"
|
|
183
|
+
],
|
|
184
|
+
values: [
|
|
185
|
+
true
|
|
186
|
+
]
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
types: [
|
|
190
|
+
"bool",
|
|
191
|
+
"bool"
|
|
192
|
+
],
|
|
193
|
+
values: [
|
|
194
|
+
false,
|
|
195
|
+
false
|
|
196
|
+
]
|
|
197
|
+
}
|
|
198
|
+
];
|
|
199
|
+
testSolidityKeccak256(inputs);
|
|
200
|
+
});
|
|
201
|
+
it("should match ethers.js (signed and unsigned) integers", function() {
|
|
202
|
+
var inputs = [
|
|
203
|
+
{
|
|
204
|
+
types: [
|
|
205
|
+
"int16"
|
|
206
|
+
],
|
|
207
|
+
values: [
|
|
208
|
+
-1
|
|
209
|
+
]
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
types: [
|
|
213
|
+
"uint48"
|
|
214
|
+
],
|
|
215
|
+
values: [
|
|
216
|
+
12
|
|
217
|
+
]
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
types: [
|
|
221
|
+
"int16",
|
|
222
|
+
"uint48"
|
|
223
|
+
],
|
|
224
|
+
values: [
|
|
225
|
+
-1,
|
|
226
|
+
12
|
|
227
|
+
]
|
|
228
|
+
}
|
|
229
|
+
];
|
|
230
|
+
testSolidityKeccak256(inputs);
|
|
231
|
+
});
|
|
232
|
+
it("should match ethers.js array parsing", function() {
|
|
233
|
+
var inputs = [
|
|
234
|
+
{
|
|
235
|
+
types: [
|
|
236
|
+
"string[2]"
|
|
237
|
+
],
|
|
238
|
+
values: [
|
|
239
|
+
[
|
|
240
|
+
"some-text",
|
|
241
|
+
"additional-text"
|
|
242
|
+
]
|
|
243
|
+
]
|
|
244
|
+
}
|
|
245
|
+
];
|
|
246
|
+
testSolidityKeccak256(inputs);
|
|
247
|
+
});
|
|
248
|
+
it("should match ethers.js when passing different types into the same solidityKeccak256 function call", function() {
|
|
249
|
+
var inputs = [
|
|
250
|
+
{
|
|
251
|
+
types: [
|
|
252
|
+
"int16",
|
|
253
|
+
"string",
|
|
254
|
+
"bool"
|
|
255
|
+
],
|
|
256
|
+
values: [
|
|
257
|
+
-5,
|
|
258
|
+
"essential-eth",
|
|
259
|
+
true
|
|
260
|
+
]
|
|
261
|
+
},
|
|
262
|
+
{
|
|
263
|
+
types: [
|
|
264
|
+
"address",
|
|
265
|
+
"uint48",
|
|
266
|
+
"bytes4"
|
|
267
|
+
],
|
|
268
|
+
values: [
|
|
269
|
+
"0xB5503a7db1A9105cd459D99153e69a76a8EF1530",
|
|
270
|
+
20,
|
|
271
|
+
[
|
|
272
|
+
116,
|
|
273
|
+
101,
|
|
274
|
+
115,
|
|
275
|
+
116
|
|
276
|
+
]
|
|
277
|
+
]
|
|
278
|
+
},
|
|
279
|
+
{
|
|
280
|
+
types: [
|
|
281
|
+
"string[3]",
|
|
282
|
+
"bytes",
|
|
283
|
+
"bool[2]"
|
|
284
|
+
],
|
|
285
|
+
values: [
|
|
286
|
+
[
|
|
287
|
+
"ethereum",
|
|
288
|
+
"blockchain",
|
|
289
|
+
"204"
|
|
290
|
+
],
|
|
291
|
+
[
|
|
292
|
+
115,
|
|
293
|
+
111,
|
|
294
|
+
108,
|
|
295
|
+
105,
|
|
296
|
+
100,
|
|
297
|
+
105,
|
|
298
|
+
116,
|
|
299
|
+
121
|
|
300
|
+
],
|
|
301
|
+
[
|
|
302
|
+
false,
|
|
303
|
+
true
|
|
304
|
+
]
|
|
305
|
+
]
|
|
306
|
+
}
|
|
307
|
+
];
|
|
308
|
+
testSolidityKeccak256(inputs);
|
|
309
|
+
});
|
|
310
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { utils } from "ethers";
|
|
2
|
+
import { splitSignature } from "../split-signature";
|
|
3
|
+
describe("splitSignature", function() {
|
|
4
|
+
it("should match ethers.js", function() {
|
|
5
|
+
var signatures = [
|
|
6
|
+
"0x60bc4ed91f2021aefe7045f3f77bd12f87eb733aee24bd1965343b3c27b3971647252185b7d2abb411b01b5d1ac4ab41ea486df1e9b396758c1aec6c1b6eee331b",
|
|
7
|
+
"0x60bc4ed91f2021aefe7045f3f77bd12f87eb733aee24bd1965343b3c27b3971647252185b7d2abb411b01b5d1ac4ab41ea486df1e9b396758c1aec6c1b6eee33"
|
|
8
|
+
];
|
|
9
|
+
signatures.forEach(function(signature) {
|
|
10
|
+
expect(splitSignature(signature)).toStrictEqual(utils.splitSignature(signature));
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
it("should throw error on invalid signature", function() {
|
|
14
|
+
var signature = "0x60bc4ed91f2021aefe7045f3f77bd12f87eb733aee24bd1965343b3c27b3971647252185b7d2abb411b01b5d1ac4ab41ea486df1e9b396758c1aec6c1b6eee33238423849234765127"; //invalid signature length
|
|
15
|
+
expect(function() {
|
|
16
|
+
splitSignature(signature);
|
|
17
|
+
}).toThrow("invalid signature string");
|
|
18
|
+
});
|
|
19
|
+
it("should throw error on invalid v byte", function() {
|
|
20
|
+
var signature = "0x60bc4ed91f2021aefe7045f3f77bd12f87eb733aee24bd1965343b3c27b3971647252185b7d2abb411b01b5d1ac4ab41ea486df1e9b396758c1aec6c1b6eee3305";
|
|
21
|
+
expect(function() {
|
|
22
|
+
splitSignature(signature);
|
|
23
|
+
}).toThrow("signature invalid v byte");
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { utils } from "ethers";
|
|
2
|
+
import web3 from "web3";
|
|
3
|
+
import { toChecksumAddress } from "../../index";
|
|
4
|
+
describe("ether-to-wei", function() {
|
|
5
|
+
it("happy path", function() {
|
|
6
|
+
var addresses = [
|
|
7
|
+
"0x52908400098527886E0F7030069857D2E4169EE7",
|
|
8
|
+
"0x8617E340B3D01FA5F11F306F4090FD50E238070D",
|
|
9
|
+
"0xde709f2102306220921060314715629080e2fb77",
|
|
10
|
+
"0x27b1fdb04752bbc536007a920d24acb045561c26",
|
|
11
|
+
"0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed",
|
|
12
|
+
"0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359",
|
|
13
|
+
"0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB",
|
|
14
|
+
"0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"
|
|
15
|
+
];
|
|
16
|
+
addresses.forEach(function(address) {
|
|
17
|
+
expect(toChecksumAddress(address)).toStrictEqual(utils.getAddress(address));
|
|
18
|
+
expect(toChecksumAddress(address)).toStrictEqual(web3.utils.toChecksumAddress(address));
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
it("invalid inputs", function() {
|
|
22
|
+
var invalidChecksumAddress = "0x8617E340b3D01FA5F11F306F4090FD50E238070D";
|
|
23
|
+
expect(function() {
|
|
24
|
+
toChecksumAddress(invalidChecksumAddress);
|
|
25
|
+
}).toThrow('Invalid Checksum address for "'.concat(invalidChecksumAddress, '"'));
|
|
26
|
+
expect(function() {
|
|
27
|
+
// @ts-expect-error should not accept boolean
|
|
28
|
+
toChecksumAddress(false);
|
|
29
|
+
}).toThrow("string required. Received boolean");
|
|
30
|
+
expect(function() {
|
|
31
|
+
// @ts-expect-error should not accept array
|
|
32
|
+
toChecksumAddress([
|
|
33
|
+
1,
|
|
34
|
+
2,
|
|
35
|
+
3
|
|
36
|
+
]);
|
|
37
|
+
}).toThrow("string required. Received object");
|
|
38
|
+
expect(function() {
|
|
39
|
+
toChecksumAddress("0x1");
|
|
40
|
+
}).toThrow('Invalid Ethereum address "0x1"');
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { utils } from "ethers";
|
|
2
|
+
import { toUtf8Bytes } from "../../index";
|
|
3
|
+
describe("utils.toUtf8Bytes", function() {
|
|
4
|
+
it("should match ethers.js", function() {
|
|
5
|
+
var inputs = [
|
|
6
|
+
"0xa",
|
|
7
|
+
"1",
|
|
8
|
+
"false"
|
|
9
|
+
];
|
|
10
|
+
inputs.forEach(function(input) {
|
|
11
|
+
expect(toUtf8Bytes(input)).toStrictEqual(utils.toUtf8Bytes(input));
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import Big from "big.js";
|
|
2
|
+
import * as ethers from "ethers";
|
|
3
|
+
import web3 from "web3";
|
|
4
|
+
import { tinyBig, weiToEther } from "../../index";
|
|
5
|
+
describe("wei-to-ether", function() {
|
|
6
|
+
it("happy path", function() {
|
|
7
|
+
expect(weiToEther("100000000000000000000.0").toString()).toBe("100");
|
|
8
|
+
expect(weiToEther(100000000000000000000.0).toString()).toBe("100");
|
|
9
|
+
expect(weiToEther("1000000000000000000000.0").toNumber()).toBe(1000);
|
|
10
|
+
expect(weiToEther(1000000000000000000000.0).toNumber()).toBe(1000);
|
|
11
|
+
expect(weiToEther(tinyBig("1000000000000000000000.0")).toNumber()).toBe(1000);
|
|
12
|
+
expect(weiToEther(tinyBig(1000000000000000000000.0)).toNumber()).toBe(1000);
|
|
13
|
+
expect(weiToEther(Big("1000000000000000000000.0")).toNumber()).toBe(1000);
|
|
14
|
+
expect(weiToEther(Big(1000000000000000000000.0)).toNumber()).toBe(1000);
|
|
15
|
+
});
|
|
16
|
+
describe("matches ethers and web3 toString", function() {
|
|
17
|
+
it("toString", function() {
|
|
18
|
+
expect(weiToEther("10").toString()).toStrictEqual(ethers.utils.formatEther("10"));
|
|
19
|
+
expect(weiToEther("1000000000000000000000").toString()).toBe("1000");
|
|
20
|
+
expect(weiToEther("10").toString()).toStrictEqual(web3.utils.fromWei("10", "ether"));
|
|
21
|
+
expect(weiToEther("1000000000000000000000").toString()).toStrictEqual(web3.utils.fromWei("1000000000000000000000", "ether"));
|
|
22
|
+
});
|
|
23
|
+
it("toNumber", function() {
|
|
24
|
+
/* easy */ expect(weiToEther("9").toNumber()).toStrictEqual(Number(ethers.utils.formatEther("9")));
|
|
25
|
+
// expect(weiToEther("9").toNumber()).toStrictEqual(
|
|
26
|
+
// Number(ethers.utils.formatEther("9")),
|
|
27
|
+
// );
|
|
28
|
+
expect(weiToEther("9").toNumber()).toStrictEqual(Number(web3.utils.fromWei("9", "ether")));
|
|
29
|
+
/* hard because they respond with scientific notation or overflowing */ expect(weiToEther("999999").toNumber()).toStrictEqual(Number(ethers.utils.formatEther("999999")));
|
|
30
|
+
expect(weiToEther("999999").toNumber()).toStrictEqual(Number(web3.utils.fromWei("999999", "ether")));
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
it("support hex", function() {
|
|
34
|
+
expect(weiToEther("0x14").toString()).toStrictEqual(ethers.utils.formatEther("20"));
|
|
35
|
+
expect(weiToEther(0x14).toString()).toStrictEqual(ethers.utils.formatEther("20"));
|
|
36
|
+
});
|
|
37
|
+
it("wrong types", function() {
|
|
38
|
+
expect(function() {
|
|
39
|
+
// @ts-expect-error should not accept boolean
|
|
40
|
+
weiToEther(false);
|
|
41
|
+
}).toThrow();
|
|
42
|
+
expect(function() {
|
|
43
|
+
// @ts-expect-error should not accept array
|
|
44
|
+
weiToEther([
|
|
45
|
+
1,
|
|
46
|
+
2,
|
|
47
|
+
3
|
|
48
|
+
]);
|
|
49
|
+
}).toThrow();
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.toChecksumAddress = void 0;
|
|
4
|
-
const sha3_1 = require("sha3");
|
|
5
|
-
const validate_type_1 = require("../shared/validate-type");
|
|
1
|
+
import { Keccak } from "sha3";
|
|
2
|
+
import { validateType } from "../shared/validate-type";
|
|
6
3
|
/**
|
|
7
4
|
* Returns an Ethereum address in proper mixed-case checksum.
|
|
8
5
|
* Does NOT support ICAP
|
|
@@ -18,29 +15,27 @@ const validate_type_1 = require("../shared/validate-type");
|
|
|
18
15
|
* Similar to ["getAddress" in ethers.js](https://docs.ethers.io/v5/api/utils/address/#utils-getAddress)
|
|
19
16
|
*
|
|
20
17
|
* Similar to ["toChecksumAddress" in web3.js](https://web3js.readthedocs.io/en/v1.7.1/web3-utils.html#tochecksumaddress)
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
*/ export function toChecksumAddress(address) {
|
|
19
|
+
validateType(address, [
|
|
20
|
+
"string"
|
|
21
|
+
]);
|
|
24
22
|
if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
|
|
25
|
-
throw new Error(
|
|
23
|
+
throw new Error('Invalid Ethereum address "'.concat(address, '"'));
|
|
26
24
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
for
|
|
25
|
+
var _address = address.toLowerCase().replace(/^0x/i, "");
|
|
26
|
+
var keccak = new Keccak(256);
|
|
27
|
+
var addressHash = keccak.update(_address).digest("hex").replace(/^0x/i, "");
|
|
28
|
+
var checksumAddress = "0x";
|
|
29
|
+
for(var i = 0; i < _address.length; i++){
|
|
32
30
|
// If ith character is 8 to f then make it uppercase
|
|
33
31
|
if (parseInt(addressHash[i], 16) > 7) {
|
|
34
32
|
checksumAddress += _address[i].toUpperCase();
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
33
|
+
} else {
|
|
37
34
|
checksumAddress += _address[i];
|
|
38
35
|
}
|
|
39
36
|
}
|
|
40
|
-
if (address.match(/([A-F].*[a-f])|([a-f].*[A-F])/) &&
|
|
41
|
-
|
|
42
|
-
throw new Error(`Invalid Checksum address for "${address}"`);
|
|
37
|
+
if (address.match(/([A-F].*[a-f])|([a-f].*[A-F])/) && checksumAddress !== address) {
|
|
38
|
+
throw new Error('Invalid Checksum address for "'.concat(address, '"'));
|
|
43
39
|
}
|
|
44
40
|
return checksumAddress;
|
|
45
41
|
}
|
|
46
|
-
exports.toChecksumAddress = toChecksumAddress;
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toUtf8Bytes = void 0;
|
|
4
1
|
/**
|
|
5
2
|
* Converts a string into a UTF-8 Byte Array
|
|
6
3
|
*
|
|
@@ -14,8 +11,6 @@ exports.toUtf8Bytes = void 0;
|
|
|
14
11
|
* toUtf8Bytes('ethereum');
|
|
15
12
|
* // Uint8Array { [Iterator] 0: 101, 1: 116, 2: 104, 3: 101, 4: 114, 5: 101, 6: 117, 7: 109 }
|
|
16
13
|
* ```
|
|
17
|
-
*/
|
|
18
|
-
function toUtf8Bytes(data) {
|
|
14
|
+
*/ export function toUtf8Bytes(data) {
|
|
19
15
|
return new Uint8Array(Buffer.from(data));
|
|
20
16
|
}
|
|
21
|
-
exports.toUtf8Bytes = toUtf8Bytes;
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.weiToEther = void 0;
|
|
4
|
-
const tiny_big_1 = require("../shared/tiny-big/tiny-big");
|
|
5
|
-
const validate_type_1 = require("../shared/validate-type");
|
|
1
|
+
import { tinyBig } from "../shared/tiny-big/tiny-big";
|
|
2
|
+
import { validateType } from "../shared/validate-type";
|
|
6
3
|
/**
|
|
7
4
|
* Convert from Wei to Ether
|
|
8
5
|
*
|
|
@@ -26,20 +23,21 @@ const validate_type_1 = require("../shared/validate-type");
|
|
|
26
23
|
* weiToEther(1000000000000000000000).toNumber()
|
|
27
24
|
* // 1000
|
|
28
25
|
* ```
|
|
29
|
-
*/
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
*/ export function weiToEther(weiQuantity) {
|
|
27
|
+
validateType(weiQuantity, [
|
|
28
|
+
"string",
|
|
29
|
+
"number",
|
|
30
|
+
"object"
|
|
31
|
+
]);
|
|
32
32
|
// eslint-disable-next-line no-useless-catch
|
|
33
33
|
try {
|
|
34
|
-
|
|
35
|
-
if (typeof weiQuantity ===
|
|
34
|
+
var _weiQuantity = weiQuantity;
|
|
35
|
+
if (typeof weiQuantity === "string" && weiQuantity.slice(0, 2) === "0x") {
|
|
36
36
|
_weiQuantity = BigInt(weiQuantity).toString();
|
|
37
37
|
}
|
|
38
|
-
|
|
39
|
-
return
|
|
40
|
-
}
|
|
41
|
-
catch (error) {
|
|
38
|
+
var result = tinyBig(_weiQuantity).div("1000000000000000000");
|
|
39
|
+
return tinyBig(result);
|
|
40
|
+
} catch (error) {
|
|
42
41
|
throw error;
|
|
43
42
|
}
|
|
44
43
|
}
|
|
45
|
-
exports.weiToEther = weiToEther;
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import type { BlockResponse } from '
|
|
2
|
-
import type { RPCBlock } from '../../types/Block.types';
|
|
1
|
+
import type { BlockResponse, RPCBlock } from '../../types/Block.types';
|
|
3
2
|
export declare function cleanBlock(block: RPCBlock, returnTransactionObjects: boolean): BlockResponse;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { toChecksumAddress } from '../..';
|
|
2
1
|
import { tinyBig } from '../../shared/tiny-big/tiny-big';
|
|
2
|
+
import { toChecksumAddress } from '../../utils/to-checksum-address';
|
|
3
3
|
import { cleanTransaction } from './clean-transaction';
|
|
4
4
|
import { hexToDecimal } from './hex-to-decimal';
|
|
5
5
|
export function cleanBlock(block, returnTransactionObjects) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { tinyBig
|
|
1
|
+
import { tinyBig } from '../../shared/tiny-big/tiny-big';
|
|
2
|
+
import { toChecksumAddress } from '../../utils/to-checksum-address';
|
|
2
3
|
import { cleanLog } from './clean-log';
|
|
3
4
|
import { cleanTransaction } from './clean-transaction';
|
|
4
5
|
import { hexToDecimal } from './hex-to-decimal';
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { tinyBig
|
|
1
|
+
import { tinyBig } from '../../shared/tiny-big/tiny-big';
|
|
2
|
+
import { toChecksumAddress } from '../../utils/to-checksum-address';
|
|
2
3
|
import { hexToDecimal } from './hex-to-decimal';
|
|
3
4
|
export function cleanTransaction(transaction) {
|
|
4
5
|
const cleanedTransaction = Object.assign({}, transaction);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { JSONABIArgument } from '../../types/Contract.types';
|
|
2
2
|
export declare const hexFalse: string;
|
|
3
3
|
export declare function encodeData(jsonABIArgument: JSONABIArgument, args: any[]): string;
|
|
4
|
-
export declare function decodeRPCResponse(jsonABIArgument: JSONABIArgument, nodeResponse: string): string | number | boolean | import("
|
|
4
|
+
export declare function decodeRPCResponse(jsonABIArgument: JSONABIArgument, nodeResponse: string): string | number | boolean | import("../../shared/tiny-big/tiny-big").TinyBig | (string | number | boolean | import("../../shared/tiny-big/tiny-big").TinyBig)[];
|
|
@@ -1,8 +1,29 @@
|
|
|
1
1
|
import { Keccak } from 'sha3';
|
|
2
|
-
import { tinyBig
|
|
2
|
+
import { tinyBig } from '../../shared/tiny-big/tiny-big';
|
|
3
|
+
import { toChecksumAddress } from '../../utils/to-checksum-address';
|
|
3
4
|
import { hexToDecimal } from './hex-to-decimal';
|
|
4
5
|
export const hexFalse = '0'.repeat(64);
|
|
5
6
|
const hexTrue = '0'.repeat(63) + '1';
|
|
7
|
+
function hexToUtf8(hex) {
|
|
8
|
+
let str = '';
|
|
9
|
+
let i = 0;
|
|
10
|
+
const l = hex.length;
|
|
11
|
+
if (hex.substring(0, 2) === '0x') {
|
|
12
|
+
i = 2;
|
|
13
|
+
}
|
|
14
|
+
for (; i < l; i += 2) {
|
|
15
|
+
const code = parseInt(hex.substr(i, 2), 16);
|
|
16
|
+
if (code === 0)
|
|
17
|
+
continue;
|
|
18
|
+
str += String.fromCharCode(code);
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
return decodeURIComponent(escape(str));
|
|
22
|
+
}
|
|
23
|
+
catch (e) {
|
|
24
|
+
return str;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
6
27
|
function expandType(type) {
|
|
7
28
|
if (type === 'uint[]') {
|
|
8
29
|
return 'uint256[]';
|
|
@@ -75,8 +96,22 @@ export function encodeData(jsonABIArgument, args) {
|
|
|
75
96
|
return data;
|
|
76
97
|
}
|
|
77
98
|
export function decodeRPCResponse(jsonABIArgument, nodeResponse) {
|
|
99
|
+
var _a;
|
|
78
100
|
const rawOutputs = jsonABIArgument.outputs;
|
|
79
|
-
const
|
|
101
|
+
const slicedResponse = nodeResponse.slice(2);
|
|
102
|
+
if (((_a = jsonABIArgument === null || jsonABIArgument === void 0 ? void 0 : jsonABIArgument.outputs) === null || _a === void 0 ? void 0 : _a.length) === 1 &&
|
|
103
|
+
jsonABIArgument.outputs[0].type === 'string') {
|
|
104
|
+
const [hexOffset, responseData] = [
|
|
105
|
+
slicedResponse.slice(0, 64),
|
|
106
|
+
slicedResponse.slice(64),
|
|
107
|
+
];
|
|
108
|
+
const decimalOffset = Number(hexToDecimal(`0x${hexOffset}`));
|
|
109
|
+
const hexLength = responseData.slice(0, decimalOffset * 2);
|
|
110
|
+
const decimalLength = Number(hexToDecimal(`0x${hexLength}`));
|
|
111
|
+
const hexToDecode = responseData.slice(decimalOffset * 2, decimalOffset * 2 + decimalLength * 2);
|
|
112
|
+
return hexToUtf8(hexToDecode);
|
|
113
|
+
}
|
|
114
|
+
const encodedOutputs = slicedResponse.match(/.{1,64}/g);
|
|
80
115
|
const outputs = (encodedOutputs || []).map((output, i) => {
|
|
81
116
|
const outputType = (rawOutputs || [])[i].type;
|
|
82
117
|
switch (outputType) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const version = "0.
|
|
1
|
+
export declare const version = "0.7.0";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = '0.
|
|
1
|
+
export const version = '0.7.0';
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { computePublicKey, toChecksumAddress } from '..';
|
|
2
1
|
import { hexDataSlice } from './bytes';
|
|
2
|
+
import { computePublicKey } from './compute-public-key';
|
|
3
3
|
import { keccak256 } from './keccak256';
|
|
4
|
+
import { toChecksumAddress } from './to-checksum-address';
|
|
4
5
|
export function computeAddress(key) {
|
|
5
6
|
if (!key.startsWith('0x04') &&
|
|
6
7
|
!key.startsWith('0x03') &&
|