essential-eth 0.4.10 → 0.5.1

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 (52) hide show
  1. package/lib/cjs/classes/utils/clean-block.d.ts +3 -3
  2. package/lib/cjs/classes/utils/clean-block.js +3 -2
  3. package/lib/cjs/classes/utils/clean-transaction.d.ts +3 -3
  4. package/lib/cjs/classes/utils/clean-transaction.js +10 -6
  5. package/lib/cjs/classes/utils/fetchers.d.ts +3 -3
  6. package/lib/cjs/index.d.ts +5 -3
  7. package/lib/cjs/index.js +16 -0
  8. package/lib/cjs/logger/logger.d.ts +11 -0
  9. package/lib/cjs/logger/logger.js +36 -0
  10. package/lib/cjs/logger/package-version.d.ts +1 -0
  11. package/lib/cjs/logger/package-version.js +5 -0
  12. package/lib/cjs/providers/JsonRpcProvider.d.ts +190 -3
  13. package/lib/cjs/providers/JsonRpcProvider.js +214 -19
  14. package/lib/cjs/providers/test/rpc-urls.d.ts +2 -0
  15. package/lib/cjs/providers/test/rpc-urls.js +2 -0
  16. package/lib/cjs/providers/utils/chains-info.d.ts +11 -0
  17. package/lib/cjs/providers/utils/chains-info.js +34 -1
  18. package/lib/cjs/shared/tiny-big/tiny-big.d.ts +7 -0
  19. package/lib/cjs/shared/tiny-big/tiny-big.js +24 -0
  20. package/lib/cjs/types/Block.types.d.ts +9 -19
  21. package/lib/cjs/types/Transaction.types.d.ts +23 -14
  22. package/lib/cjs/utils/bytes.d.ts +90 -0
  23. package/lib/cjs/utils/bytes.js +484 -0
  24. package/lib/cjs/utils/solidity-keccak256.d.ts +30 -0
  25. package/lib/cjs/utils/solidity-keccak256.js +125 -0
  26. package/lib/esm/classes/utils/clean-block.d.ts +2 -2
  27. package/lib/esm/classes/utils/clean-block.js +2 -1
  28. package/lib/esm/classes/utils/clean-transaction.d.ts +2 -2
  29. package/lib/esm/classes/utils/clean-transaction.js +10 -6
  30. package/lib/esm/classes/utils/fetchers.d.ts +3 -3
  31. package/lib/esm/index.d.ts +5 -3
  32. package/lib/esm/index.js +2 -0
  33. package/lib/esm/logger/logger.d.ts +11 -0
  34. package/lib/esm/logger/logger.js +33 -0
  35. package/lib/esm/logger/package-version.d.ts +1 -0
  36. package/lib/esm/logger/package-version.js +1 -0
  37. package/lib/esm/providers/JsonRpcProvider.d.ts +6 -2
  38. package/lib/esm/providers/JsonRpcProvider.js +30 -19
  39. package/lib/esm/providers/test/rpc-urls.d.ts +2 -0
  40. package/lib/esm/providers/test/rpc-urls.js +2 -0
  41. package/lib/esm/providers/utils/chains-info.d.ts +11 -0
  42. package/lib/esm/providers/utils/chains-info.js +34 -1
  43. package/lib/esm/shared/tiny-big/tiny-big.d.ts +2 -0
  44. package/lib/esm/shared/tiny-big/tiny-big.js +19 -0
  45. package/lib/esm/types/Block.types.d.ts +8 -19
  46. package/lib/esm/types/Transaction.types.d.ts +22 -14
  47. package/lib/esm/utils/bytes.d.ts +39 -0
  48. package/lib/esm/utils/bytes.js +245 -0
  49. package/lib/esm/utils/solidity-keccak256.d.ts +3 -0
  50. package/lib/esm/utils/solidity-keccak256.js +91 -0
  51. package/package.json +15 -17
  52. package/readme.md +252 -62
