@injectivelabs/wallet-evm 1.20.4 → 1.20.5
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/dist/cjs/index.cjs +286 -239
- package/dist/cjs/index.d.cts +3 -2
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/index.js +286 -240
- package/package.json +6 -6
package/dist/cjs/index.cjs
CHANGED
|
@@ -4,218 +4,235 @@ let __injectivelabs_wallet_base = require("@injectivelabs/wallet-base");
|
|
|
4
4
|
let __injectivelabs_sdk_ts_core_tx = require("@injectivelabs/sdk-ts/core/tx");
|
|
5
5
|
let __injectivelabs_sdk_ts_utils = require("@injectivelabs/sdk-ts/utils");
|
|
6
6
|
|
|
7
|
-
//#region src/strategy/utils/
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
7
|
+
//#region src/strategy/utils/providerResolver.ts
|
|
8
|
+
const EIP6963_REQUEST_TIMEOUT = 250;
|
|
9
|
+
const eip6963Providers = {};
|
|
10
|
+
const getWindow = () => typeof window === "undefined" ? {} : window;
|
|
11
|
+
const normalize = (value) => (value === null || value === void 0 ? void 0 : value.toLowerCase().trim()) || "";
|
|
12
|
+
const walletProviderMetadata = {
|
|
13
|
+
[__injectivelabs_wallet_base.Wallet.Metamask]: {
|
|
14
|
+
names: ["metamask"],
|
|
15
|
+
rdns: ["io.metamask", "io.metamask.flask"]
|
|
16
|
+
},
|
|
17
|
+
[__injectivelabs_wallet_base.Wallet.Rabby]: {
|
|
18
|
+
names: ["rabby", "rabby wallet"],
|
|
19
|
+
rdns: ["io.rabby"]
|
|
20
|
+
},
|
|
21
|
+
[__injectivelabs_wallet_base.Wallet.Rainbow]: {
|
|
22
|
+
names: ["rainbow", "rainbow wallet"],
|
|
23
|
+
rdns: ["me.rainbow"]
|
|
24
|
+
},
|
|
25
|
+
[__injectivelabs_wallet_base.Wallet.Phantom]: {
|
|
26
|
+
names: ["phantom", "phantom wallet"],
|
|
27
|
+
rdns: ["app.phantom"]
|
|
28
|
+
},
|
|
29
|
+
[__injectivelabs_wallet_base.Wallet.OkxWallet]: {
|
|
30
|
+
names: [
|
|
31
|
+
"okx",
|
|
32
|
+
"okx wallet",
|
|
33
|
+
"okxwallet"
|
|
34
|
+
],
|
|
35
|
+
rdns: ["com.okex.wallet"]
|
|
36
|
+
},
|
|
37
|
+
[__injectivelabs_wallet_base.Wallet.BitGet]: {
|
|
38
|
+
names: [
|
|
39
|
+
"bitget",
|
|
40
|
+
"bitget wallet",
|
|
41
|
+
"bitkeep"
|
|
42
|
+
],
|
|
43
|
+
rdns: ["com.bitget.web3", "com.bitget.wallet"]
|
|
44
|
+
},
|
|
45
|
+
[__injectivelabs_wallet_base.Wallet.TrustWallet]: {
|
|
46
|
+
names: [
|
|
47
|
+
"trust",
|
|
48
|
+
"trust wallet",
|
|
49
|
+
"trustwallet"
|
|
50
|
+
],
|
|
51
|
+
rdns: ["com.trustwallet.app", "com.trustwallet"]
|
|
52
|
+
},
|
|
53
|
+
[__injectivelabs_wallet_base.Wallet.KeplrEvm]: {
|
|
54
|
+
names: [
|
|
55
|
+
"keplr",
|
|
56
|
+
"keplr wallet",
|
|
57
|
+
"keplr-evm"
|
|
58
|
+
],
|
|
59
|
+
rdns: [
|
|
60
|
+
"app.keplr",
|
|
61
|
+
"io.keplr",
|
|
62
|
+
"io.keplr.wallet"
|
|
63
|
+
]
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const legacyProviderInitializedEvents = {
|
|
67
|
+
[__injectivelabs_wallet_base.Wallet.Metamask]: "ethereum#initialized",
|
|
68
|
+
[__injectivelabs_wallet_base.Wallet.Rabby]: "rabby#initialized",
|
|
69
|
+
[__injectivelabs_wallet_base.Wallet.Rainbow]: "rainbow#initialized",
|
|
70
|
+
[__injectivelabs_wallet_base.Wallet.Phantom]: "phantom#initialized",
|
|
71
|
+
[__injectivelabs_wallet_base.Wallet.OkxWallet]: "okxwallet#initialized",
|
|
72
|
+
[__injectivelabs_wallet_base.Wallet.BitGet]: "bitkeep#initialized",
|
|
73
|
+
[__injectivelabs_wallet_base.Wallet.TrustWallet]: "trustwallet#initialized",
|
|
74
|
+
[__injectivelabs_wallet_base.Wallet.KeplrEvm]: "keplr#initialized"
|
|
75
|
+
};
|
|
76
|
+
const competingMetaMaskFlags = [
|
|
77
|
+
"isRabby",
|
|
78
|
+
"isRainbow",
|
|
79
|
+
"isPhantom",
|
|
80
|
+
"isOkxWallet",
|
|
81
|
+
"isTrustWallet",
|
|
82
|
+
"isTrust",
|
|
83
|
+
"isKeplr"
|
|
84
|
+
];
|
|
85
|
+
const isSupportedEip6963Wallet = (wallet) => wallet in walletProviderMetadata;
|
|
86
|
+
const providerHasCompetingMetaMaskFlag = (provider) => competingMetaMaskFlags.some((flag) => Boolean(provider[flag]));
|
|
87
|
+
const providerMatchesWallet = (provider, wallet) => {
|
|
88
|
+
if (!provider) return false;
|
|
89
|
+
const legacyProvider = provider;
|
|
90
|
+
if (wallet === __injectivelabs_wallet_base.Wallet.Metamask) return Boolean(provider.isMetaMask) && !providerHasCompetingMetaMaskFlag(provider);
|
|
91
|
+
if (wallet === __injectivelabs_wallet_base.Wallet.Rabby) return Boolean(provider.isRabby);
|
|
92
|
+
if (wallet === __injectivelabs_wallet_base.Wallet.Rainbow) return Boolean(provider.isRainbow);
|
|
93
|
+
if (wallet === __injectivelabs_wallet_base.Wallet.Phantom) return Boolean(provider.isPhantom);
|
|
94
|
+
if (wallet === __injectivelabs_wallet_base.Wallet.OkxWallet) return Boolean(provider.isOkxWallet);
|
|
95
|
+
if (wallet === __injectivelabs_wallet_base.Wallet.BitGet) return Boolean(legacyProvider.isBitKeep || legacyProvider.isBitGet);
|
|
96
|
+
if (wallet === __injectivelabs_wallet_base.Wallet.TrustWallet) return Boolean(provider.isTrustWallet || provider.isTrust);
|
|
97
|
+
if (wallet === __injectivelabs_wallet_base.Wallet.KeplrEvm) return Boolean(provider.isKeplr);
|
|
98
|
+
return false;
|
|
99
|
+
};
|
|
100
|
+
const getWalletFromEip6963ProviderDetail = (detail) => {
|
|
101
|
+
var _detail$info, _detail$info2, _entries$find;
|
|
102
|
+
const rdns = normalize((_detail$info = detail.info) === null || _detail$info === void 0 ? void 0 : _detail$info.rdns);
|
|
103
|
+
const name = normalize((_detail$info2 = detail.info) === null || _detail$info2 === void 0 ? void 0 : _detail$info2.name);
|
|
104
|
+
const entries = Object.entries(walletProviderMetadata);
|
|
105
|
+
const rdnsMatch = entries.find(([, metadata]) => metadata.rdns.some((value) => normalize(value) === rdns));
|
|
106
|
+
if (rdnsMatch) return rdnsMatch[0];
|
|
107
|
+
return (_entries$find = entries.find(([, metadata]) => metadata.names.some((value) => normalize(value) === name))) === null || _entries$find === void 0 ? void 0 : _entries$find[0];
|
|
108
|
+
};
|
|
109
|
+
const registerEip6963Provider = (detail, providerCache) => {
|
|
110
|
+
if (!(detail === null || detail === void 0 ? void 0 : detail.provider)) return;
|
|
111
|
+
const wallet = getWalletFromEip6963ProviderDetail(detail);
|
|
112
|
+
if (!wallet) return;
|
|
113
|
+
eip6963Providers[wallet] = detail.provider;
|
|
114
|
+
if (providerCache) providerCache[wallet] = detail.provider;
|
|
115
|
+
return wallet;
|
|
116
|
+
};
|
|
117
|
+
const listenForEip6963Providers = (providerCache) => {
|
|
118
|
+
if (typeof window === "undefined") return;
|
|
119
|
+
const handleAnnouncement = (announcement) => {
|
|
120
|
+
registerEip6963Provider(announcement.detail, providerCache);
|
|
121
|
+
};
|
|
122
|
+
window.addEventListener("eip6963:announceProvider", handleAnnouncement);
|
|
123
|
+
return () => window.removeEventListener("eip6963:announceProvider", handleAnnouncement);
|
|
124
|
+
};
|
|
125
|
+
const requestEip6963Providers = () => {
|
|
126
|
+
if (typeof window === "undefined") return;
|
|
127
|
+
window.dispatchEvent(new Event("eip6963:requestProvider"));
|
|
128
|
+
};
|
|
129
|
+
const requestEip6963Provider = (wallet, providerCache, timeout = EIP6963_REQUEST_TIMEOUT) => {
|
|
130
|
+
if (typeof window === "undefined" || timeout <= 0) return Promise.resolve(void 0);
|
|
43
131
|
return new Promise((resolve) => {
|
|
44
|
-
|
|
45
|
-
|
|
132
|
+
let timeoutId;
|
|
133
|
+
let resolved = false;
|
|
134
|
+
let handleAnnouncement = (_announcement) => {};
|
|
135
|
+
const cleanup = () => {
|
|
136
|
+
window.removeEventListener("eip6963:announceProvider", handleAnnouncement);
|
|
137
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
46
138
|
};
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
resolve(
|
|
52
|
-
}, timeout);
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
function getOkxWalletFromWindow() {
|
|
56
|
-
const $window = getWindow$6();
|
|
57
|
-
if (!(typeof window !== "undefined" && (typeof $window.ethereum !== "undefined" || typeof $window.okxwallet !== "undefined"))) return;
|
|
58
|
-
if ($window.okxwallet) return $window.okxwallet;
|
|
59
|
-
if ($window.ethereum.isOkxWallet) return $window.ethereum;
|
|
60
|
-
if ($window.providers) return $window.providers.find((p) => p.isOkxWallet);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
//#endregion
|
|
64
|
-
//#region src/strategy/utils/bitget.ts
|
|
65
|
-
const getWindow$5 = () => typeof window === "undefined" ? {} : window;
|
|
66
|
-
async function getBitGetProvider({ timeout } = { timeout: 3e3 }) {
|
|
67
|
-
const provider = getBitGetFromWindow();
|
|
68
|
-
if (provider) return provider;
|
|
69
|
-
return listenForBitGetInitialized({ timeout });
|
|
70
|
-
}
|
|
71
|
-
async function listenForBitGetInitialized({ timeout } = { timeout: 3e3 }) {
|
|
72
|
-
return new Promise((resolve) => {
|
|
73
|
-
const handleInitialization = () => {
|
|
74
|
-
resolve(getBitGetFromWindow());
|
|
139
|
+
const resolveProvider = (provider) => {
|
|
140
|
+
if (resolved) return;
|
|
141
|
+
resolved = true;
|
|
142
|
+
cleanup();
|
|
143
|
+
resolve(provider);
|
|
75
144
|
};
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
setTimeout(() => {
|
|
79
|
-
$window.removeEventListener("bitkeep#initialized", handleInitialization);
|
|
80
|
-
resolve(null);
|
|
81
|
-
}, timeout);
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
function getBitGetFromWindow() {
|
|
85
|
-
const $window = getWindow$5();
|
|
86
|
-
if (!(typeof window !== "undefined" && (typeof $window.ethereum !== "undefined" || typeof $window.bitkeep !== "undefined"))) return;
|
|
87
|
-
if ($window.bitkeep && $window.bitkeep.ethereum) return $window.bitkeep.ethereum;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
//#endregion
|
|
91
|
-
//#region src/strategy/utils/phantom.ts
|
|
92
|
-
const getWindow$4 = () => typeof window === "undefined" ? {} : window;
|
|
93
|
-
async function getPhantomProvider({ timeout } = { timeout: 3e3 }) {
|
|
94
|
-
const provider = getPhantomFromWindow();
|
|
95
|
-
if (provider) return provider;
|
|
96
|
-
return listenForPhantomInitialized({ timeout });
|
|
97
|
-
}
|
|
98
|
-
async function listenForPhantomInitialized({ timeout } = { timeout: 3e3 }) {
|
|
99
|
-
return new Promise((resolve) => {
|
|
100
|
-
const handleInitialization = () => {
|
|
101
|
-
resolve(getPhantomFromWindow());
|
|
145
|
+
handleAnnouncement = (announcement) => {
|
|
146
|
+
if (registerEip6963Provider(announcement.detail, providerCache) === wallet) resolveProvider((providerCache === null || providerCache === void 0 ? void 0 : providerCache[wallet]) || eip6963Providers[wallet]);
|
|
102
147
|
};
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
$window.removeEventListener("phantom#initialized", handleInitialization);
|
|
107
|
-
resolve(null);
|
|
148
|
+
window.addEventListener("eip6963:announceProvider", handleAnnouncement);
|
|
149
|
+
timeoutId = setTimeout(() => {
|
|
150
|
+
resolveProvider((providerCache === null || providerCache === void 0 ? void 0 : providerCache[wallet]) || eip6963Providers[wallet]);
|
|
108
151
|
}, timeout);
|
|
152
|
+
requestEip6963Providers();
|
|
109
153
|
});
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
if ($window.ethereum.isPhantom) return $window.ethereum;
|
|
116
|
-
if ($window.providers) return $window.providers.find((p) => p.isPhantom);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
//#endregion
|
|
120
|
-
//#region src/strategy/utils/keplrEvm.ts
|
|
121
|
-
const getWindow$3 = () => typeof window === "undefined" ? {} : window;
|
|
122
|
-
async function getKeplrEvmProvider({ timeout } = { timeout: 3e3 }) {
|
|
123
|
-
const provider = getKeplrEvmFromWindow();
|
|
124
|
-
if (provider) return provider;
|
|
125
|
-
return listenForKeplrEvmInitialized({ timeout });
|
|
126
|
-
}
|
|
127
|
-
async function listenForKeplrEvmInitialized({ timeout } = { timeout: 3e3 }) {
|
|
128
|
-
return new Promise((resolve) => {
|
|
129
|
-
const handleInitialization = () => {
|
|
130
|
-
resolve(getKeplrEvmFromWindow());
|
|
131
|
-
};
|
|
132
|
-
const $window = getWindow$3();
|
|
133
|
-
$window.addEventListener("keplr#initialized", handleInitialization, { once: true });
|
|
134
|
-
setTimeout(() => {
|
|
135
|
-
$window.removeEventListener("keplr#initialized", handleInitialization);
|
|
136
|
-
resolve(null);
|
|
137
|
-
}, timeout);
|
|
154
|
+
};
|
|
155
|
+
const uniqueProviders = (providers) => {
|
|
156
|
+
const providerSet = /* @__PURE__ */ new Set();
|
|
157
|
+
providers.forEach((provider) => {
|
|
158
|
+
if (provider) providerSet.add(provider);
|
|
138
159
|
});
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
if (
|
|
144
|
-
if (
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
160
|
+
return [...providerSet];
|
|
161
|
+
};
|
|
162
|
+
const getWalletGlobalProvider = (wallet, $window) => {
|
|
163
|
+
if (wallet === __injectivelabs_wallet_base.Wallet.Rabby) return $window.rabby;
|
|
164
|
+
if (wallet === __injectivelabs_wallet_base.Wallet.Rainbow) return $window.rainbow;
|
|
165
|
+
if (wallet === __injectivelabs_wallet_base.Wallet.Phantom) {
|
|
166
|
+
var _$window$phantom;
|
|
167
|
+
return (_$window$phantom = $window.phantom) === null || _$window$phantom === void 0 ? void 0 : _$window$phantom.ethereum;
|
|
168
|
+
}
|
|
169
|
+
if (wallet === __injectivelabs_wallet_base.Wallet.OkxWallet) return $window.okxwallet;
|
|
170
|
+
if (wallet === __injectivelabs_wallet_base.Wallet.BitGet) {
|
|
171
|
+
var _$window$bitkeep;
|
|
172
|
+
return (_$window$bitkeep = $window.bitkeep) === null || _$window$bitkeep === void 0 ? void 0 : _$window$bitkeep.ethereum;
|
|
173
|
+
}
|
|
174
|
+
if (wallet === __injectivelabs_wallet_base.Wallet.TrustWallet) return $window.trustWallet;
|
|
175
|
+
if (wallet === __injectivelabs_wallet_base.Wallet.KeplrEvm) {
|
|
176
|
+
var _$window$keplr;
|
|
177
|
+
return (_$window$keplr = $window.keplr) === null || _$window$keplr === void 0 ? void 0 : _$window$keplr.ethereum;
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
const getLegacyEvmProvider = (wallet) => {
|
|
181
|
+
const $window = getWindow();
|
|
182
|
+
const ethereum = $window.ethereum;
|
|
183
|
+
const walletGlobalProvider = getWalletGlobalProvider(wallet, $window);
|
|
184
|
+
if (walletGlobalProvider) return walletGlobalProvider;
|
|
185
|
+
const providerFromArray = uniqueProviders([...(ethereum === null || ethereum === void 0 ? void 0 : ethereum.providers) || [], ...$window.providers || []]).find((provider) => providerMatchesWallet(provider, wallet));
|
|
186
|
+
if (providerFromArray) return providerFromArray;
|
|
187
|
+
if (providerMatchesWallet(ethereum, wallet)) return ethereum;
|
|
188
|
+
};
|
|
189
|
+
const resolveEvmProvider = async (wallet, options = {}) => {
|
|
190
|
+
if (!isSupportedEip6963Wallet(wallet)) return;
|
|
191
|
+
const { eip6963Providers: providerCache, requestEip6963 = true, timeout = EIP6963_REQUEST_TIMEOUT } = options;
|
|
192
|
+
const cachedProvider = (providerCache === null || providerCache === void 0 ? void 0 : providerCache[wallet]) || eip6963Providers[wallet];
|
|
193
|
+
if (cachedProvider) return cachedProvider;
|
|
194
|
+
if (requestEip6963) {
|
|
195
|
+
const announcedProvider = await requestEip6963Provider(wallet, providerCache, timeout);
|
|
196
|
+
if (announcedProvider) return announcedProvider;
|
|
197
|
+
}
|
|
198
|
+
return getLegacyEvmProvider(wallet);
|
|
199
|
+
};
|
|
200
|
+
const getEvmProviderWithFallback = async (wallet, options = {}) => {
|
|
201
|
+
const { eip6963Providers: providerCache, timeout = 3e3 } = options;
|
|
202
|
+
const provider = await resolveEvmProvider(wallet, { eip6963Providers: providerCache });
|
|
154
203
|
if (provider) return provider;
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
async function listenForMetamaskInitialized({ timeout } = { timeout: 3e3 }) {
|
|
204
|
+
const eventName = legacyProviderInitializedEvents[wallet];
|
|
205
|
+
if (typeof window === "undefined" || !eventName) return;
|
|
158
206
|
return new Promise((resolve) => {
|
|
159
|
-
|
|
160
|
-
|
|
207
|
+
let timeoutId;
|
|
208
|
+
let handleInitialization = async () => {};
|
|
209
|
+
const cleanup = () => {
|
|
210
|
+
window.removeEventListener(eventName, handleInitialization);
|
|
211
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
161
212
|
};
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
function getMetamaskFromWindow() {
|
|
171
|
-
const $window = getWindow$2();
|
|
172
|
-
if (!(typeof window !== "undefined" && typeof $window.ethereum !== "undefined")) return;
|
|
173
|
-
if ($window.ethereum.isMetaMask) return $window.ethereum;
|
|
174
|
-
if ($window.providers) return $window.providers.find((p) => p.isMetaMask);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
//#endregion
|
|
178
|
-
//#region src/strategy/utils/trustWallet.ts
|
|
179
|
-
const getWindow$1 = () => typeof window === "undefined" ? {} : window;
|
|
180
|
-
async function getTrustWalletProvider({ timeout } = { timeout: 3e3 }) {
|
|
181
|
-
const provider = getTrustWalletFromWindow();
|
|
182
|
-
if (provider) return provider;
|
|
183
|
-
return listenForTrustWalletInitialized({ timeout });
|
|
184
|
-
}
|
|
185
|
-
async function listenForTrustWalletInitialized({ timeout } = { timeout: 3e3 }) {
|
|
186
|
-
return new Promise((resolve) => {
|
|
187
|
-
const handleInitialization = () => {
|
|
188
|
-
resolve(getTrustWalletFromWindow());
|
|
213
|
+
handleInitialization = async () => {
|
|
214
|
+
cleanup();
|
|
215
|
+
resolve(await resolveEvmProvider(wallet, {
|
|
216
|
+
eip6963Providers: providerCache,
|
|
217
|
+
requestEip6963: false
|
|
218
|
+
}));
|
|
189
219
|
};
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
resolve(null);
|
|
220
|
+
window.addEventListener(eventName, handleInitialization, { once: true });
|
|
221
|
+
timeoutId = setTimeout(() => {
|
|
222
|
+
cleanup();
|
|
223
|
+
resolve(void 0);
|
|
195
224
|
}, timeout);
|
|
196
225
|
});
|
|
197
|
-
}
|
|
198
|
-
function getTrustWalletFromWindow() {
|
|
199
|
-
const $window = getWindow$1();
|
|
200
|
-
if (!(typeof window !== "undefined" && (typeof $window.ethereum !== "undefined" || typeof $window.trustWallet !== "undefined"))) return;
|
|
201
|
-
if ($window.trustWallet) return $window.trustWallet;
|
|
202
|
-
if ($window.ethereum.isTrustWallet || $window.ethereum.isTrust) return $window.ethereum;
|
|
203
|
-
if ($window.providers) return $window.providers.find((p) => p.isTrustWallet);
|
|
204
|
-
}
|
|
226
|
+
};
|
|
205
227
|
|
|
206
228
|
//#endregion
|
|
207
229
|
//#region src/utils/index.ts
|
|
230
|
+
const EVM_WALLET_INVALID_REQUEST_ERROR_CODE = -32600;
|
|
231
|
+
const CHAIN_NOT_SUPPORTED_MESSAGE_REGEX = /\b(chain(?:\s+id)?\s+not supported|unsupported\s+chain(?:\s+id)?)\b/;
|
|
208
232
|
const getEvmProvider = async (wallet) => {
|
|
209
233
|
if (!(0, __injectivelabs_wallet_base.isEvmBrowserWallet)(wallet)) throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`Evm Wallet for ${(0, __injectivelabs_utils.capitalize)(wallet)} is not supported.`));
|
|
210
234
|
try {
|
|
211
|
-
|
|
212
|
-
if (wallet === __injectivelabs_wallet_base.Wallet.Metamask) provider = await getMetamaskProvider();
|
|
213
|
-
if (wallet === __injectivelabs_wallet_base.Wallet.Rabby) provider = await getRabbyProvider();
|
|
214
|
-
if (wallet === __injectivelabs_wallet_base.Wallet.BitGet) provider = await getBitGetProvider();
|
|
215
|
-
if (wallet === __injectivelabs_wallet_base.Wallet.Phantom) provider = await getPhantomProvider();
|
|
216
|
-
if (wallet === __injectivelabs_wallet_base.Wallet.TrustWallet) provider = await getTrustWalletProvider();
|
|
217
|
-
if (wallet === __injectivelabs_wallet_base.Wallet.OkxWallet) provider = await getOkxWalletProvider();
|
|
218
|
-
if (wallet === __injectivelabs_wallet_base.Wallet.KeplrEvm) provider = await getKeplrEvmProvider();
|
|
235
|
+
const provider = await getEvmProviderWithFallback(wallet);
|
|
219
236
|
if (!provider) throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`Please install ${(0, __injectivelabs_utils.capitalize)(wallet)} Extension`));
|
|
220
237
|
return provider;
|
|
221
238
|
} catch (_unused) {
|
|
@@ -230,6 +247,15 @@ const extractNormalizedErrorCode = (error) => {
|
|
|
230
247
|
const rawCode = (code != null ? Number(code) : void 0) === __injectivelabs_wallet_base.EvmWalletProviderErrorCode.InternalError || code === void 0 || code === null ? originalCode !== null && originalCode !== void 0 ? originalCode : code : code;
|
|
231
248
|
return rawCode != null ? Number(rawCode) : NaN;
|
|
232
249
|
};
|
|
250
|
+
const isUnrecognizedChainError = (error) => {
|
|
251
|
+
var _ref, _normalizedError$data2, _normalizedError$data3;
|
|
252
|
+
const errorCode = extractNormalizedErrorCode(error);
|
|
253
|
+
if (errorCode === __injectivelabs_wallet_base.EvmWalletProviderErrorCode.UnrecognizedChain) return true;
|
|
254
|
+
if (errorCode !== EVM_WALLET_INVALID_REQUEST_ERROR_CODE) return false;
|
|
255
|
+
const normalizedError = error && typeof error === "object" ? error : void 0;
|
|
256
|
+
const message = String((_ref = (_normalizedError$data2 = normalizedError === null || normalizedError === void 0 || (_normalizedError$data3 = normalizedError.data) === null || _normalizedError$data3 === void 0 || (_normalizedError$data3 = _normalizedError$data3.originalError) === null || _normalizedError$data3 === void 0 ? void 0 : _normalizedError$data3.message) !== null && _normalizedError$data2 !== void 0 ? _normalizedError$data2 : normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.message) !== null && _ref !== void 0 ? _ref : "").toLowerCase();
|
|
257
|
+
return CHAIN_NOT_SUPPORTED_MESSAGE_REGEX.test(message);
|
|
258
|
+
};
|
|
233
259
|
const switchEthereumChainWithTimeout = async (provider, chainIdHex, timeoutMs = 3e4) => {
|
|
234
260
|
let timeoutId;
|
|
235
261
|
let handleChainChanged;
|
|
@@ -272,10 +298,9 @@ const updateEvmNetwork = async (wallet, chainId, providerOverride) => {
|
|
|
272
298
|
await switchEthereumChainWithTimeout(provider, chainIdToHex, TIMEOUT_MS);
|
|
273
299
|
} catch (switchError) {
|
|
274
300
|
var _chainConfig$rpcUrls, _chainConfig$blockExp;
|
|
275
|
-
|
|
276
|
-
if (errorCode === __injectivelabs_wallet_base.EvmWalletProviderErrorCode.UserRejectedRequest) throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`${(0, __injectivelabs_utils.capitalize)(wallet)} chain switch was rejected`));
|
|
301
|
+
if (extractNormalizedErrorCode(switchError) === __injectivelabs_wallet_base.EvmWalletProviderErrorCode.UserRejectedRequest) throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`${(0, __injectivelabs_utils.capitalize)(wallet)} chain switch was rejected`));
|
|
277
302
|
if (switchError.message === "Chain switch timed out") throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error("Chain switch timed out"));
|
|
278
|
-
if (
|
|
303
|
+
if (!isUnrecognizedChainError(switchError)) throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`Please update your ${(0, __injectivelabs_utils.capitalize)(wallet)} network`));
|
|
279
304
|
const chainConfig = (0, __injectivelabs_wallet_base.getEvmChainConfig)(chainId);
|
|
280
305
|
const rpcUrl = (_chainConfig$rpcUrls = chainConfig.rpcUrls) === null || _chainConfig$rpcUrls === void 0 || (_chainConfig$rpcUrls = _chainConfig$rpcUrls.default) === null || _chainConfig$rpcUrls === void 0 || (_chainConfig$rpcUrls = _chainConfig$rpcUrls.http) === null || _chainConfig$rpcUrls === void 0 ? void 0 : _chainConfig$rpcUrls[0];
|
|
281
306
|
const explorerUrl = ((_chainConfig$blockExp = chainConfig.blockExplorers) === null || _chainConfig$blockExp === void 0 || (_chainConfig$blockExp = _chainConfig$blockExp.default) === null || _chainConfig$blockExp === void 0 ? void 0 : _chainConfig$blockExp.url) || void 0;
|
|
@@ -314,33 +339,68 @@ const updateEvmNetwork = async (wallet, chainId, providerOverride) => {
|
|
|
314
339
|
}
|
|
315
340
|
};
|
|
316
341
|
|
|
342
|
+
//#endregion
|
|
343
|
+
//#region src/strategy/utils/rabby.ts
|
|
344
|
+
async function getRabbyProvider({ timeout } = { timeout: 3e3 }) {
|
|
345
|
+
const provider = await getEvmProviderWithFallback(__injectivelabs_wallet_base.Wallet.Rabby, { timeout });
|
|
346
|
+
if (!provider) throw new Error(`Please install the ${__injectivelabs_wallet_base.Wallet.Rabby} wallet extension.`);
|
|
347
|
+
return provider;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
//#endregion
|
|
351
|
+
//#region src/strategy/utils/bitget.ts
|
|
352
|
+
async function getBitGetProvider({ timeout } = { timeout: 3e3 }) {
|
|
353
|
+
const provider = await getEvmProviderWithFallback(__injectivelabs_wallet_base.Wallet.BitGet, { timeout });
|
|
354
|
+
if (!provider) throw new Error(`Please install the ${__injectivelabs_wallet_base.Wallet.BitGet} wallet extension.`);
|
|
355
|
+
return provider;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
//#endregion
|
|
359
|
+
//#region src/strategy/utils/Okx.ts
|
|
360
|
+
async function getOkxWalletProvider({ timeout } = { timeout: 3e3 }) {
|
|
361
|
+
const provider = await getEvmProviderWithFallback(__injectivelabs_wallet_base.Wallet.OkxWallet, { timeout });
|
|
362
|
+
if (!provider) throw new Error(`Please install the ${__injectivelabs_wallet_base.Wallet.OkxWallet} wallet extension.`);
|
|
363
|
+
return provider;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
//#endregion
|
|
367
|
+
//#region src/strategy/utils/phantom.ts
|
|
368
|
+
async function getPhantomProvider({ timeout } = { timeout: 3e3 }) {
|
|
369
|
+
const provider = await getEvmProviderWithFallback(__injectivelabs_wallet_base.Wallet.Phantom, { timeout });
|
|
370
|
+
if (!provider) throw new Error(`Please install the ${__injectivelabs_wallet_base.Wallet.Phantom} wallet extension.`);
|
|
371
|
+
return provider;
|
|
372
|
+
}
|
|
373
|
+
|
|
317
374
|
//#endregion
|
|
318
375
|
//#region src/strategy/utils/rainbow.ts
|
|
319
|
-
const getWindow = () => typeof window === "undefined" ? {} : window;
|
|
320
376
|
async function getRainbowProvider({ timeout } = { timeout: 3e3 }) {
|
|
321
|
-
const provider =
|
|
322
|
-
if (provider)
|
|
323
|
-
return
|
|
377
|
+
const provider = await getEvmProviderWithFallback(__injectivelabs_wallet_base.Wallet.Rainbow, { timeout });
|
|
378
|
+
if (!provider) throw new Error(`Please install the ${__injectivelabs_wallet_base.Wallet.Rainbow} wallet extension.`);
|
|
379
|
+
return provider;
|
|
324
380
|
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
setTimeout(() => {
|
|
333
|
-
$window.removeEventListener("rainbow#initialized", handleInitialization);
|
|
334
|
-
resolve(null);
|
|
335
|
-
}, timeout);
|
|
336
|
-
});
|
|
381
|
+
|
|
382
|
+
//#endregion
|
|
383
|
+
//#region src/strategy/utils/metamask.ts
|
|
384
|
+
async function getMetamaskProvider({ timeout } = { timeout: 3e3 }) {
|
|
385
|
+
const provider = await getEvmProviderWithFallback(__injectivelabs_wallet_base.Wallet.Metamask, { timeout });
|
|
386
|
+
if (!provider) throw new Error(`Please install the ${__injectivelabs_wallet_base.Wallet.Metamask} wallet extension.`);
|
|
387
|
+
return provider;
|
|
337
388
|
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
if (
|
|
389
|
+
|
|
390
|
+
//#endregion
|
|
391
|
+
//#region src/strategy/utils/keplrEvm.ts
|
|
392
|
+
async function getKeplrEvmProvider({ timeout } = { timeout: 3e3 }) {
|
|
393
|
+
const provider = await getEvmProviderWithFallback(__injectivelabs_wallet_base.Wallet.KeplrEvm, { timeout });
|
|
394
|
+
if (!provider) throw new Error(`Please install the ${__injectivelabs_wallet_base.Wallet.KeplrEvm} wallet extension.`);
|
|
395
|
+
return provider;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
//#endregion
|
|
399
|
+
//#region src/strategy/utils/trustWallet.ts
|
|
400
|
+
async function getTrustWalletProvider({ timeout } = { timeout: 3e3 }) {
|
|
401
|
+
const provider = await getEvmProviderWithFallback(__injectivelabs_wallet_base.Wallet.TrustWallet, { timeout });
|
|
402
|
+
if (!provider) throw new Error(`Please install the ${__injectivelabs_wallet_base.Wallet.TrustWallet} wallet extension.`);
|
|
403
|
+
return provider;
|
|
344
404
|
}
|
|
345
405
|
|
|
346
406
|
//#endregion
|
|
@@ -394,19 +454,8 @@ var EvmWallet = class extends __injectivelabs_wallet_base.BaseConcreteStrategy {
|
|
|
394
454
|
_defineProperty(this, "evmProviders", {});
|
|
395
455
|
if (!(0, __injectivelabs_wallet_base.isEvmBrowserWallet)(args.wallet)) throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`Evm Wallet for ${(0, __injectivelabs_utils.capitalize)(args.wallet)} is not supported.`));
|
|
396
456
|
if (!(0, __injectivelabs_sdk_ts_utils.isServerSide)()) {
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
const walletName = event.detail.info.name.toLowerCase();
|
|
400
|
-
if (walletName === "keplr") this.evmProviders[__injectivelabs_wallet_base.Wallet.KeplrEvm] = event.detail.provider;
|
|
401
|
-
if (walletName === __injectivelabs_wallet_base.Wallet.Metamask.toLowerCase()) this.evmProviders[__injectivelabs_wallet_base.Wallet.Metamask] = event.detail.provider;
|
|
402
|
-
if (walletName === "rabby wallet") this.evmProviders[__injectivelabs_wallet_base.Wallet.Rabby] = event.detail.provider;
|
|
403
|
-
if (walletName === __injectivelabs_wallet_base.Wallet.Rainbow.toLowerCase()) this.evmProviders[__injectivelabs_wallet_base.Wallet.Rainbow] = event.detail.provider;
|
|
404
|
-
if (walletName === __injectivelabs_wallet_base.Wallet.Phantom.toLowerCase()) this.evmProviders[__injectivelabs_wallet_base.Wallet.Phantom] = event.detail.provider;
|
|
405
|
-
if (walletName === __injectivelabs_wallet_base.Wallet.OkxWallet.toLowerCase()) this.evmProviders[__injectivelabs_wallet_base.Wallet.OkxWallet] = event.detail.provider;
|
|
406
|
-
if (walletName === __injectivelabs_wallet_base.Wallet.BitGet.toLowerCase()) this.evmProviders[__injectivelabs_wallet_base.Wallet.BitGet] = event.detail.provider;
|
|
407
|
-
if (walletName === __injectivelabs_wallet_base.Wallet.TrustWallet.toLowerCase()) this.evmProviders[__injectivelabs_wallet_base.Wallet.TrustWallet] = event.detail.provider;
|
|
408
|
-
});
|
|
409
|
-
window.dispatchEvent(new Event("eip6963:requestProvider"));
|
|
457
|
+
listenForEip6963Providers(this.evmProviders);
|
|
458
|
+
requestEip6963Providers();
|
|
410
459
|
}
|
|
411
460
|
this.wallet = args.wallet;
|
|
412
461
|
}
|
|
@@ -588,8 +637,7 @@ var EvmWallet = class extends __injectivelabs_wallet_base.BaseConcreteStrategy {
|
|
|
588
637
|
try {
|
|
589
638
|
await switchEthereumChainWithTimeout(ethereum, chainIdHex, TIMEOUT_MS);
|
|
590
639
|
} catch (error) {
|
|
591
|
-
|
|
592
|
-
if (errorCode === __injectivelabs_wallet_base.EvmWalletProviderErrorCode.UserRejectedRequest) throw this.EvmWalletException(/* @__PURE__ */ new Error(`${(0, __injectivelabs_utils.capitalize)(this.wallet || "wallet")} chain switch was rejected`), {
|
|
640
|
+
if (extractNormalizedErrorCode(error) === __injectivelabs_wallet_base.EvmWalletProviderErrorCode.UserRejectedRequest) throw this.EvmWalletException(/* @__PURE__ */ new Error(`${(0, __injectivelabs_utils.capitalize)(this.wallet || "wallet")} chain switch was rejected`), {
|
|
593
641
|
code: __injectivelabs_exceptions.UnspecifiedErrorCode,
|
|
594
642
|
type: __injectivelabs_exceptions.ErrorType.WalletError,
|
|
595
643
|
contextModule: __injectivelabs_wallet_base.WalletAction.GetChainId
|
|
@@ -599,7 +647,7 @@ var EvmWallet = class extends __injectivelabs_wallet_base.BaseConcreteStrategy {
|
|
|
599
647
|
type: __injectivelabs_exceptions.ErrorType.WalletError,
|
|
600
648
|
contextModule: __injectivelabs_wallet_base.WalletAction.GetChainId
|
|
601
649
|
});
|
|
602
|
-
if (
|
|
650
|
+
if (!isUnrecognizedChainError(error)) throw this.EvmWalletException(/* @__PURE__ */ new Error(`Something went wrong while switching ${(0, __injectivelabs_utils.capitalize)(this.wallet || "wallet")} network`), {
|
|
603
651
|
code: __injectivelabs_exceptions.UnspecifiedErrorCode,
|
|
604
652
|
type: __injectivelabs_exceptions.ErrorType.WalletError,
|
|
605
653
|
contextModule: __injectivelabs_wallet_base.WalletAction.GetChainId
|
|
@@ -653,15 +701,13 @@ var EvmWallet = class extends __injectivelabs_wallet_base.BaseConcreteStrategy {
|
|
|
653
701
|
}
|
|
654
702
|
}
|
|
655
703
|
async getEthereum() {
|
|
656
|
-
const
|
|
657
|
-
if (
|
|
658
|
-
const backUpProvider = this.wallet === __injectivelabs_wallet_base.Wallet.Metamask ? await getMetamaskProvider() : this.wallet === __injectivelabs_wallet_base.Wallet.Rabby ? await getRabbyProvider() : this.wallet === __injectivelabs_wallet_base.Wallet.Phantom ? await getPhantomProvider() : this.wallet === __injectivelabs_wallet_base.Wallet.BitGet ? await getBitGetProvider() : this.wallet === __injectivelabs_wallet_base.Wallet.OkxWallet ? await getOkxWalletProvider() : this.wallet === __injectivelabs_wallet_base.Wallet.TrustWallet ? await getTrustWalletProvider() : this.wallet === __injectivelabs_wallet_base.Wallet.Rainbow ? await getRainbowProvider() : this.wallet === __injectivelabs_wallet_base.Wallet.KeplrEvm ? await getKeplrEvmProvider() : void 0;
|
|
659
|
-
if (!backUpProvider) throw this.EvmWalletException(/* @__PURE__ */ new Error(`Please install the ${this.wallet} wallet extension.`), {
|
|
704
|
+
const provider = await getEvmProviderWithFallback(this.wallet, { eip6963Providers: this.evmProviders });
|
|
705
|
+
if (!provider) throw this.EvmWalletException(/* @__PURE__ */ new Error(`Please install the ${this.wallet} wallet extension.`), {
|
|
660
706
|
code: __injectivelabs_exceptions.UnspecifiedErrorCode,
|
|
661
707
|
type: __injectivelabs_exceptions.ErrorType.WalletNotInstalledError,
|
|
662
708
|
contextModule: __injectivelabs_wallet_base.WalletAction.GetAccounts
|
|
663
709
|
});
|
|
664
|
-
return
|
|
710
|
+
return provider;
|
|
665
711
|
}
|
|
666
712
|
};
|
|
667
713
|
|
|
@@ -677,5 +723,6 @@ exports.getPhantomProvider = getPhantomProvider;
|
|
|
677
723
|
exports.getRabbyProvider = getRabbyProvider;
|
|
678
724
|
exports.getRainbowProvider = getRainbowProvider;
|
|
679
725
|
exports.getTrustWalletProvider = getTrustWalletProvider;
|
|
726
|
+
exports.isUnrecognizedChainError = isUnrecognizedChainError;
|
|
680
727
|
exports.switchEthereumChainWithTimeout = switchEthereumChainWithTimeout;
|
|
681
728
|
exports.updateEvmNetwork = updateEvmNetwork;
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { BaseConcreteStrategy, BrowserEip1993Provider, ConcreteEvmWalletStrategyArgs, ConcreteWalletStrategy, ConcreteWalletStrategyArgs, Eip1193Provider, SendTransactionOptions, StdSignDoc, Wallet, WalletDeviceType } from "@injectivelabs/wallet-base";
|
|
2
1
|
import { AccountAddress, EvmChainId } from "@injectivelabs/ts-types";
|
|
2
|
+
import { BaseConcreteStrategy, BrowserEip1993Provider, ConcreteEvmWalletStrategyArgs, ConcreteWalletStrategy, ConcreteWalletStrategyArgs, Eip1193Provider, SendTransactionOptions, StdSignDoc, Wallet, WalletDeviceType } from "@injectivelabs/wallet-base";
|
|
3
3
|
import { TxResponse } from "@injectivelabs/sdk-ts/core/tx";
|
|
4
4
|
import { ErrorContext, ThrownException } from "@injectivelabs/exceptions";
|
|
5
5
|
import { AminoSignResponse, DirectSignResponse, TxRaw } from "@injectivelabs/sdk-ts/types";
|
|
@@ -7,6 +7,7 @@ import { AminoSignResponse, DirectSignResponse, TxRaw } from "@injectivelabs/sdk
|
|
|
7
7
|
//#region src/utils/index.d.ts
|
|
8
8
|
declare const getEvmProvider: (wallet: Wallet) => Promise<BrowserEip1993Provider>;
|
|
9
9
|
declare const extractNormalizedErrorCode: (error: unknown) => number;
|
|
10
|
+
declare const isUnrecognizedChainError: (error: unknown) => boolean;
|
|
10
11
|
declare const switchEthereumChainWithTimeout: (provider: BrowserEip1993Provider, chainIdHex: string, timeoutMs?: number) => Promise<void>;
|
|
11
12
|
declare const updateEvmNetwork: (wallet: Wallet, chainId: EvmChainId, providerOverride?: BrowserEip1993Provider) => Promise<void>;
|
|
12
13
|
//#endregion
|
|
@@ -111,4 +112,4 @@ declare class EvmWallet extends BaseConcreteStrategy implements ConcreteWalletSt
|
|
|
111
112
|
private getEthereum;
|
|
112
113
|
}
|
|
113
114
|
//#endregion
|
|
114
|
-
export { EvmWallet as EvmWalletStrategy, extractNormalizedErrorCode, getBitGetProvider, getEvmProvider, getKeplrEvmProvider, getMetamaskProvider, getOkxWalletProvider, getPhantomProvider, getRabbyProvider, getRainbowProvider, getTrustWalletProvider, switchEthereumChainWithTimeout, updateEvmNetwork };
|
|
115
|
+
export { EvmWallet as EvmWalletStrategy, extractNormalizedErrorCode, getBitGetProvider, getEvmProvider, getKeplrEvmProvider, getMetamaskProvider, getOkxWalletProvider, getPhantomProvider, getRabbyProvider, getRainbowProvider, getTrustWalletProvider, isUnrecognizedChainError, switchEthereumChainWithTimeout, updateEvmNetwork };
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { AminoSignResponse, DirectSignResponse, TxRaw } from "@injectivelabs/sdk
|
|
|
7
7
|
//#region src/utils/index.d.ts
|
|
8
8
|
declare const getEvmProvider: (wallet: Wallet) => Promise<BrowserEip1993Provider>;
|
|
9
9
|
declare const extractNormalizedErrorCode: (error: unknown) => number;
|
|
10
|
+
declare const isUnrecognizedChainError: (error: unknown) => boolean;
|
|
10
11
|
declare const switchEthereumChainWithTimeout: (provider: BrowserEip1993Provider, chainIdHex: string, timeoutMs?: number) => Promise<void>;
|
|
11
12
|
declare const updateEvmNetwork: (wallet: Wallet, chainId: EvmChainId, providerOverride?: BrowserEip1993Provider) => Promise<void>;
|
|
12
13
|
//#endregion
|
|
@@ -111,4 +112,4 @@ declare class EvmWallet extends BaseConcreteStrategy implements ConcreteWalletSt
|
|
|
111
112
|
private getEthereum;
|
|
112
113
|
}
|
|
113
114
|
//#endregion
|
|
114
|
-
export { EvmWallet as EvmWalletStrategy, extractNormalizedErrorCode, getBitGetProvider, getEvmProvider, getKeplrEvmProvider, getMetamaskProvider, getOkxWalletProvider, getPhantomProvider, getRabbyProvider, getRainbowProvider, getTrustWalletProvider, switchEthereumChainWithTimeout, updateEvmNetwork };
|
|
115
|
+
export { EvmWallet as EvmWalletStrategy, extractNormalizedErrorCode, getBitGetProvider, getEvmProvider, getKeplrEvmProvider, getMetamaskProvider, getOkxWalletProvider, getPhantomProvider, getRabbyProvider, getRainbowProvider, getTrustWalletProvider, isUnrecognizedChainError, switchEthereumChainWithTimeout, updateEvmNetwork };
|
package/dist/esm/index.js
CHANGED
|
@@ -4,218 +4,235 @@ import { BaseConcreteStrategy, EvmWalletProviderErrorCode, Wallet, WalletAction,
|
|
|
4
4
|
import { TxGrpcApi } from "@injectivelabs/sdk-ts/core/tx";
|
|
5
5
|
import { isServerSide, stringToUint8Array, toUtf8, uint8ArrayToHex } from "@injectivelabs/sdk-ts/utils";
|
|
6
6
|
|
|
7
|
-
//#region src/strategy/utils/
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
7
|
+
//#region src/strategy/utils/providerResolver.ts
|
|
8
|
+
const EIP6963_REQUEST_TIMEOUT = 250;
|
|
9
|
+
const eip6963Providers = {};
|
|
10
|
+
const getWindow = () => typeof window === "undefined" ? {} : window;
|
|
11
|
+
const normalize = (value) => (value === null || value === void 0 ? void 0 : value.toLowerCase().trim()) || "";
|
|
12
|
+
const walletProviderMetadata = {
|
|
13
|
+
[Wallet.Metamask]: {
|
|
14
|
+
names: ["metamask"],
|
|
15
|
+
rdns: ["io.metamask", "io.metamask.flask"]
|
|
16
|
+
},
|
|
17
|
+
[Wallet.Rabby]: {
|
|
18
|
+
names: ["rabby", "rabby wallet"],
|
|
19
|
+
rdns: ["io.rabby"]
|
|
20
|
+
},
|
|
21
|
+
[Wallet.Rainbow]: {
|
|
22
|
+
names: ["rainbow", "rainbow wallet"],
|
|
23
|
+
rdns: ["me.rainbow"]
|
|
24
|
+
},
|
|
25
|
+
[Wallet.Phantom]: {
|
|
26
|
+
names: ["phantom", "phantom wallet"],
|
|
27
|
+
rdns: ["app.phantom"]
|
|
28
|
+
},
|
|
29
|
+
[Wallet.OkxWallet]: {
|
|
30
|
+
names: [
|
|
31
|
+
"okx",
|
|
32
|
+
"okx wallet",
|
|
33
|
+
"okxwallet"
|
|
34
|
+
],
|
|
35
|
+
rdns: ["com.okex.wallet"]
|
|
36
|
+
},
|
|
37
|
+
[Wallet.BitGet]: {
|
|
38
|
+
names: [
|
|
39
|
+
"bitget",
|
|
40
|
+
"bitget wallet",
|
|
41
|
+
"bitkeep"
|
|
42
|
+
],
|
|
43
|
+
rdns: ["com.bitget.web3", "com.bitget.wallet"]
|
|
44
|
+
},
|
|
45
|
+
[Wallet.TrustWallet]: {
|
|
46
|
+
names: [
|
|
47
|
+
"trust",
|
|
48
|
+
"trust wallet",
|
|
49
|
+
"trustwallet"
|
|
50
|
+
],
|
|
51
|
+
rdns: ["com.trustwallet.app", "com.trustwallet"]
|
|
52
|
+
},
|
|
53
|
+
[Wallet.KeplrEvm]: {
|
|
54
|
+
names: [
|
|
55
|
+
"keplr",
|
|
56
|
+
"keplr wallet",
|
|
57
|
+
"keplr-evm"
|
|
58
|
+
],
|
|
59
|
+
rdns: [
|
|
60
|
+
"app.keplr",
|
|
61
|
+
"io.keplr",
|
|
62
|
+
"io.keplr.wallet"
|
|
63
|
+
]
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const legacyProviderInitializedEvents = {
|
|
67
|
+
[Wallet.Metamask]: "ethereum#initialized",
|
|
68
|
+
[Wallet.Rabby]: "rabby#initialized",
|
|
69
|
+
[Wallet.Rainbow]: "rainbow#initialized",
|
|
70
|
+
[Wallet.Phantom]: "phantom#initialized",
|
|
71
|
+
[Wallet.OkxWallet]: "okxwallet#initialized",
|
|
72
|
+
[Wallet.BitGet]: "bitkeep#initialized",
|
|
73
|
+
[Wallet.TrustWallet]: "trustwallet#initialized",
|
|
74
|
+
[Wallet.KeplrEvm]: "keplr#initialized"
|
|
75
|
+
};
|
|
76
|
+
const competingMetaMaskFlags = [
|
|
77
|
+
"isRabby",
|
|
78
|
+
"isRainbow",
|
|
79
|
+
"isPhantom",
|
|
80
|
+
"isOkxWallet",
|
|
81
|
+
"isTrustWallet",
|
|
82
|
+
"isTrust",
|
|
83
|
+
"isKeplr"
|
|
84
|
+
];
|
|
85
|
+
const isSupportedEip6963Wallet = (wallet) => wallet in walletProviderMetadata;
|
|
86
|
+
const providerHasCompetingMetaMaskFlag = (provider) => competingMetaMaskFlags.some((flag) => Boolean(provider[flag]));
|
|
87
|
+
const providerMatchesWallet = (provider, wallet) => {
|
|
88
|
+
if (!provider) return false;
|
|
89
|
+
const legacyProvider = provider;
|
|
90
|
+
if (wallet === Wallet.Metamask) return Boolean(provider.isMetaMask) && !providerHasCompetingMetaMaskFlag(provider);
|
|
91
|
+
if (wallet === Wallet.Rabby) return Boolean(provider.isRabby);
|
|
92
|
+
if (wallet === Wallet.Rainbow) return Boolean(provider.isRainbow);
|
|
93
|
+
if (wallet === Wallet.Phantom) return Boolean(provider.isPhantom);
|
|
94
|
+
if (wallet === Wallet.OkxWallet) return Boolean(provider.isOkxWallet);
|
|
95
|
+
if (wallet === Wallet.BitGet) return Boolean(legacyProvider.isBitKeep || legacyProvider.isBitGet);
|
|
96
|
+
if (wallet === Wallet.TrustWallet) return Boolean(provider.isTrustWallet || provider.isTrust);
|
|
97
|
+
if (wallet === Wallet.KeplrEvm) return Boolean(provider.isKeplr);
|
|
98
|
+
return false;
|
|
99
|
+
};
|
|
100
|
+
const getWalletFromEip6963ProviderDetail = (detail) => {
|
|
101
|
+
var _detail$info, _detail$info2, _entries$find;
|
|
102
|
+
const rdns = normalize((_detail$info = detail.info) === null || _detail$info === void 0 ? void 0 : _detail$info.rdns);
|
|
103
|
+
const name = normalize((_detail$info2 = detail.info) === null || _detail$info2 === void 0 ? void 0 : _detail$info2.name);
|
|
104
|
+
const entries = Object.entries(walletProviderMetadata);
|
|
105
|
+
const rdnsMatch = entries.find(([, metadata]) => metadata.rdns.some((value) => normalize(value) === rdns));
|
|
106
|
+
if (rdnsMatch) return rdnsMatch[0];
|
|
107
|
+
return (_entries$find = entries.find(([, metadata]) => metadata.names.some((value) => normalize(value) === name))) === null || _entries$find === void 0 ? void 0 : _entries$find[0];
|
|
108
|
+
};
|
|
109
|
+
const registerEip6963Provider = (detail, providerCache) => {
|
|
110
|
+
if (!(detail === null || detail === void 0 ? void 0 : detail.provider)) return;
|
|
111
|
+
const wallet = getWalletFromEip6963ProviderDetail(detail);
|
|
112
|
+
if (!wallet) return;
|
|
113
|
+
eip6963Providers[wallet] = detail.provider;
|
|
114
|
+
if (providerCache) providerCache[wallet] = detail.provider;
|
|
115
|
+
return wallet;
|
|
116
|
+
};
|
|
117
|
+
const listenForEip6963Providers = (providerCache) => {
|
|
118
|
+
if (typeof window === "undefined") return;
|
|
119
|
+
const handleAnnouncement = (announcement) => {
|
|
120
|
+
registerEip6963Provider(announcement.detail, providerCache);
|
|
121
|
+
};
|
|
122
|
+
window.addEventListener("eip6963:announceProvider", handleAnnouncement);
|
|
123
|
+
return () => window.removeEventListener("eip6963:announceProvider", handleAnnouncement);
|
|
124
|
+
};
|
|
125
|
+
const requestEip6963Providers = () => {
|
|
126
|
+
if (typeof window === "undefined") return;
|
|
127
|
+
window.dispatchEvent(new Event("eip6963:requestProvider"));
|
|
128
|
+
};
|
|
129
|
+
const requestEip6963Provider = (wallet, providerCache, timeout = EIP6963_REQUEST_TIMEOUT) => {
|
|
130
|
+
if (typeof window === "undefined" || timeout <= 0) return Promise.resolve(void 0);
|
|
43
131
|
return new Promise((resolve) => {
|
|
44
|
-
|
|
45
|
-
|
|
132
|
+
let timeoutId;
|
|
133
|
+
let resolved = false;
|
|
134
|
+
let handleAnnouncement = (_announcement) => {};
|
|
135
|
+
const cleanup = () => {
|
|
136
|
+
window.removeEventListener("eip6963:announceProvider", handleAnnouncement);
|
|
137
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
46
138
|
};
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
resolve(
|
|
52
|
-
}, timeout);
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
function getOkxWalletFromWindow() {
|
|
56
|
-
const $window = getWindow$6();
|
|
57
|
-
if (!(typeof window !== "undefined" && (typeof $window.ethereum !== "undefined" || typeof $window.okxwallet !== "undefined"))) return;
|
|
58
|
-
if ($window.okxwallet) return $window.okxwallet;
|
|
59
|
-
if ($window.ethereum.isOkxWallet) return $window.ethereum;
|
|
60
|
-
if ($window.providers) return $window.providers.find((p) => p.isOkxWallet);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
//#endregion
|
|
64
|
-
//#region src/strategy/utils/bitget.ts
|
|
65
|
-
const getWindow$5 = () => typeof window === "undefined" ? {} : window;
|
|
66
|
-
async function getBitGetProvider({ timeout } = { timeout: 3e3 }) {
|
|
67
|
-
const provider = getBitGetFromWindow();
|
|
68
|
-
if (provider) return provider;
|
|
69
|
-
return listenForBitGetInitialized({ timeout });
|
|
70
|
-
}
|
|
71
|
-
async function listenForBitGetInitialized({ timeout } = { timeout: 3e3 }) {
|
|
72
|
-
return new Promise((resolve) => {
|
|
73
|
-
const handleInitialization = () => {
|
|
74
|
-
resolve(getBitGetFromWindow());
|
|
139
|
+
const resolveProvider = (provider) => {
|
|
140
|
+
if (resolved) return;
|
|
141
|
+
resolved = true;
|
|
142
|
+
cleanup();
|
|
143
|
+
resolve(provider);
|
|
75
144
|
};
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
setTimeout(() => {
|
|
79
|
-
$window.removeEventListener("bitkeep#initialized", handleInitialization);
|
|
80
|
-
resolve(null);
|
|
81
|
-
}, timeout);
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
function getBitGetFromWindow() {
|
|
85
|
-
const $window = getWindow$5();
|
|
86
|
-
if (!(typeof window !== "undefined" && (typeof $window.ethereum !== "undefined" || typeof $window.bitkeep !== "undefined"))) return;
|
|
87
|
-
if ($window.bitkeep && $window.bitkeep.ethereum) return $window.bitkeep.ethereum;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
//#endregion
|
|
91
|
-
//#region src/strategy/utils/phantom.ts
|
|
92
|
-
const getWindow$4 = () => typeof window === "undefined" ? {} : window;
|
|
93
|
-
async function getPhantomProvider({ timeout } = { timeout: 3e3 }) {
|
|
94
|
-
const provider = getPhantomFromWindow();
|
|
95
|
-
if (provider) return provider;
|
|
96
|
-
return listenForPhantomInitialized({ timeout });
|
|
97
|
-
}
|
|
98
|
-
async function listenForPhantomInitialized({ timeout } = { timeout: 3e3 }) {
|
|
99
|
-
return new Promise((resolve) => {
|
|
100
|
-
const handleInitialization = () => {
|
|
101
|
-
resolve(getPhantomFromWindow());
|
|
145
|
+
handleAnnouncement = (announcement) => {
|
|
146
|
+
if (registerEip6963Provider(announcement.detail, providerCache) === wallet) resolveProvider((providerCache === null || providerCache === void 0 ? void 0 : providerCache[wallet]) || eip6963Providers[wallet]);
|
|
102
147
|
};
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
$window.removeEventListener("phantom#initialized", handleInitialization);
|
|
107
|
-
resolve(null);
|
|
148
|
+
window.addEventListener("eip6963:announceProvider", handleAnnouncement);
|
|
149
|
+
timeoutId = setTimeout(() => {
|
|
150
|
+
resolveProvider((providerCache === null || providerCache === void 0 ? void 0 : providerCache[wallet]) || eip6963Providers[wallet]);
|
|
108
151
|
}, timeout);
|
|
152
|
+
requestEip6963Providers();
|
|
109
153
|
});
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
if ($window.ethereum.isPhantom) return $window.ethereum;
|
|
116
|
-
if ($window.providers) return $window.providers.find((p) => p.isPhantom);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
//#endregion
|
|
120
|
-
//#region src/strategy/utils/keplrEvm.ts
|
|
121
|
-
const getWindow$3 = () => typeof window === "undefined" ? {} : window;
|
|
122
|
-
async function getKeplrEvmProvider({ timeout } = { timeout: 3e3 }) {
|
|
123
|
-
const provider = getKeplrEvmFromWindow();
|
|
124
|
-
if (provider) return provider;
|
|
125
|
-
return listenForKeplrEvmInitialized({ timeout });
|
|
126
|
-
}
|
|
127
|
-
async function listenForKeplrEvmInitialized({ timeout } = { timeout: 3e3 }) {
|
|
128
|
-
return new Promise((resolve) => {
|
|
129
|
-
const handleInitialization = () => {
|
|
130
|
-
resolve(getKeplrEvmFromWindow());
|
|
131
|
-
};
|
|
132
|
-
const $window = getWindow$3();
|
|
133
|
-
$window.addEventListener("keplr#initialized", handleInitialization, { once: true });
|
|
134
|
-
setTimeout(() => {
|
|
135
|
-
$window.removeEventListener("keplr#initialized", handleInitialization);
|
|
136
|
-
resolve(null);
|
|
137
|
-
}, timeout);
|
|
154
|
+
};
|
|
155
|
+
const uniqueProviders = (providers) => {
|
|
156
|
+
const providerSet = /* @__PURE__ */ new Set();
|
|
157
|
+
providers.forEach((provider) => {
|
|
158
|
+
if (provider) providerSet.add(provider);
|
|
138
159
|
});
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
if (
|
|
144
|
-
if (
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
160
|
+
return [...providerSet];
|
|
161
|
+
};
|
|
162
|
+
const getWalletGlobalProvider = (wallet, $window) => {
|
|
163
|
+
if (wallet === Wallet.Rabby) return $window.rabby;
|
|
164
|
+
if (wallet === Wallet.Rainbow) return $window.rainbow;
|
|
165
|
+
if (wallet === Wallet.Phantom) {
|
|
166
|
+
var _$window$phantom;
|
|
167
|
+
return (_$window$phantom = $window.phantom) === null || _$window$phantom === void 0 ? void 0 : _$window$phantom.ethereum;
|
|
168
|
+
}
|
|
169
|
+
if (wallet === Wallet.OkxWallet) return $window.okxwallet;
|
|
170
|
+
if (wallet === Wallet.BitGet) {
|
|
171
|
+
var _$window$bitkeep;
|
|
172
|
+
return (_$window$bitkeep = $window.bitkeep) === null || _$window$bitkeep === void 0 ? void 0 : _$window$bitkeep.ethereum;
|
|
173
|
+
}
|
|
174
|
+
if (wallet === Wallet.TrustWallet) return $window.trustWallet;
|
|
175
|
+
if (wallet === Wallet.KeplrEvm) {
|
|
176
|
+
var _$window$keplr;
|
|
177
|
+
return (_$window$keplr = $window.keplr) === null || _$window$keplr === void 0 ? void 0 : _$window$keplr.ethereum;
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
const getLegacyEvmProvider = (wallet) => {
|
|
181
|
+
const $window = getWindow();
|
|
182
|
+
const ethereum = $window.ethereum;
|
|
183
|
+
const walletGlobalProvider = getWalletGlobalProvider(wallet, $window);
|
|
184
|
+
if (walletGlobalProvider) return walletGlobalProvider;
|
|
185
|
+
const providerFromArray = uniqueProviders([...(ethereum === null || ethereum === void 0 ? void 0 : ethereum.providers) || [], ...$window.providers || []]).find((provider) => providerMatchesWallet(provider, wallet));
|
|
186
|
+
if (providerFromArray) return providerFromArray;
|
|
187
|
+
if (providerMatchesWallet(ethereum, wallet)) return ethereum;
|
|
188
|
+
};
|
|
189
|
+
const resolveEvmProvider = async (wallet, options = {}) => {
|
|
190
|
+
if (!isSupportedEip6963Wallet(wallet)) return;
|
|
191
|
+
const { eip6963Providers: providerCache, requestEip6963 = true, timeout = EIP6963_REQUEST_TIMEOUT } = options;
|
|
192
|
+
const cachedProvider = (providerCache === null || providerCache === void 0 ? void 0 : providerCache[wallet]) || eip6963Providers[wallet];
|
|
193
|
+
if (cachedProvider) return cachedProvider;
|
|
194
|
+
if (requestEip6963) {
|
|
195
|
+
const announcedProvider = await requestEip6963Provider(wallet, providerCache, timeout);
|
|
196
|
+
if (announcedProvider) return announcedProvider;
|
|
197
|
+
}
|
|
198
|
+
return getLegacyEvmProvider(wallet);
|
|
199
|
+
};
|
|
200
|
+
const getEvmProviderWithFallback = async (wallet, options = {}) => {
|
|
201
|
+
const { eip6963Providers: providerCache, timeout = 3e3 } = options;
|
|
202
|
+
const provider = await resolveEvmProvider(wallet, { eip6963Providers: providerCache });
|
|
154
203
|
if (provider) return provider;
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
async function listenForMetamaskInitialized({ timeout } = { timeout: 3e3 }) {
|
|
204
|
+
const eventName = legacyProviderInitializedEvents[wallet];
|
|
205
|
+
if (typeof window === "undefined" || !eventName) return;
|
|
158
206
|
return new Promise((resolve) => {
|
|
159
|
-
|
|
160
|
-
|
|
207
|
+
let timeoutId;
|
|
208
|
+
let handleInitialization = async () => {};
|
|
209
|
+
const cleanup = () => {
|
|
210
|
+
window.removeEventListener(eventName, handleInitialization);
|
|
211
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
161
212
|
};
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
function getMetamaskFromWindow() {
|
|
171
|
-
const $window = getWindow$2();
|
|
172
|
-
if (!(typeof window !== "undefined" && typeof $window.ethereum !== "undefined")) return;
|
|
173
|
-
if ($window.ethereum.isMetaMask) return $window.ethereum;
|
|
174
|
-
if ($window.providers) return $window.providers.find((p) => p.isMetaMask);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
//#endregion
|
|
178
|
-
//#region src/strategy/utils/trustWallet.ts
|
|
179
|
-
const getWindow$1 = () => typeof window === "undefined" ? {} : window;
|
|
180
|
-
async function getTrustWalletProvider({ timeout } = { timeout: 3e3 }) {
|
|
181
|
-
const provider = getTrustWalletFromWindow();
|
|
182
|
-
if (provider) return provider;
|
|
183
|
-
return listenForTrustWalletInitialized({ timeout });
|
|
184
|
-
}
|
|
185
|
-
async function listenForTrustWalletInitialized({ timeout } = { timeout: 3e3 }) {
|
|
186
|
-
return new Promise((resolve) => {
|
|
187
|
-
const handleInitialization = () => {
|
|
188
|
-
resolve(getTrustWalletFromWindow());
|
|
213
|
+
handleInitialization = async () => {
|
|
214
|
+
cleanup();
|
|
215
|
+
resolve(await resolveEvmProvider(wallet, {
|
|
216
|
+
eip6963Providers: providerCache,
|
|
217
|
+
requestEip6963: false
|
|
218
|
+
}));
|
|
189
219
|
};
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
resolve(null);
|
|
220
|
+
window.addEventListener(eventName, handleInitialization, { once: true });
|
|
221
|
+
timeoutId = setTimeout(() => {
|
|
222
|
+
cleanup();
|
|
223
|
+
resolve(void 0);
|
|
195
224
|
}, timeout);
|
|
196
225
|
});
|
|
197
|
-
}
|
|
198
|
-
function getTrustWalletFromWindow() {
|
|
199
|
-
const $window = getWindow$1();
|
|
200
|
-
if (!(typeof window !== "undefined" && (typeof $window.ethereum !== "undefined" || typeof $window.trustWallet !== "undefined"))) return;
|
|
201
|
-
if ($window.trustWallet) return $window.trustWallet;
|
|
202
|
-
if ($window.ethereum.isTrustWallet || $window.ethereum.isTrust) return $window.ethereum;
|
|
203
|
-
if ($window.providers) return $window.providers.find((p) => p.isTrustWallet);
|
|
204
|
-
}
|
|
226
|
+
};
|
|
205
227
|
|
|
206
228
|
//#endregion
|
|
207
229
|
//#region src/utils/index.ts
|
|
230
|
+
const EVM_WALLET_INVALID_REQUEST_ERROR_CODE = -32600;
|
|
231
|
+
const CHAIN_NOT_SUPPORTED_MESSAGE_REGEX = /\b(chain(?:\s+id)?\s+not supported|unsupported\s+chain(?:\s+id)?)\b/;
|
|
208
232
|
const getEvmProvider = async (wallet) => {
|
|
209
233
|
if (!isEvmBrowserWallet(wallet)) throw new WalletException(/* @__PURE__ */ new Error(`Evm Wallet for ${capitalize(wallet)} is not supported.`));
|
|
210
234
|
try {
|
|
211
|
-
|
|
212
|
-
if (wallet === Wallet.Metamask) provider = await getMetamaskProvider();
|
|
213
|
-
if (wallet === Wallet.Rabby) provider = await getRabbyProvider();
|
|
214
|
-
if (wallet === Wallet.BitGet) provider = await getBitGetProvider();
|
|
215
|
-
if (wallet === Wallet.Phantom) provider = await getPhantomProvider();
|
|
216
|
-
if (wallet === Wallet.TrustWallet) provider = await getTrustWalletProvider();
|
|
217
|
-
if (wallet === Wallet.OkxWallet) provider = await getOkxWalletProvider();
|
|
218
|
-
if (wallet === Wallet.KeplrEvm) provider = await getKeplrEvmProvider();
|
|
235
|
+
const provider = await getEvmProviderWithFallback(wallet);
|
|
219
236
|
if (!provider) throw new WalletException(/* @__PURE__ */ new Error(`Please install ${capitalize(wallet)} Extension`));
|
|
220
237
|
return provider;
|
|
221
238
|
} catch (_unused) {
|
|
@@ -230,6 +247,15 @@ const extractNormalizedErrorCode = (error) => {
|
|
|
230
247
|
const rawCode = (code != null ? Number(code) : void 0) === EvmWalletProviderErrorCode.InternalError || code === void 0 || code === null ? originalCode !== null && originalCode !== void 0 ? originalCode : code : code;
|
|
231
248
|
return rawCode != null ? Number(rawCode) : NaN;
|
|
232
249
|
};
|
|
250
|
+
const isUnrecognizedChainError = (error) => {
|
|
251
|
+
var _ref, _normalizedError$data2, _normalizedError$data3;
|
|
252
|
+
const errorCode = extractNormalizedErrorCode(error);
|
|
253
|
+
if (errorCode === EvmWalletProviderErrorCode.UnrecognizedChain) return true;
|
|
254
|
+
if (errorCode !== EVM_WALLET_INVALID_REQUEST_ERROR_CODE) return false;
|
|
255
|
+
const normalizedError = error && typeof error === "object" ? error : void 0;
|
|
256
|
+
const message = String((_ref = (_normalizedError$data2 = normalizedError === null || normalizedError === void 0 || (_normalizedError$data3 = normalizedError.data) === null || _normalizedError$data3 === void 0 || (_normalizedError$data3 = _normalizedError$data3.originalError) === null || _normalizedError$data3 === void 0 ? void 0 : _normalizedError$data3.message) !== null && _normalizedError$data2 !== void 0 ? _normalizedError$data2 : normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.message) !== null && _ref !== void 0 ? _ref : "").toLowerCase();
|
|
257
|
+
return CHAIN_NOT_SUPPORTED_MESSAGE_REGEX.test(message);
|
|
258
|
+
};
|
|
233
259
|
const switchEthereumChainWithTimeout = async (provider, chainIdHex, timeoutMs = 3e4) => {
|
|
234
260
|
let timeoutId;
|
|
235
261
|
let handleChainChanged;
|
|
@@ -272,10 +298,9 @@ const updateEvmNetwork = async (wallet, chainId, providerOverride) => {
|
|
|
272
298
|
await switchEthereumChainWithTimeout(provider, chainIdToHex, TIMEOUT_MS);
|
|
273
299
|
} catch (switchError) {
|
|
274
300
|
var _chainConfig$rpcUrls, _chainConfig$blockExp;
|
|
275
|
-
|
|
276
|
-
if (errorCode === EvmWalletProviderErrorCode.UserRejectedRequest) throw new WalletException(/* @__PURE__ */ new Error(`${capitalize(wallet)} chain switch was rejected`));
|
|
301
|
+
if (extractNormalizedErrorCode(switchError) === EvmWalletProviderErrorCode.UserRejectedRequest) throw new WalletException(/* @__PURE__ */ new Error(`${capitalize(wallet)} chain switch was rejected`));
|
|
277
302
|
if (switchError.message === "Chain switch timed out") throw new WalletException(/* @__PURE__ */ new Error("Chain switch timed out"));
|
|
278
|
-
if (
|
|
303
|
+
if (!isUnrecognizedChainError(switchError)) throw new WalletException(/* @__PURE__ */ new Error(`Please update your ${capitalize(wallet)} network`));
|
|
279
304
|
const chainConfig = getEvmChainConfig(chainId);
|
|
280
305
|
const rpcUrl = (_chainConfig$rpcUrls = chainConfig.rpcUrls) === null || _chainConfig$rpcUrls === void 0 || (_chainConfig$rpcUrls = _chainConfig$rpcUrls.default) === null || _chainConfig$rpcUrls === void 0 || (_chainConfig$rpcUrls = _chainConfig$rpcUrls.http) === null || _chainConfig$rpcUrls === void 0 ? void 0 : _chainConfig$rpcUrls[0];
|
|
281
306
|
const explorerUrl = ((_chainConfig$blockExp = chainConfig.blockExplorers) === null || _chainConfig$blockExp === void 0 || (_chainConfig$blockExp = _chainConfig$blockExp.default) === null || _chainConfig$blockExp === void 0 ? void 0 : _chainConfig$blockExp.url) || void 0;
|
|
@@ -314,33 +339,68 @@ const updateEvmNetwork = async (wallet, chainId, providerOverride) => {
|
|
|
314
339
|
}
|
|
315
340
|
};
|
|
316
341
|
|
|
342
|
+
//#endregion
|
|
343
|
+
//#region src/strategy/utils/rabby.ts
|
|
344
|
+
async function getRabbyProvider({ timeout } = { timeout: 3e3 }) {
|
|
345
|
+
const provider = await getEvmProviderWithFallback(Wallet.Rabby, { timeout });
|
|
346
|
+
if (!provider) throw new Error(`Please install the ${Wallet.Rabby} wallet extension.`);
|
|
347
|
+
return provider;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
//#endregion
|
|
351
|
+
//#region src/strategy/utils/bitget.ts
|
|
352
|
+
async function getBitGetProvider({ timeout } = { timeout: 3e3 }) {
|
|
353
|
+
const provider = await getEvmProviderWithFallback(Wallet.BitGet, { timeout });
|
|
354
|
+
if (!provider) throw new Error(`Please install the ${Wallet.BitGet} wallet extension.`);
|
|
355
|
+
return provider;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
//#endregion
|
|
359
|
+
//#region src/strategy/utils/Okx.ts
|
|
360
|
+
async function getOkxWalletProvider({ timeout } = { timeout: 3e3 }) {
|
|
361
|
+
const provider = await getEvmProviderWithFallback(Wallet.OkxWallet, { timeout });
|
|
362
|
+
if (!provider) throw new Error(`Please install the ${Wallet.OkxWallet} wallet extension.`);
|
|
363
|
+
return provider;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
//#endregion
|
|
367
|
+
//#region src/strategy/utils/phantom.ts
|
|
368
|
+
async function getPhantomProvider({ timeout } = { timeout: 3e3 }) {
|
|
369
|
+
const provider = await getEvmProviderWithFallback(Wallet.Phantom, { timeout });
|
|
370
|
+
if (!provider) throw new Error(`Please install the ${Wallet.Phantom} wallet extension.`);
|
|
371
|
+
return provider;
|
|
372
|
+
}
|
|
373
|
+
|
|
317
374
|
//#endregion
|
|
318
375
|
//#region src/strategy/utils/rainbow.ts
|
|
319
|
-
const getWindow = () => typeof window === "undefined" ? {} : window;
|
|
320
376
|
async function getRainbowProvider({ timeout } = { timeout: 3e3 }) {
|
|
321
|
-
const provider =
|
|
322
|
-
if (provider)
|
|
323
|
-
return
|
|
377
|
+
const provider = await getEvmProviderWithFallback(Wallet.Rainbow, { timeout });
|
|
378
|
+
if (!provider) throw new Error(`Please install the ${Wallet.Rainbow} wallet extension.`);
|
|
379
|
+
return provider;
|
|
324
380
|
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
setTimeout(() => {
|
|
333
|
-
$window.removeEventListener("rainbow#initialized", handleInitialization);
|
|
334
|
-
resolve(null);
|
|
335
|
-
}, timeout);
|
|
336
|
-
});
|
|
381
|
+
|
|
382
|
+
//#endregion
|
|
383
|
+
//#region src/strategy/utils/metamask.ts
|
|
384
|
+
async function getMetamaskProvider({ timeout } = { timeout: 3e3 }) {
|
|
385
|
+
const provider = await getEvmProviderWithFallback(Wallet.Metamask, { timeout });
|
|
386
|
+
if (!provider) throw new Error(`Please install the ${Wallet.Metamask} wallet extension.`);
|
|
387
|
+
return provider;
|
|
337
388
|
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
if (
|
|
389
|
+
|
|
390
|
+
//#endregion
|
|
391
|
+
//#region src/strategy/utils/keplrEvm.ts
|
|
392
|
+
async function getKeplrEvmProvider({ timeout } = { timeout: 3e3 }) {
|
|
393
|
+
const provider = await getEvmProviderWithFallback(Wallet.KeplrEvm, { timeout });
|
|
394
|
+
if (!provider) throw new Error(`Please install the ${Wallet.KeplrEvm} wallet extension.`);
|
|
395
|
+
return provider;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
//#endregion
|
|
399
|
+
//#region src/strategy/utils/trustWallet.ts
|
|
400
|
+
async function getTrustWalletProvider({ timeout } = { timeout: 3e3 }) {
|
|
401
|
+
const provider = await getEvmProviderWithFallback(Wallet.TrustWallet, { timeout });
|
|
402
|
+
if (!provider) throw new Error(`Please install the ${Wallet.TrustWallet} wallet extension.`);
|
|
403
|
+
return provider;
|
|
344
404
|
}
|
|
345
405
|
|
|
346
406
|
//#endregion
|
|
@@ -394,19 +454,8 @@ var EvmWallet = class extends BaseConcreteStrategy {
|
|
|
394
454
|
_defineProperty(this, "evmProviders", {});
|
|
395
455
|
if (!isEvmBrowserWallet(args.wallet)) throw new WalletException(/* @__PURE__ */ new Error(`Evm Wallet for ${capitalize(args.wallet)} is not supported.`));
|
|
396
456
|
if (!isServerSide()) {
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
const walletName = event.detail.info.name.toLowerCase();
|
|
400
|
-
if (walletName === "keplr") this.evmProviders[Wallet.KeplrEvm] = event.detail.provider;
|
|
401
|
-
if (walletName === Wallet.Metamask.toLowerCase()) this.evmProviders[Wallet.Metamask] = event.detail.provider;
|
|
402
|
-
if (walletName === "rabby wallet") this.evmProviders[Wallet.Rabby] = event.detail.provider;
|
|
403
|
-
if (walletName === Wallet.Rainbow.toLowerCase()) this.evmProviders[Wallet.Rainbow] = event.detail.provider;
|
|
404
|
-
if (walletName === Wallet.Phantom.toLowerCase()) this.evmProviders[Wallet.Phantom] = event.detail.provider;
|
|
405
|
-
if (walletName === Wallet.OkxWallet.toLowerCase()) this.evmProviders[Wallet.OkxWallet] = event.detail.provider;
|
|
406
|
-
if (walletName === Wallet.BitGet.toLowerCase()) this.evmProviders[Wallet.BitGet] = event.detail.provider;
|
|
407
|
-
if (walletName === Wallet.TrustWallet.toLowerCase()) this.evmProviders[Wallet.TrustWallet] = event.detail.provider;
|
|
408
|
-
});
|
|
409
|
-
window.dispatchEvent(new Event("eip6963:requestProvider"));
|
|
457
|
+
listenForEip6963Providers(this.evmProviders);
|
|
458
|
+
requestEip6963Providers();
|
|
410
459
|
}
|
|
411
460
|
this.wallet = args.wallet;
|
|
412
461
|
}
|
|
@@ -588,8 +637,7 @@ var EvmWallet = class extends BaseConcreteStrategy {
|
|
|
588
637
|
try {
|
|
589
638
|
await switchEthereumChainWithTimeout(ethereum, chainIdHex, TIMEOUT_MS);
|
|
590
639
|
} catch (error) {
|
|
591
|
-
|
|
592
|
-
if (errorCode === EvmWalletProviderErrorCode.UserRejectedRequest) throw this.EvmWalletException(/* @__PURE__ */ new Error(`${capitalize(this.wallet || "wallet")} chain switch was rejected`), {
|
|
640
|
+
if (extractNormalizedErrorCode(error) === EvmWalletProviderErrorCode.UserRejectedRequest) throw this.EvmWalletException(/* @__PURE__ */ new Error(`${capitalize(this.wallet || "wallet")} chain switch was rejected`), {
|
|
593
641
|
code: UnspecifiedErrorCode,
|
|
594
642
|
type: ErrorType.WalletError,
|
|
595
643
|
contextModule: WalletAction.GetChainId
|
|
@@ -599,7 +647,7 @@ var EvmWallet = class extends BaseConcreteStrategy {
|
|
|
599
647
|
type: ErrorType.WalletError,
|
|
600
648
|
contextModule: WalletAction.GetChainId
|
|
601
649
|
});
|
|
602
|
-
if (
|
|
650
|
+
if (!isUnrecognizedChainError(error)) throw this.EvmWalletException(/* @__PURE__ */ new Error(`Something went wrong while switching ${capitalize(this.wallet || "wallet")} network`), {
|
|
603
651
|
code: UnspecifiedErrorCode,
|
|
604
652
|
type: ErrorType.WalletError,
|
|
605
653
|
contextModule: WalletAction.GetChainId
|
|
@@ -653,17 +701,15 @@ var EvmWallet = class extends BaseConcreteStrategy {
|
|
|
653
701
|
}
|
|
654
702
|
}
|
|
655
703
|
async getEthereum() {
|
|
656
|
-
const
|
|
657
|
-
if (
|
|
658
|
-
const backUpProvider = this.wallet === Wallet.Metamask ? await getMetamaskProvider() : this.wallet === Wallet.Rabby ? await getRabbyProvider() : this.wallet === Wallet.Phantom ? await getPhantomProvider() : this.wallet === Wallet.BitGet ? await getBitGetProvider() : this.wallet === Wallet.OkxWallet ? await getOkxWalletProvider() : this.wallet === Wallet.TrustWallet ? await getTrustWalletProvider() : this.wallet === Wallet.Rainbow ? await getRainbowProvider() : this.wallet === Wallet.KeplrEvm ? await getKeplrEvmProvider() : void 0;
|
|
659
|
-
if (!backUpProvider) throw this.EvmWalletException(/* @__PURE__ */ new Error(`Please install the ${this.wallet} wallet extension.`), {
|
|
704
|
+
const provider = await getEvmProviderWithFallback(this.wallet, { eip6963Providers: this.evmProviders });
|
|
705
|
+
if (!provider) throw this.EvmWalletException(/* @__PURE__ */ new Error(`Please install the ${this.wallet} wallet extension.`), {
|
|
660
706
|
code: UnspecifiedErrorCode,
|
|
661
707
|
type: ErrorType.WalletNotInstalledError,
|
|
662
708
|
contextModule: WalletAction.GetAccounts
|
|
663
709
|
});
|
|
664
|
-
return
|
|
710
|
+
return provider;
|
|
665
711
|
}
|
|
666
712
|
};
|
|
667
713
|
|
|
668
714
|
//#endregion
|
|
669
|
-
export { EvmWallet as EvmWalletStrategy, extractNormalizedErrorCode, getBitGetProvider, getEvmProvider, getKeplrEvmProvider, getMetamaskProvider, getOkxWalletProvider, getPhantomProvider, getRabbyProvider, getRainbowProvider, getTrustWalletProvider, switchEthereumChainWithTimeout, updateEvmNetwork };
|
|
715
|
+
export { EvmWallet as EvmWalletStrategy, extractNormalizedErrorCode, getBitGetProvider, getEvmProvider, getKeplrEvmProvider, getMetamaskProvider, getOkxWalletProvider, getPhantomProvider, getRabbyProvider, getRainbowProvider, getTrustWalletProvider, isUnrecognizedChainError, switchEthereumChainWithTimeout, updateEvmNetwork };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@injectivelabs/wallet-evm",
|
|
3
|
-
"version": "1.20.
|
|
3
|
+
"version": "1.20.5",
|
|
4
4
|
"description": "EVM wallet strategies for use with @injectivelabs/wallet-core.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": {
|
|
@@ -42,11 +42,11 @@
|
|
|
42
42
|
"dist"
|
|
43
43
|
],
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@injectivelabs/
|
|
46
|
-
"@injectivelabs/
|
|
47
|
-
"@injectivelabs/ts
|
|
48
|
-
"@injectivelabs/
|
|
49
|
-
"@injectivelabs/wallet-base": "1.20.
|
|
45
|
+
"@injectivelabs/ts-types": "1.20.5",
|
|
46
|
+
"@injectivelabs/exceptions": "1.20.5",
|
|
47
|
+
"@injectivelabs/sdk-ts": "1.20.5",
|
|
48
|
+
"@injectivelabs/utils": "1.20.5",
|
|
49
|
+
"@injectivelabs/wallet-base": "1.20.5"
|
|
50
50
|
},
|
|
51
51
|
"publishConfig": {
|
|
52
52
|
"access": "public"
|