@tomo-inc/inject-providers 0.0.13 → 0.0.15

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.
@@ -0,0 +1,189 @@
1
+ import { EventEmitter } from "events";
2
+ import { JsonRpcRequest, JsonRpcResponse } from "json-rpc-engine";
3
+ import { ChainTypes } from "types";
4
+ import { RequestArguments, SendSyncJsonRpcRequest } from "./type";
5
+ import { Maybe } from "./utils";
6
+
7
+ /**
8
+ * Connect event data interface
9
+ */
10
+ export interface IConnectEvent {
11
+ chainId?: string;
12
+ }
13
+
14
+ /**
15
+ * Disconnect event data interface
16
+ */
17
+ export interface IDisconnectEvent {
18
+ code: number;
19
+ message: string;
20
+ data?: unknown;
21
+ }
22
+
23
+ /**
24
+ * Provider message interface
25
+ */
26
+ export interface IProviderMessage {
27
+ type: string;
28
+ data: unknown;
29
+ }
30
+
31
+ /**
32
+ * Main interface for EvmProvider
33
+ * Extends EventEmitter to support event handling
34
+ * Implements EIP-1193 standard
35
+ */
36
+ export interface IEvmProvider extends EventEmitter {
37
+ type: ChainTypes.EVM;
38
+ // Public properties
39
+ /**
40
+ * The chain ID of the currently connected Ethereum chain.
41
+ * See https://chainid.network for more information.
42
+ */
43
+ chainId: string | null;
44
+
45
+ /**
46
+ * The user's currently selected Ethereum address.
47
+ * If null, wallet is either locked or the user has not permitted any
48
+ * addresses to be viewed.
49
+ */
50
+ selectedAddress: string | null;
51
+
52
+ /**
53
+ * Provider name
54
+ */
55
+ name: string;
56
+
57
+ /**
58
+ * Provider icon
59
+ */
60
+ icon: string;
61
+
62
+ // Connection methods
63
+ /**
64
+ * Returns whether the provider can process RPC requests.
65
+ * @returns true if connected, false otherwise
66
+ */
67
+ isConnected(): boolean;
68
+
69
+ /**
70
+ * Submits an RPC request for the given method, with the given params.
71
+ * Resolves with the result of the method call, or rejects on error.
72
+ *
73
+ * @param args - The RPC request arguments.
74
+ * @param args.method - The RPC method name.
75
+ * @param args.params - The parameters for the RPC method.
76
+ * @returns A Promise that resolves with the result of the RPC method,
77
+ * or rejects if an error is encountered.
78
+ */
79
+ request<T = unknown>(args: RequestArguments): Promise<Maybe<T>>;
80
+
81
+ /**
82
+ * Connect to the wallet
83
+ * Equivalent to: ethereum.request('eth_requestAccounts')
84
+ * @returns Promise resolving to array of account addresses
85
+ */
86
+ connect(): Promise<string[]>;
87
+
88
+ /**
89
+ * Disconnect from the wallet
90
+ * @returns Promise resolving to boolean indicating disconnect status
91
+ */
92
+ disconnect(): Promise<boolean>;
93
+
94
+ /**
95
+ * Equivalent to: ethereum.request('eth_requestAccounts')
96
+ * @deprecated Use request({ method: 'eth_requestAccounts' }) instead.
97
+ * @returns Promise resolving to array of addresses
98
+ */
99
+ enable(): Promise<string[]>;
100
+
101
+ // Legacy send methods (deprecated)
102
+ /**
103
+ * Submits an RPC request for the given method, with the given params.
104
+ * @deprecated Use request() instead.
105
+ * @param method - The method to request.
106
+ * @param params - Any params for the method.
107
+ * @returns A Promise that resolves with the JSON-RPC response object for the request.
108
+ */
109
+ send<T>(method: string, params?: T[]): Promise<JsonRpcResponse<T>>;
110
+
111
+ /**
112
+ * Submits an RPC request per the given JSON-RPC request object.
113
+ * @deprecated Use request() instead.
114
+ * @param payload - A JSON-RPC request object.
115
+ * @param callback - An error-first callback that will receive the JSON-RPC response object.
116
+ */
117
+ send<T>(payload: JsonRpcRequest<unknown>, callback: (error: Error | null, result?: JsonRpcResponse<T>) => void): void;
118
+
119
+ /**
120
+ * Accepts a JSON-RPC request object, and synchronously returns the cached result
121
+ * for the given method. Only supports 4 specific RPC methods.
122
+ * @deprecated Use request() instead.
123
+ * @param payload - A JSON-RPC request object.
124
+ * @returns A JSON-RPC response object.
125
+ */
126
+ send<T>(payload: SendSyncJsonRpcRequest): JsonRpcResponse<T>;
127
+
128
+ /**
129
+ * Adds a listener for the specified event.
130
+ * @param eventName - Event name
131
+ * @param listener - Event listener function
132
+ * @returns This instance for chaining
133
+ */
134
+ on(eventName: string, listener: (...args: unknown[]) => void): this;
135
+
136
+ /**
137
+ * Adds a one-time listener for the specified event.
138
+ * @param eventName - Event name
139
+ * @param listener - Event listener function
140
+ * @returns This instance for chaining
141
+ */
142
+ once(eventName: string, listener: (...args: unknown[]) => void): this;
143
+
144
+ // Event types
145
+ /**
146
+ * Emitted when the provider connects to a chain.
147
+ * @event
148
+ */
149
+ on(event: "connect", listener: (connectInfo: IConnectEvent) => void): this;
150
+
151
+ /**
152
+ * Emitted when the provider disconnects from a chain.
153
+ * @event
154
+ */
155
+ on(event: "disconnect", listener: (error: IDisconnectEvent) => void): this;
156
+
157
+ /**
158
+ * Emitted when the chain ID changes.
159
+ * @event
160
+ */
161
+ on(event: "chainChanged", listener: (chainId: string) => void): this;
162
+
163
+ /**
164
+ * Emitted when accounts change.
165
+ * @event
166
+ */
167
+ on(event: "accountsChanged", listener: (accounts: string[]) => void): this;
168
+
169
+ /**
170
+ * Emitted when the connection closes (deprecated).
171
+ * @event
172
+ * @deprecated
173
+ */
174
+ on(event: "close", listener: (error: IDisconnectEvent) => void): this;
175
+
176
+ /**
177
+ * Emitted when data is received (deprecated).
178
+ * @event
179
+ * @deprecated
180
+ */
181
+ on(event: "data", listener: (payload: unknown) => void): this;
182
+
183
+ /**
184
+ * Emitted when the network changes (deprecated).
185
+ * @event
186
+ * @deprecated
187
+ */
188
+ on(event: "networkChanged", listener: (networkId: string) => void): this;
189
+ }
@@ -25,6 +25,7 @@ import {
25
25
  } from "./utils";
