essential-eth 0.4.11 → 0.5.4-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/lib/cjs/classes/Contract.d.ts +3 -2
  2. package/lib/cjs/classes/Contract.js +3 -2
  3. package/lib/cjs/classes/utils/clean-block.d.ts +3 -3
  4. package/lib/cjs/classes/utils/clean-block.js +3 -2
  5. package/lib/cjs/classes/utils/clean-transaction.d.ts +3 -3
  6. package/lib/cjs/classes/utils/clean-transaction.js +10 -6
  7. package/lib/cjs/classes/utils/encode-decode-transaction.d.ts +1 -0
  8. package/lib/cjs/classes/utils/encode-decode-transaction.js +4 -4
  9. package/lib/cjs/classes/utils/fetchers.d.ts +1 -1
  10. package/lib/cjs/index.d.ts +6 -3
  11. package/lib/cjs/index.js +19 -1
  12. package/lib/cjs/logger/logger.d.ts +11 -0
  13. package/lib/cjs/logger/logger.js +36 -0
  14. package/lib/cjs/logger/package-version.d.ts +1 -0
  15. package/lib/cjs/logger/package-version.js +5 -0
  16. package/lib/cjs/providers/BaseProvider.d.ts +261 -0
  17. package/lib/cjs/providers/BaseProvider.js +340 -0
  18. package/lib/cjs/providers/FallthroughProvider.d.ts +25 -0
  19. package/lib/cjs/providers/FallthroughProvider.js +65 -0
  20. package/lib/cjs/providers/JsonRpcProvider.d.ts +7 -33
  21. package/lib/cjs/providers/JsonRpcProvider.js +11 -93
  22. package/lib/cjs/providers/test/rpc-urls.d.ts +1 -0
  23. package/lib/cjs/providers/test/rpc-urls.js +1 -0
  24. package/lib/cjs/providers/utils/chains-info.d.ts +14 -0
  25. package/lib/cjs/providers/utils/chains-info.js +42 -0
  26. package/lib/cjs/shared/tiny-big/tiny-big.d.ts +9 -2
  27. package/lib/cjs/shared/tiny-big/tiny-big.js +26 -2
  28. package/lib/cjs/types/Block.types.d.ts +8 -19
  29. package/lib/cjs/types/Transaction.types.d.ts +23 -14
  30. package/lib/cjs/utils/bytes.d.ts +171 -0
  31. package/lib/cjs/utils/bytes.js +564 -0
  32. package/lib/cjs/utils/solidity-keccak256.d.ts +30 -0
  33. package/lib/cjs/utils/solidity-keccak256.js +125 -0
  34. package/lib/esm/classes/Contract.js +1 -1
  35. package/lib/esm/classes/utils/clean-block.d.ts +2 -2
  36. package/lib/esm/classes/utils/clean-block.js +2 -1
  37. package/lib/esm/classes/utils/clean-transaction.d.ts +2 -2
  38. package/lib/esm/classes/utils/clean-transaction.js +10 -6
  39. package/lib/esm/classes/utils/encode-decode-transaction.d.ts +1 -0
  40. package/lib/esm/classes/utils/encode-decode-transaction.js +2 -2
  41. package/lib/esm/classes/utils/fetchers.d.ts +1 -1
  42. package/lib/esm/index.d.ts +6 -3
  43. package/lib/esm/index.js +4 -1
  44. package/lib/esm/logger/logger.d.ts +11 -0
  45. package/lib/esm/logger/logger.js +33 -0
  46. package/lib/esm/logger/package-version.d.ts +1 -0
  47. package/lib/esm/logger/package-version.js +1 -0
  48. package/lib/esm/providers/BaseProvider.d.ts +17 -0
  49. package/lib/esm/providers/BaseProvider.js +88 -0
  50. package/lib/esm/providers/FallthroughProvider.d.ts +12 -0
  51. package/lib/esm/providers/FallthroughProvider.js +41 -0
  52. package/lib/esm/providers/JsonRpcProvider.d.ts +4 -9
  53. package/lib/esm/providers/JsonRpcProvider.js +8 -67
  54. package/lib/esm/providers/test/rpc-urls.d.ts +1 -0
  55. package/lib/esm/providers/test/rpc-urls.js +1 -0
  56. package/lib/esm/providers/utils/chains-info.d.ts +14 -0
  57. package/lib/esm/providers/utils/chains-info.js +42 -0
  58. package/lib/esm/shared/tiny-big/tiny-big.d.ts +2 -0
  59. package/lib/esm/shared/tiny-big/tiny-big.js +19 -0
  60. package/lib/esm/types/Block.types.d.ts +7 -19
  61. package/lib/esm/types/Transaction.types.d.ts +22 -14
  62. package/lib/esm/utils/bytes.d.ts +39 -0
  63. package/lib/esm/utils/bytes.js +245 -0
  64. package/lib/esm/utils/solidity-keccak256.d.ts +3 -0
  65. package/lib/esm/utils/solidity-keccak256.js +91 -0
  66. package/package.json +19 -19
  67. package/readme.md +251 -61
