essential-eth 0.5.0 → 0.5.4

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 (77) 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-transaction-receipt.d.ts +5 -0
  4. package/lib/cjs/classes/utils/clean-transaction-receipt.js +55 -0
  5. package/lib/cjs/classes/utils/encode-decode-transaction.d.ts +1 -0
  6. package/lib/cjs/classes/utils/encode-decode-transaction.js +4 -4
  7. package/lib/cjs/classes/utils/fetchers.d.ts +1 -1
  8. package/lib/cjs/index.d.ts +7 -2
  9. package/lib/cjs/index.js +24 -3
  10. package/lib/cjs/logger/logger.d.ts +11 -0
  11. package/lib/cjs/logger/logger.js +36 -0
  12. package/lib/cjs/logger/package-version.d.ts +1 -0
  13. package/lib/cjs/logger/package-version.js +5 -0
  14. package/lib/cjs/providers/BaseProvider.d.ts +270 -0
  15. package/lib/cjs/providers/BaseProvider.js +361 -0
  16. package/lib/cjs/providers/FallthroughProvider.d.ts +25 -0
  17. package/lib/cjs/providers/FallthroughProvider.js +65 -0
  18. package/lib/cjs/providers/JsonRpcProvider.d.ts +4 -200
  19. package/lib/cjs/providers/JsonRpcProvider.js +11 -263
  20. package/lib/cjs/providers/test/rpc-urls.d.ts +1 -0
  21. package/lib/cjs/providers/test/rpc-urls.js +1 -0
  22. package/lib/cjs/providers/utils/chains-info.d.ts +8 -0
  23. package/lib/cjs/providers/utils/chains-info.js +24 -0
  24. package/lib/cjs/shared/tiny-big/tiny-big.d.ts +9 -2
  25. package/lib/cjs/shared/tiny-big/tiny-big.js +27 -9
  26. package/lib/cjs/types/Transaction.types.d.ts +53 -0
  27. package/lib/cjs/utils/bytes.d.ts +172 -0
  28. package/lib/cjs/utils/bytes.js +564 -0
  29. package/lib/cjs/utils/hash-message.d.ts +12 -0
  30. package/lib/cjs/utils/hash-message.js +26 -0
  31. package/lib/cjs/utils/keccak256.d.ts +2 -0
  32. package/lib/cjs/utils/keccak256.js +17 -0
  33. package/lib/cjs/utils/solidity-keccak256.d.ts +29 -0
  34. package/lib/cjs/utils/solidity-keccak256.js +118 -0
  35. package/lib/cjs/utils/to-utf8-bytes.d.ts +1 -0
  36. package/lib/cjs/utils/to-utf8-bytes.js +7 -0
  37. package/lib/esm/classes/Contract.js +1 -1
  38. package/lib/esm/classes/utils/clean-transaction-receipt.d.ts +2 -0
  39. package/lib/esm/classes/utils/clean-transaction-receipt.js +48 -0
  40. package/lib/esm/classes/utils/encode-decode-transaction.d.ts +1 -0
  41. package/lib/esm/classes/utils/encode-decode-transaction.js +2 -2
  42. package/lib/esm/classes/utils/fetchers.d.ts +1 -1
  43. package/lib/esm/index.d.ts +7 -2
  44. package/lib/esm/index.js +7 -2
  45. package/lib/esm/logger/logger.d.ts +11 -0
  46. package/lib/esm/logger/logger.js +33 -0
  47. package/lib/esm/logger/package-version.d.ts +1 -0
  48. package/lib/esm/logger/package-version.js +1 -0
  49. package/lib/esm/providers/BaseProvider.d.ts +18 -0
  50. package/lib/esm/providers/BaseProvider.js +101 -0
  51. package/lib/esm/providers/FallthroughProvider.d.ts +12 -0
  52. package/lib/esm/providers/FallthroughProvider.js +41 -0
  53. package/lib/esm/providers/JsonRpcProvider.d.ts +4 -12
  54. package/lib/esm/providers/JsonRpcProvider.js +8 -69
  55. package/lib/esm/providers/test/rpc-urls.d.ts +1 -0
  56. package/lib/esm/providers/test/rpc-urls.js +1 -0
  57. package/lib/esm/providers/utils/chains-info.d.ts +8 -0
  58. package/lib/esm/providers/utils/chains-info.js +24 -0
  59. package/lib/esm/shared/tiny-big/tiny-big.d.ts +2 -0
  60. package/lib/esm/shared/tiny-big/tiny-big.js +20 -7
  61. package/lib/esm/types/Transaction.types.d.ts +45 -0
  62. package/lib/esm/utils/bytes.d.ts +40 -0
  63. package/lib/esm/utils/bytes.js +245 -0
  64. package/lib/esm/utils/hash-message.d.ts +2 -0
  65. package/lib/esm/utils/hash-message.js +12 -0
  66. package/lib/esm/utils/keccak256.d.ts +2 -0
  67. package/lib/esm/utils/keccak256.js +13 -0
  68. package/lib/esm/utils/solidity-keccak256.d.ts +2 -0
  69. package/lib/esm/utils/solidity-keccak256.js +85 -0
  70. package/lib/esm/utils/to-utf8-bytes.d.ts +1 -0
  71. package/lib/esm/utils/to-utf8-bytes.js +3 -0
  72. package/package.json +19 -19
  73. package/readme.md +36 -5
  74. package/lib/cjs/utils/hex-zero-pad.d.ts +0 -32
  75. package/lib/cjs/utils/hex-zero-pad.js +0 -52
  76. package/lib/esm/utils/hex-zero-pad.d.ts +0 -1
  77. package/lib/esm/utils/hex-zero-pad.js +0 -17