26
26
 
27
27
  import { ChainTypes, IProductInfo } from "../types";
28
+ import { IEvmProvider } from "./interface";
28
29
  import {
29
30
  InitializeProviderOptions,
30
31
  InpageProviderOptions,
@@ -35,9 +36,8 @@ import {
35
36
  WarningEventName,
36
37
  } from "./type";
37
38
 
38
- const chainType = ChainTypes.EVM;
39
-
40
- export class EvmProvider extends EventEmitter {
39
+ export class EvmProvider extends EventEmitter implements IEvmProvider {
40
+ type: ChainTypes.EVM = ChainTypes.EVM;
41
41
  private readonly _log: ConsoleLike;
42
42
 
43
43
  private _state: InternalState;
@@ -298,7 +298,7 @@ export class EvmProvider extends EventEmitter {
298
298
  args = { ...args, ...{ dappInfo } };
299
299
  // }
300
300
 
301
- this.sendRequest(chainType, args);
301
+ this.sendRequest(this.type, args);
302
302
 
303
303
  return this.onResponse(args).then((res: any) => {
304
304
  const { data, method } = res || {};
@@ -345,7 +345,7 @@ export class EvmProvider extends EventEmitter {
345
345
  return super.addListener(eventName, listener);
346
346
  }
347
347
 
348
- on(eventName: string, listener: (...args: unknown[]) => void) {
348
+ on(eventName: string, listener: (...args: any[]) => void) {
349
349
  this._warnOfDeprecation(eventName);
350
350
  return super.on(eventName, listener);
351
351
  }
@@ -415,13 +415,13 @@ export class EvmProvider extends EventEmitter {
415
415
 
416
416
  private subscribeWalletEventsCallback = ({ method, data }: { method: string; data: any }) => {
417
417
  if (method === "accountsChanged") {
418
- const addresses = data?.[chainType] || [];
418
+ const addresses = data?.[this.type] || [];
419
419
  const accounts = addresses.map(({ address }: { address: string }) => address);
420
420
  this._handleAccountsChanged(accounts, true);
421
421
  }
422
422
  if (method === "chainChanged") {
423
423
  const { chainId, type }: { chainId: string; type: string } = data;
424
- if (type.indexOf(`${chainType}:`) === 0) {
424
+ if (type.indexOf(`${this.type}:`) === 0) {
425
425
  const chainIdHex = toHex(parseInt(chainId));
426
426
  this._handleChainChanged({ chainId: chainIdHex, isConnected: true });
427
427
  }
package/src/index.ts CHANGED
@@ -1,7 +1,11 @@
1
- import { BtcProvider } from "./btc";
1
+ import { BitcoinProvider } from "./btc";
2
2
  import { DogecoinProvider } from "./dogecoin";
3
+ import { IDogecoinProvider } from "./dogecoin/interface";
3
4
  import { EvmProvider } from "./evm";
5
+ import { IEvmProvider } from "./evm/interface";
4
6
  import { SolanaProvider } from "./solana";
5
7
  import { TronProvider } from "./tron";
6
8
 
7
- export { BtcProvider, DogecoinProvider, EvmProvider, SolanaProvider, TronProvider };
9
+ export { BitcoinProvider, DogecoinProvider, EvmProvider, SolanaProvider, TronProvider };
10
+
11
+ export type WalletProvider = IDogecoinProvider | IEvmProvider;
@@ -9,8 +9,6 @@ import { hexToTx, txToHex } from "./utils";
9
9
 
10
10
  import { ChainTypes, IConnectors, IProductInfo } from "../types";
11
11
  import { OriginTransaction, SignInInput } from "./types";
12
- const chainType = ChainTypes.SOL;
13
-
14
12
  interface StateProvider {
15
13
  accounts: any[] | null;
16
14
  isConnected: boolean;
@@ -20,6 +18,7 @@ interface StateProvider {
20
18
  }
21
19
 
22
20
  export class PhantomProvider extends EventEmitter {
21
+ chainType = ChainTypes.SOLANA;
23
22
  _isUnlocked = false;
24
23
  name = "";
25
24
  icon = "";
@@ -92,7 +91,7 @@ export class PhantomProvider extends EventEmitter {
92
91
 
93
92
  subscribeWalletEventsCallback = ({ method, data }: { method: string; data: any }) => {
94
93
  if (method === "accountsChanged") {
95
- const accounts = data?.[chainType];
94
+ const accounts = data?.[this.chainType];
96
95
  this._handleAccountsChanged(accounts);
97
96
  }
98
97
  };
@@ -153,7 +152,7 @@ export class PhantomProvider extends EventEmitter {
153
152
 
154
153
  const dappInfo = await getDappInfo();
155
154
 
156
- this.sendRequest(chainType, { ...data, dappInfo });
155
+ this.sendRequest(this.chainType, { ...data, dappInfo });
157
156
 
158
157
  return this.onResponse(data).then((res: any) => {
159
158
  let { data } = res || {};
@@ -5,8 +5,7 @@ import type { Transaction } from "tronweb/lib/esm/types/Transaction";
5
5
 
6
6
  import { ChainTypes, IConnectors, IProductInfo } from "../types";
7
7
  import { ReadyPromise, domReadyCall, getDappInfo } from "../utils/index";
8
-
9
- const chainType = ChainTypes.TRON;
8
+ import { ITomoTronProvider } from "./types";
10
9
 
11
10
  interface StateProvider {
12
11
  accounts: any[] | null;
@@ -16,12 +15,15 @@ interface StateProvider {
16
15
  isPermanentlyDisconnected: boolean;
17
16
  }
18
17
 
19
- export class TomoTronProvider extends EventEmitter {
18
+ const _fullHost = "https://api.trongrid.io";
19
+ export class TomoTronProvider extends EventEmitter implements ITomoTronProvider {
20
+ type: ChainTypes.TRON = ChainTypes.TRON;
20
21
  _isUnlocked = false;
21
22
  name = "";
22
23
  icon = "";
23
24
  ready = false;
24
- tronWeb: TronWeb | null = null;
25
+ tronWeb: TronWeb = new TronWeb({ fullHost: _fullHost });
26
+ address: string = "";
25
27
  sendRequest: (chainType: string, data: any) => void;
26
28
  onResponse: any;
27
29
  events = {
@@ -73,7 +75,7 @@ export class TomoTronProvider extends EventEmitter {
73
75
  this.on("disconnect", (result: any) => {
74
76
  this._state.isConnected = false;
75
77
 
76
- this.tronWeb = null;
78
+ this.tronWeb = new TronWeb({ fullHost: _fullHost });
77
79
 
78
80
  window.postMessage(
79
81
  {
@@ -100,11 +102,12 @@ export class TomoTronProvider extends EventEmitter {
100
102
  );
101
103
  });
102
104
  }
105
+ switchChain?: ((chainId: string) => Promise<void>) | undefined;
103
106
 
104
107
  private _initTronWeb = async (res: any) => {
105
- const { fullHost = "https://api.trongrid.io", address } = res || {};
108
+ const { fullHost, address } = res || {};
106
109
  this.tronWeb = new TronWeb({
107
- fullHost,
110
+ fullHost: fullHost || _fullHost,
108
111
  });
109
112
 
110
113
  this.tronWeb.defaultAddress.base58 = address;
@@ -183,7 +186,7 @@ export class TomoTronProvider extends EventEmitter {
183
186
 
184
187
  subscribeWalletEventsCallback = ({ method, data }: { method: string; data: any }) => {
185
188
  if (method === "accountsChanged") {
186
- const accounts = data?.[chainType];
189
+ const accounts = data?.[this.type];
187
190
  this._handleAccountsChanged(accounts);
188
191
  }
189
192
  };
@@ -222,7 +225,7 @@ export class TomoTronProvider extends EventEmitter {
222
225
 
223
226
  const dappInfo = await getDappInfo();
224
227
 
225
- this.sendRequest(chainType, { ...data, dappInfo });
228
+ this.sendRequest(this.type, { ...data, dappInfo });
226
229
 
227
230
  return this.onResponse(data).then((res: any) => {
228
231
  const { data, method } = res || {};
package/src/tron/types.ts CHANGED
@@ -1,7 +1,10 @@
1
- import type { Transaction, SignedTransaction } from "tronweb/lib/esm/types/Transaction";
2
- import type { TronLinkWallet } from "@tronweb3/tronwallet-adapter-tronlink"; // Import TronLink wallet class
1
+ import type { TronLinkWallet, TronWeb } from "@tronweb3/tronwallet-adapter-tronlink"; // Import TronLink wallet class
2
+ import type { SignedTransaction, Transaction } from "tronweb/lib/esm/types/Transaction";
3
+ import { ChainTypes } from "types";
3
4
 
4
5
  export interface ITomoTronProvider extends TronLinkWallet {
6
+ type: ChainTypes.TRON;
7
+ tronWeb: TronWeb;
5
8
  address: string | null;
6
9
  connect(options?: Record<string, unknown>): Promise<void | any>;
7
10
  disconnect(): Promise<void | any>;
@@ -1,5 +1,6 @@
1
- export * from "./dapp";
1
+ export { ChainTypeEnum as ChainTypes } from "@tomo-inc/wallet-utils";
2
2
 
3
+ export * from "./dapp";
3
4
  export interface IProductInfo {
4
5
  name: string;
5
6
  rdns: string;
@@ -20,15 +21,3 @@ export interface IConnectors {
20
21
  sendRequest: (chainType: string, data: any) => void;
21
22
  onResponse: (data: any) => void;
22
23
  }
23
-
24
- export enum ChainTypes {
25
- BTC = "btc",
26
- DOGE = "doge",
27
- EVM = "evm",
28
- SOL = "sol",
29
- SUI = "sui",
30
- TON = "ton",
31
- TRON = "tron",
32
- COSMOS = "cosmos",
33
- APTOS = "aptos",
34
- }