@@ -61,12 +61,15 @@ declare const _default: {
61
61
  "67": string[];
62
62
  "68": string[];
63
63
  "69": string[];
64
+ "70": string[];
64
65
  "71": string[];
65
66
  "74": string[];
66
67
  "76": string[];
67
68
  "77": string[];
68
69
  "78": string[];
70
+ "79": string[];
69
71
  "80": string[];
72
+ "81": string[];
70
73
  "82": string[];
71
74
  "83": string[];
72
75
  "85": string[];
@@ -147,6 +150,7 @@ declare const _default: {
147
150
  "600": string[];
148
151
  "666": string[];
149
152
  "686": string[];
153
+ "700": string[];
150
154
  "707": string[];
151
155
  "708": string[];
152
156
  "721": string[];
@@ -208,9 +212,13 @@ declare const _default: {
208
212
  "2025": string[];
209
213
  "2100": string[];
210
214
  "2101": string[];
215
+ "2152": string[];
216
+ "2153": string[];
211
217
  "2213": string[];
212
218
  "2221": string[];
219
+ "2223": string[];
213
220
  "2559": string[];
221
+ "2569": string[];
214
222
  "3000": string[];
215
223
  "3001": string[];
216
224
  "3331": string[];
@@ -228,6 +236,8 @@ declare const _default: {
228
236
  "4918": string[];
229
237
  "5197": string[];
230
238
  "5315": string[];
239
+ "5551": string[];
240
+ "5553": string[];
231
241
  "5700": string[];
232
242
  "5777": string[];
233
243
  "5851": string[];
@@ -290,8 +300,10 @@ declare const _default: {
290
300
  "60002": string[];
291
301
  "60103": string[];
292
302
  "62320": string[];
303
+ "62621": string[];
293
304
  "63000": string[];
294
305
  "63001": string[];
306
+ "69420": string[];
295
307
  "70000": string[];
296
308
  "70001": string[];
297
309
  "70002": string[];
@@ -334,6 +346,7 @@ declare const _default: {
334
346
  "333999": string[];
335
347
  "421611": string[];
336
348
  "444900": string[];
349
+ "512512": string[];
337
350
  "666666": string[];
338
351
  "888888": string[];
339
352
  "955305": string[];
@@ -345,6 +358,7 @@ declare const _default: {
345
358
  "11155111": string[];
346
359
  "13371337": string[];
347
360
  "18289463": string[];
361
+ "20180430": string[];
348
362
  "20181205": string[];
349
363
  "28945486": string[];
350
364
  "35855456": string[];
@@ -189,6 +189,9 @@ export default {
189
189
  "69": [
190
190
  "okov"
191
191
  ],
192
+ "70": [
193
+ "hsc"
194
+ ],
192
195
  "71": [
193
196
  "cfxtest"
194
197
  ],
@@ -204,9 +207,15 @@ export default {
204
207
  "78": [
205
208
  "primuschain"
206
209
  ],
210
+ "79": [
211
+ "zenith"
212
+ ],
207
213
  "80": [
208
214
  "GeneChain"
209
215
  ],
216
+ "81": [
217
+ "VIL"
218
+ ],
210
219
  "82": [
211
220
  "Meter"
212
221
  ],
@@ -447,6 +456,9 @@ export default {
447
456
  "686": [
448
457
  "kar"
449
458
  ],
459
+ "700": [
460
+ "SNS"
461
+ ],
450
462
  "707": [
451
463
  "bcs"
452
464
  ],
@@ -630,15 +642,27 @@ export default {
630
642
  "2101": [
631
643
  "esp"
632
644
  ],
645
+ "2152": [
646
+ "fra"
647
+ ],
648
+ "2153": [
649
+ "findora-testnet"
650
+ ],
633
651
  "2213": [
634
652
  "evanesco"
635
653
  ],
636
654
  "2221": [
637
655
  "kava"
638
656
  ],
657
+ "2223": [
658
+ "VChain"
659
+ ],
639
660
  "2559": [
640
661
  "ktoc"
641
662
  ],
663
+ "2569": [
664
+ "tpc"
665
+ ],
642
666
  "3000": [
643
667
  "cennz-r"
644
668
  ],
@@ -690,6 +714,12 @@ export default {
690
714
  "5315": [
691
715
  "UZMI"
692
716
  ],
717
+ "5551": [
718
+ "Nahmii"
719
+ ],
720
+ "5553": [
721
+ "Nahmii testnet"
722
+ ],
693
723
  "5700": [
694
724
  "tsys"
695
725
  ],
@@ -876,12 +906,18 @@ export default {
876
906
  "62320": [
877
907
  "BKLV"
878
908
  ],
909
+ "62621": [
910
+ "mtv"
911
+ ],
879
912
  "63000": [
880
913
  "ecs"
881
914
  ],
882
915
  "63001": [
883
916
  "ecs-testnet"
884
917
  ],
918
+ "69420": [
919
+ "cndr"
920
+ ],
885
921
  "70000": [
886
922
  "TKM0"
887
923
  ],
@@ -1008,6 +1044,9 @@ export default {
1008
1044
  "444900": [
1009
1045
  "wlkt"
1010
1046
  ],
1047
+ "512512": [
1048
+ "cmp"
1049
+ ],
1011
1050
  "666666": [
1012
1051
  "vpioneer"
1013
1052
  ],
@@ -1041,6 +1080,9 @@ export default {
1041
1080
  "18289463": [
1042
1081
  "ilt"
1043
1082
  ],
1083
+ "20180430": [
1084
+ "spectrum"
1085
+ ],
1044
1086
  "20181205": [
1045
1087
  "qki"
1046
1088
  ],
@@ -4,5 +4,7 @@ export declare class TinyBig extends Big {
4
4
  toHexString(): string;
5
5
  toNumber(): number;
6
6
  toString(): string;
7
+ private padAndChop;
8
+ toTwos(bitCount: number): Big;
7
9
  }
8
10
  export declare function tinyBig(value: string | number | TinyBig | Big): TinyBig;
@@ -3,6 +3,9 @@ import { scientificStrToDecimalStr } from './helpers';
3
3
  export class TinyBig extends Big {
4
4
  constructor(value) {
5
5
  super(value);
6
+ this.padAndChop = (str, padChar, length) => {
7
+ return (Array(length).fill(padChar).join('') + str).slice(length * -1);
8
+ };
6
9
  }
7
10
  toHexString() {
8
11
  return `0x${BigInt(this.toString()).toString(16)}`;
@@ -16,6 +19,22 @@ export class TinyBig extends Big {
16
19
  }
17
20
  return scientificStrToDecimalStr(super.toString());
18
21
  }
22
+ toTwos(bitCount) {
23
+ let binaryStr;
24
+ if (this.gte(0)) {
25
+ const twosComp = this.toNumber().toString(2);
26
+ binaryStr = this.padAndChop(twosComp, '0', bitCount || twosComp.length);
27
+ }
28
+ else {
29
+ binaryStr = this.plus(Math.pow(2, bitCount)).toNumber().toString(2);
30
+ if (Number(binaryStr) < 0) {
31
+ throw new Error('Cannot calculate twos complement');
32
+ }
33
+ }
34
+ const binary = `0b${binaryStr}`;
35
+ const decimal = Number(binary);
36
+ return tinyBig(decimal);
37
+ }
19
38
  }
20
39
  export function tinyBig(value) {
21
40
  return new TinyBig(value);
@@ -1,27 +1,14 @@
1
- import { RPCTransaction, Transaction } from './Transaction.types';
2
- export interface Block {
3
- baseFeePerGas: string;
4
- difficulty: number;
5
- extraData: string;
1
+ import { BlockTransactionResponse, RPCTransaction } from './Transaction.types';
2
+ declare type Modify<T, R> = Omit<T, keyof R> & R;
3
+ export declare type BlockResponse = Modify<RPCBlock, {
6
4
  gasLimit: number;
7
5
  gasUsed: number;
8
- hash: string;
9
- logsBloom: string;
10
- miner: string;
11
- mixHash: string;
12
- nonce: string;
13
6
  number: number;
14
- parentHash: string;
15
- receiptsRoot: string;
16
- sha3Uncles: string;
17
7
  size: number;
18
- stateRoot: string;
19
8
  timestamp: number;
20
- totalDifficulty: number;
21
- transactions: string[] | Transaction[];
22
- transactionsRoot: string;
23
- uncles: unknown[];
24
- }
9
+ baseFeePerGas: number;
10
+ transactions: Array<string | BlockTransactionResponse>;
11
+ }>;
25
12
  export interface RPCBlock {
26
13
  baseFeePerGas: string;
27
14
  difficulty: string;
@@ -46,3 +33,4 @@ export interface RPCBlock {
46
33
  uncles: unknown[];
47
34
  }
48
35
  export declare type BlockTag = 'latest' | 'earliest' | 'pending' | number | string;
36
+ export {};
@@ -1,23 +1,30 @@
1
- export interface Transaction {
2
- blockHash: string;
1
+ import { TinyBig } from '../shared/tiny-big/tiny-big';
2
+ declare type Modify<T, R> = Omit<T, keyof R> & R;
3
+ export interface RPCTransaction extends RPCBlockTransaction {
4
+ maxFeePerGas: string;
5
+ maxPriorityFeePerGas: string;
6
+ }
7
+ export declare type TransactionResponse = Modify<RPCTransaction, {
3
8
  blockNumber: number;
4
- from: string;
5
- gas: number;
6
- gasPrice: string;
7
- hash: string;
8
- input: string;
9
+ chainId: number;
9
10
  nonce: number;
10
- r: string;
11
- s: string;
12
- to: string;
13
11
  transactionIndex: number;
14
12
  type: number;
15
- v: string;
16
- value: string;
17
- }
18
- export interface RPCTransaction {
13
+ v: number;
14
+ value: TinyBig;
15
+ gasPrice: TinyBig;
16
+ gas: TinyBig;
17
+ gasLimit: TinyBig;
18
+ } & {
19
+ maxFeePerGas: TinyBig;
20
+ maxPriorityFeePerGas: TinyBig;
21
+ confirmations: number;
22
+ }>;
23
+ export declare type BlockTransactionResponse = Omit<TransactionResponse, 'maxFeePerGas' | 'maxPriorityFeePerGas'>;
24
+ export interface RPCBlockTransaction {
19
25
  blockHash: string;
20
26
  blockNumber: string;
27
+ chainId: string;
21
28
  from: string;
22
29
  gas: string;
23
30
  gasPrice: string;
@@ -32,3 +39,4 @@ export interface RPCTransaction {
32
39
  v: string;
33
40
  value: string;
34
41
  }
42
+ export {};
@@ -0,0 +1,39 @@
1
+ export declare type Bytes = ArrayLike<number>;
2
+ export declare type BytesLike = Bytes | string | number;
3
+ export interface DataOptions {
4
+ allowMissingPrefix?: boolean;
5
+ hexPad?: 'left' | 'right' | null;
6
+ }
7
+ export interface Hexable {
8
+ toHexString(): string;
9
+ }
10
+ export declare type SignatureLike = {
11
+ r: string;
12
+ s?: string;
13
+ _vs?: string;
14
+ recoveryParam?: number;
15
+ v?: number;
16
+ } | BytesLike;
17
+ export interface Signature {
18
+ r: string;
19
+ s: string;
20
+ _vs: string;
21
+ recoveryParam: number;
22
+ v: number;
23
+ yParityAndS: string;
24
+ compact: string;
25
+ }
26
+ export declare function isBytesLike(value: any): value is BytesLike;
27
+ export declare function isBytes(value: any): value is Bytes;
28
+ export declare function arrayify(value: BytesLike | Hexable | number, options?: DataOptions): Uint8Array;
29
+ export declare function concat(arrayOfBytesLike: ReadonlyArray<BytesLike>): Uint8Array;
30
+ export declare function stripZeros(value: BytesLike): Uint8Array;
31
+ export declare function zeroPad(value: BytesLike, length: number): Uint8Array;
32
+ export declare function isHexString(value: any, length?: number): boolean;
33
+ export declare function hexlify(value: BytesLike | Hexable | number | bigint, options?: DataOptions): string;
34
+ export declare function hexDataLength(data: BytesLike): number | null;
35
+ export declare function hexDataSlice(data: BytesLike, offset: number, endOffset?: number): string;
36
+ export declare function hexConcat(items: ReadonlyArray<BytesLike>): string;
37
+ export declare function hexValue(value: BytesLike | Hexable | number | bigint): string;
38
+ export declare function hexStripZeros(value: BytesLike): string;
39
+ export declare function hexZeroPad(value: BytesLike, length: number): string;
@@ -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
+ }
@@ -0,0 +1,3 @@
1
+ export declare function pack(types: ReadonlyArray<string>, values: ReadonlyArray<any>): string;
2
+ export declare const hashKeccak256: (data: string) => string;
3
+ export declare function solidityKeccak256(types: ReadonlyArray<string>, values: ReadonlyArray<any>): string;
@@ -0,0 +1,91 @@
1
+ import { Buffer } from 'buffer';
2
+ import { Keccak } from 'sha3';
3
+ import { hexFalse } from '../classes/utils/encode-decode-transaction';
4
+ import { logger } from '../logger/logger';
5
+ import { tinyBig } from '../shared/tiny-big/tiny-big';
6
+ import { arrayify, concat, hexlify, zeroPad } from './bytes';
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 const hashKeccak256 = (data) => {
84
+ const keccak = new Keccak(256);
85
+ const bufferableData = Buffer.from(data.replace(/^0x/, ''), 'hex');
86
+ const addressHash = '0x' + keccak.update(bufferableData).digest('hex');
87
+ return addressHash;
88
+ };
89
+ export function solidityKeccak256(types, values) {
90
+ return hashKeccak256(pack(types, values));
91
+ }