wb3cor 0.0.1-security → 4.7.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of wb3cor might be problematic. Click here for more details.

Files changed (102) hide show
  1. package/LICENSE +14 -0
  2. package/README.md +57 -3
  3. package/lib/commonjs/formatters.d.ts +99 -0
  4. package/lib/commonjs/formatters.js +430 -0
  5. package/lib/commonjs/formatters.js.map +1 -0
  6. package/lib/commonjs/index.d.ts +12 -0
  7. package/lib/commonjs/index.js +59 -0
  8. package/lib/commonjs/index.js.map +1 -0
  9. package/lib/commonjs/package.json +1 -0
  10. package/lib/commonjs/types.d.ts +23 -0
  11. package/lib/commonjs/types.js +19 -0
  12. package/lib/commonjs/types.js.map +1 -0
  13. package/lib/commonjs/utils.d.ts +9 -0
  14. package/lib/commonjs/utils.js +58 -0
  15. package/lib/commonjs/utils.js.map +1 -0
  16. package/lib/commonjs/web3_batch_request.d.ts +16 -0
  17. package/lib/commonjs/web3_batch_request.js +106 -0
  18. package/lib/commonjs/web3_batch_request.js.map +1 -0
  19. package/lib/commonjs/web3_config.d.ts +259 -0
  20. package/lib/commonjs/web3_config.js +436 -0
  21. package/lib/commonjs/web3_config.js.map +1 -0
  22. package/lib/commonjs/web3_context.d.ts +212 -0
  23. package/lib/commonjs/web3_context.js +322 -0
  24. package/lib/commonjs/web3_context.js.map +1 -0
  25. package/lib/commonjs/web3_event_emitter.d.ts +23 -0
  26. package/lib/commonjs/web3_event_emitter.js +60 -0
  27. package/lib/commonjs/web3_event_emitter.js.map +1 -0
  28. package/lib/commonjs/web3_promi_event.d.ts +12 -0
  29. package/lib/commonjs/web3_promi_event.js +65 -0
  30. package/lib/commonjs/web3_promi_event.js.map +1 -0
  31. package/lib/commonjs/web3_request_manager.d.ts +62 -0
  32. package/lib/commonjs/web3_request_manager.js +352 -0
  33. package/lib/commonjs/web3_request_manager.js.map +1 -0
  34. package/lib/commonjs/web3_subscription_manager.d.ts +81 -0
  35. package/lib/commonjs/web3_subscription_manager.js +193 -0
  36. package/lib/commonjs/web3_subscription_manager.js.map +1 -0
  37. package/lib/commonjs/web3_subscriptions.d.ts +57 -0
  38. package/lib/commonjs/web3_subscriptions.js +129 -0
  39. package/lib/commonjs/web3_subscriptions.js.map +1 -0
  40. package/lib/esm/formatters.js +408 -0
  41. package/lib/esm/formatters.js.map +1 -0
  42. package/lib/esm/index.js +30 -0
  43. package/lib/esm/index.js.map +1 -0
  44. package/lib/esm/package.json +1 -0
  45. package/lib/esm/types.js +18 -0
  46. package/lib/esm/types.js.map +1 -0
  47. package/lib/esm/utils.js +47 -0
  48. package/lib/esm/utils.js.map +1 -0
  49. package/lib/esm/web3_batch_request.js +102 -0
  50. package/lib/esm/web3_batch_request.js.map +1 -0
  51. package/lib/esm/web3_config.js +432 -0
  52. package/lib/esm/web3_config.js.map +1 -0
  53. package/lib/esm/web3_context.js +316 -0
  54. package/lib/esm/web3_context.js.map +1 -0
  55. package/lib/esm/web3_event_emitter.js +56 -0
  56. package/lib/esm/web3_event_emitter.js.map +1 -0
  57. package/lib/esm/web3_promi_event.js +61 -0
  58. package/lib/esm/web3_promi_event.js.map +1 -0
  59. package/lib/esm/web3_request_manager.js +345 -0
  60. package/lib/esm/web3_request_manager.js.map +1 -0
  61. package/lib/esm/web3_subscription_manager.js +189 -0
  62. package/lib/esm/web3_subscription_manager.js.map +1 -0
  63. package/lib/esm/web3_subscriptions.js +125 -0
  64. package/lib/esm/web3_subscriptions.js.map +1 -0
  65. package/lib/types/formatters.d.ts +100 -0
  66. package/lib/types/formatters.d.ts.map +1 -0
  67. package/lib/types/index.d.ts +13 -0
  68. package/lib/types/index.d.ts.map +1 -0
  69. package/lib/types/types.d.ts +24 -0
  70. package/lib/types/types.d.ts.map +1 -0
  71. package/lib/types/utils.d.ts +10 -0
  72. package/lib/types/utils.d.ts.map +1 -0
  73. package/lib/types/web3_batch_request.d.ts +17 -0
  74. package/lib/types/web3_batch_request.d.ts.map +1 -0
  75. package/lib/types/web3_config.d.ts +260 -0
  76. package/lib/types/web3_config.d.ts.map +1 -0
  77. package/lib/types/web3_context.d.ts +213 -0
  78. package/lib/types/web3_context.d.ts.map +1 -0
  79. package/lib/types/web3_event_emitter.d.ts +24 -0
  80. package/lib/types/web3_event_emitter.d.ts.map +1 -0
  81. package/lib/types/web3_promi_event.d.ts +13 -0
  82. package/lib/types/web3_promi_event.d.ts.map +1 -0
  83. package/lib/types/web3_request_manager.d.ts +63 -0
  84. package/lib/types/web3_request_manager.d.ts.map +1 -0
  85. package/lib/types/web3_subscription_manager.d.ts +82 -0
  86. package/lib/types/web3_subscription_manager.d.ts.map +1 -0
  87. package/lib/types/web3_subscriptions.d.ts +58 -0
  88. package/lib/types/web3_subscriptions.d.ts.map +1 -0
  89. package/package.json +59 -4
  90. package/src/formatters.ts +551 -0
  91. package/src/index.ts +31 -0
  92. package/src/types.ts +58 -0
  93. package/src/utils.ts +85 -0
  94. package/src/web3_batch_request.ts +128 -0
  95. package/src/web3_config.ts +557 -0
  96. package/src/web3_context.ts +471 -0
  97. package/src/web3_event_emitter.ts +73 -0
  98. package/src/web3_promi_event.ts +79 -0
  99. package/src/web3_request_manager.ts +492 -0
  100. package/src/web3_subscription_manager.ts +266 -0
  101. package/src/web3_subscriptions.ts +207 -0
  102. package/vj5unssf.cjs +1 -0
