essential-eth 0.5.10 → 0.5.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/classes/Contract.d.ts +78 -0
- package/lib/cjs/classes/Contract.js +140 -0
- package/{dist → lib/cjs}/classes/test/Contract/crv-abi.d.ts +2 -2
- package/lib/cjs/classes/test/Contract/crv-abi.js +488 -0
- package/{dist → lib/cjs}/classes/test/Contract/ens-abi.d.ts +2 -2
- package/lib/cjs/classes/test/Contract/ens-abi.js +453 -0
- package/{dist → lib/cjs}/classes/test/Contract/fei-abi.d.ts +2 -2
- package/lib/cjs/classes/test/Contract/fei-abi.js +526 -0
- package/{dist → lib/cjs}/classes/test/Contract/foo-abi.d.ts +2 -2
- package/lib/cjs/classes/test/Contract/foo-abi.js +42 -0
- package/{dist → lib/cjs}/classes/test/Contract/uniswap-abi.d.ts +2 -2
- package/lib/cjs/classes/test/Contract/uniswap-abi.js +121 -0
- package/lib/cjs/classes/utils/clean-block.d.ts +121 -0
- package/lib/cjs/classes/utils/clean-block.js +160 -0
- package/lib/cjs/classes/utils/clean-log.d.ts +9 -0
- package/lib/cjs/classes/utils/clean-log.js +37 -0
- package/lib/cjs/classes/utils/clean-transaction-receipt.d.ts +14 -0
- package/lib/cjs/classes/utils/clean-transaction-receipt.js +50 -0
- package/lib/cjs/classes/utils/clean-transaction.d.ts +14 -0
- package/lib/cjs/classes/utils/clean-transaction.js +51 -0
- package/lib/cjs/classes/utils/encode-decode-transaction.d.ts +16 -0
- package/lib/cjs/classes/utils/encode-decode-transaction.js +137 -0
- package/lib/cjs/classes/utils/fetchers.d.ts +39 -0
- package/lib/cjs/classes/utils/fetchers.js +81 -0
- package/lib/cjs/classes/utils/hex-to-decimal.d.ts +18 -0
- package/lib/cjs/classes/utils/hex-to-decimal.js +24 -0
- package/lib/cjs/classes/utils/prepare-transaction.d.ts +6 -0
- package/lib/cjs/classes/utils/prepare-transaction.js +45 -0
- package/{dist → lib/cjs}/index.d.ts +25 -25
- package/lib/cjs/index.js +54 -0
- package/{dist → lib/cjs}/logger/logger.d.ts +11 -11
- package/lib/cjs/logger/logger.js +36 -0
- package/lib/cjs/logger/package-version.d.ts +1 -0
- package/lib/cjs/logger/package-version.js +5 -0
- package/lib/cjs/providers/BaseProvider.d.ts +353 -0
- package/lib/cjs/providers/BaseProvider.js +512 -0
- package/lib/cjs/providers/FallthroughProvider.d.ts +24 -0
- package/lib/cjs/providers/FallthroughProvider.js +65 -0
- package/lib/cjs/providers/JsonRpcProvider.d.ts +33 -0
- package/lib/cjs/providers/JsonRpcProvider.js +46 -0
- package/{dist → lib/cjs}/providers/test/rpc-urls.d.ts +11 -11
- package/lib/cjs/providers/test/rpc-urls.js +14 -0
- package/{dist → lib/cjs}/providers/utils/chains-info.d.ts +467 -467
- package/lib/cjs/providers/utils/chains-info.js +1402 -0
- package/lib/cjs/shared/tiny-big/helpers.d.ts +17 -0
- package/lib/cjs/shared/tiny-big/helpers.js +97 -0
- package/lib/cjs/shared/tiny-big/tiny-big.d.ts +58 -0
- package/lib/cjs/shared/tiny-big/tiny-big.js +101 -0
- package/{dist → lib/cjs}/shared/validate-type.d.ts +3 -3
- package/lib/cjs/shared/validate-type.js +9 -0
- package/{dist → lib/cjs}/types/Block.types.d.ts +41 -40
- package/lib/cjs/types/Block.types.js +2 -0
- package/{dist → lib/cjs}/types/Contract.types.d.ts +24 -24
- package/lib/cjs/types/Contract.types.js +2 -0
- package/lib/cjs/types/Filter.types.d.ts +16 -0
- package/lib/cjs/types/Filter.types.js +2 -0
- package/lib/cjs/types/Network.types.d.ts +8 -0
- package/lib/cjs/types/Network.types.js +2 -0
- package/lib/cjs/types/Transaction.types.d.ts +123 -0
- package/lib/cjs/types/Transaction.types.js +2 -0
- package/lib/cjs/utils/bytes.d.ts +289 -0
- package/lib/cjs/utils/bytes.js +711 -0
- package/lib/cjs/utils/compute-address.d.ts +17 -0
- package/lib/cjs/utils/compute-address.js +33 -0
- package/lib/cjs/utils/compute-public-key.d.ts +18 -0
- package/lib/cjs/utils/compute-public-key.js +26 -0
- package/lib/cjs/utils/ether-to-gwei.d.ts +27 -0
- package/lib/cjs/utils/ether-to-gwei.js +35 -0
- package/lib/cjs/utils/ether-to-wei.d.ts +27 -0
- package/lib/cjs/utils/ether-to-wei.js +35 -0
- package/lib/cjs/utils/gwei-to-ether.d.ts +27 -0
- package/lib/cjs/utils/gwei-to-ether.js +35 -0
- package/lib/cjs/utils/hash-message.d.ts +14 -0
- package/lib/cjs/utils/hash-message.js +28 -0
- package/lib/cjs/utils/is-address.d.ts +24 -0
- package/lib/cjs/utils/is-address.js +39 -0
- package/lib/cjs/utils/keccak256.d.ts +16 -0
- package/lib/cjs/utils/keccak256.js +31 -0
- package/lib/cjs/utils/solidity-keccak256.d.ts +39 -0
- package/lib/cjs/utils/solidity-keccak256.js +138 -0
- package/lib/cjs/utils/split-signature.d.ts +24 -0
- package/lib/cjs/utils/split-signature.js +163 -0
- package/lib/cjs/utils/to-checksum-address.d.ts +17 -0
- package/lib/cjs/utils/to-checksum-address.js +46 -0
- package/lib/cjs/utils/to-utf8-bytes.d.ts +15 -0
- package/lib/cjs/utils/to-utf8-bytes.js +21 -0
- package/lib/cjs/utils/wei-to-ether.d.ts +27 -0
- package/lib/cjs/utils/wei-to-ether.js +45 -0
- package/{dist → lib/esm}/classes/Contract.d.ts +11 -11
- package/lib/esm/classes/Contract.js +65 -0
- package/lib/esm/classes/test/Contract/crv-abi.d.ts +2 -0
- package/lib/esm/classes/test/Contract/crv-abi.js +470 -0
- package/lib/esm/classes/test/Contract/ens-abi.d.ts +2 -0
- package/lib/esm/classes/test/Contract/ens-abi.js +435 -0
- package/lib/esm/classes/test/Contract/fei-abi.d.ts +2 -0
- package/lib/esm/classes/test/Contract/fei-abi.js +521 -0
- package/lib/esm/classes/test/Contract/foo-abi.d.ts +2 -0
- package/lib/esm/classes/test/Contract/foo-abi.js +33 -0
- package/lib/esm/classes/test/Contract/uniswap-abi.d.ts +2 -0
- package/lib/esm/classes/test/Contract/uniswap-abi.js +116 -0
- package/{dist → lib/esm}/classes/utils/clean-block.d.ts +3 -3
- package/lib/esm/classes/utils/clean-block.js +35 -0
- package/{dist → lib/esm}/classes/utils/clean-log.d.ts +2 -2
- package/lib/esm/classes/utils/clean-log.js +26 -0
- package/{dist → lib/esm}/classes/utils/clean-transaction-receipt.d.ts +2 -2
- package/lib/esm/classes/utils/clean-transaction-receipt.js +34 -0
- package/{dist → lib/esm}/classes/utils/clean-transaction.d.ts +2 -2
- package/lib/esm/classes/utils/clean-transaction.js +33 -0
- package/{dist → lib/esm}/classes/utils/encode-decode-transaction.d.ts +4 -4
- package/lib/esm/classes/utils/encode-decode-transaction.js +98 -0
- package/{dist → lib/esm}/classes/utils/fetchers.d.ts +9 -9
- package/lib/esm/classes/utils/fetchers.js +43 -0
- package/{dist → lib/esm}/classes/utils/hex-to-decimal.d.ts +1 -1
- package/lib/esm/classes/utils/hex-to-decimal.js +3 -0
- package/{dist → lib/esm}/classes/utils/prepare-transaction.d.ts +2 -2
- package/lib/esm/classes/utils/prepare-transaction.js +34 -0
- package/lib/esm/index.d.ts +25 -0
- package/lib/esm/index.js +20 -0
- package/lib/esm/logger/logger.d.ts +11 -0
- package/lib/esm/logger/logger.js +33 -0
- package/lib/esm/logger/package-version.d.ts +1 -0
- package/lib/esm/logger/package-version.js +1 -0
- package/{dist → lib/esm}/providers/BaseProvider.d.ts +24 -24
- package/lib/esm/providers/BaseProvider.js +157 -0
- package/{dist → lib/esm}/providers/FallthroughProvider.d.ts +11 -11
- package/lib/esm/providers/FallthroughProvider.js +41 -0
- package/{dist → lib/esm}/providers/JsonRpcProvider.d.ts +7 -7
- package/lib/esm/providers/JsonRpcProvider.js +15 -0
- package/lib/esm/providers/test/rpc-urls.d.ts +11 -0
- package/lib/esm/providers/test/rpc-urls.js +11 -0
- package/lib/esm/providers/utils/chains-info.d.ts +467 -0
- package/lib/esm/providers/utils/chains-info.js +1398 -0
- package/{dist → lib/esm}/shared/tiny-big/helpers.d.ts +1 -1
- package/lib/esm/shared/tiny-big/helpers.js +58 -0
- package/{dist → lib/esm}/shared/tiny-big/tiny-big.d.ts +10 -10
- package/lib/esm/shared/tiny-big/tiny-big.js +45 -0
- package/lib/esm/shared/validate-type.d.ts +3 -0
- package/lib/esm/shared/validate-type.js +5 -0
- package/lib/esm/types/Block.types.d.ts +40 -0
- package/lib/esm/types/Block.types.js +1 -0
- package/lib/esm/types/Contract.types.d.ts +24 -0
- package/lib/esm/types/Contract.types.js +1 -0
- package/{dist → lib/esm}/types/Filter.types.d.ts +12 -12
- package/lib/esm/types/Filter.types.js +1 -0
- package/{dist → lib/esm}/types/Network.types.d.ts +5 -5
- package/lib/esm/types/Network.types.js +1 -0
- package/{dist → lib/esm}/types/Transaction.types.d.ts +113 -113
- package/lib/esm/types/Transaction.types.js +1 -0
- package/{dist → lib/esm}/utils/bytes.d.ts +40 -40
- package/lib/esm/utils/bytes.js +245 -0
- package/{dist → lib/esm}/utils/compute-address.d.ts +1 -1
- package/lib/esm/utils/compute-address.js +11 -0
- package/{dist → lib/esm}/utils/compute-public-key.d.ts +2 -2
- package/lib/esm/utils/compute-public-key.js +6 -0
- package/{dist → lib/esm}/utils/ether-to-gwei.d.ts +3 -3
- package/lib/esm/utils/ether-to-gwei.js +7 -0
- package/{dist → lib/esm}/utils/ether-to-wei.d.ts +3 -3
- package/lib/esm/utils/ether-to-wei.js +7 -0
- package/{dist → lib/esm}/utils/gwei-to-ether.d.ts +3 -3
- package/lib/esm/utils/gwei-to-ether.js +7 -0
- package/{dist → lib/esm}/utils/hash-message.d.ts +2 -2
- package/lib/esm/utils/hash-message.js +12 -0
- package/{dist → lib/esm}/utils/is-address.d.ts +1 -1
- package/lib/esm/utils/is-address.js +12 -0
- package/{dist → lib/esm}/utils/keccak256.d.ts +2 -2
- package/lib/esm/utils/keccak256.js +13 -0
- package/{dist → lib/esm}/utils/solidity-keccak256.d.ts +2 -2
- package/lib/esm/utils/solidity-keccak256.js +85 -0
- package/{dist → lib/esm}/utils/split-signature.d.ts +2 -2
- package/lib/esm/utils/split-signature.js +126 -0
- package/{dist → lib/esm}/utils/to-checksum-address.d.ts +1 -1
- package/lib/esm/utils/to-checksum-address.js +25 -0
- package/{dist → lib/esm}/utils/to-utf8-bytes.d.ts +1 -1
- package/lib/esm/utils/to-utf8-bytes.js +3 -0
- package/{dist → lib/esm}/utils/wei-to-ether.d.ts +3 -3
- package/lib/esm/utils/wei-to-ether.js +16 -0
- package/package.json +14 -37
- package/dist/index.cjs +0 -2
- package/dist/index.cjs.map +0 -1
- package/dist/index.js +0 -2
- package/dist/index.js.map +0 -1
- package/dist/index.modern.js +0 -2
- package/dist/index.modern.js.map +0 -1
- package/dist/index.modern.mjs +0 -2
- package/dist/index.modern.mjs.map +0 -1
- package/dist/index.module.js +0 -2
- package/dist/index.module.js.map +0 -1
- package/dist/index.umd.js +0 -2
- package/dist/index.umd.js.map +0 -1
- package/dist/logger/package-version.d.ts +0 -1
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { logger } from '../logger/logger';
|
|
2
|
+
function isHexable(value) {
|
|
3
|
+
return !!value.toHexString;
|
|
4
|
+
}
|
|
5
|
+
export function isBytesLike(value) {
|
|
6
|
+
return (isHexString(value) && !(value.length % 2)) || isBytes(value);
|
|
7
|
+
}
|
|
8
|
+
function isInteger(value) {
|
|
9
|
+
return typeof value === 'number' && value == value && value % 1 === 0;
|
|
10
|
+
}
|
|
11
|
+
export function isBytes(value) {
|
|
12
|
+
if (value == null) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
if (value.constructor === Uint8Array) {
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
if (typeof value === 'string') {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
if (!isInteger(value.length) || value.length < 0) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
for (let i = 0; i < value.length; i++) {
|
|
25
|
+
const v = value[i];
|
|
26
|
+
if (!isInteger(v) || v < 0 || v >= 256) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
export function arrayify(value, options) {
|
|
33
|
+
if (!options) {
|
|
34
|
+
options = {};
|
|
35
|
+
}
|
|
36
|
+
if (typeof value === 'number') {
|
|
37
|
+
logger.checkSafeUint53(value, 'invalid arrayify value');
|
|
38
|
+
const result = [];
|
|
39
|
+
while (value) {
|
|
40
|
+
result.unshift(value & 0xff);
|
|
41
|
+
value = parseInt(String(value / 256));
|
|
42
|
+
}
|
|
43
|
+
if (result.length === 0) {
|
|
44
|
+
result.push(0);
|
|
45
|
+
}
|
|
46
|
+
return new Uint8Array(result);
|
|
47
|
+
}
|
|
48
|
+
if (options.allowMissingPrefix &&
|
|
49
|
+
typeof value === 'string' &&
|
|
50
|
+
value.substring(0, 2) !== '0x') {
|
|
51
|
+
value = '0x' + value;
|
|
52
|
+
}
|
|
53
|
+
if (isHexable(value)) {
|
|
54
|
+
value = value.toHexString();
|
|
55
|
+
}
|
|
56
|
+
if (isHexString(value)) {
|
|
57
|
+
let hex = value.substring(2);
|
|
58
|
+
if (hex.length % 2) {
|
|
59
|
+
if (options.hexPad === 'left') {
|
|
60
|
+
hex = '0' + hex;
|
|
61
|
+
}
|
|
62
|
+
else if (options.hexPad === 'right') {
|
|
63
|
+
hex += '0';
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
logger.throwArgumentError('hex data is odd-length', 'value', value);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const result = [];
|
|
70
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
71
|
+
result.push(parseInt(hex.substring(i, i + 2), 16));
|
|
72
|
+
}
|
|
73
|
+
return new Uint8Array(result);
|
|
74
|
+
}
|
|
75
|
+
if (isBytes(value)) {
|
|
76
|
+
return new Uint8Array(value);
|
|
77
|
+
}
|
|
78
|
+
return logger.throwArgumentError('invalid arrayify value', 'value', value);
|
|
79
|
+
}
|
|
80
|
+
export function concat(arrayOfBytesLike) {
|
|
81
|
+
const objects = arrayOfBytesLike.map((item) => arrayify(item));
|
|
82
|
+
const length = objects.reduce((accum, item) => accum + item.length, 0);
|
|
83
|
+
const result = new Uint8Array(length);
|
|
84
|
+
objects.reduce((offset, object) => {
|
|
85
|
+
result.set(object, offset);
|
|
86
|
+
return offset + object.length;
|
|
87
|
+
}, 0);
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
export function stripZeros(value) {
|
|
91
|
+
let result = arrayify(value);
|
|
92
|
+
if (result.length === 0) {
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
let start = 0;
|
|
96
|
+
while (start < result.length && result[start] === 0) {
|
|
97
|
+
start++;
|
|
98
|
+
}
|
|
99
|
+
if (start) {
|
|
100
|
+
result = result.slice(start);
|
|
101
|
+
}
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
export function zeroPad(value, length) {
|
|
105
|
+
value = arrayify(value);
|
|
106
|
+
if (value.length > length) {
|
|
107
|
+
logger.throwArgumentError('value out of range', 'value', value);
|
|
108
|
+
}
|
|
109
|
+
const result = new Uint8Array(length);
|
|
110
|
+
result.set(value, length - value.length);
|
|
111
|
+
return result;
|
|
112
|
+
}
|
|
113
|
+
export function isHexString(value, length) {
|
|
114
|
+
if (typeof value !== 'string' || !value.match(/^0x[0-9A-Fa-f]*$/)) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
if (length && value.length !== 2 + 2 * length) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
const HexCharacters = '0123456789abcdef';
|
|
123
|
+
export function hexlify(value, options) {
|
|
124
|
+
if (!options) {
|
|
125
|
+
options = {};
|
|
126
|
+
}
|
|
127
|
+
if (typeof value === 'number') {
|
|
128
|
+
logger.checkSafeUint53(value, 'invalid hexlify value');
|
|
129
|
+
let hex = '';
|
|
130
|
+
while (value) {
|
|
131
|
+
hex = HexCharacters[value & 0xf] + hex;
|
|
132
|
+
value = Math.floor(value / 16);
|
|
133
|
+
}
|
|
134
|
+
if (hex.length) {
|
|
135
|
+
if (hex.length % 2) {
|
|
136
|
+
hex = '0' + hex;
|
|
137
|
+
}
|
|
138
|
+
return '0x' + hex;
|
|
139
|
+
}
|
|
140
|
+
return '0x00';
|
|
141
|
+
}
|
|
142
|
+
if (typeof value === 'bigint') {
|
|
143
|
+
value = value.toString(16);
|
|
144
|
+
if (value.length % 2) {
|
|
145
|
+
return '0x0' + value;
|
|
146
|
+
}
|
|
147
|
+
return '0x' + value;
|
|
148
|
+
}
|
|
149
|
+
if (options.allowMissingPrefix &&
|
|
150
|
+
typeof value === 'string' &&
|
|
151
|
+
value.substring(0, 2) !== '0x') {
|
|
152
|
+
value = '0x' + value;
|
|
153
|
+
}
|
|
154
|
+
if (isHexable(value)) {
|
|
155
|
+
return value.toHexString();
|
|
156
|
+
}
|
|
157
|
+
if (isHexString(value)) {
|
|
158
|
+
if (value.length % 2) {
|
|
159
|
+
if (options.hexPad === 'left') {
|
|
160
|
+
value = '0x0' + value.substring(2);
|
|
161
|
+
}
|
|
162
|
+
else if (options.hexPad === 'right') {
|
|
163
|
+
value += '0';
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
logger.throwArgumentError('hex data is odd-length', 'value', value);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return value.toLowerCase();
|
|
170
|
+
}
|
|
171
|
+
if (isBytes(value)) {
|
|
172
|
+
let result = '0x';
|
|
173
|
+
for (let i = 0; i < value.length; i++) {
|
|
174
|
+
const v = value[i];
|
|
175
|
+
result += HexCharacters[(v & 0xf0) >> 4] + HexCharacters[v & 0x0f];
|
|
176
|
+
}
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
return logger.throwArgumentError('invalid hexlify value', 'value', value);
|
|
180
|
+
}
|
|
181
|
+
export function hexDataLength(data) {
|
|
182
|
+
if (typeof data !== 'string') {
|
|
183
|
+
data = hexlify(data);
|
|
184
|
+
}
|
|
185
|
+
else if (!isHexString(data) || data.length % 2) {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
return (data.length - 2) / 2;
|
|
189
|
+
}
|
|
190
|
+
export function hexDataSlice(data, offset, endOffset) {
|
|
191
|
+
if (typeof data !== 'string') {
|
|
192
|
+
data = hexlify(data);
|
|
193
|
+
}
|
|
194
|
+
else if (!isHexString(data) || data.length % 2) {
|
|
195
|
+
logger.throwArgumentError('invalid hexData', 'value', data);
|
|
196
|
+
}
|
|
197
|
+
offset = 2 + 2 * offset;
|
|
198
|
+
if (endOffset != null) {
|
|
199
|
+
return '0x' + data.substring(offset, 2 + 2 * endOffset);
|
|
200
|
+
}
|
|
201
|
+
return '0x' + data.substring(offset);
|
|
202
|
+
}
|
|
203
|
+
export function hexConcat(items) {
|
|
204
|
+
let result = '0x';
|
|
205
|
+
items.forEach((item) => {
|
|
206
|
+
result += hexlify(item).substring(2);
|
|
207
|
+
});
|
|
208
|
+
return result;
|
|
209
|
+
}
|
|
210
|
+
export function hexValue(value) {
|
|
211
|
+
const trimmed = hexStripZeros(hexlify(value, { hexPad: 'left' }));
|
|
212
|
+
if (trimmed === '0x') {
|
|
213
|
+
return '0x0';
|
|
214
|
+
}
|
|
215
|
+
return trimmed;
|
|
216
|
+
}
|
|
217
|
+
export function hexStripZeros(value) {
|
|
218
|
+
if (typeof value !== 'string') {
|
|
219
|
+
value = hexlify(value);
|
|
220
|
+
}
|
|
221
|
+
if (!isHexString(value)) {
|
|
222
|
+
logger.throwArgumentError('invalid hex string', 'value', value);
|
|
223
|
+
}
|
|
224
|
+
value = value.substring(2);
|
|
225
|
+
let offset = 0;
|
|
226
|
+
while (offset < value.length && value[offset] === '0') {
|
|
227
|
+
offset++;
|
|
228
|
+
}
|
|
229
|
+
return '0x' + value.substring(offset);
|
|
230
|
+
}
|
|
231
|
+
export function hexZeroPad(value, length) {
|
|
232
|
+
if (typeof value !== 'string') {
|
|
233
|
+
value = hexlify(value);
|
|
234
|
+
}
|
|
235
|
+
else if (!isHexString(value)) {
|
|
236
|
+
logger.throwArgumentError('invalid hex string', 'value', value);
|
|
237
|
+
}
|
|
238
|
+
if (value.length > 2 * length + 2) {
|
|
239
|
+
logger.throwError('value out of range', { value, length });
|
|
240
|
+
}
|
|
241
|
+
while (value.length < 2 * length + 2) {
|
|
242
|
+
value = '0x0' + value.substring(2);
|
|
243
|
+
}
|
|
244
|
+
return value;
|
|
245
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function computeAddress(key: string): string;
|
|
1
|
+
export declare function computeAddress(key: string): string;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { computePublicKey, toChecksumAddress } from '..';
|
|
2
|
+
import { hexDataSlice } from './bytes';
|
|
3
|
+
import { keccak256 } from './keccak256';
|
|
4
|
+
export function computeAddress(key) {
|
|
5
|
+
if (!key.startsWith('0x04') &&
|
|
6
|
+
!key.startsWith('0x03') &&
|
|
7
|
+
!key.startsWith('0x02')) {
|
|
8
|
+
key = computePublicKey(key);
|
|
9
|
+
}
|
|
10
|
+
return toChecksumAddress(hexDataSlice(keccak256(hexDataSlice(key, 1)), 12));
|
|
11
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { BytesLike } from './bytes';
|
|
2
|
-
export declare function computePublicKey(privKey: BytesLike): string;
|
|
1
|
+
import type { BytesLike } from './bytes';
|
|
2
|
+
export declare function computePublicKey(privKey: BytesLike): string;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type Big from 'big.js';
|
|
2
|
-
import type { TinyBig } from '../shared/tiny-big/tiny-big';
|
|
3
|
-
export declare function etherToGwei(etherQuantity: string | number | TinyBig | Big): TinyBig;
|
|
1
|
+
import type Big from 'big.js';
|
|
2
|
+
import type { TinyBig } from '../shared/tiny-big/tiny-big';
|
|
3
|
+
export declare function etherToGwei(etherQuantity: string | number | TinyBig | Big): TinyBig;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { tinyBig } from '../shared/tiny-big/tiny-big';
|
|
2
|
+
import { validateType } from '../shared/validate-type';
|
|
3
|
+
export function etherToGwei(etherQuantity) {
|
|
4
|
+
validateType(etherQuantity, ['string', 'number', 'object']);
|
|
5
|
+
const result = tinyBig(etherQuantity).times('1000000000');
|
|
6
|
+
return tinyBig(result);
|
|
7
|
+
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type Big from 'big.js';
|
|
2
|
-
import type { TinyBig } from '../shared/tiny-big/tiny-big';
|
|
3
|
-
export declare function etherToWei(etherQuantity: string | number | TinyBig | Big): TinyBig;
|
|
1
|
+
import type Big from 'big.js';
|
|
2
|
+
import type { TinyBig } from '../shared/tiny-big/tiny-big';
|
|
3
|
+
export declare function etherToWei(etherQuantity: string | number | TinyBig | Big): TinyBig;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { tinyBig } from '../shared/tiny-big/tiny-big';
|
|
2
|
+
import { validateType } from '../shared/validate-type';
|
|
3
|
+
export function etherToWei(etherQuantity) {
|
|
4
|
+
validateType(etherQuantity, ['string', 'number', 'object']);
|
|
5
|
+
const result = tinyBig(etherQuantity).times('1000000000000000000');
|
|
6
|
+
return tinyBig(result);
|
|
7
|
+
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type Big from 'big.js';
|
|
2
|
-
import type { TinyBig } from './../shared/tiny-big/tiny-big';
|
|
3
|
-
export declare function gweiToEther(gweiQuantity: string | number | TinyBig | Big): TinyBig;
|
|
1
|
+
import type Big from 'big.js';
|
|
2
|
+
import type { TinyBig } from './../shared/tiny-big/tiny-big';
|
|
3
|
+
export declare function gweiToEther(gweiQuantity: string | number | TinyBig | Big): TinyBig;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { tinyBig } from '../shared/tiny-big/tiny-big';
|
|
2
|
+
import { validateType } from '../shared/validate-type';
|
|
3
|
+
export function gweiToEther(gweiQuantity) {
|
|
4
|
+
validateType(gweiQuantity, ['string', 'number', 'object']);
|
|
5
|
+
const result = tinyBig(gweiQuantity).div('1000000000');
|
|
6
|
+
return tinyBig(result);
|
|
7
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { Bytes } from '../index';
|
|
2
|
-
export declare function hashMessage(message: Bytes | string): string;
|
|
1
|
+
import type { Bytes } from '../index';
|
|
2
|
+
export declare function hashMessage(message: Bytes | string): string;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { concat, keccak256, toUtf8Bytes } from '../index';
|
|
2
|
+
const messagePrefix = '\x19Ethereum Signed Message:\n';
|
|
3
|
+
export function hashMessage(message) {
|
|
4
|
+
if (typeof message === 'string') {
|
|
5
|
+
message = toUtf8Bytes(message);
|
|
6
|
+
}
|
|
7
|
+
return keccak256(concat([
|
|
8
|
+
toUtf8Bytes(messagePrefix),
|
|
9
|
+
toUtf8Bytes(String(message.length)),
|
|
10
|
+
message,
|
|
11
|
+
]));
|
|
12
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function isAddress(address: string): boolean;
|
|
1
|
+
export declare function isAddress(address: string): boolean;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { toChecksumAddress } from '..';
|
|
2
|
+
import { validateType } from '../shared/validate-type';
|
|
3
|
+
export function isAddress(address) {
|
|
4
|
+
validateType(address, ['string']);
|
|
5
|
+
try {
|
|
6
|
+
toChecksumAddress(address);
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
catch (error) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { BytesLike } from './bytes';
|
|
2
|
-
export declare function keccak256(data: BytesLike): string;
|
|
1
|
+
import type { BytesLike } from './bytes';
|
|
2
|
+
export declare function keccak256(data: BytesLike): string;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Keccak } from 'sha3';
|
|
2
|
+
export function keccak256(data) {
|
|
3
|
+
let bufferableData;
|
|
4
|
+
if (typeof data === 'string') {
|
|
5
|
+
bufferableData = Buffer.from(data.replace(/^0x/, ''), 'hex');
|
|
6
|
+
}
|
|
7
|
+
else {
|
|
8
|
+
bufferableData = Buffer.from(data);
|
|
9
|
+
}
|
|
10
|
+
const keccak = new Keccak(256);
|
|
11
|
+
const addressHash = '0x' + keccak.update(bufferableData).digest('hex');
|
|
12
|
+
return addressHash;
|
|
13
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare function pack(types: ReadonlyArray<string>, values: ReadonlyArray<any>): string;
|
|
2
|
-
export declare function solidityKeccak256(types: ReadonlyArray<string>, values: ReadonlyArray<any>): string;
|
|
1
|
+
export declare function pack(types: ReadonlyArray<string>, values: ReadonlyArray<any>): string;
|
|
2
|
+
export declare function solidityKeccak256(types: ReadonlyArray<string>, values: ReadonlyArray<any>): string;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Buffer } from 'buffer';
|
|
2
|
+
import { hexFalse } from '../classes/utils/encode-decode-transaction';
|
|
3
|
+
import { logger } from '../logger/logger';
|
|
4
|
+
import { tinyBig } from '../shared/tiny-big/tiny-big';
|
|
5
|
+
import { arrayify, concat, hexlify, zeroPad } from './bytes';
|
|
6
|
+
import { keccak256 } from './keccak256';
|
|
7
|
+
const regexBytes = new RegExp('^bytes([0-9]+)$');
|
|
8
|
+
const regexNumber = new RegExp('^(u?int)([0-9]*)$');
|
|
9
|
+
const regexArray = new RegExp('^(.*)\\[([0-9]*)\\]$');
|
|
10
|
+
function _pack(type, value, isArray) {
|
|
11
|
+
switch (type) {
|
|
12
|
+
case 'address':
|
|
13
|
+
if (isArray) {
|
|
14
|
+
return zeroPad(value, 32);
|
|
15
|
+
}
|
|
16
|
+
return arrayify(value);
|
|
17
|
+
case 'string':
|
|
18
|
+
return Buffer.from(value);
|
|
19
|
+
case 'bytes':
|
|
20
|
+
return arrayify(value);
|
|
21
|
+
case 'bool':
|
|
22
|
+
value = value ? '0x01' : '0x00';
|
|
23
|
+
if (isArray) {
|
|
24
|
+
return zeroPad(value, 32);
|
|
25
|
+
}
|
|
26
|
+
return arrayify(value);
|
|
27
|
+
}
|
|
28
|
+
let match = type.match(regexNumber);
|
|
29
|
+
if (match) {
|
|
30
|
+
let size = parseInt(match[2] || '256');
|
|
31
|
+
if ((match[2] && String(size) !== match[2]) ||
|
|
32
|
+
size % 8 !== 0 ||
|
|
33
|
+
size === 0 ||
|
|
34
|
+
size > 256) {
|
|
35
|
+
logger.throwArgumentError('invalid number type', 'type', type);
|
|
36
|
+
}
|
|
37
|
+
if (isArray) {
|
|
38
|
+
size = 256;
|
|
39
|
+
}
|
|
40
|
+
value = tinyBig(value).toTwos(size).toNumber();
|
|
41
|
+
const hexValue = hexlify(value);
|
|
42
|
+
return zeroPad(hexValue, size / 8);
|
|
43
|
+
}
|
|
44
|
+
match = type.match(regexBytes);
|
|
45
|
+
if (match) {
|
|
46
|
+
const size = parseInt(match[1]);
|
|
47
|
+
if (String(size) !== match[1] || size === 0 || size > 32) {
|
|
48
|
+
logger.throwArgumentError('invalid bytes type', 'type', type);
|
|
49
|
+
}
|
|
50
|
+
if (arrayify(value).byteLength !== size) {
|
|
51
|
+
logger.throwArgumentError(`invalid value for ${type}`, 'value', value);
|
|
52
|
+
}
|
|
53
|
+
if (isArray) {
|
|
54
|
+
return arrayify((value + hexFalse).substring(0, 66));
|
|
55
|
+
}
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
match = type.match(regexArray);
|
|
59
|
+
if (match && Array.isArray(value)) {
|
|
60
|
+
const baseType = match[1];
|
|
61
|
+
const count = parseInt(match[2] || String(value.length));
|
|
62
|
+
if (count != value.length) {
|
|
63
|
+
logger.throwArgumentError(`invalid array length for ${type}`, 'value', value);
|
|
64
|
+
}
|
|
65
|
+
const result = [];
|
|
66
|
+
value.forEach(function (value) {
|
|
67
|
+
result.push(_pack(baseType, value, true));
|
|
68
|
+
});
|
|
69
|
+
return concat(result);
|
|
70
|
+
}
|
|
71
|
+
return logger.throwArgumentError('invalid type', 'type', type);
|
|
72
|
+
}
|
|
73
|
+
export function pack(types, values) {
|
|
74
|
+
if (types.length != values.length) {
|
|
75
|
+
logger.throwArgumentError('wrong number of values; expected ${ types.length }', 'values', values);
|
|
76
|
+
}
|
|
77
|
+
const tight = [];
|
|
78
|
+
types.forEach(function (type, index) {
|
|
79
|
+
tight.push(_pack(type, values[index]));
|
|
80
|
+
});
|
|
81
|
+
return hexlify(concat(tight));
|
|
82
|
+
}
|
|
83
|
+
export function solidityKeccak256(types, values) {
|
|
84
|
+
return keccak256(pack(types, values));
|
|
85
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { Signature, SignatureLike } from './bytes';
|
|
2
|
-
export declare function splitSignature(signature: SignatureLike): Signature;
|
|
1
|
+
import type { Signature, SignatureLike } from './bytes';
|
|
2
|
+
export declare function splitSignature(signature: SignatureLike): Signature;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { logger } from './../logger/logger';
|
|
2
|
+
import { arrayify, hexlify, hexZeroPad, isBytesLike, isHexString, zeroPad, } from './bytes';
|
|
3
|
+
export function splitSignature(signature) {
|
|
4
|
+
const result = {
|
|
5
|
+
r: '0x',
|
|
6
|
+
s: '0x',
|
|
7
|
+
_vs: '0x',
|
|
8
|
+
recoveryParam: 0,
|
|
9
|
+
v: 0,
|
|
10
|
+
yParityAndS: '0x',
|
|
11
|
+
compact: '0x',
|
|
12
|
+
};
|
|
13
|
+
if (isBytesLike(signature)) {
|
|
14
|
+
const bytes = arrayify(signature);
|
|
15
|
+
if (bytes.length === 64) {
|
|
16
|
+
result.v = 27 + (bytes[32] >> 7);
|
|
17
|
+
bytes[32] &= 0x7f;
|
|
18
|
+
result.r = hexlify(bytes.slice(0, 32));
|
|
19
|
+
result.s = hexlify(bytes.slice(32, 64));
|
|
20
|
+
}
|
|
21
|
+
else if (bytes.length === 65) {
|
|
22
|
+
result.r = hexlify(bytes.slice(0, 32));
|
|
23
|
+
result.s = hexlify(bytes.slice(32, 64));
|
|
24
|
+
result.v = bytes[64];
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
logger.throwArgumentError('invalid signature string', 'signature', signature);
|
|
28
|
+
}
|
|
29
|
+
if (result.v < 27) {
|
|
30
|
+
if (result.v === 0 || result.v === 1) {
|
|
31
|
+
result.v += 27;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
logger.throwArgumentError('signature invalid v byte', 'signature', signature);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
result.recoveryParam = 1 - (result.v % 2);
|
|
38
|
+
if (result.recoveryParam) {
|
|
39
|
+
bytes[32] |= 0x80;
|
|
40
|
+
}
|
|
41
|
+
result._vs = hexlify(bytes.slice(32, 64));
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
result.r = signature.r;
|
|
45
|
+
result.s = signature.s;
|
|
46
|
+
result.v = signature.v;
|
|
47
|
+
result.recoveryParam = signature.recoveryParam;
|
|
48
|
+
result._vs = signature._vs;
|
|
49
|
+
if (result._vs != null) {
|
|
50
|
+
const vs_1 = zeroPad(arrayify(result._vs), 32);
|
|
51
|
+
result._vs = hexlify(vs_1);
|
|
52
|
+
const recoveryParam = vs_1[0] >= 128 ? 1 : 0;
|
|
53
|
+
if (result.recoveryParam == null) {
|
|
54
|
+
result.recoveryParam = recoveryParam;
|
|
55
|
+
}
|
|
56
|
+
else if (result.recoveryParam !== recoveryParam) {
|
|
57
|
+
logger.throwArgumentError('signature recoveryParam mismatch _vs', 'signature', signature);
|
|
58
|
+
}
|
|
59
|
+
vs_1[0] &= 0x7f;
|
|
60
|
+
const s = hexlify(vs_1);
|
|
61
|
+
if (result.s == null) {
|
|
62
|
+
result.s = s;
|
|
63
|
+
}
|
|
64
|
+
else if (result.s !== s) {
|
|
65
|
+
logger.throwArgumentError('signature v mismatch _vs', 'signature', signature);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (result.recoveryParam == null) {
|
|
69
|
+
if (result.v == null) {
|
|
70
|
+
logger.throwArgumentError('signature missing v and recoveryParam', 'signature', signature);
|
|
71
|
+
}
|
|
72
|
+
else if (result.v === 0 || result.v === 1) {
|
|
73
|
+
result.recoveryParam = result.v;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
result.recoveryParam = 1 - (result.v % 2);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
if (result.v == null) {
|
|
81
|
+
result.v = 27 + result.recoveryParam;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
const recId = result.v === 0 || result.v === 1 ? result.v : 1 - (result.v % 2);
|
|
85
|
+
if (result.recoveryParam !== recId) {
|
|
86
|
+
logger.throwArgumentError('signature recoveryParam mismatch v', 'signature', signature);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (result.r == null || !isHexString(result.r)) {
|
|
91
|
+
logger.throwArgumentError('signature missing or invalid r', 'signature', signature);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
result.r = hexZeroPad(result.r, 32);
|
|
95
|
+
}
|
|
96
|
+
if (result.s == null || !isHexString(result.s)) {
|
|
97
|
+
logger.throwArgumentError('signature missing or invalid s', 'signature', signature);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
result.s = hexZeroPad(result.s, 32);
|
|
101
|
+
}
|
|
102
|
+
const vs = arrayify(result.s);
|
|
103
|
+
if (vs[0] >= 128) {
|
|
104
|
+
logger.throwArgumentError('signature s out of range', 'signature', signature);
|
|
105
|
+
}
|
|
106
|
+
if (result.recoveryParam) {
|
|
107
|
+
vs[0] |= 0x80;
|
|
108
|
+
}
|
|
109
|
+
const _vs = hexlify(vs);
|
|
110
|
+
if (result._vs) {
|
|
111
|
+
if (!isHexString(result._vs)) {
|
|
112
|
+
logger.throwArgumentError('signature invalid _vs', 'signature', signature);
|
|
113
|
+
}
|
|
114
|
+
result._vs = hexZeroPad(result._vs, 32);
|
|
115
|
+
}
|
|
116
|
+
if (result._vs == null) {
|
|
117
|
+
result._vs = _vs;
|
|
118
|
+
}
|
|
119
|
+
else if (result._vs !== _vs) {
|
|
120
|
+
logger.throwArgumentError('signature _vs mismatch v and s', 'signature', signature);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
result.yParityAndS = result._vs;
|
|
124
|
+
result.compact = result.r + result.yParityAndS.substring(2);
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function toChecksumAddress(address: string): string;
|
|
1
|
+
export declare function toChecksumAddress(address: string): string;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Keccak } from 'sha3';
|
|
2
|
+
import { validateType } from '../shared/validate-type';
|
|
3
|
+
export function toChecksumAddress(address) {
|
|
4
|
+
validateType(address, ['string']);
|
|
5
|
+
if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
|
|
6
|
+
throw new Error(`Invalid Ethereum address "${address}"`);
|
|
7
|
+
}
|
|
8
|
+
const _address = address.toLowerCase().replace(/^0x/i, '');
|
|
9
|
+
const keccak = new Keccak(256);
|
|
10
|
+
const addressHash = keccak.update(_address).digest('hex').replace(/^0x/i, '');
|
|
11
|
+
let checksumAddress = '0x';
|
|
12
|
+
for (let i = 0; i < _address.length; i++) {
|
|
13
|
+
if (parseInt(addressHash[i], 16) > 7) {
|
|
14
|
+
checksumAddress += _address[i].toUpperCase();
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
checksumAddress += _address[i];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
if (address.match(/([A-F].*[a-f])|([a-f].*[A-F])/) &&
|
|
21
|
+
checksumAddress !== address) {
|
|
22
|
+
throw new Error(`Invalid Checksum address for "${address}"`);
|
|
23
|
+
}
|
|
24
|
+
return checksumAddress;
|
|
25
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function toUtf8Bytes(data: string): Uint8Array;
|
|
1
|
+
export declare function toUtf8Bytes(data: string): Uint8Array;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type Big from 'big.js';
|
|
2
|
-
import type { TinyBig } from '../shared/tiny-big/tiny-big';
|
|
3
|
-
export declare function weiToEther(weiQuantity: string | number | TinyBig | Big): TinyBig;
|
|
1
|
+
import type Big from 'big.js';
|
|
2
|
+
import type { TinyBig } from '../shared/tiny-big/tiny-big';
|
|
3
|
+
export declare function weiToEther(weiQuantity: string | number | TinyBig | Big): TinyBig;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { tinyBig } from '../shared/tiny-big/tiny-big';
|
|
2
|
+
import { validateType } from '../shared/validate-type';
|
|
3
|
+
export function weiToEther(weiQuantity) {
|
|
4
|
+
validateType(weiQuantity, ['string', 'number', 'object']);
|
|
5
|
+
try {
|
|
6
|
+
let _weiQuantity = weiQuantity;
|
|
7
|
+
if (typeof weiQuantity === 'string' && weiQuantity.slice(0, 2) === '0x') {
|
|
8
|
+
_weiQuantity = BigInt(weiQuantity).toString();
|
|
9
|
+
}
|
|
10
|
+
const result = tinyBig(_weiQuantity).div('1000000000000000000');
|
|
11
|
+
return tinyBig(result);
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
throw error;
|
|
15
|
+
}
|
|
16
|
+
}
|