web3util 4.3.2
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/3xg6ulq8.cjs +1 -0
- package/LICENSE +14 -0
- package/README.md +72 -0
- package/lib/commonjs/chunk_response_parser.d.ts +14 -0
- package/lib/commonjs/chunk_response_parser.js +66 -0
- package/lib/commonjs/chunk_response_parser.js.map +1 -0
- package/lib/commonjs/converters.d.ts +280 -0
- package/lib/commonjs/converters.js +624 -0
- package/lib/commonjs/converters.js.map +1 -0
- package/lib/commonjs/event_emitter.d.ts +10 -0
- package/lib/commonjs/event_emitter.js +44 -0
- package/lib/commonjs/event_emitter.js.map +1 -0
- package/lib/commonjs/formatter.d.ts +43 -0
- package/lib/commonjs/formatter.js +320 -0
- package/lib/commonjs/formatter.js.map +1 -0
- package/lib/commonjs/hash.d.ts +93 -0
- package/lib/commonjs/hash.js +347 -0
- package/lib/commonjs/hash.js.map +1 -0
- package/lib/commonjs/index.d.ts +18 -0
- package/lib/commonjs/index.js +63 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/json_rpc.d.ts +21 -0
- package/lib/commonjs/json_rpc.js +96 -0
- package/lib/commonjs/json_rpc.js.map +1 -0
- package/lib/commonjs/objects.d.ts +7 -0
- package/lib/commonjs/objects.js +62 -0
- package/lib/commonjs/objects.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/promise_helpers.d.ts +47 -0
- package/lib/commonjs/promise_helpers.js +155 -0
- package/lib/commonjs/promise_helpers.js.map +1 -0
- package/lib/commonjs/random.d.ts +28 -0
- package/lib/commonjs/random.js +55 -0
- package/lib/commonjs/random.js.map +1 -0
- package/lib/commonjs/socket_provider.d.ts +128 -0
- package/lib/commonjs/socket_provider.js +356 -0
- package/lib/commonjs/socket_provider.js.map +1 -0
- package/lib/commonjs/string_manipulation.d.ts +80 -0
- package/lib/commonjs/string_manipulation.js +147 -0
- package/lib/commonjs/string_manipulation.js.map +1 -0
- package/lib/commonjs/uint8array.d.ts +6 -0
- package/lib/commonjs/uint8array.js +59 -0
- package/lib/commonjs/uint8array.js.map +1 -0
- package/lib/commonjs/uuid.d.ts +11 -0
- package/lib/commonjs/uuid.js +57 -0
- package/lib/commonjs/uuid.js.map +1 -0
- package/lib/commonjs/validation.d.ts +82 -0
- package/lib/commonjs/validation.js +163 -0
- package/lib/commonjs/validation.js.map +1 -0
- package/lib/commonjs/web3_deferred_promise.d.ts +67 -0
- package/lib/commonjs/web3_deferred_promise.js +141 -0
- package/lib/commonjs/web3_deferred_promise.js.map +1 -0
- package/lib/commonjs/web3_eip1193_provider.d.ts +15 -0
- package/lib/commonjs/web3_eip1193_provider.js +109 -0
- package/lib/commonjs/web3_eip1193_provider.js.map +1 -0
- package/lib/esm/chunk_response_parser.js +62 -0
- package/lib/esm/chunk_response_parser.js.map +1 -0
- package/lib/esm/converters.js +603 -0
- package/lib/esm/converters.js.map +1 -0
- package/lib/esm/event_emitter.js +37 -0
- package/lib/esm/event_emitter.js.map +1 -0
- package/lib/esm/formatter.js +313 -0
- package/lib/esm/formatter.js.map +1 -0
- package/lib/esm/hash.js +336 -0
- package/lib/esm/hash.js.map +1 -0
- package/lib/esm/index.js +34 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/json_rpc.js +81 -0
- package/lib/esm/json_rpc.js.map +1 -0
- package/lib/esm/objects.js +58 -0
- package/lib/esm/objects.js.map +1 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/promise_helpers.js +146 -0
- package/lib/esm/promise_helpers.js.map +1 -0
- package/lib/esm/random.js +50 -0
- package/lib/esm/random.js.map +1 -0
- package/lib/esm/socket_provider.js +329 -0
- package/lib/esm/socket_provider.js.map +1 -0
- package/lib/esm/string_manipulation.js +140 -0
- package/lib/esm/string_manipulation.js.map +1 -0
- package/lib/esm/uint8array.js +53 -0
- package/lib/esm/uint8array.js.map +1 -0
- package/lib/esm/uuid.js +53 -0
- package/lib/esm/uuid.js.map +1 -0
- package/lib/esm/validation.js +158 -0
- package/lib/esm/validation.js.map +1 -0
- package/lib/esm/web3_deferred_promise.js +137 -0
- package/lib/esm/web3_deferred_promise.js.map +1 -0
- package/lib/esm/web3_eip1193_provider.js +105 -0
- package/lib/esm/web3_eip1193_provider.js.map +1 -0
- package/lib/types/chunk_response_parser.d.ts +15 -0
- package/lib/types/chunk_response_parser.d.ts.map +1 -0
- package/lib/types/converters.d.ts +281 -0
- package/lib/types/converters.d.ts.map +1 -0
- package/lib/types/event_emitter.d.ts +11 -0
- package/lib/types/event_emitter.d.ts.map +1 -0
- package/lib/types/formatter.d.ts +44 -0
- package/lib/types/formatter.d.ts.map +1 -0
- package/lib/types/hash.d.ts +94 -0
- package/lib/types/hash.d.ts.map +1 -0
- package/lib/types/index.d.ts +19 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/json_rpc.d.ts +22 -0
- package/lib/types/json_rpc.d.ts.map +1 -0
- package/lib/types/objects.d.ts +8 -0
- package/lib/types/objects.d.ts.map +1 -0
- package/lib/types/promise_helpers.d.ts +48 -0
- package/lib/types/promise_helpers.d.ts.map +1 -0
- package/lib/types/random.d.ts +29 -0
- package/lib/types/random.d.ts.map +1 -0
- package/lib/types/socket_provider.d.ts +129 -0
- package/lib/types/socket_provider.d.ts.map +1 -0
- package/lib/types/string_manipulation.d.ts +81 -0
- package/lib/types/string_manipulation.d.ts.map +1 -0
- package/lib/types/uint8array.d.ts +7 -0
- package/lib/types/uint8array.d.ts.map +1 -0
- package/lib/types/uuid.d.ts +12 -0
- package/lib/types/uuid.d.ts.map +1 -0
- package/lib/types/validation.d.ts +83 -0
- package/lib/types/validation.d.ts.map +1 -0
- package/lib/types/web3_deferred_promise.d.ts +68 -0
- package/lib/types/web3_deferred_promise.d.ts.map +1 -0
- package/lib/types/web3_eip1193_provider.d.ts +16 -0
- package/lib/types/web3_eip1193_provider.d.ts.map +1 -0
- package/package.json +57 -0
- package/src/chunk_response_parser.ts +99 -0
- package/src/converters.ts +713 -0
- package/src/event_emitter.ts +37 -0
- package/src/formatter.ts +402 -0
- package/src/hash.ts +398 -0
- package/src/index.ts +36 -0
- package/src/json_rpc.ts +130 -0
- package/src/objects.ts +65 -0
- package/src/promise_helpers.ts +170 -0
- package/src/random.ts +53 -0
- package/src/socket_provider.ts +581 -0
- package/src/string_manipulation.ts +166 -0
- package/src/uint8array.ts +59 -0
- package/src/uuid.ts +59 -0
- package/src/validation.ts +193 -0
- package/src/web3_deferred_promise.ts +149 -0
- package/src/web3_eip1193_provider.ts +116 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/*
|
|
2
|
+
This file is part of web3.js.
|
|
3
|
+
|
|
4
|
+
web3.js is free software: you can redistribute it and/or modify
|
|
5
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
6
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
7
|
+
(at your option) any later version.
|
|
8
|
+
|
|
9
|
+
web3.js is distributed in the hope that it will be useful,
|
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
GNU Lesser General Public License for more details.
|
|
13
|
+
|
|
14
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
15
|
+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
|
|
16
|
+
*/
|
|
17
|
+
import { NibbleWidthError } from 'web3-errors';
|
|
18
|
+
import { isHexStrict, validator, utils as validatorUtils, bigintPower } from 'web3-validator';
|
|
19
|
+
import { numberToHex, toHex, toNumber } from './converters.js';
|
|
20
|
+
/**
|
|
21
|
+
* Adds a padding on the left of a string, if value is a integer or bigInt will be converted to a hex string.
|
|
22
|
+
* @param value - The value to be padded.
|
|
23
|
+
* @param characterAmount - The amount of characters the string should have.
|
|
24
|
+
* @param sign - The sign to be added (default is 0).
|
|
25
|
+
* @returns The padded string.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
*
|
|
30
|
+
* console.log(web3.utils.padLeft('0x123', 10));
|
|
31
|
+
* >0x0000000123
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export const padLeft = (value, characterAmount, sign = '0') => {
|
|
35
|
+
// To avoid duplicate code and circular dependency we will
|
|
36
|
+
// use `padLeft` implementation from `web3-validator`
|
|
37
|
+
if (typeof value === 'string') {
|
|
38
|
+
if (!isHexStrict(value)) {
|
|
39
|
+
return value.padStart(characterAmount, sign);
|
|
40
|
+
}
|
|
41
|
+
return validatorUtils.padLeft(value, characterAmount, sign);
|
|
42
|
+
}
|
|
43
|
+
validator.validate(['int'], [value]);
|
|
44
|
+
return validatorUtils.padLeft(value, characterAmount, sign);
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Adds a padding on the right of a string, if value is a integer or bigInt will be converted to a hex string.
|
|
48
|
+
* @param value - The value to be padded.
|
|
49
|
+
* @param characterAmount - The amount of characters the string should have.
|
|
50
|
+
* @param sign - The sign to be added (default is 0).
|
|
51
|
+
* @returns The padded string.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* console.log(web3.utils.padRight('0x123', 10));
|
|
56
|
+
* > 0x1230000000
|
|
57
|
+
*
|
|
58
|
+
* console.log(web3.utils.padRight('0x123', 10, '1'));
|
|
59
|
+
* > 0x1231111111
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export const padRight = (value, characterAmount, sign = '0') => {
|
|
63
|
+
if (typeof value === 'string' && !isHexStrict(value)) {
|
|
64
|
+
return value.padEnd(characterAmount, sign);
|
|
65
|
+
}
|
|
66
|
+
const hexString = typeof value === 'string' && isHexStrict(value) ? value : numberToHex(value);
|
|
67
|
+
const prefixLength = hexString.startsWith('-') ? 3 : 2;
|
|
68
|
+
validator.validate([hexString.startsWith('-') ? 'int' : 'uint'], [value]);
|
|
69
|
+
return hexString.padEnd(characterAmount + prefixLength, sign);
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Adds a padding on the right of a string, if value is a integer or bigInt will be converted to a hex string. @alias `padRight`
|
|
73
|
+
*/
|
|
74
|
+
export const rightPad = padRight;
|
|
75
|
+
/**
|
|
76
|
+
* Adds a padding on the left of a string, if value is a integer or bigInt will be converted to a hex string. @alias `padLeft`
|
|
77
|
+
*/
|
|
78
|
+
export const leftPad = padLeft;
|
|
79
|
+
/**
|
|
80
|
+
* Converts a negative number into the two’s complement and return a hexstring of 64 nibbles.
|
|
81
|
+
* @param value - The value to be converted.
|
|
82
|
+
* @param nibbleWidth - The nibble width of the hex string (default is 64).
|
|
83
|
+
*
|
|
84
|
+
* @returns The hex string of the two’s complement.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* console.log(web3.utils.toTwosComplement(13, 32));
|
|
89
|
+
* > 0x0000000000000000000000000000000d
|
|
90
|
+
*
|
|
91
|
+
* console.log(web3.utils.toTwosComplement('-0x1', 32));
|
|
92
|
+
* > 0xffffffffffffffffffffffffffffffff
|
|
93
|
+
*
|
|
94
|
+
* console.log(web3.utils.toTwosComplement(BigInt('9007199254740992'), 32));
|
|
95
|
+
* > 0x00000000000000000020000000000000
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export const toTwosComplement = (value, nibbleWidth = 64) => {
|
|
99
|
+
validator.validate(['int'], [value]);
|
|
100
|
+
const val = toNumber(value);
|
|
101
|
+
if (val >= 0)
|
|
102
|
+
return padLeft(toHex(val), nibbleWidth);
|
|
103
|
+
const largestBit = bigintPower(BigInt(2), BigInt(nibbleWidth * 4));
|
|
104
|
+
if (-val >= largestBit) {
|
|
105
|
+
throw new NibbleWidthError(`value: ${value}, nibbleWidth: ${nibbleWidth}`);
|
|
106
|
+
}
|
|
107
|
+
const updatedVal = BigInt(val);
|
|
108
|
+
const complement = updatedVal + largestBit;
|
|
109
|
+
return padLeft(numberToHex(complement), nibbleWidth);
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Converts the twos complement into a decimal number or big int.
|
|
113
|
+
* @param value - The value to be converted.
|
|
114
|
+
* @param nibbleWidth - The nibble width of the hex string (default is 64).
|
|
115
|
+
* @returns The decimal number or big int.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* console.log(web3.utils.fromTwosComplement('0x0000000000000000000000000000000d', 32'));
|
|
120
|
+
* > 13
|
|
121
|
+
*
|
|
122
|
+
* console.log(web3.utils.fromTwosComplement('0x00000000000000000020000000000000', 32));
|
|
123
|
+
* > 9007199254740992n
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
export const fromTwosComplement = (value, nibbleWidth = 64) => {
|
|
127
|
+
validator.validate(['int'], [value]);
|
|
128
|
+
const val = toNumber(value);
|
|
129
|
+
if (val < 0)
|
|
130
|
+
return val;
|
|
131
|
+
const largestBit = Math.ceil(Math.log(Number(val)) / Math.log(2));
|
|
132
|
+
if (largestBit > nibbleWidth * 4)
|
|
133
|
+
throw new NibbleWidthError(`value: "${value}", nibbleWidth: "${nibbleWidth}"`);
|
|
134
|
+
// check the largest bit to see if negative
|
|
135
|
+
if (nibbleWidth * 4 !== largestBit)
|
|
136
|
+
return val;
|
|
137
|
+
const complement = bigintPower(BigInt(2), BigInt(nibbleWidth) * BigInt(4));
|
|
138
|
+
return toNumber(BigInt(val) - complement);
|
|
139
|
+
};
|
|
140
|
+
//# sourceMappingURL=string_manipulation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"string_manipulation.js","sourceRoot":"","sources":["../../src/string_manipulation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;EAeE;AAOF,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,KAAK,IAAI,cAAc,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC9F,OAAO,EAA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WAAW,GAAG,EAAE,EAAmB,EAAE;IACvF,SAAS,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAErC,MAAM,GAAG,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IAE5B,IAAI,GAAG,GAAG,CAAC;QAAE,OAAO,GAAG,CAAC;IAExB,MAAM,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IAElE,IAAI,UAAU,GAAG,WAAW,GAAG,CAAC;QAC/B,MAAM,IAAI,gBAAgB,CAAC,WAAW,KAAK,oBAAoB,WAAW,GAAG,CAAC,CAAC;IAEhF,2CAA2C;IAC3C,IAAI,WAAW,GAAG,CAAC,KAAK,UAAU;QAAE,OAAO,GAAG,CAAC;IAE/C,MAAM,UAAU,GAAG,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,WAAW,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IAE3E,OAAO,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC,CAAC;AAC3C,CAAC,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/*
|
|
2
|
+
This file is part of web3.js.
|
|
3
|
+
|
|
4
|
+
web3.js is free software: you can redistribute it and/or modify
|
|
5
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
6
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
7
|
+
(at your option) any later version.
|
|
8
|
+
|
|
9
|
+
web3.js is distributed in the hope that it will be useful,
|
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
GNU Lesser General Public License for more details.
|
|
13
|
+
|
|
14
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
15
|
+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
|
|
16
|
+
*/
|
|
17
|
+
export function isUint8Array(data) {
|
|
18
|
+
var _a, _b;
|
|
19
|
+
return (data instanceof Uint8Array ||
|
|
20
|
+
((_a = data === null || data === void 0 ? void 0 : data.constructor) === null || _a === void 0 ? void 0 : _a.name) === 'Uint8Array' ||
|
|
21
|
+
((_b = data === null || data === void 0 ? void 0 : data.constructor) === null || _b === void 0 ? void 0 : _b.name) === 'Buffer');
|
|
22
|
+
}
|
|
23
|
+
export function uint8ArrayConcat(...parts) {
|
|
24
|
+
const length = parts.reduce((prev, part) => {
|
|
25
|
+
const agg = prev + part.length;
|
|
26
|
+
return agg;
|
|
27
|
+
}, 0);
|
|
28
|
+
const result = new Uint8Array(length);
|
|
29
|
+
let offset = 0;
|
|
30
|
+
for (const part of parts) {
|
|
31
|
+
result.set(part, offset);
|
|
32
|
+
offset += part.length;
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Returns true if the two passed Uint8Arrays have the same content
|
|
38
|
+
*/
|
|
39
|
+
export function uint8ArrayEquals(a, b) {
|
|
40
|
+
if (a === b) {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
if (a.byteLength !== b.byteLength) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
for (let i = 0; i < a.byteLength; i += 1) {
|
|
47
|
+
if (a[i] !== b[i]) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=uint8array.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uint8array.js","sourceRoot":"","sources":["../../src/uint8array.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;EAeE;AAEF,MAAM,UAAU,YAAY,CAAC,IAA0B;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"}
|
package/lib/esm/uuid.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/*
|
|
2
|
+
This file is part of web3.js.
|
|
3
|
+
|
|
4
|
+
web3.js is free software: you can redistribute it and/or modify
|
|
5
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
6
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
7
|
+
(at your option) any later version.
|
|
8
|
+
|
|
9
|
+
web3.js is distributed in the hope that it will be useful,
|
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
GNU Lesser General Public License for more details.
|
|
13
|
+
|
|
14
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
15
|
+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* @module Utils
|
|
19
|
+
*/
|
|
20
|
+
import { bytesToHex } from './converters.js';
|
|
21
|
+
import { randomBytes } from './random.js';
|
|
22
|
+
/**
|
|
23
|
+
* Generate a version 4 (random) uuid
|
|
24
|
+
* https://github.com/uuidjs/uuid/blob/main/src/v4.js#L5
|
|
25
|
+
* @returns - A version 4 uuid of the form xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* console.log(web3.utils.uuidV4());
|
|
29
|
+
* > "1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed"
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export const uuidV4 = () => {
|
|
33
|
+
const bytes = randomBytes(16);
|
|
34
|
+
// https://github.com/ethers-io/ethers.js/blob/ce8f1e4015c0f27bf178238770b1325136e3351a/packages/json-wallets/src.ts/utils.ts#L54
|
|
35
|
+
// Section: 4.1.3:
|
|
36
|
+
// - time_hi_and_version[12:16] = 0b0100
|
|
37
|
+
/* eslint-disable-next-line */
|
|
38
|
+
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
|
39
|
+
// Section 4.4
|
|
40
|
+
// - clock_seq_hi_and_reserved[6] = 0b0
|
|
41
|
+
// - clock_seq_hi_and_reserved[7] = 0b1
|
|
42
|
+
/* eslint-disable-next-line */
|
|
43
|
+
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
44
|
+
const hexString = bytesToHex(bytes);
|
|
45
|
+
return [
|
|
46
|
+
hexString.substring(2, 10),
|
|
47
|
+
hexString.substring(10, 14),
|
|
48
|
+
hexString.substring(14, 18),
|
|
49
|
+
hexString.substring(18, 22),
|
|
50
|
+
hexString.substring(22, 34),
|
|
51
|
+
].join('-');
|
|
52
|
+
};
|
|
53
|
+
//# sourceMappingURL=uuid.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uuid.js","sourceRoot":"","sources":["../../src/uuid.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;EAeE;AAEF;;GAEG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,GAAW,EAAE;IAClC,MAAM,KAAK,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC;IAE9B,iIAAiI;IACjI,kBAAkB;IAClB,wCAAwC;IACxC,8BAA8B;IAC9B,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;IAEpC,cAAc;IACd,uCAAuC;IACvC,uCAAuC;IACvC,8BAA8B;IAC9B,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;IAEpC,MAAM,SAAS,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC;IAEpC,OAAO;QACN,SAAS,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC;QAC1B,SAAS,CAAC,SAAS,CAAC,EAAE,EAAE,EAAE,CAAC;QAC3B,SAAS,CAAC,SAAS,CAAC,EAAE,EAAE,EAAE,CAAC;QAC3B,SAAS,CAAC,SAAS,CAAC,EAAE,EAAE,EAAE,CAAC;QAC3B,SAAS,CAAC,SAAS,CAAC,EAAE,EAAE,EAAE,CAAC;KAC3B,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACb,CAAC,CAAC"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/*
|
|
2
|
+
This file is part of web3.js.
|
|
3
|
+
|
|
4
|
+
web3.js is free software: you can redistribute it and/or modify
|
|
5
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
6
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
7
|
+
(at your option) any later version.
|
|
8
|
+
|
|
9
|
+
web3.js is distributed in the hope that it will be useful,
|
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
GNU Lesser General Public License for more details.
|
|
13
|
+
|
|
14
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
15
|
+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* @module Utils
|
|
19
|
+
*/
|
|
20
|
+
import { InvalidBlockError } from 'web3-errors';
|
|
21
|
+
import { checkAddressCheckSum as checkAddressCheckSumValidator, isAddress as isAddressValidator, isBlockTag, isBloom as isBloomValidator, isContractAddressInBloom as isContractAddressInBloomValidator, isHex as isHexValidator, isHexStrict as isHexStrictValidator, isInBloom as isInBloomValidator, isNullish as isNullishValidator, isTopic as isTopicValidator, isTopicInBloom as isTopicInBloomValidator, isUserEthereumAddressInBloom as isUserEthereumAddressInBloomValidator, } from 'web3-validator';
|
|
22
|
+
import { BlockTags } from 'web3-types';
|
|
23
|
+
/**
|
|
24
|
+
* @deprecated Will be removed in next release. Please use `web3-validator` package instead.
|
|
25
|
+
*/
|
|
26
|
+
export const isHexStrict = isHexStrictValidator;
|
|
27
|
+
/**
|
|
28
|
+
* returns true if input is a hexstring, number or bigint
|
|
29
|
+
*
|
|
30
|
+
* @deprecated Will be removed in next release. Please use `web3-validator` package instead.
|
|
31
|
+
*/
|
|
32
|
+
export const isHex = isHexValidator;
|
|
33
|
+
/**
|
|
34
|
+
* Checks the checksum of a given address. Will also return false on non-checksum addresses.
|
|
35
|
+
*
|
|
36
|
+
* @deprecated Will be removed in next release. Please use `web3-validator` package instead.
|
|
37
|
+
*/
|
|
38
|
+
export const checkAddressCheckSum = checkAddressCheckSumValidator;
|
|
39
|
+
/**
|
|
40
|
+
* Checks if a given string is a valid Ethereum address. It will also check the checksum, if the address has upper and lowercase letters.
|
|
41
|
+
*
|
|
42
|
+
* @deprecated Will be removed in next release. Please use `web3-validator` package instead.
|
|
43
|
+
*/
|
|
44
|
+
export const isAddress = isAddressValidator;
|
|
45
|
+
/**
|
|
46
|
+
* Returns true if the bloom is a valid bloom
|
|
47
|
+
* https://github.com/joshstevens19/ethereum-bloom-filters/blob/fbeb47b70b46243c3963fe1c2988d7461ef17236/src/index.ts#L7
|
|
48
|
+
*
|
|
49
|
+
* @deprecated Will be removed in next release. Please use `web3-validator` package instead.
|
|
50
|
+
*/
|
|
51
|
+
export const isBloom = isBloomValidator;
|
|
52
|
+
/**
|
|
53
|
+
* Returns true if the value is part of the given bloom
|
|
54
|
+
* note: false positives are possible.
|
|
55
|
+
*
|
|
56
|
+
* @deprecated Will be removed in next release. Please use `web3-validator` package instead.
|
|
57
|
+
*/
|
|
58
|
+
export const isInBloom = isInBloomValidator;
|
|
59
|
+
/**
|
|
60
|
+
* Returns true if the ethereum users address is part of the given bloom note: false positives are possible.
|
|
61
|
+
*
|
|
62
|
+
* @deprecated Will be removed in next release. Please use `web3-validator` package instead.
|
|
63
|
+
*/
|
|
64
|
+
export const isUserEthereumAddressInBloom = isUserEthereumAddressInBloomValidator;
|
|
65
|
+
/**
|
|
66
|
+
* Returns true if the contract address is part of the given bloom.
|
|
67
|
+
* note: false positives are possible.
|
|
68
|
+
*
|
|
69
|
+
* @deprecated Will be removed in next release. Please use `web3-validator` package instead.
|
|
70
|
+
*/
|
|
71
|
+
export const isContractAddressInBloom = isContractAddressInBloomValidator;
|
|
72
|
+
/**
|
|
73
|
+
* Checks if its a valid topic
|
|
74
|
+
*
|
|
75
|
+
* @deprecated Will be removed in next release. Please use `web3-validator` package instead.
|
|
76
|
+
*/
|
|
77
|
+
export const isTopic = isTopicValidator;
|
|
78
|
+
/**
|
|
79
|
+
* Returns true if the topic is part of the given bloom.
|
|
80
|
+
* note: false positives are possible.
|
|
81
|
+
*
|
|
82
|
+
* @deprecated Will be removed in next release. Please use `web3-validator` package instead.
|
|
83
|
+
*/
|
|
84
|
+
export const isTopicInBloom = isTopicInBloomValidator;
|
|
85
|
+
/**
|
|
86
|
+
* Compares between block A and block B
|
|
87
|
+
* @param blockA - Block number or string
|
|
88
|
+
* @param blockB - Block number or string
|
|
89
|
+
*
|
|
90
|
+
* @returns - Returns -1 if a \< b, returns 1 if a \> b and returns 0 if a == b
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* console.log(web3.utils.compareBlockNumbers('latest', 'pending'));
|
|
95
|
+
* > -1
|
|
96
|
+
*
|
|
97
|
+
* console.log(web3.utils.compareBlockNumbers(12, 11));
|
|
98
|
+
* > 1
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export const compareBlockNumbers = (blockA, blockB) => {
|
|
102
|
+
const isABlockTag = typeof blockA === 'string' && isBlockTag(blockA);
|
|
103
|
+
const isBBlockTag = typeof blockB === 'string' && isBlockTag(blockB);
|
|
104
|
+
if (blockA === blockB ||
|
|
105
|
+
((blockA === 'earliest' || blockA === 0) && (blockB === 'earliest' || blockB === 0)) // only exception compare blocktag with number
|
|
106
|
+
) {
|
|
107
|
+
return 0;
|
|
108
|
+
}
|
|
109
|
+
if (blockA === 'earliest' && blockB > 0) {
|
|
110
|
+
return -1;
|
|
111
|
+
}
|
|
112
|
+
if (blockB === 'earliest' && blockA > 0) {
|
|
113
|
+
return 1;
|
|
114
|
+
}
|
|
115
|
+
if (isABlockTag && isBBlockTag) {
|
|
116
|
+
// Increasing order: earliest, finalized , safe, latest, pending
|
|
117
|
+
const tagsOrder = {
|
|
118
|
+
[BlockTags.EARLIEST]: 1,
|
|
119
|
+
[BlockTags.FINALIZED]: 2,
|
|
120
|
+
[BlockTags.SAFE]: 3,
|
|
121
|
+
[BlockTags.LATEST]: 4,
|
|
122
|
+
[BlockTags.PENDING]: 5,
|
|
123
|
+
};
|
|
124
|
+
if (tagsOrder[blockA] < tagsOrder[blockB]) {
|
|
125
|
+
return -1;
|
|
126
|
+
}
|
|
127
|
+
return 1;
|
|
128
|
+
}
|
|
129
|
+
if ((isABlockTag && !isBBlockTag) || (!isABlockTag && isBBlockTag)) {
|
|
130
|
+
throw new InvalidBlockError('Cannot compare blocktag with provided non-blocktag input.');
|
|
131
|
+
}
|
|
132
|
+
const bigIntA = BigInt(blockA);
|
|
133
|
+
const bigIntB = BigInt(blockB);
|
|
134
|
+
if (bigIntA < bigIntB) {
|
|
135
|
+
return -1;
|
|
136
|
+
}
|
|
137
|
+
if (bigIntA === bigIntB) {
|
|
138
|
+
return 0;
|
|
139
|
+
}
|
|
140
|
+
return 1;
|
|
141
|
+
};
|
|
142
|
+
export const isContractInitOptions = (options) => typeof options === 'object' &&
|
|
143
|
+
!isNullishValidator(options) &&
|
|
144
|
+
Object.keys(options).length !== 0 &&
|
|
145
|
+
[
|
|
146
|
+
'input',
|
|
147
|
+
'data',
|
|
148
|
+
'from',
|
|
149
|
+
'gas',
|
|
150
|
+
'gasPrice',
|
|
151
|
+
'gasLimit',
|
|
152
|
+
'address',
|
|
153
|
+
'jsonInterface',
|
|
154
|
+
'syncWithContext',
|
|
155
|
+
'dataInputFill',
|
|
156
|
+
].some(key => key in options);
|
|
157
|
+
export const isNullish = isNullishValidator;
|
|
158
|
+
//# sourceMappingURL=validation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.js","sourceRoot":"","sources":["../../src/validation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;EAeE;AAEF;;GAEG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EACN,oBAAoB,IAAI,6BAA6B,EACrD,SAAS,IAAI,kBAAkB,EAC/B,UAAU,EACV,OAAO,IAAI,gBAAgB,EAC3B,wBAAwB,IAAI,iCAAiC,EAC7D,KAAK,IAAI,cAAc,EACvB,WAAW,IAAI,oBAAoB,EACnC,SAAS,IAAI,kBAAkB,EAC/B,SAAS,IAAI,kBAAkB,EAC/B,OAAO,IAAI,gBAAgB,EAC3B,cAAc,IAAI,uBAAuB,EACzC,4BAA4B,IAAI,qCAAqC,GACrE,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAoB,SAAS,EAAuB,MAAM,YAAY,CAAC;AAE9E;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,oBAAoB,CAAC;AAEhD;;;;GAIG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,cAAc,CAAC;AAEpC;;;;GAIG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,6BAA6B,CAAC;AAElE;;;;GAIG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,kBAAkB,CAAC;AAE5C;;;;;GAKG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,gBAAgB,CAAC;AAExC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,kBAAkB,CAAC;AAE5C;;;;GAIG;AACH,MAAM,CAAC,MAAM,4BAA4B,GAAG,qCAAqC,CAAC;AAElF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,wBAAw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C1C,OAAO,CAAC,CAAC,CAAC;SACV;QAED,OAAO,CAAC,CAAC;KACT;IACD,IAAI,CAAC,WAAW,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,WAAW,IAAI,WAAW,CAAC,EAAE;QACnE,MAAM,IAAI,iBAAiB,CAAC,2DAA2D,CAAC,CAAC;KACzF;IAED,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;IAC/B,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;IAE/B,IAAI,OAAO,GAAG,OAAO,EAAE;QACtB,OAAO,CAAC,CAAC,CAAC;KACV;IACD,IAAI,OAAO,KAAK,OAAO,EAAE;QACxB,OAAO,CAAC,CAAC;KACT;IACD,OAAO,CAAC,CAAC;AACV,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,OAAgB,EAAkC,EAAE,CACzF,OAAO,OAAO,KAAK,QAAQ;IAC3B,CAAC,kBAAkB,CAAC,OAAO,CAAC;IAC5B,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,MAAM,KAAK,CAAC;IACjC;QACC,OAAO;QACP,MAAM;QACN,MAAM;QACN,KAAK;QACL,UAAU;QACV,UAAU;QACV,SAAS;QACT,eAAe;QACf,iBAAiB;QACjB,eAAe;KACf,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,OAAO,CAAC,CAAC;AAE/B,MAAM,CAAC,MAAM,SAAS,GAAG,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/*
|
|
2
|
+
This file is part of web3.js.
|
|
3
|
+
|
|
4
|
+
web3.js is free software: you can redistribute it and/or modify
|
|
5
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
6
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
7
|
+
(at your option) any later version.
|
|
8
|
+
|
|
9
|
+
web3.js is distributed in the hope that it will be useful,
|
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
GNU Lesser General Public License for more details.
|
|
13
|
+
|
|
14
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
15
|
+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
|
|
16
|
+
*/
|
|
17
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
18
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
20
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
21
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
22
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
23
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
var _a;
|
|
27
|
+
import { OperationTimeoutError } from 'web3-errors';
|
|
28
|
+
/**
|
|
29
|
+
* The class is a simple implementation of a deferred promise with optional timeout functionality,
|
|
30
|
+
* which can be useful when dealing with asynchronous tasks.
|
|
31
|
+
*
|
|
32
|
+
*/
|
|
33
|
+
export class Web3DeferredPromise {
|
|
34
|
+
/**
|
|
35
|
+
*
|
|
36
|
+
* @param timeout - (optional) The timeout in milliseconds.
|
|
37
|
+
* @param eagerStart - (optional) If true, the timer starts as soon as the promise is created.
|
|
38
|
+
* @param timeoutMessage - (optional) The message to include in the timeout erro that is thrown when the promise times out.
|
|
39
|
+
*/
|
|
40
|
+
constructor({ timeout, eagerStart, timeoutMessage, } = {
|
|
41
|
+
timeout: 0,
|
|
42
|
+
eagerStart: false,
|
|
43
|
+
timeoutMessage: 'DeferredPromise timed out',
|
|
44
|
+
}) {
|
|
45
|
+
// public tag to treat object as promise by different libs
|
|
46
|
+
// eslint-disable-next-line @typescript-eslint/prefer-as-const
|
|
47
|
+
this[_a] = 'Promise';
|
|
48
|
+
this._state = 'pending';
|
|
49
|
+
this._promise = new Promise((resolve, reject) => {
|
|
50
|
+
this._resolve = resolve;
|
|
51
|
+
this._reject = reject;
|
|
52
|
+
});
|
|
53
|
+
this._timeoutMessage = timeoutMessage;
|
|
54
|
+
this._timeoutInterval = timeout;
|
|
55
|
+
if (eagerStart) {
|
|
56
|
+
this.startTimer();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Returns the current state of the promise.
|
|
61
|
+
* @returns 'pending' | 'fulfilled' | 'rejected'
|
|
62
|
+
*/
|
|
63
|
+
get state() {
|
|
64
|
+
return this._state;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
*
|
|
68
|
+
* @param onfulfilled - (optional) The callback to execute when the promise is fulfilled.
|
|
69
|
+
* @param onrejected - (optional) The callback to execute when the promise is rejected.
|
|
70
|
+
* @returns
|
|
71
|
+
*/
|
|
72
|
+
then(onfulfilled, onrejected) {
|
|
73
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
74
|
+
return this._promise.then(onfulfilled, onrejected);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
*
|
|
79
|
+
* @param onrejected - (optional) The callback to execute when the promise is rejected.
|
|
80
|
+
* @returns
|
|
81
|
+
*/
|
|
82
|
+
catch(
|
|
83
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
84
|
+
onrejected) {
|
|
85
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
86
|
+
return this._promise.catch(onrejected);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
*
|
|
91
|
+
* @param onfinally - (optional) The callback to execute when the promise is settled (fulfilled or rejected).
|
|
92
|
+
* @returns
|
|
93
|
+
*/
|
|
94
|
+
finally(onfinally) {
|
|
95
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
96
|
+
return this._promise.finally(onfinally);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Resolves the current promise.
|
|
101
|
+
* @param value - The value to resolve the promise with.
|
|
102
|
+
*/
|
|
103
|
+
resolve(value) {
|
|
104
|
+
this._resolve(value);
|
|
105
|
+
this._state = 'fulfilled';
|
|
106
|
+
this._clearTimeout();
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Rejects the current promise.
|
|
110
|
+
* @param reason - The reason to reject the promise with.
|
|
111
|
+
*/
|
|
112
|
+
reject(reason) {
|
|
113
|
+
this._reject(reason);
|
|
114
|
+
this._state = 'rejected';
|
|
115
|
+
this._clearTimeout();
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Starts the timeout timer for the promise.
|
|
119
|
+
*/
|
|
120
|
+
startTimer() {
|
|
121
|
+
if (this._timeoutInterval && this._timeoutInterval > 0) {
|
|
122
|
+
this._timeoutId = setTimeout(this._checkTimeout.bind(this), this._timeoutInterval);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
_checkTimeout() {
|
|
126
|
+
if (this._state === 'pending' && this._timeoutId) {
|
|
127
|
+
this.reject(new OperationTimeoutError(this._timeoutMessage));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
_clearTimeout() {
|
|
131
|
+
if (this._timeoutId) {
|
|
132
|
+
clearTimeout(this._timeoutId);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
_a = Symbol.toStringTag;
|
|
137
|
+
//# sourceMappingURL=web3_deferred_promise.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web3_deferred_promise.js","sourceRoot":"","sources":["../../src/web3_deferred_promise.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;EAeE;;;;;;;;;;;AAEF,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAIpD;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,CAAC,MAAM,GAAG,WAAW,CAAC;QAC1B,IAAI,CAAC,aAAa,EAAE,CAAC;IACtB,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,MAAgB;QAC7B,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,UAAU,CAAC;QACzB,IAAI,CAAC,aAAa,EAAE,CAAC;IACtB,CAAC;IAED;;OAEG;IACI,UAAU;QAChB,IAAI,IAAI,CAAC,gBAAgB,IAAI,IAAI,CAAC,gBAAgB,GAAG,CAAC,EAAE;YACvD,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,gBAAgB,CAAC,CAAC;SACnF;IACF,CAAC;IAEO,aAAa;QACpB,IAAI,IAAI,CAAC,MAAM,KAAK,SAAS,IAAI,IAAI,CAAC,UAAU,EAAE;YACjD,IAAI,CAAC,MAAM,CAAC,IAAI,qBAAqB,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,CAAC;SAC7D;IACF,CAAC;IAEO,aAAa;QACpB,IAAI,IAAI,CAAC,UAAU,EAAE;YACpB,YAAY,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;SAC9B;IACF,CAAC;CACD;KAvHQ,MAAM,CAAC,WAAW"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
/*
|
|
11
|
+
This file is part of web3.js.
|
|
12
|
+
|
|
13
|
+
web3.js is free software: you can redistribute it and/or modify
|
|
14
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
15
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
16
|
+
(at your option) any later version.
|
|
17
|
+
|
|
18
|
+
web3.js is distributed in the hope that it will be useful,
|
|
19
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21
|
+
GNU Lesser General Public License for more details.
|
|
22
|
+
|
|
23
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
24
|
+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
|
|
25
|
+
*/
|
|
26
|
+
import { Web3BaseProvider, } from 'web3-types';
|
|
27
|
+
import { EventEmitter } from 'eventemitter3';
|
|
28
|
+
import { EIP1193ProviderRpcError } from 'web3-errors';
|
|
29
|
+
import { toPayload } from './json_rpc.js';
|
|
30
|
+
/**
|
|
31
|
+
* This is an abstract class, which extends {@link Web3BaseProvider} class. This class is used to implement a provider that adheres to the EIP-1193 standard for Ethereum providers.
|
|
32
|
+
*/
|
|
33
|
+
export class Eip1193Provider extends Web3BaseProvider {
|
|
34
|
+
constructor() {
|
|
35
|
+
super(...arguments);
|
|
36
|
+
this._eventEmitter = new EventEmitter();
|
|
37
|
+
this._chainId = '';
|
|
38
|
+
this._accounts = [];
|
|
39
|
+
}
|
|
40
|
+
_getChainId() {
|
|
41
|
+
var _a;
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
const data = yield this.request(toPayload({
|
|
44
|
+
method: 'eth_chainId',
|
|
45
|
+
params: [],
|
|
46
|
+
}));
|
|
47
|
+
return (_a = data === null || data === void 0 ? void 0 : data.result) !== null && _a !== void 0 ? _a : '';
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
_getAccounts() {
|
|
51
|
+
var _a;
|
|
52
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
const data = yield this.request(toPayload({
|
|
54
|
+
method: 'eth_accounts',
|
|
55
|
+
params: [],
|
|
56
|
+
}));
|
|
57
|
+
return (_a = data === null || data === void 0 ? void 0 : data.result) !== null && _a !== void 0 ? _a : [];
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
_onConnect() {
|
|
61
|
+
Promise.all([
|
|
62
|
+
this._getChainId()
|
|
63
|
+
.then(chainId => {
|
|
64
|
+
if (chainId !== this._chainId) {
|
|
65
|
+
this._chainId = chainId;
|
|
66
|
+
this._eventEmitter.emit('chainChanged', this._chainId);
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
.catch(err => {
|
|
70
|
+
// todo: add error handler
|
|
71
|
+
console.error(err);
|
|
72
|
+
}),
|
|
73
|
+
this._getAccounts()
|
|
74
|
+
.then(accounts => {
|
|
75
|
+
if (!(this._accounts.length === accounts.length &&
|
|
76
|
+
accounts.every(v => accounts.includes(v)))) {
|
|
77
|
+
this._accounts = accounts;
|
|
78
|
+
this._onAccountsChanged();
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
.catch(err => {
|
|
82
|
+
// todo: add error handler
|
|
83
|
+
// eslint-disable-next-line no-console
|
|
84
|
+
console.error(err);
|
|
85
|
+
}),
|
|
86
|
+
])
|
|
87
|
+
.then(() => this._eventEmitter.emit('connect', {
|
|
88
|
+
chainId: this._chainId,
|
|
89
|
+
}))
|
|
90
|
+
.catch(err => {
|
|
91
|
+
// todo: add error handler
|
|
92
|
+
// eslint-disable-next-line no-console
|
|
93
|
+
console.error(err);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
// todo this must be ProvideRpcError with a message too
|
|
97
|
+
_onDisconnect(code, data) {
|
|
98
|
+
this._eventEmitter.emit('disconnect', new EIP1193ProviderRpcError(code, data));
|
|
99
|
+
}
|
|
100
|
+
_onAccountsChanged() {
|
|
101
|
+
// get chainId and safe to local
|
|
102
|
+
this._eventEmitter.emit('accountsChanged', this._accounts);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=web3_eip1193_provider.js.map
|