@@ -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(items) {
81
+ const objects = items.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 { logger } from '../logger/logger';
4
+ import { tinyBig } from '../shared/tiny-big/tiny-big';
5
+ import { arrayify, concat, hexlify, zeroPad } from './bytes';
6
+ const regexBytes = new RegExp('^bytes([0-9]+)$');
7
+ const regexNumber = new RegExp('^(u?int)([0-9]*)$');
8
+ const regexArray = new RegExp('^(.*)\\[([0-9]*)\\]$');
9
+ const Zeros = '0000000000000000000000000000000000000000000000000000000000000000';
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 + Zeros).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
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "essential-eth",
3
3
  "description": "Ultralight JS library for Ethereum utilities",
4
- "version": "0.4.10",
4
+ "version": "0.5.1",
5
5
  "license": "MIT",
6
6
  "sideEffects": false,
7
7
  "main": "./lib/cjs/index.js",
@@ -17,7 +17,7 @@
17
17
  "scripts": {
18
18
  "test": "npm-run-all --parallel jest compile lint",
19
19
  "test:all-node-versions": "npx trevor",
20
- "lint": "eslint .",
20
+ "lint": "eslint --cache --fix .",
21
21
  "compile": "npm-run-all --parallel tsc:esm tsc:cjs",
22
22
  "tsc:esm": "tsc -p tsconfig.json",
23
23
  "tsc:cjs": "tsc -p tsconfig-cjs.json",
@@ -25,7 +25,9 @@
25
25
  "prepublishOnly": "npm run build",
26
26
  "jest": "jest",
27
27
  "build:chains-info": "npx ts-node scripts/fetch-chains-info.ts # used in getNetwork()",
28
- "pre-commit": "sh scripts/pre-commit.sh"
28
+ "update-deps": "sh ./scripts/pre-commit.sh",
29
+ "pre-commit": "run-p build:chains-info update-deps",
30
+ "prepare": "husky install"
29
31
  },
30
32
  "devDependencies": {
31
33
  "@ethersproject/keccak256": "^5.4.0",
@@ -36,19 +38,19 @@
36
38
  "@types/jest-dev-server": "^5.0.0",
37
39
  "@types/node": "^16.10.1",
38
40
  "@types/prettier": "^2.4.4",
39
- "@typescript-eslint/eslint-plugin": "^5.18.0",
40
- "@typescript-eslint/parser": "^5.18.0",
41
+ "@typescript-eslint/eslint-plugin": "^5.20.0",
42
+ "@typescript-eslint/parser": "^5.20.0",
41
43
  "body-parser": "^1.19.0",
42
44
  "dotenv": "^16.0.0",
43
- "eslint": "^8.12.0",
44
- "eslint-plugin-jest": "^26.1.3",
45
- "ethers": "^5.6.2",
45
+ "eslint": "^8.13.0",
46
+ "eslint-plugin-jest": "^26.1.4",
47
+ "ethers": "^5.6.4",
46
48
  "express": "^4.17.1",
47
- "husky": "^4.3.0",
49
+ "husky": "^7.0.4",
48
50
  "jest": "^27.5.1",
49
51
  "jest-dev-server": "^6.0.3",
50
52
  "just-omit": "^2.0.1",
51
- "lint-staged": "^12.3.7",
53
+ "lint-staged": "^12.4.0",
52
54
  "npm-run-all": "^4.1.5",
53
55
  "prettier": "^2.6.2",
54
56
  "prettier-plugin-organize-imports": "^2.3.4",
@@ -56,7 +58,7 @@
56
58
  "ts-node": "^10.2.1",
57
59
  "typedoc": "^0.22.13",
58
60
  "typescript": "^4.6.3",
59
- "web3": "^1.7.1"
61
+ "web3": "^1.7.3"
60
62
  },
61
63
  "dependencies": {
62
64
  "@types/big.js": "^6.1.3",
@@ -64,15 +66,11 @@
64
66
  "isomorphic-unfetch": "^3.1.0",
65
67
  "sha3": "^2.1.4"
66
68
  },
67
- "husky": {
68
- "hooks": {
69
- "pre-commit": "lint-staged && run-p pre-commit test build:chains-info"
70
- }
71
- },
72
69
  "lint-staged": {
73
70
  "*.{js,jsx,ts,tsx,css,scss,md,json,html,yml,yaml}": [
74
71
  "prettier --write"
75
- ]
72
+ ],
73
+ "*.{ts,tsx}": "eslint --cache --fix"
76
74
  },
77
75
  "prettier": {
78
76
  "trailingComma": "all",