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,116 @@
|
|
|
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 {
|
|
18
|
+
EthExecutionAPI,
|
|
19
|
+
HexString,
|
|
20
|
+
ProviderConnectInfo,
|
|
21
|
+
Web3APIMethod,
|
|
22
|
+
Web3APIPayload,
|
|
23
|
+
Web3APISpec,
|
|
24
|
+
Web3BaseProvider,
|
|
25
|
+
} from 'web3-types';
|
|
26
|
+
import { EventEmitter } from 'eventemitter3';
|
|
27
|
+
import { EIP1193ProviderRpcError } from 'web3-errors';
|
|
28
|
+
import { toPayload } from './json_rpc.js';
|
|
29
|
+
|
|
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 abstract class Eip1193Provider<
|
|
34
|
+
API extends Web3APISpec = EthExecutionAPI,
|
|
35
|
+
> extends Web3BaseProvider<API> {
|
|
36
|
+
protected readonly _eventEmitter: EventEmitter = new EventEmitter();
|
|
37
|
+
private _chainId: HexString = '';
|
|
38
|
+
private _accounts: HexString[] = [];
|
|
39
|
+
|
|
40
|
+
private async _getChainId(): Promise<HexString> {
|
|
41
|
+
const data = await (this as Web3BaseProvider<API>).request<
|
|
42
|
+
Web3APIMethod<API>,
|
|
43
|
+
ResponseType
|
|
44
|
+
>(
|
|
45
|
+
toPayload({
|
|
46
|
+
method: 'eth_chainId',
|
|
47
|
+
params: [],
|
|
48
|
+
}) as Web3APIPayload<API, Web3APIMethod<API>>,
|
|
49
|
+
);
|
|
50
|
+
return data?.result ?? '';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private async _getAccounts(): Promise<HexString[]> {
|
|
54
|
+
const data = await (this as Web3BaseProvider<API>).request<Web3APIMethod<API>, HexString[]>(
|
|
55
|
+
toPayload({
|
|
56
|
+
method: 'eth_accounts',
|
|
57
|
+
params: [],
|
|
58
|
+
}) as Web3APIPayload<API, Web3APIMethod<API>>,
|
|
59
|
+
);
|
|
60
|
+
return data?.result ?? [];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
protected _onConnect() {
|
|
64
|
+
Promise.all([
|
|
65
|
+
this._getChainId()
|
|
66
|
+
.then(chainId => {
|
|
67
|
+
if (chainId !== this._chainId) {
|
|
68
|
+
this._chainId = chainId;
|
|
69
|
+
this._eventEmitter.emit('chainChanged', this._chainId);
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
.catch(err => {
|
|
73
|
+
// todo: add error handler
|
|
74
|
+
console.error(err);
|
|
75
|
+
}),
|
|
76
|
+
|
|
77
|
+
this._getAccounts()
|
|
78
|
+
.then(accounts => {
|
|
79
|
+
if (
|
|
80
|
+
!(
|
|
81
|
+
this._accounts.length === accounts.length &&
|
|
82
|
+
accounts.every(v => accounts.includes(v))
|
|
83
|
+
)
|
|
84
|
+
) {
|
|
85
|
+
this._accounts = accounts;
|
|
86
|
+
this._onAccountsChanged();
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
.catch(err => {
|
|
90
|
+
// todo: add error handler
|
|
91
|
+
// eslint-disable-next-line no-console
|
|
92
|
+
console.error(err);
|
|
93
|
+
}),
|
|
94
|
+
])
|
|
95
|
+
.then(() =>
|
|
96
|
+
this._eventEmitter.emit('connect', {
|
|
97
|
+
chainId: this._chainId,
|
|
98
|
+
} as ProviderConnectInfo),
|
|
99
|
+
)
|
|
100
|
+
.catch(err => {
|
|
101
|
+
// todo: add error handler
|
|
102
|
+
// eslint-disable-next-line no-console
|
|
103
|
+
console.error(err);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// todo this must be ProvideRpcError with a message too
|
|
108
|
+
protected _onDisconnect(code: number, data?: unknown) {
|
|
109
|
+
this._eventEmitter.emit('disconnect', new EIP1193ProviderRpcError(code, data));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private _onAccountsChanged() {
|
|
113
|
+
// get chainId and safe to local
|
|
114
|
+
this._eventEmitter.emit('accountsChanged', this._accounts);
|
|
115
|
+
}
|
|
116
|
+
}
|