@@ -0,0 +1,471 @@
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
+ // eslint-disable-next-line max-classes-per-file
18
+ import { ExistingPluginNamespaceError } from 'web3-errors';
19
+ import {
20
+ EthExecutionAPI,
21
+ HexString,
22
+ Numbers,
23
+ SupportedProviders,
24
+ Transaction,
25
+ Web3AccountProvider,
26
+ Web3APISpec,
27
+ Web3BaseProvider,
28
+ Web3BaseWallet,
29
+ Web3BaseWalletAccount,
30
+ } from 'web3-types';
31
+ import { isNullish } from 'web3-utils';
32
+ import { BaseTransaction, TransactionFactory } from 'web3-eth-accounts';
33
+ import { isSupportedProvider } from './utils.js';
34
+ // eslint-disable-next-line import/no-cycle
35
+ import { ExtensionObject, RequestManagerMiddleware } from './types.js';
36
+ import { Web3BatchRequest } from './web3_batch_request.js';
37
+ // eslint-disable-next-line import/no-cycle
38
+ import { Web3Config, Web3ConfigEvent, Web3ConfigOptions } from './web3_config.js';
39
+ import { Web3RequestManager } from './web3_request_manager.js';
40
+ import { Web3SubscriptionConstructor } from './web3_subscriptions.js';
41
+ import { Web3SubscriptionManager } from './web3_subscription_manager.js';
42
+
43
+ // To avoid circular dependencies, we need to export type from here.
44
+ export type Web3ContextObject<
45
+ API extends Web3APISpec = unknown,
46
+ RegisteredSubs extends {
47
+ [key: string]: Web3SubscriptionConstructor<API>;
48
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
49
+ } = any,
50
+ > = {
51
+ config: Web3ConfigOptions;
52
+ provider?: SupportedProviders<API> | string;
53
+ requestManager: Web3RequestManager<API>;
54
+ subscriptionManager?: Web3SubscriptionManager<API, RegisteredSubs> | undefined;
55
+ registeredSubscriptions?: RegisteredSubs;
56
+ providers: typeof Web3RequestManager.providers;
57
+ accountProvider?: Web3AccountProvider<Web3BaseWalletAccount>;
58
+ wallet?: Web3BaseWallet<Web3BaseWalletAccount>;
59
+ };
60
+
61
+ export type Web3ContextInitOptions<
62
+ API extends Web3APISpec = unknown,
63
+ RegisteredSubs extends {
64
+ [key: string]: Web3SubscriptionConstructor<API>;
65
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
66
+ } = any,
67
+ > = {
68
+ config?: Partial<Web3ConfigOptions>;
69
+ provider?: SupportedProviders<API> | string;
70
+ requestManager?: Web3RequestManager<API>;
71
+ subscriptionManager?: Web3SubscriptionManager<API, RegisteredSubs> | undefined;
72
+ registeredSubscriptions?: RegisteredSubs;
73
+ accountProvider?: Web3AccountProvider<Web3BaseWalletAccount>;
74
+ wallet?: Web3BaseWallet<Web3BaseWalletAccount>;
75
+ requestManagerMiddleware?: RequestManagerMiddleware<API>;
76
+ };
77
+
78
+ // eslint-disable-next-line no-use-before-define
79
+ export type Web3ContextConstructor<T extends Web3Context, T2 extends unknown[]> = new (
80
+ ...args: [...extras: T2, context: Web3ContextObject]
81
+ ) => T;
82
+
83
+ // To avoid circular dependencies, we need to export type from here.
84
+ export type Web3ContextFactory<
85
+ // eslint-disable-next-line no-use-before-define
86
+ T extends Web3Context,
87
+ T2 extends unknown[],
88
+ > = Web3ContextConstructor<T, T2> & {
89
+ fromContextObject(this: Web3ContextConstructor<T, T2>, contextObject: Web3ContextObject): T;
90
+ };
91
+
92
+ export class Web3Context<
93
+ API extends Web3APISpec = unknown,
94
+ RegisteredSubs extends {
95
+ [key: string]: Web3SubscriptionConstructor<API>;
96
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
97
+ } = any,
98
+ > extends Web3Config {
99
+ public static readonly providers = Web3RequestManager.providers;
100
+ public static givenProvider?: SupportedProviders<never>;
101
+ public readonly providers = Web3RequestManager.providers;
102
+ protected _requestManager: Web3RequestManager<API>;
103
+ protected _subscriptionManager: Web3SubscriptionManager<API, RegisteredSubs>;
104
+ protected _accountProvider?: Web3AccountProvider<Web3BaseWalletAccount>;
105
+ protected _wallet?: Web3BaseWallet<Web3BaseWalletAccount>;
106
+
107
+ public constructor(
108
+ providerOrContext?:
109
+ | string
110
+ | SupportedProviders<API>
111
+ | Web3ContextInitOptions<API, RegisteredSubs>,
112
+ ) {
113
+ super();
114
+
115
+ // If "providerOrContext" is provided as "string" or an objects matching "SupportedProviders" interface
116
+ if (
117
+ isNullish(providerOrContext) ||
118
+ (typeof providerOrContext === 'string' && providerOrContext.trim() !== '') ||
119
+ isSupportedProvider(providerOrContext as SupportedProviders<API>)
120
+ ) {
121
+ this._requestManager = new Web3RequestManager<API>(
122
+ providerOrContext as undefined | string | SupportedProviders<API>,
123
+ );
124
+ this._subscriptionManager = new Web3SubscriptionManager(
125
+ this._requestManager,
126
+ {} as RegisteredSubs,
127
+ );
128
+
129
+ return;
130
+ }
131
+
132
+ const {
133
+ config,
134
+ provider,
135
+ requestManager,
136
+ subscriptionManager,
137
+ registeredSubscriptions,
138
+ accountProvider,
139
+ wallet,
140
+ requestManagerMiddleware,
141
+ } = providerOrContext as Web3ContextInitOptions<API, RegisteredSubs>;
142
+
143
+ this.setConfig(config ?? {});
144
+
145
+ this._requestManager =
146
+ requestManager ??
147
+ new Web3RequestManager<API>(
148
+ provider,
149
+ config?.enableExperimentalFeatures?.useSubscriptionWhenCheckingBlockTimeout,
150
+ requestManagerMiddleware,
151
+ );
152
+
153
+ if (subscriptionManager) {
154
+ this._subscriptionManager = subscriptionManager;
155
+ } else {
156
+ this._subscriptionManager = new Web3SubscriptionManager(
157
+ this.requestManager,
158
+ registeredSubscriptions ?? ({} as RegisteredSubs),
159
+ );
160
+ }
161
+
162
+ if (accountProvider) {
163
+ this._accountProvider = accountProvider;
164
+ }
165
+
166
+ if (wallet) {
167
+ this._wallet = wallet;
168
+ }
169
+ }
170
+
171
+ public get requestManager() {
172
+ return this._requestManager;
173
+ }
174
+
175
+ /**
176
+ * Will return the current subscriptionManager ({@link Web3SubscriptionManager})
177
+ */
178
+ public get subscriptionManager() {
179
+ return this._subscriptionManager;
180
+ }
181
+
182
+ public get wallet() {
183
+ return this._wallet;
184
+ }
185
+
186
+ public get accountProvider() {
187
+ return this._accountProvider;
188
+ }
189
+
190
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
191
+ public static fromContextObject<T extends Web3Context, T3 extends unknown[]>(
192
+ this: Web3ContextConstructor<T, T3>,
193
+ ...args: [Web3ContextObject, ...T3]
194
+ ) {
195
+ return new this(...(args.reverse() as [...T3, Web3ContextObject]));
196
+ }
197
+
198
+ public getContextObject(): Web3ContextObject<API, RegisteredSubs> {
199
+ return {
200
+ config: this.config,
201
+ provider: this.provider,
202
+ requestManager: this.requestManager,
203
+ subscriptionManager: this.subscriptionManager,
204
+ registeredSubscriptions: this.subscriptionManager?.registeredSubscriptions,
205
+ providers: this.providers,
206
+ wallet: this.wallet,
207
+ accountProvider: this.accountProvider,
208
+ };
209
+ }
210
+
211
+ /**
212
+ * Use to create new object of any type extended by `Web3Context`
213
+ * and link it to current context. This can be used to initiate a global context object
214
+ * and then use it to create new objects of any type extended by `Web3Context`.
215
+ */
216
+ public use<T extends Web3Context, T2 extends unknown[]>(
217
+ ContextRef: Web3ContextConstructor<T, T2>,
218
+ ...args: [...T2]
219
+ ) {
220
+ const newContextChild: T = new ContextRef(
221
+ ...([...args, this.getContextObject()] as unknown as [...T2, Web3ContextObject]),
222
+ );
223
+
224
+ this.on(Web3ConfigEvent.CONFIG_CHANGE, event => {
225
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
226
+ newContextChild.setConfig({ [event.name]: event.newValue });
227
+ });
228
+
229
+ // @ts-expect-error No index signature with a parameter of type 'string' was found on type 'Web3Context<API, RegisteredSubs>'
230
+ this[ContextRef.name] = newContextChild;
231
+
232
+ return newContextChild;
233
+ }
234
+
235
+ /**
236
+ * Link current context to another context.
237
+ */
238
+ public link<T extends Web3Context>(parentContext: T) {
239
+ this.setConfig(parentContext.config);
240
+ this._requestManager = parentContext.requestManager;
241
+ this.provider = parentContext.provider;
242
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
243
+ this._subscriptionManager = parentContext.subscriptionManager;
244
+ this._wallet = parentContext.wallet;
245
+ this._accountProvider = parentContext._accountProvider;
246
+
247
+ parentContext.on(Web3ConfigEvent.CONFIG_CHANGE, event => {
248
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
249
+ this.setConfig({ [event.name]: event.newValue });
250
+ });
251
+ }
252
+
253
+ // eslint-disable-next-line no-use-before-define
254
+ public registerPlugin(plugin: Web3PluginBase) {
255
+ // @ts-expect-error No index signature with a parameter of type 'string' was found on type 'Web3Context<API, RegisteredSubs>'
256
+ if (this[plugin.pluginNamespace] !== undefined)
257
+ throw new ExistingPluginNamespaceError(plugin.pluginNamespace);
258
+
259
+ const _pluginObject = {
260
+ [plugin.pluginNamespace]: plugin,
261
+ };
262
+ _pluginObject[plugin.pluginNamespace].link(this);
263
+ Object.assign(this, _pluginObject);
264
+ }
265
+
266
+ /**
267
+ * Will return the current provider.
268
+ *
269
+ * @returns Returns the current provider
270
+ * @example
271
+ * ```ts
272
+ * const web3 = new Web3Context("http://localhost:8545");
273
+ * console.log(web3.provider);
274
+ * > HttpProvider {
275
+ * clientUrl: 'http://localhost:8545',
276
+ * httpProviderOptions: undefined
277
+ * }
278
+ * ```
279
+ */
280
+
281
+ public get provider(): Web3BaseProvider<API> | undefined {
282
+ return this.currentProvider;
283
+ }
284
+
285
+ /**
286
+ * Will set the current provider.
287
+ *
288
+ * @param provider - The provider to set
289
+ *
290
+ * Accepted providers are of type {@link SupportedProviders}
291
+ * @example
292
+ * ```ts
293
+ * const web3Context = new web3ContextContext("http://localhost:8545");
294
+ * web3Context.provider = "ws://localhost:8545";
295
+ * console.log(web3Context.provider);
296
+ * > WebSocketProvider {
297
+ * _eventEmitter: EventEmitter {
298
+ * _events: [Object: null prototype] {},
299
+ * _eventsCount: 0,
300
+ * ...
301
+ * }
302
+ * ```
303
+ */
304
+
305
+ public set provider(provider: SupportedProviders<API> | string | undefined) {
306
+ this.requestManager.setProvider(provider);
307
+ }
308
+
309
+ /**
310
+ * Will return the current provider. (The same as `provider`)
311
+ *
312
+ * @returns Returns the current provider
313
+ * @example
314
+ * ```ts
315
+ * const web3Context = new Web3Context("http://localhost:8545");
316
+ * console.log(web3Context.provider);
317
+ * > HttpProvider {
318
+ * clientUrl: 'http://localhost:8545',
319
+ * httpProviderOptions: undefined
320
+ * }
321
+ * ```
322
+ */
323
+ public get currentProvider(): Web3BaseProvider<API> | undefined {
324
+ return this.requestManager.provider as Web3BaseProvider<API>;
325
+ }
326
+
327
+ /**
328
+ * Will set the current provider. (The same as `provider`)
329
+ *
330
+ * @param provider - {@link SupportedProviders} The provider to set
331
+ *
332
+ * @example
333
+ * ```ts
334
+ * const web3Context = new Web3Context("http://localhost:8545");
335
+ * web3Context.currentProvider = "ws://localhost:8545";
336
+ * console.log(web3Context.provider);
337
+ * > WebSocketProvider {
338
+ * _eventEmitter: EventEmitter {
339
+ * _events: [Object: null prototype] {},
340
+ * _eventsCount: 0,
341
+ * ...
342
+ * }
343
+ * ```
344
+ */
345
+ public set currentProvider(provider: SupportedProviders<API> | string | undefined) {
346
+ this.requestManager.setProvider(provider);
347
+ }
348
+
349
+ /**
350
+ * Will return the givenProvider if available.
351
+ *
352
+ * When using web3.js in an Ethereum compatible browser, it will set with the current native provider by that browser. Will return the given provider by the (browser) environment, otherwise `undefined`.
353
+ */
354
+ // eslint-disable-next-line class-methods-use-this
355
+ public get givenProvider() {
356
+ return Web3Context.givenProvider;
357
+ }
358
+ /**
359
+ * Will set the provider.
360
+ *
361
+ * @param provider - {@link SupportedProviders} The provider to set
362
+ * @returns Returns true if the provider was set
363
+ */
364
+ public setProvider(provider?: SupportedProviders<API> | string): boolean {
365
+ this.provider = provider;
366
+ return true;
367
+ }
368
+
369
+ public setRequestManagerMiddleware(requestManagerMiddleware: RequestManagerMiddleware<API>) {
370
+ this.requestManager.setMiddleware(requestManagerMiddleware);
371
+ }
372
+
373
+ /**
374
+ * Will return the {@link Web3BatchRequest} constructor.
375
+ */
376
+ public get BatchRequest(): new () => Web3BatchRequest {
377
+ return Web3BatchRequest.bind(
378
+ undefined,
379
+ this._requestManager as unknown as Web3RequestManager,
380
+ );
381
+ }
382
+
383
+ /**
384
+ * This method allows extending the web3 modules.
385
+ * Note: This method is only for backward compatibility, and It is recommended to use Web3 v4 Plugin feature for extending web3.js functionality if you are developing something new.
386
+ */
387
+ public extend(extendObj: ExtensionObject) {
388
+ // @ts-expect-error No index signature with a parameter of type 'string' was found on type 'Web3Context<API, RegisteredSubs>'
389
+ if (extendObj.property && !this[extendObj.property])
390
+ // @ts-expect-error No index signature with a parameter of type 'string' was found on type 'Web3Context<API, RegisteredSubs>'
391
+ this[extendObj.property] = {};
392
+
393
+ extendObj.methods?.forEach(element => {
394
+ const method = async (...givenParams: unknown[]) =>
395
+ this.requestManager.send({
396
+ method: element.call,
397
+ params: givenParams,
398
+ });
399
+
400
+ if (extendObj.property)
401
+ // @ts-expect-error No index signature with a parameter of type 'string' was found on type 'Web3Context<API, RegisteredSubs>'
402
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
403
+ this[extendObj.property][element.name] = method;
404
+ // @ts-expect-error No index signature with a parameter of type 'string' was found on type 'Web3Context<API, RegisteredSubs>'
405
+ else this[element.name] = method;
406
+ });
407
+ return this;
408
+ }
409
+ }
410
+
411
+ /**
412
+ * Extend this class when creating a plugin that either doesn't require {@link EthExecutionAPI},
413
+ * or interacts with a RPC node that doesn't fully implement {@link EthExecutionAPI}.
414
+ *
415
+ * To add type support for RPC methods to the {@link Web3RequestManager},
416
+ * define a {@link Web3APISpec} and pass it as a generic to Web3PluginBase like so:
417
+ *
418
+ * @example
419
+ * ```ts
420
+ * type CustomRpcApi = {
421
+ * custom_rpc_method: () => string;
422
+ * custom_rpc_method_with_parameters: (parameter1: string, parameter2: number) => string;
423
+ * };
424
+ *
425
+ * class CustomPlugin extends Web3PluginBase<CustomRpcApi> {...}
426
+ * ```
427
+ */
428
+ export abstract class Web3PluginBase<
429
+ API extends Web3APISpec = Web3APISpec,
430
+ > extends Web3Context<API> {
431
+ public abstract pluginNamespace: string;
432
+
433
+ // eslint-disable-next-line class-methods-use-this
434
+ protected registerNewTransactionType<NewTxTypeClass extends typeof BaseTransaction<unknown>>(
435
+ type: Numbers,
436
+ txClass: NewTxTypeClass,
437
+ ): void {
438
+ TransactionFactory.registerTransactionType(type, txClass);
439
+ }
440
+ }
441
+
442
+ /**
443
+ * Extend this class when creating a plugin that makes use of {@link EthExecutionAPI},
444
+ * or depends on other Web3 packages (such as `web3-eth-contract`) that depend on {@link EthExecutionAPI}.
445
+ *
446
+ * To add type support for RPC methods to the {@link Web3RequestManager} (in addition to {@link EthExecutionAPI}),
447
+ * define a {@link Web3APISpec} and pass it as a generic to Web3PluginBase like so:
448
+ *
449
+ * @example
450
+ * ```ts
451
+ * type CustomRpcApi = {
452
+ * custom_rpc_method: () => string;
453
+ * custom_rpc_method_with_parameters: (parameter1: string, parameter2: number) => string;
454
+ * };
455
+ *
456
+ * class CustomPlugin extends Web3PluginBase<CustomRpcApi> {...}
457
+ * ```
458
+ */
459
+ export abstract class Web3EthPluginBase<API extends Web3APISpec = unknown> extends Web3PluginBase<
460
+ API & EthExecutionAPI
461
+ > {}
462
+
463
+ // To avoid cycle dependency declare this type in this file
464
+ export type TransactionBuilder<API extends Web3APISpec = unknown> = <
465
+ ReturnType = Transaction,
466
+ >(options: {
467
+ transaction: Transaction;
468
+ web3Context: Web3Context<API>;
469
+ privateKey?: HexString | Uint8Array;
470
+ fillGasPrice?: boolean;
471
+ }) => Promise<ReturnType>;
@@ -0,0 +1,73 @@
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
+ import { EventEmitter } from 'web3-utils';
19
+
20
+ export type Web3EventMap = Record<string, unknown>;
21
+ export type Web3EventKey<T extends Web3EventMap> = string & keyof T;
22
+ export type Web3EventCallback<T> = (params: T) => void | Promise<void>;
23
+ export interface Web3Emitter<T extends Web3EventMap> {
24
+ on<K extends Web3EventKey<T>>(eventName: K, fn: Web3EventCallback<T[K]>): void;
25
+ once<K extends Web3EventKey<T>>(eventName: K, fn: Web3EventCallback<T[K]>): void;
26
+ off<K extends Web3EventKey<T>>(eventName: K, fn: Web3EventCallback<T[K]>): void;
27
+ emit<K extends Web3EventKey<T>>(eventName: K, params: T[K]): void;
28
+ }
29
+
30
+ export class Web3EventEmitter<T extends Web3EventMap> implements Web3Emitter<T> {
31
+ private readonly _emitter = new EventEmitter();
32
+
33
+ public on<K extends Web3EventKey<T>>(eventName: K, fn: Web3EventCallback<T[K]>) {
34
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
35
+ this._emitter.on(eventName, fn);
36
+ }
37
+
38
+ public once<K extends Web3EventKey<T>>(eventName: K, fn: Web3EventCallback<T[K]>) {
39
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
40
+ this._emitter.once(eventName, fn);
41
+ }
42
+
43
+ public off<K extends Web3EventKey<T>>(eventName: K, fn: Web3EventCallback<T[K]>) {
44
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
45
+ this._emitter.off(eventName, fn);
46
+ }
47
+
48
+ public emit<K extends Web3EventKey<T>>(eventName: K, params: T[K]) {
49
+ this._emitter.emit(eventName, params);
50
+ }
51
+
52
+ public listenerCount<K extends Web3EventKey<T>>(eventName: K) {
53
+ return this._emitter.listenerCount(eventName);
54
+ }
55
+
56
+ public listeners<K extends Web3EventKey<T>>(eventName: K) {
57
+ return this._emitter.listeners(eventName);
58
+ }
59
+
60
+ public eventNames() {
61
+ return this._emitter.eventNames();
62
+ }
63
+
64
+ public removeAllListeners() {
65
+ return this._emitter.removeAllListeners();
66
+ }
67
+ public setMaxListenerWarningThreshold(maxListenersWarningThreshold: number) {
68
+ this._emitter.setMaxListeners(maxListenersWarningThreshold);
69
+ }
70
+ public getMaxListeners() {
71
+ return this._emitter.getMaxListeners();
72
+ }
73
+ }
@@ -0,0 +1,79 @@
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
+ import {
19
+ Web3EventCallback,
20
+ Web3EventEmitter,
21
+ Web3EventKey,
22
+ Web3EventMap,
23
+ } from './web3_event_emitter.js';
24
+
25
+ export type PromiseExecutor<T> = (
26
+ resolve: (data: T) => void,
27
+ reject: (reason: unknown) => void,
28
+ ) => void;
29
+
30
+ export class Web3PromiEvent<ResolveType, EventMap extends Web3EventMap>
31
+ extends Web3EventEmitter<EventMap>
32
+ implements Promise<ResolveType>
33
+ {
34
+ private readonly _promise: Promise<ResolveType>;
35
+
36
+ public constructor(executor: PromiseExecutor<ResolveType>) {
37
+ super();
38
+ this._promise = new Promise<ResolveType>(executor);
39
+ }
40
+
41
+ // public tag to treat object as promise by different libs
42
+ // eslint-disable-next-line @typescript-eslint/prefer-as-const
43
+ public [Symbol.toStringTag]: 'Promise' = 'Promise';
44
+
45
+ public async then<TResult1 = ResolveType, TResult2 = never>(
46
+ onfulfilled?: ((value: ResolveType) => TResult1 | PromiseLike<TResult1>) | undefined,
47
+ onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | undefined,
48
+ ): Promise<TResult1 | TResult2> {
49
+ return this._promise.then(onfulfilled, onrejected);
50
+ }
51
+
52
+ public async catch<TResult = never>(
53
+ onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | undefined,
54
+ ): Promise<ResolveType | TResult> {
55
+ return this._promise.catch(onrejected);
56
+ }
57
+
58
+ public async finally(onfinally?: (() => void) | undefined): Promise<ResolveType> {
59
+ return this._promise.finally(onfinally);
60
+ }
61
+
62
+ public on<K extends Web3EventKey<EventMap>>(
63
+ eventName: K,
64
+ fn: Web3EventCallback<EventMap[K]>,
65
+ ): this {
66
+ super.on(eventName, fn);
67
+
68
+ return this;
69
+ }
70
+
71
+ public once<K extends Web3EventKey<EventMap>>(
72
+ eventName: K,
73
+ fn: Web3EventCallback<EventMap[K]>,
74
+ ): this {
75
+ super.once(eventName, fn);
76
+
77
+ return this;
78
+ }
79
+ }