@injectivelabs/wallet-evm 1.15.0 → 1.15.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.
Files changed (39) hide show
  1. package/dist/cjs/index.d.ts +3 -0
  2. package/dist/cjs/index.js +21 -0
  3. package/dist/cjs/package.json +3 -0
  4. package/dist/cjs/strategy/strategy.d.ts +39 -0
  5. package/dist/cjs/strategy/strategy.js +239 -0
  6. package/dist/cjs/strategy/utils/Okx.d.ts +4 -0
  7. package/dist/cjs/strategy/utils/Okx.js +49 -0
  8. package/dist/cjs/strategy/utils/bitget.d.ts +4 -0
  9. package/dist/cjs/strategy/utils/bitget.js +43 -0
  10. package/dist/cjs/strategy/utils/index.d.ts +5 -0
  11. package/dist/cjs/strategy/utils/index.js +13 -0
  12. package/dist/cjs/strategy/utils/metamask.d.ts +4 -0
  13. package/dist/cjs/strategy/utils/metamask.js +44 -0
  14. package/dist/cjs/strategy/utils/phantom.d.ts +4 -0
  15. package/dist/cjs/strategy/utils/phantom.js +49 -0
  16. package/dist/cjs/strategy/utils/trustWallet.d.ts +4 -0
  17. package/dist/cjs/strategy/utils/trustWallet.js +49 -0
  18. package/dist/cjs/utils/index.d.ts +4 -0
  19. package/dist/cjs/utils/index.js +69 -0
  20. package/dist/esm/index.d.ts +3 -0
  21. package/dist/esm/index.js +3 -0
  22. package/dist/esm/package.json +3 -0
  23. package/dist/esm/strategy/strategy.d.ts +39 -0
  24. package/dist/esm/strategy/strategy.js +235 -0
  25. package/dist/esm/strategy/utils/Okx.d.ts +4 -0
  26. package/dist/esm/strategy/utils/Okx.js +46 -0
  27. package/dist/esm/strategy/utils/bitget.d.ts +4 -0
  28. package/dist/esm/strategy/utils/bitget.js +40 -0
  29. package/dist/esm/strategy/utils/index.d.ts +5 -0
  30. package/dist/esm/strategy/utils/index.js +5 -0
  31. package/dist/esm/strategy/utils/metamask.d.ts +4 -0
  32. package/dist/esm/strategy/utils/metamask.js +41 -0
  33. package/dist/esm/strategy/utils/phantom.d.ts +4 -0
  34. package/dist/esm/strategy/utils/phantom.js +46 -0
  35. package/dist/esm/strategy/utils/trustWallet.d.ts +4 -0
  36. package/dist/esm/strategy/utils/trustWallet.js +46 -0
  37. package/dist/esm/utils/index.d.ts +4 -0
  38. package/dist/esm/utils/index.js +64 -0
  39. package/package.json +8 -8
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.updateEvmNetwork = exports.getEvmProvider = void 0;
4
+ const wallet_base_1 = require("@injectivelabs/wallet-base");
5
+ const utils_1 = require("@injectivelabs/utils");
6
+ const exceptions_1 = require("@injectivelabs/exceptions");
7
+ const Okx_js_1 = require("../strategy/utils/Okx.js");
8
+ const bitget_js_1 = require("../strategy/utils/bitget.js");
9
+ const phantom_js_1 = require("../strategy/utils/phantom.js");
10
+ const metamask_js_1 = require("../strategy/utils/metamask.js");
11
+ const trustWallet_js_1 = require("../strategy/utils/trustWallet.js");
12
+ const getEvmProvider = async (wallet) => {
13
+ if (!(0, wallet_base_1.isEvmBrowserWallet)(wallet)) {
14
+ throw new exceptions_1.WalletException(new Error(`Evm Wallet for ${(0, utils_1.capitalize)(wallet)} is not supported.`));
15
+ }
16
+ try {
17
+ let provider;
18
+ if (wallet === wallet_base_1.Wallet.Metamask) {
19
+ provider = (await (0, metamask_js_1.getMetamaskProvider)());
20
+ }
21
+ if (wallet === wallet_base_1.Wallet.BitGet) {
22
+ provider = (await (0, bitget_js_1.getBitGetProvider)());
23
+ }
24
+ if (wallet === wallet_base_1.Wallet.Phantom) {
25
+ provider = (await (0, phantom_js_1.getPhantomProvider)());
26
+ }
27
+ if (wallet === wallet_base_1.Wallet.TrustWallet) {
28
+ provider = (await (0, trustWallet_js_1.getTrustWalletProvider)());
29
+ }
30
+ if (wallet === wallet_base_1.Wallet.OkxWallet) {
31
+ provider = (await (0, Okx_js_1.getOkxWalletProvider)());
32
+ }
33
+ if (!provider) {
34
+ throw new exceptions_1.WalletException(new Error(`Please install ${(0, utils_1.capitalize)(wallet)} Extension`));
35
+ }
36
+ return provider;
37
+ }
38
+ catch (e) {
39
+ throw new exceptions_1.WalletException(new Error(`Please install ${(0, utils_1.capitalize)(wallet)} Extension`));
40
+ }
41
+ };
42
+ exports.getEvmProvider = getEvmProvider;
43
+ const updateEvmNetwork = async (wallet, chainId) => {
44
+ if (!(0, wallet_base_1.isEvmBrowserWallet)(wallet)) {
45
+ throw new exceptions_1.WalletException(new Error(`Evm Wallet for ${(0, utils_1.capitalize)(wallet)} is not supported.`));
46
+ }
47
+ const provider = (await (0, exports.getEvmProvider)(wallet));
48
+ if (!provider) {
49
+ throw new exceptions_1.WalletException(new Error(`Please install ${(0, utils_1.capitalize)(wallet)} Extension`));
50
+ }
51
+ try {
52
+ const chainIdToHex = chainId.toString(16);
53
+ return await Promise.race([
54
+ provider.request({
55
+ method: 'wallet_switchEthereumChain',
56
+ params: [{ chainId: `0x${chainIdToHex}` }],
57
+ }),
58
+ new Promise((resolve) => provider.on('chainChanged', ({ chain }) => {
59
+ if (chain?.id === chainIdToHex) {
60
+ resolve();
61
+ }
62
+ })),
63
+ ]);
64
+ }
65
+ catch (e) {
66
+ throw new exceptions_1.WalletException(new Error(`Please update your ${(0, utils_1.capitalize)(wallet)} network`));
67
+ }
68
+ };
69
+ exports.updateEvmNetwork = updateEvmNetwork;
@@ -0,0 +1,3 @@
1
+ export { EvmWallet as EvmWalletStrategy } from './strategy/strategy.js';
2
+ export * from './strategy/utils/index.js';
3
+ export * from './utils/index.js';
@@ -0,0 +1,3 @@
1
+ export { EvmWallet as EvmWalletStrategy } from './strategy/strategy.js';
2
+ export * from './strategy/utils/index.js';
3
+ export * from './utils/index.js';
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,39 @@
1
+ import { TxRaw, TxResponse, DirectSignResponse, AminoSignResponse } from '@injectivelabs/sdk-ts';
2
+ import { ErrorContext, ThrownException } from '@injectivelabs/exceptions';
3
+ import { Wallet, StdSignDoc, WalletDeviceType, BaseConcreteStrategy, SendTransactionOptions, ConcreteWalletStrategy, ConcreteWalletStrategyArgs, ConcreteEthereumWalletStrategyArgs } from '@injectivelabs/wallet-base';
4
+ import { AccountAddress, EthereumChainId } from '@injectivelabs/ts-types';
5
+ export declare class EvmWallet extends BaseConcreteStrategy implements ConcreteWalletStrategy {
6
+ wallet?: Wallet;
7
+ constructor(args: (ConcreteWalletStrategyArgs | ConcreteEthereumWalletStrategyArgs) & {
8
+ wallet: Wallet;
9
+ });
10
+ EvmWalletException(error: Error, context?: ErrorContext): ThrownException;
11
+ getWalletDeviceType(): Promise<WalletDeviceType>;
12
+ enable(): Promise<boolean>;
13
+ disconnect(): Promise<void>;
14
+ getAddresses(): Promise<string[]>;
15
+ getSessionOrConfirm(address: AccountAddress): Promise<string>;
16
+ sendEthereumTransaction(transaction: unknown, _options: {
17
+ address: AccountAddress;
18
+ ethereumChainId: EthereumChainId;
19
+ }): Promise<string>;
20
+ sendTransaction(transaction: TxRaw, options: SendTransactionOptions): Promise<TxResponse>;
21
+ signEip712TypedData(eip712json: string, address: AccountAddress): Promise<string>;
22
+ signAminoCosmosTransaction(_transaction: {
23
+ address: string;
24
+ signDoc: StdSignDoc;
25
+ }): Promise<AminoSignResponse>;
26
+ signCosmosTransaction(_transaction: {
27
+ txRaw: TxRaw;
28
+ accountNumber: number;
29
+ chainId: string;
30
+ address: string;
31
+ }): Promise<DirectSignResponse>;
32
+ signArbitrary(signer: AccountAddress, data: string | Uint8Array): Promise<string>;
33
+ getEthereumChainId(): Promise<string>;
34
+ getEthereumTransactionReceipt(txHash: string): Promise<string>;
35
+ getPubKey(): Promise<string>;
36
+ onChainIdChanged(callback: (chain: string) => void): Promise<void>;
37
+ onAccountChange(callback: (account: AccountAddress | string[]) => void): Promise<void>;
38
+ private getEthereum;
39
+ }
@@ -0,0 +1,235 @@
1
+ /* eslint-disable class-methods-use-this */
2
+ import { isEvmBrowserWallet } from '@injectivelabs/wallet-base';
3
+ import { toUtf8, TxGrpcApi, } from '@injectivelabs/sdk-ts';
4
+ import { ErrorType, WalletException, BitGetException, MetamaskException, OkxWalletException, UnspecifiedErrorCode, TransactionException, TrustWalletException, } from '@injectivelabs/exceptions';
5
+ import { Wallet, WalletAction, WalletDeviceType, WalletEventListener, BaseConcreteStrategy, } from '@injectivelabs/wallet-base';
6
+ import { sleep, capitalize } from '@injectivelabs/utils';
7
+ import { getBitGetProvider, getPhantomProvider, getMetamaskProvider, getOkxWalletProvider, getTrustWalletProvider, } from './utils/index.js';
8
+ export class EvmWallet extends BaseConcreteStrategy {
9
+ wallet;
10
+ constructor(args) {
11
+ super(args);
12
+ if (!isEvmBrowserWallet(args.wallet)) {
13
+ throw new WalletException(new Error(`Evm Wallet for ${capitalize(args.wallet)} is not supported.`));
14
+ }
15
+ this.wallet = args.wallet;
16
+ }
17
+ EvmWalletException(error, context) {
18
+ if (this.wallet === Wallet.Metamask) {
19
+ return new MetamaskException(error, context);
20
+ }
21
+ if (this.wallet === Wallet.BitGet) {
22
+ return new BitGetException(error, context);
23
+ }
24
+ if (this.wallet === Wallet.OkxWallet) {
25
+ return new OkxWalletException(error, context);
26
+ }
27
+ if (this.wallet === Wallet.Phantom) {
28
+ return new MetamaskException(error, context);
29
+ }
30
+ if (this.wallet === Wallet.TrustWallet) {
31
+ return new TrustWalletException(error, context);
32
+ }
33
+ return new WalletException(error, context);
34
+ }
35
+ async getWalletDeviceType() {
36
+ return Promise.resolve(WalletDeviceType.Browser);
37
+ }
38
+ async enable() {
39
+ return Promise.resolve(true);
40
+ }
41
+ async disconnect() {
42
+ if (this.listeners[WalletEventListener.AccountChange]) {
43
+ const ethereum = await this.getEthereum();
44
+ ethereum.removeListener('accountsChanged', this.listeners[WalletEventListener.AccountChange]);
45
+ }
46
+ if (this.listeners[WalletEventListener.ChainIdChange]) {
47
+ const ethereum = await this.getEthereum();
48
+ ethereum.removeListener('chainChanged', this.listeners[WalletEventListener.ChainIdChange]);
49
+ }
50
+ this.listeners = {};
51
+ }
52
+ async getAddresses() {
53
+ const ethereum = await this.getEthereum();
54
+ try {
55
+ return (await ethereum.request({
56
+ method: 'eth_requestAccounts',
57
+ }));
58
+ }
59
+ catch (e) {
60
+ throw this.EvmWalletException(new Error(e.message), {
61
+ code: UnspecifiedErrorCode,
62
+ type: ErrorType.WalletError,
63
+ contextModule: WalletAction.GetAccounts,
64
+ });
65
+ }
66
+ }
67
+ // eslint-disable-next-line class-methods-use-this
68
+ async getSessionOrConfirm(address) {
69
+ return Promise.resolve(`0x${Buffer.from(`Confirmation for ${address} at time: ${Date.now()}`).toString('hex')}`);
70
+ }
71
+ async sendEthereumTransaction(transaction, _options) {
72
+ const ethereum = await this.getEthereum();
73
+ try {
74
+ return (await ethereum.request({
75
+ method: 'eth_sendTransaction',
76
+ params: [transaction],
77
+ }));
78
+ }
79
+ catch (e) {
80
+ throw this.EvmWalletException(new Error(e.message), {
81
+ code: UnspecifiedErrorCode,
82
+ type: ErrorType.WalletError,
83
+ contextModule: WalletAction.SendEthereumTransaction,
84
+ });
85
+ }
86
+ }
87
+ async sendTransaction(transaction, options) {
88
+ const { endpoints, txTimeout } = options;
89
+ if (!endpoints) {
90
+ throw new WalletException(new Error('You have to pass endpoints within the options for using Ethereum native wallets'));
91
+ }
92
+ const txApi = new TxGrpcApi(endpoints.grpc);
93
+ const response = await txApi.broadcast(transaction, { txTimeout });
94
+ if (response.code !== 0) {
95
+ throw new TransactionException(new Error(response.rawLog), {
96
+ code: UnspecifiedErrorCode,
97
+ contextCode: response.code,
98
+ contextModule: response.codespace,
99
+ });
100
+ }
101
+ return response;
102
+ }
103
+ async signEip712TypedData(eip712json, address) {
104
+ const ethereum = await this.getEthereum();
105
+ try {
106
+ return (await ethereum.request({
107
+ method: 'eth_signTypedData_v4',
108
+ params: [address, eip712json],
109
+ }));
110
+ }
111
+ catch (e) {
112
+ if (e.message.includes('Ledger: The signature doesnt match the right address')) {
113
+ throw new MetamaskException(new Error('Please connect directly with Ledger to sign this transaction'), {
114
+ code: UnspecifiedErrorCode,
115
+ type: ErrorType.WalletError,
116
+ contextModule: WalletAction.SignTransaction,
117
+ });
118
+ }
119
+ throw new MetamaskException(new Error(e.message), {
120
+ code: UnspecifiedErrorCode,
121
+ type: ErrorType.WalletError,
122
+ contextModule: WalletAction.SignTransaction,
123
+ });
124
+ }
125
+ }
126
+ async signAminoCosmosTransaction(_transaction) {
127
+ throw new WalletException(new Error('This wallet does not support signing Cosmos transactions'), {
128
+ code: UnspecifiedErrorCode,
129
+ type: ErrorType.WalletError,
130
+ contextModule: WalletAction.SignTransaction,
131
+ });
132
+ }
133
+ // eslint-disable-next-line class-methods-use-this
134
+ async signCosmosTransaction(_transaction) {
135
+ throw new WalletException(new Error('This wallet does not support signing Cosmos transactions'), {
136
+ code: UnspecifiedErrorCode,
137
+ type: ErrorType.WalletError,
138
+ contextModule: WalletAction.SignTransaction,
139
+ });
140
+ }
141
+ async signArbitrary(signer, data) {
142
+ const ethereum = await this.getEthereum();
143
+ try {
144
+ const signature = await ethereum.request({
145
+ method: 'personal_sign',
146
+ params: [toUtf8(data), signer],
147
+ });
148
+ return signature;
149
+ }
150
+ catch (e) {
151
+ throw this.EvmWalletException(new Error(e.message), {
152
+ code: UnspecifiedErrorCode,
153
+ type: ErrorType.WalletError,
154
+ contextModule: WalletAction.SignArbitrary,
155
+ });
156
+ }
157
+ }
158
+ async getEthereumChainId() {
159
+ const ethereum = await this.getEthereum();
160
+ try {
161
+ return ethereum.request({ method: 'eth_chainId' });
162
+ }
163
+ catch (e) {
164
+ throw this.EvmWalletException(new Error(e.message), {
165
+ code: UnspecifiedErrorCode,
166
+ type: ErrorType.WalletError,
167
+ contextModule: WalletAction.GetChainId,
168
+ });
169
+ }
170
+ }
171
+ async getEthereumTransactionReceipt(txHash) {
172
+ const ethereum = await this.getEthereum();
173
+ const interval = 1000;
174
+ const transactionReceiptRetry = async () => {
175
+ const receipt = (await ethereum.request({
176
+ method: 'eth_getTransactionReceipt',
177
+ params: [txHash],
178
+ }));
179
+ if (!receipt) {
180
+ await sleep(interval);
181
+ await transactionReceiptRetry();
182
+ }
183
+ return receipt;
184
+ };
185
+ try {
186
+ return await transactionReceiptRetry();
187
+ }
188
+ catch (e) {
189
+ throw this.EvmWalletException(new Error(e.message), {
190
+ code: UnspecifiedErrorCode,
191
+ type: ErrorType.WalletError,
192
+ contextModule: WalletAction.GetEthereumTransactionReceipt,
193
+ });
194
+ }
195
+ }
196
+ // eslint-disable-next-line class-methods-use-this
197
+ async getPubKey() {
198
+ throw new WalletException(new Error('You can only fetch PubKey from Cosmos native wallets'));
199
+ }
200
+ async onChainIdChanged(callback) {
201
+ const ethereum = await this.getEthereum();
202
+ this.listeners = {
203
+ [WalletEventListener.ChainIdChange]: callback,
204
+ };
205
+ ethereum.on('chainChanged', callback);
206
+ }
207
+ async onAccountChange(callback) {
208
+ const ethereum = await this.getEthereum();
209
+ this.listeners = {
210
+ [WalletEventListener.AccountChange]: callback,
211
+ };
212
+ ethereum.on('accountsChanged', callback);
213
+ }
214
+ async getEthereum() {
215
+ const provider = this.wallet === Wallet.Metamask
216
+ ? await getMetamaskProvider()
217
+ : this.wallet === Wallet.Phantom
218
+ ? await getPhantomProvider()
219
+ : this.wallet === Wallet.BitGet
220
+ ? await getBitGetProvider()
221
+ : this.wallet === Wallet.OkxWallet
222
+ ? await getOkxWalletProvider()
223
+ : this.wallet === Wallet.TrustWallet
224
+ ? await getTrustWalletProvider()
225
+ : undefined;
226
+ if (!provider) {
227
+ throw this.EvmWalletException(new Error(`Please install the ${this.wallet} wallet extension.`), {
228
+ code: UnspecifiedErrorCode,
229
+ type: ErrorType.WalletNotInstalledError,
230
+ contextModule: WalletAction.GetAccounts,
231
+ });
232
+ }
233
+ return provider;
234
+ }
235
+ }
@@ -0,0 +1,4 @@
1
+ import { BrowserEip1993Provider } from '@injectivelabs/wallet-base';
2
+ export declare function getOkxWalletProvider({ timeout }?: {
3
+ timeout: number;
4
+ }): Promise<BrowserEip1993Provider>;
@@ -0,0 +1,46 @@
1
+ import { isServerSide } from '@injectivelabs/sdk-ts';
2
+ const $window = (isServerSide()
3
+ ? {}
4
+ : window);
5
+ export async function getOkxWalletProvider({ timeout } = { timeout: 3000 }) {
6
+ const provider = getOkxWalletFromWindow();
7
+ if (provider) {
8
+ return provider;
9
+ }
10
+ return listenForOkxWalletInitialized({
11
+ timeout,
12
+ });
13
+ }
14
+ async function listenForOkxWalletInitialized({ timeout } = { timeout: 3000 }) {
15
+ return new Promise((resolve) => {
16
+ const handleInitialization = () => {
17
+ resolve(getOkxWalletFromWindow());
18
+ };
19
+ $window.addEventListener('okxwallet#initialized', handleInitialization, {
20
+ once: true,
21
+ });
22
+ setTimeout(() => {
23
+ $window.removeEventListener('okxwallet#initialized', handleInitialization);
24
+ resolve(null);
25
+ }, timeout);
26
+ });
27
+ }
28
+ function getOkxWalletFromWindow() {
29
+ const injectedProviderExist = typeof window !== 'undefined' &&
30
+ (typeof $window.ethereum !== 'undefined' ||
31
+ typeof $window.okxwallet !== 'undefined');
32
+ // No injected providers exist.
33
+ if (!injectedProviderExist) {
34
+ return;
35
+ }
36
+ if ($window.okxwallet) {
37
+ return $window.okxwallet;
38
+ }
39
+ if ($window.ethereum.isOkxWallet) {
40
+ return $window.ethereum;
41
+ }
42
+ if ($window.providers) {
43
+ return $window.providers.find((p) => p.isOkxWallet);
44
+ }
45
+ return;
46
+ }
@@ -0,0 +1,4 @@
1
+ import { BrowserEip1993Provider } from '@injectivelabs/wallet-base';
2
+ export declare function getBitGetProvider({ timeout }?: {
3
+ timeout: number;
4
+ }): Promise<BrowserEip1993Provider>;
@@ -0,0 +1,40 @@
1
+ import { isServerSide } from '@injectivelabs/sdk-ts';
2
+ const $window = (isServerSide()
3
+ ? {}
4
+ : window);
5
+ export async function getBitGetProvider({ timeout } = { timeout: 3000 }) {
6
+ const provider = getBitGetFromWindow();
7
+ if (provider) {
8
+ return provider;
9
+ }
10
+ return listenForBitGetInitialized({
11
+ timeout,
12
+ });
13
+ }
14
+ async function listenForBitGetInitialized({ timeout } = { timeout: 3000 }) {
15
+ return new Promise((resolve) => {
16
+ const handleInitialization = () => {
17
+ resolve(getBitGetFromWindow());
18
+ };
19
+ $window.addEventListener('bitkeep#initialized', handleInitialization, {
20
+ once: true,
21
+ });
22
+ setTimeout(() => {
23
+ $window.removeEventListener('bitkeep#initialized', handleInitialization);
24
+ resolve(null);
25
+ }, timeout);
26
+ });
27
+ }
28
+ function getBitGetFromWindow() {
29
+ const injectedProviderExist = typeof window !== 'undefined' &&
30
+ (typeof $window.ethereum !== 'undefined' ||
31
+ typeof $window.bitkeep !== 'undefined');
32
+ // No injected providers exist.
33
+ if (!injectedProviderExist) {
34
+ return;
35
+ }
36
+ if ($window.bitkeep && $window.bitkeep.ethereum) {
37
+ return $window.bitkeep.ethereum;
38
+ }
39
+ return;
40
+ }
@@ -0,0 +1,5 @@
1
+ export { getOkxWalletProvider } from './Okx.js';
2
+ export { getBitGetProvider } from './bitget.js';
3
+ export { getPhantomProvider } from './phantom.js';
4
+ export { getMetamaskProvider } from './metamask.js';
5
+ export { getTrustWalletProvider } from './trustWallet.js';
@@ -0,0 +1,5 @@
1
+ export { getOkxWalletProvider } from './Okx.js';
2
+ export { getBitGetProvider } from './bitget.js';
3
+ export { getPhantomProvider } from './phantom.js';
4
+ export { getMetamaskProvider } from './metamask.js';
5
+ export { getTrustWalletProvider } from './trustWallet.js';
@@ -0,0 +1,4 @@
1
+ import { BrowserEip1993Provider } from '@injectivelabs/wallet-base';
2
+ export declare function getMetamaskProvider({ timeout }?: {
3
+ timeout: number;
4
+ }): Promise<BrowserEip1993Provider>;
@@ -0,0 +1,41 @@
1
+ import { isServerSide } from '@injectivelabs/sdk-ts';
2
+ const $window = (isServerSide()
3
+ ? {}
4
+ : window);
5
+ export async function getMetamaskProvider({ timeout } = { timeout: 3000 }) {
6
+ const provider = getMetamaskFromWindow();
7
+ if (provider) {
8
+ return provider;
9
+ }
10
+ return listenForMetamaskInitialized({
11
+ timeout,
12
+ });
13
+ }
14
+ async function listenForMetamaskInitialized({ timeout } = { timeout: 3000 }) {
15
+ return new Promise((resolve) => {
16
+ const handleInitialization = () => {
17
+ resolve(getMetamaskFromWindow());
18
+ };
19
+ $window.addEventListener('ethereum#initialized', handleInitialization, {
20
+ once: true,
21
+ });
22
+ setTimeout(() => {
23
+ $window.removeEventListener('ethereum#initialized', handleInitialization);
24
+ resolve(null);
25
+ }, timeout);
26
+ });
27
+ }
28
+ function getMetamaskFromWindow() {
29
+ const injectedProviderExist = typeof window !== 'undefined' && typeof $window.ethereum !== 'undefined';
30
+ // No injected providers exist.
31
+ if (!injectedProviderExist) {
32
+ return;
33
+ }
34
+ if ($window.ethereum.isMetaMask) {
35
+ return $window.ethereum;
36
+ }
37
+ if ($window.providers) {
38
+ return $window.providers.find((p) => p.isMetaMask);
39
+ }
40
+ return;
41
+ }
@@ -0,0 +1,4 @@
1
+ import { BrowserEip1993Provider } from '@injectivelabs/wallet-base';
2
+ export declare function getPhantomProvider({ timeout }?: {
3
+ timeout: number;
4
+ }): Promise<BrowserEip1993Provider>;
@@ -0,0 +1,46 @@
1
+ import { isServerSide } from '@injectivelabs/sdk-ts';
2
+ const $window = (isServerSide()
3
+ ? {}
4
+ : window);
5
+ export async function getPhantomProvider({ timeout } = { timeout: 3000 }) {
6
+ const provider = getPhantomFromWindow();
7
+ if (provider) {
8
+ return provider;
9
+ }
10
+ return listenForPhantomInitialized({
11
+ timeout,
12
+ });
13
+ }
14
+ async function listenForPhantomInitialized({ timeout } = { timeout: 3000 }) {
15
+ return new Promise((resolve) => {
16
+ const handleInitialization = () => {
17
+ resolve(getPhantomFromWindow());
18
+ };
19
+ $window.addEventListener('phantom#initialized', handleInitialization, {
20
+ once: true,
21
+ });
22
+ setTimeout(() => {
23
+ $window.removeEventListener('phantom#initialized', handleInitialization);
24
+ resolve(null);
25
+ }, timeout);
26
+ });
27
+ }
28
+ function getPhantomFromWindow() {
29
+ const injectedProviderExist = typeof window !== 'undefined' &&
30
+ (typeof $window.ethereum !== 'undefined' ||
31
+ typeof $window.phantom !== 'undefined');
32
+ // No injected providers exist.
33
+ if (!injectedProviderExist) {
34
+ return;
35
+ }
36
+ if ($window.phantom) {
37
+ return $window.phantom.ethereum;
38
+ }
39
+ if ($window.ethereum.isPhantom) {
40
+ return $window.ethereum;
41
+ }
42
+ if ($window.providers) {
43
+ return $window.providers.find((p) => p.isPhantom);
44
+ }
45
+ return;
46
+ }
@@ -0,0 +1,4 @@
1
+ import { BrowserEip1993Provider } from '@injectivelabs/wallet-base';
2
+ export declare function getTrustWalletProvider({ timeout }?: {
3
+ timeout: number;
4
+ }): Promise<BrowserEip1993Provider>;
@@ -0,0 +1,46 @@
1
+ import { isServerSide } from '@injectivelabs/sdk-ts';
2
+ const $window = (isServerSide()
3
+ ? {}
4
+ : window);
5
+ export async function getTrustWalletProvider({ timeout } = { timeout: 3000 }) {
6
+ const provider = getTrustWalletFromWindow();
7
+ if (provider) {
8
+ return provider;
9
+ }
10
+ return listenForTrustWalletInitialized({
11
+ timeout,
12
+ });
13
+ }
14
+ async function listenForTrustWalletInitialized({ timeout } = { timeout: 3000 }) {
15
+ return new Promise((resolve) => {
16
+ const handleInitialization = () => {
17
+ resolve(getTrustWalletFromWindow());
18
+ };
19
+ $window.addEventListener('trustwallet#initialized', handleInitialization, {
20
+ once: true,
21
+ });
22
+ setTimeout(() => {
23
+ $window.removeEventListener('trustwallet#initialized', handleInitialization);
24
+ resolve(null);
25
+ }, timeout);
26
+ });
27
+ }
28
+ function getTrustWalletFromWindow() {
29
+ const injectedProviderExist = typeof window !== 'undefined' &&
30
+ (typeof $window.ethereum !== 'undefined' ||
31
+ typeof $window.trustWallet !== 'undefined');
32
+ // No injected providers exist.
33
+ if (!injectedProviderExist) {
34
+ return;
35
+ }
36
+ if ($window.trustWallet) {
37
+ return $window.trustWallet;
38
+ }
39
+ if ($window.ethereum.isTrustWallet || $window.ethereum.isTrust) {
40
+ return $window.ethereum;
41
+ }
42
+ if ($window.providers) {
43
+ return $window.providers.find((p) => p.isTrustWallet);
44
+ }
45
+ return;
46
+ }
@@ -0,0 +1,4 @@
1
+ import { Wallet, BrowserEip1993Provider } from '@injectivelabs/wallet-base';
2
+ import { EthereumChainId } from '@injectivelabs/ts-types';
3
+ export declare const getEvmProvider: (wallet: Wallet) => Promise<BrowserEip1993Provider>;
4
+ export declare const updateEvmNetwork: (wallet: Wallet, chainId: EthereumChainId) => Promise<unknown>;