@@ -61,6 +61,7 @@ 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[];
@@ -215,7 +216,9 @@ declare const _default: {
215
216
  "2153": string[];
216
217
  "2213": string[];
217
218
  "2221": string[];
219
+ "2223": string[];
218
220
  "2559": string[];
221
+ "2569": string[];
219
222
  "3000": string[];
220
223
  "3001": string[];
221
224
  "3331": string[];
@@ -233,6 +236,8 @@ declare const _default: {
233
236
  "4918": string[];
234
237
  "5197": string[];
235
238
  "5315": string[];
239
+ "5551": string[];
240
+ "5553": string[];
236
241
  "5700": string[];
237
242
  "5777": string[];
238
243
  "5851": string[];
@@ -295,6 +300,7 @@ declare const _default: {
295
300
  "60002": string[];
296
301
  "60103": string[];
297
302
  "62320": string[];
303
+ "62621": string[];
298
304
  "63000": string[];
299
305
  "63001": string[];
300
306
  "69420": string[];
@@ -340,6 +346,7 @@ declare const _default: {
340
346
  "333999": string[];
341
347
  "421611": string[];
342
348
  "444900": string[];
349
+ "512512": string[];
343
350
  "666666": string[];
344
351
  "888888": string[];
345
352
  "955305": string[];
@@ -351,6 +358,7 @@ declare const _default: {
351
358
  "11155111": string[];
352
359
  "13371337": string[];
353
360
  "18289463": string[];
361
+ "20180430": string[];
354
362
  "20181205": string[];
355
363
  "28945486": string[];
356
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
  ],
@@ -651,9 +654,15 @@ export default {
651
654
  "2221": [
652
655
  "kava"
653
656
  ],
657
+ "2223": [
658
+ "VChain"
659
+ ],
654
660
  "2559": [
655
661
  "ktoc"
656
662
  ],
663
+ "2569": [
664
+ "tpc"
665
+ ],
657
666
  "3000": [
658
667
  "cennz-r"
659
668
  ],
@@ -705,6 +714,12 @@ export default {
705
714
  "5315": [
706
715
  "UZMI"
707
716
  ],
717
+ "5551": [
718
+ "Nahmii"
719
+ ],
720
+ "5553": [
721
+ "Nahmii testnet"
722
+ ],
708
723
  "5700": [
709
724
  "tsys"
710
725
  ],
@@ -891,6 +906,9 @@ export default {
891
906
  "62320": [
892
907
  "BKLV"
893
908
  ],
909
+ "62621": [
910
+ "mtv"
911
+ ],
894
912
  "63000": [
895
913
  "ecs"
896
914
  ],
@@ -1026,6 +1044,9 @@ export default {
1026
1044
  "444900": [
1027
1045
  "wlkt"
1028
1046
  ],
1047
+ "512512": [
1048
+ "cmp"
1049
+ ],
1029
1050
  "666666": [
1030
1051
  "vpioneer"
1031
1052
  ],
@@ -1059,6 +1080,9 @@ export default {
1059
1080
  "18289463": [
1060
1081
  "ilt"
1061
1082
  ],
1083
+ "20180430": [
1084
+ "spectrum"
1085
+ ],
1062
1086
  "20181205": [
1063
1087
  "qki"
1064
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;
@@ -2,13 +2,10 @@ import Big from 'big.js';
2
2
  import { scientificStrToDecimalStr } from './helpers';
3
3
  export class TinyBig extends Big {
4
4
  constructor(value) {
5
- try {
6
- super(value);
7
- }
8
- catch (e) {
9
- console.error(`TinyBig cannot parse value (value=${value})`);
10
- throw e;
11
- }
5
+ super(value);
6
+ this.padAndChop = (str, padChar, length) => {
7
+ return (Array(length).fill(padChar).join('') + str).slice(length * -1);
8
+ };
12
9
  }
13
10
  toHexString() {
14
11
  return `0x${BigInt(this.toString()).toString(16)}`;
@@ -22,6 +19,22 @@ export class TinyBig extends Big {
22
19
  }
23
20
  return scientificStrToDecimalStr(super.toString());
24
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
+ }
25
38
  }
26
39
  export function tinyBig(value) {
27
40
  return new TinyBig(value);
@@ -20,6 +20,24 @@ export declare type TransactionResponse = Modify<RPCTransaction, {
20
20
  maxPriorityFeePerGas: TinyBig;
21
21
  confirmations: number;
22
22
  }>;
23
+ export declare type TransactionReceipt = Modify<RPCTransactionReceipt, {
24
+ blockNumber: number;
25
+ cumulativeGasUsed: TinyBig;
26
+ effectiveGasPrice: TinyBig;
27
+ gasUsed: TinyBig;
28
+ logs: Array<Log>;
29
+ status: number;
30
+ transactionIndex: number;
31
+ type: number;
32
+ } & {
33
+ byzantium: boolean;
34
+ confirmations: number;
35
+ }>;
36
+ export declare type Log = Modify<Omit<RPCLog, 'removed'>, {
37
+ blockNumber: number;
38
+ logIndex: number;
39
+ transactionIndex: number;
40
+ }>;
23
41
  export declare type BlockTransactionResponse = Omit<TransactionResponse, 'maxFeePerGas' | 'maxPriorityFeePerGas'>;
24
42
  export interface RPCBlockTransaction {
25
43
  blockHash: string;
@@ -39,4 +57,31 @@ export interface RPCBlockTransaction {
39
57
  v: string;
40
58
  value: string;
41
59
  }
60
+ export interface RPCTransactionReceipt {
61
+ blockHash: string;
62
+ blockNumber: string;
63
+ contractAddress: string;
64
+ cumulativeGasUsed: string;
65
+ effectiveGasPrice: string;
66
+ from: string;
67
+ gasUsed: string;
68
+ logs: Array<RPCLog>;
69
+ logsBloom: string;
70
+ status: string;
71
+ to: string;
72
+ transactionHash: string;
73
+ transactionIndex: string;
74
+ type: string;
75
+ }
76
+ export interface RPCLog {
77
+ address: string;
78
+ blockHash: string;
79
+ blockNumber: string;
80
+ data: string;
81
+ logIndex: string;
82
+ removed?: boolean;
83
+ topics: Array<string>;
84
+ transactionHash: string;
85
+ transactionIndex: string;
86
+ }
42
87
  export {};
@@ -0,0 +1,40 @@
1
+ export declare type Bytes = ArrayLike<number>;
2
+ export declare type BytesLike = Bytes | string;
3
+ export declare type BytesLikeWithNumber = BytesLike | number;
4
+ export interface DataOptions {
5
+ allowMissingPrefix?: boolean;
6
+ hexPad?: 'left' | 'right' | null;
7
+ }
8
+ export interface Hexable {
9
+ toHexString(): string;
10
+ }
11
+ export declare type SignatureLike = {
12
+ r: string;
13
+ s?: string;
14
+ _vs?: string;
15
+ recoveryParam?: number;
16
+ v?: number;
17
+ } | BytesLike;
18
+ export interface Signature {
19
+ r: string;
20
+ s: string;
21
+ _vs: string;
22
+ recoveryParam: number;
23
+ v: number;
24
+ yParityAndS: string;
25
+ compact: string;
26
+ }
27
+ export declare function isBytesLike(value: any): value is BytesLike;
28
+ export declare function isBytes(value: any): value is Bytes;
29
+ export declare function arrayify(value: BytesLike | Hexable | number, options?: DataOptions): Uint8Array;
30
+ export declare function concat(arrayOfBytesLike: ReadonlyArray<BytesLikeWithNumber>): Uint8Array;
31
+ export declare function stripZeros(value: BytesLike): Uint8Array;
32
+ export declare function zeroPad(value: BytesLike, length: number): Uint8Array;
33
+ export declare function isHexString(value: any, length?: number): boolean;
34
+ export declare function hexlify(value: BytesLike | Hexable | number | bigint, options?: DataOptions): string;
35
+ export declare function hexDataLength(data: BytesLike): number | null;
36
+ export declare function hexDataSlice(data: BytesLikeWithNumber, offset: number, endOffset?: number): string;
37
+ export declare function hexConcat(items: ReadonlyArray<BytesLike>): string;
38
+ export declare function hexValue(value: BytesLike | Hexable | number | bigint): string;
39
+ export declare function hexStripZeros(value: BytesLike): string;
40
+ export declare function hexZeroPad(value: BytesLikeWithNumber, 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,2 @@
1
+ import { 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
+ }
@@ -0,0 +1,2 @@
1
+ import { BytesLike } from './bytes';
2
+ export declare const keccak256: (data: BytesLike) => string;
@@ -0,0 +1,13 @@
1
+ import { Keccak } from 'sha3';
2
+ export const 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
+ };
@@ -0,0 +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;
@@ -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
+ }
@@ -0,0 +1 @@
1
+ export declare function toUtf8Bytes(data: string): Uint8Array;
@@ -0,0 +1,3 @@
1
+ export function toUtf8Bytes(data) {
2
+ return new Uint8Array(Buffer.from(data));
3
+ }