@dynamic-labs/ethereum 4.0.0-alpha.8 → 4.0.0
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/CHANGELOG.md +496 -1
- package/package.cjs +1 -1
- package/package.js +1 -1
- package/package.json +11 -9
- package/src/coinbase/coinbase.cjs +1 -3
- package/src/coinbase/coinbase.d.ts +2 -5
- package/src/coinbase/coinbase.js +2 -4
- package/src/coinbase/types.d.ts +2 -2
- package/src/ethProviderHelper.cjs +49 -43
- package/src/ethProviderHelper.d.ts +5 -7
- package/src/ethProviderHelper.js +50 -44
- package/src/index.cjs +14 -3
- package/src/index.d.ts +2 -1
- package/src/index.js +5 -4
- package/src/injected/ExodusEvm.cjs +3 -5
- package/src/injected/ExodusEvm.d.ts +1 -3
- package/src/injected/ExodusEvm.js +3 -5
- package/src/injected/FallbackEvmConnector.cjs +3 -5
- package/src/injected/FallbackEvmConnector.d.ts +1 -3
- package/src/injected/FallbackEvmConnector.js +3 -5
- package/src/injected/InjectedWalletBase.cjs +18 -31
- package/src/injected/InjectedWalletBase.d.ts +4 -10
- package/src/injected/InjectedWalletBase.js +17 -32
- package/src/injected/PhantomEvm.cjs +4 -6
- package/src/injected/PhantomEvm.d.ts +1 -3
- package/src/injected/PhantomEvm.js +4 -6
- package/src/injected/fetchInjectedWalletConnectors.cjs +48 -39
- package/src/injected/fetchInjectedWalletConnectors.js +49 -40
- package/src/injected/index.d.ts +1 -1
- package/src/metaMask/MetaMaskConnector.cjs +376 -0
- package/src/metaMask/MetaMaskConnector.d.ts +52 -0
- package/src/metaMask/MetaMaskConnector.js +372 -0
- package/src/metaMask/utils/createMetaMaskSDKDisplayUriState.cjs +58 -0
- package/src/metaMask/utils/createMetaMaskSDKDisplayUriState.d.ts +14 -0
- package/src/metaMask/utils/createMetaMaskSDKDisplayUriState.js +54 -0
- package/src/metaMask/utils/isPendingWalletRequestPermissionError.cjs +11 -0
- package/src/metaMask/utils/isPendingWalletRequestPermissionError.d.ts +1 -0
- package/src/metaMask/utils/isPendingWalletRequestPermissionError.js +7 -0
- package/src/metaMask/utils/waitForConnection.cjs +10 -0
- package/src/metaMask/utils/waitForConnection.d.ts +2 -0
- package/src/metaMask/utils/waitForConnection.js +6 -0
- package/src/utils/createInjectedConnector/createInjectedConnector.cjs +93 -0
- package/src/utils/createInjectedConnector/createInjectedConnector.d.ts +16 -0
- package/src/utils/createInjectedConnector/createInjectedConnector.js +89 -0
- package/src/utils/createInjectedConnector/index.d.ts +1 -0
- package/src/utils/getConnectorConstructorForEip6963Wallet/getConnectorConstructorForEip6963Wallet.cjs +30 -0
- package/src/utils/getConnectorConstructorForEip6963Wallet/getConnectorConstructorForEip6963Wallet.d.ts +3 -0
- package/src/utils/getConnectorConstructorForEip6963Wallet/getConnectorConstructorForEip6963Wallet.js +26 -0
- package/src/utils/getConnectorConstructorForEip6963Wallet/index.d.ts +1 -0
- package/src/utils/getConnectorConstructorInjectedWallet/getConnectorConstructorInjectedWallet.cjs +44 -0
- package/src/utils/getConnectorConstructorInjectedWallet/getConnectorConstructorInjectedWallet.d.ts +3 -0
- package/src/utils/getConnectorConstructorInjectedWallet/getConnectorConstructorInjectedWallet.js +40 -0
- package/src/utils/getConnectorConstructorInjectedWallet/index.d.ts +1 -0
- package/src/utils/logger.cjs +10 -0
- package/src/utils/logger.d.ts +2 -0
- package/src/utils/logger.js +6 -0
- package/src/utils/normalizeRpcError/index.d.ts +1 -0
- package/src/utils/normalizeRpcError/normalizeRpcError.cjs +51 -0
- package/src/utils/normalizeRpcError/normalizeRpcError.d.ts +5 -0
- package/src/utils/normalizeRpcError/normalizeRpcError.js +47 -0
- package/src/walletConnect/walletConnect.cjs +175 -204
- package/src/walletConnect/walletConnect.d.ts +7681 -27
- package/src/walletConnect/walletConnect.js +175 -204
- package/src/injected/UnknownInjected.cjs +0 -21
- package/src/injected/UnknownInjected.d.ts +0 -8
- package/src/injected/UnknownInjected.js +0 -17
- package/src/utils/isString.d.ts +0 -1
- package/src/utils/last.d.ts +0 -1
- package/src/utils/parseIntSafe.cjs +0 -23
- package/src/utils/parseIntSafe.d.ts +0 -1
- package/src/utils/parseIntSafe.js +0 -19
|
@@ -4,56 +4,159 @@ import EthereumProvider from '@walletconnect/ethereum-provider';
|
|
|
4
4
|
import EventEmitter from 'eventemitter3';
|
|
5
5
|
import { createWalletClient, custom } from 'viem';
|
|
6
6
|
import { logger, performPlatformSpecificConnectionMethod, getDeepLink } from '@dynamic-labs/wallet-connector-core';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { EthWalletConnector, chainsMap } from '@dynamic-labs/ethereum-core';
|
|
10
|
-
import { parseIntSafe } from '../utils/parseIntSafe.js';
|
|
7
|
+
import { parseIntSafe, sleep, DynamicError, isMobile } from '@dynamic-labs/utils';
|
|
8
|
+
import { EthereumWalletConnector, chainsMap } from '@dynamic-labs/ethereum-core';
|
|
11
9
|
|
|
12
10
|
const activeAccountKey = (walletName) => `dynamic-wc2-active-account-${walletName}`;
|
|
13
11
|
const sessionTopicKey = (walletName) => `dynamic-wc2-session-topic-${walletName}`;
|
|
14
|
-
const swicthedNetworkKey = (walletName) => `dynamic-wc2-switched-network-${walletName}`;
|
|
15
12
|
const currentChainKey = (walletName) => `dynamic-wc2-current-chain-${walletName}`;
|
|
16
13
|
const ee = new EventEmitter();
|
|
17
|
-
class WalletConnect extends
|
|
14
|
+
class WalletConnect extends EthereumWalletConnector {
|
|
15
|
+
supportsNetworkSwitching() {
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
18
|
constructor(opts) {
|
|
19
|
-
var _a;
|
|
20
19
|
super(opts);
|
|
21
|
-
this.supportedChains = ['EVM', 'ETH'];
|
|
22
|
-
this.connectedChain = 'EVM';
|
|
23
20
|
this.isInitialized = false;
|
|
24
21
|
this.canConnectViaQrCode = true;
|
|
25
22
|
this.isWalletConnect = true;
|
|
26
23
|
this.preferredChains = [];
|
|
27
|
-
// When trying to switch network for MetaMask, the switch promise gets stuck
|
|
28
|
-
// if the switch got trigged once already, so we need to keep track of that
|
|
29
|
-
this._hasSwitchedNetwork = false;
|
|
30
24
|
this.sessionEventHandler = () => { };
|
|
31
25
|
this.sessionDeleteHandler = () => { };
|
|
32
26
|
this.name = opts.walletName;
|
|
33
27
|
this.projectId = opts.projectId;
|
|
34
28
|
this.deepLinkPreference = opts.deepLinkPreference || 'native';
|
|
35
29
|
this.preferredChains = opts.walletConnectPreferredChains || [];
|
|
36
|
-
this.hasSwitchedNetwork =
|
|
37
|
-
(_a = Boolean(localStorage.getItem(this.swicthedNetworkKey))) !== null && _a !== void 0 ? _a : false;
|
|
38
30
|
const lsCurrentChain = localStorage.getItem(this.currentChainKey);
|
|
39
31
|
this.currentChainId = lsCurrentChain
|
|
40
32
|
? parseIntSafe(lsCurrentChain)
|
|
41
33
|
: undefined;
|
|
42
34
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
35
|
+
/**
|
|
36
|
+
* This method is used to get the address of the active account.
|
|
37
|
+
* It will re-initialize the provider if the provider is not found.
|
|
38
|
+
* @param opts
|
|
39
|
+
* @returns
|
|
40
|
+
*/
|
|
41
|
+
getAddress(opts) {
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
var _a, _b;
|
|
44
|
+
const activeAccount = this.getActiveAccount();
|
|
45
|
+
if (activeAccount === null || activeAccount === void 0 ? void 0 : activeAccount.address) {
|
|
46
|
+
return activeAccount.address;
|
|
47
|
+
}
|
|
48
|
+
if (!WalletConnect.provider || !((_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.signer.uri)) {
|
|
49
|
+
logger.debug('No WC2 provider found, re-initializing...');
|
|
50
|
+
yield this.endSession();
|
|
51
|
+
yield this.init();
|
|
52
|
+
// sleep 1 s to wait for connect call to finish
|
|
53
|
+
// the connect call isn't await-ed because it only resolves once
|
|
54
|
+
// the connection is established, but we need to wait for it to
|
|
55
|
+
// finish setting up the connection URI and making it available
|
|
56
|
+
// on the provider
|
|
57
|
+
yield sleep(1000);
|
|
58
|
+
if (!WalletConnect.provider || !((_b = WalletConnect.provider) === null || _b === void 0 ? void 0 : _b.signer.uri)) {
|
|
59
|
+
logger.debug('No WC2 provider was found after init attempt, aborting');
|
|
60
|
+
throw new DynamicError('No provider found');
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
logger.debug('provider was successfully initialized, continuing');
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// Set Deep links
|
|
67
|
+
performPlatformSpecificConnectionMethod(WalletConnect.provider.signer.uri, this.metadata.deepLinks, {
|
|
68
|
+
onDesktopUri: opts === null || opts === void 0 ? void 0 : opts.onDesktopUri,
|
|
69
|
+
onDisplayUri: opts === null || opts === void 0 ? void 0 : opts.onDisplayUri,
|
|
70
|
+
}, this.deepLinkPreference);
|
|
71
|
+
return new Promise((resolve, reject) => {
|
|
72
|
+
if (!WalletConnect.provider) {
|
|
73
|
+
reject(new DynamicError('No provider found'));
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const onFail = () => {
|
|
77
|
+
logger.debug('onConnection faile re-initializing provider');
|
|
78
|
+
const error = new DynamicError('Connection rejected. Please try again.');
|
|
79
|
+
error.code = 'connection_rejected';
|
|
80
|
+
if (WalletConnect.provider) {
|
|
81
|
+
WalletConnect.provider.signer.uri = undefined;
|
|
82
|
+
// this is needed for mobile to work when using universal links.
|
|
83
|
+
// if the user cancels the connection, we need to re-initialize the provider
|
|
84
|
+
// so that the async work is done ahead of time, before the user tries to connect again,
|
|
85
|
+
// otherwise they will trigger the iOS bug where they are redirected to the app store
|
|
86
|
+
this.init();
|
|
87
|
+
logger.debug('provider was re-initialized in onConnection fail, continuing');
|
|
88
|
+
}
|
|
89
|
+
reject(error);
|
|
90
|
+
// We must clean up the onConnect and onFail listeners
|
|
91
|
+
// whenever the connection attempt either succeeds or fails
|
|
92
|
+
cleanupListeners();
|
|
93
|
+
};
|
|
94
|
+
const onConnect = () => {
|
|
95
|
+
var _a;
|
|
96
|
+
logger.debug('onConnection success, setting session');
|
|
97
|
+
const session = (_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.session;
|
|
98
|
+
if (!session) {
|
|
99
|
+
logger.debug('onConnection success, but no session found rejecting');
|
|
100
|
+
reject(new DynamicError('No session found'));
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
this.setSession(session);
|
|
104
|
+
this.setWCActiveAccount(session.namespaces.eip155.accounts[0].split(':')[2]);
|
|
105
|
+
this.getNetwork().then((chainId) => {
|
|
106
|
+
var _a, _b;
|
|
107
|
+
logger.debug('onConnection success, resolving, chainId:', chainId);
|
|
108
|
+
this.currentChainId = chainId;
|
|
109
|
+
logger.debug('onConnection success, resolving, activeAccountAddress:', (_a = this.getActiveAccount()) === null || _a === void 0 ? void 0 : _a.address);
|
|
110
|
+
resolve((_b = this.getActiveAccount()) === null || _b === void 0 ? void 0 : _b.address);
|
|
111
|
+
});
|
|
112
|
+
// We must clean up the onConnect and onFail listeners
|
|
113
|
+
// whenever the connection attempt either succeeds or fails
|
|
114
|
+
cleanupListeners();
|
|
115
|
+
};
|
|
116
|
+
const cleanupListeners = () => {
|
|
117
|
+
var _a;
|
|
118
|
+
ee.off('walletconnect_connection_failed', onFail);
|
|
119
|
+
(_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.off('connect', onConnect);
|
|
120
|
+
};
|
|
121
|
+
ee.on('walletconnect_connection_failed', onFail);
|
|
122
|
+
WalletConnect.provider.on('connect', onConnect);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
48
125
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
126
|
+
init() {
|
|
127
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
128
|
+
logger.debug('init was called', this.name);
|
|
129
|
+
yield this.initProvider();
|
|
130
|
+
yield this.initConnection();
|
|
131
|
+
this.isInitialized = true;
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
// We need to add a gate to this method since we will be calling it asynchronously
|
|
135
|
+
// from different places (such as setShowAuthFlow), which means there's a chance for
|
|
136
|
+
// a race condition to happen where createInitProviderPromise is called multiple times
|
|
137
|
+
initProvider() {
|
|
138
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
139
|
+
logger.debug('initProvider was called', this.name);
|
|
140
|
+
const { provider } = WalletConnect;
|
|
141
|
+
if (!provider) {
|
|
142
|
+
logger.debug('provider is undefined', this.name);
|
|
143
|
+
if (this.initializePromise === undefined) {
|
|
144
|
+
this.initializePromise = this.createInitProviderPromise();
|
|
145
|
+
}
|
|
146
|
+
yield this.initializePromise;
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
createInitProviderPromise() {
|
|
151
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
152
|
+
WalletConnect.provider = yield this.createProvider();
|
|
153
|
+
this.teardownEventListeners();
|
|
154
|
+
this.setupEventListeners();
|
|
155
|
+
});
|
|
54
156
|
}
|
|
55
157
|
initConnection() {
|
|
56
158
|
return __awaiter(this, void 0, void 0, function* () {
|
|
159
|
+
logger.debug('initConnection was called', this.name);
|
|
57
160
|
const { provider } = WalletConnect;
|
|
58
161
|
if (!provider) {
|
|
59
162
|
throw new DynamicError('No provider found (init connection)');
|
|
@@ -70,6 +173,7 @@ class WalletConnect extends EthWalletConnector {
|
|
|
70
173
|
}
|
|
71
174
|
createProvider() {
|
|
72
175
|
return __awaiter(this, void 0, void 0, function* () {
|
|
176
|
+
logger.debug('createProvider was called', this.name);
|
|
73
177
|
return EthereumProvider.init({
|
|
74
178
|
events: ['chainChanged', 'accountsChanged'],
|
|
75
179
|
methods: [],
|
|
@@ -91,65 +195,49 @@ class WalletConnect extends EthWalletConnector {
|
|
|
91
195
|
});
|
|
92
196
|
});
|
|
93
197
|
}
|
|
94
|
-
|
|
95
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
96
|
-
const walletConnect = this.createProvider();
|
|
97
|
-
const walletClient = createWalletClient({
|
|
98
|
-
account: this.getActiveAccount(),
|
|
99
|
-
transport: custom(yield walletConnect),
|
|
100
|
-
});
|
|
101
|
-
return walletClient;
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
createInitProviderPromise() {
|
|
105
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
106
|
-
WalletConnect.provider = yield this.createProvider();
|
|
107
|
-
this.teardownEventListeners();
|
|
108
|
-
this.setupEventListeners();
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
// We need to add a gate to this method since we will be calling it asynchronously
|
|
112
|
-
// from different places (such as setShowAuthFlow), which means there's a chance for
|
|
113
|
-
// a race condition to happen where createInitProviderPromise is called multiple times
|
|
114
|
-
initProvider() {
|
|
198
|
+
refreshSession() {
|
|
115
199
|
return __awaiter(this, void 0, void 0, function* () {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
200
|
+
var _a, _b, _c, _d, _e;
|
|
201
|
+
logger.debug('refreshSession was called', this.name);
|
|
202
|
+
if ((_b = (_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.session) === null || _b === void 0 ? void 0 : _b.topic) {
|
|
203
|
+
if (localStorage.getItem(this.sessionTopicKey) ===
|
|
204
|
+
((_d = (_c = WalletConnect.provider) === null || _c === void 0 ? void 0 : _c.session) === null || _d === void 0 ? void 0 : _d.topic)) {
|
|
205
|
+
this.session = WalletConnect.provider.session;
|
|
206
|
+
this.setActiveAccount(((_e = localStorage.getItem(this.activeAccountKey)) !== null && _e !== void 0 ? _e : undefined));
|
|
120
207
|
}
|
|
121
|
-
yield this.initializePromise;
|
|
122
208
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
((_d = (_c = WalletConnect.provider) === null || _c === void 0 ? void 0 : _c.session) === null || _d === void 0 ? void 0 : _d.topic)) {
|
|
130
|
-
this.session = WalletConnect.provider.session;
|
|
131
|
-
this.setActiveAccount(((_e = localStorage.getItem(this.activeAccountKey)) !== null && _e !== void 0 ? _e : undefined));
|
|
209
|
+
const walletClient = this.getWalletClient();
|
|
210
|
+
const walletChainId = yield walletClient.getChainId();
|
|
211
|
+
logger.debug('checking selected chain in refreshSession', this.name, walletChainId, this.currentChainId);
|
|
212
|
+
if (this.currentChainId && this.currentChainId !== walletChainId) {
|
|
213
|
+
logger.debug('switching to selected chain', this.currentChainId);
|
|
214
|
+
yield walletClient.switchChain({ id: this.currentChainId });
|
|
132
215
|
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
init() {
|
|
136
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
137
|
-
yield this.initProvider();
|
|
138
|
-
yield this.initConnection();
|
|
139
|
-
this.isInitialized = true;
|
|
140
216
|
});
|
|
141
217
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
return
|
|
218
|
+
getMappedChainsByPreferredOrder() {
|
|
219
|
+
const allChains = this.evmNetworks.map((network) => `eip155:${network.chainId}`);
|
|
220
|
+
const reorderedChains = this.preferredChains.filter((chain) => allChains.includes(chain));
|
|
221
|
+
const remainingChains = allChains.filter((chain) => !this.preferredChains.includes(chain));
|
|
222
|
+
return [...reorderedChains, ...remainingChains].map((chain) => Number(chain.split(':')[1]));
|
|
147
223
|
}
|
|
148
|
-
|
|
149
|
-
|
|
224
|
+
getWalletClient(chainId) {
|
|
225
|
+
logger.debug('getWalletClient was called', this.name);
|
|
226
|
+
const walletConnect = WalletConnect.provider || this.createProvider();
|
|
227
|
+
const walletClient = createWalletClient({
|
|
228
|
+
account: this.getActiveAccount(),
|
|
229
|
+
chain: chainsMap[chainId !== null && chainId !== void 0 ? chainId : String(this.currentChainId)],
|
|
230
|
+
transport: custom({
|
|
231
|
+
request: (args) => __awaiter(this, void 0, void 0, function* () {
|
|
232
|
+
const provider = yield walletConnect;
|
|
233
|
+
return provider.request(args);
|
|
234
|
+
}),
|
|
235
|
+
}),
|
|
236
|
+
});
|
|
237
|
+
return walletClient;
|
|
150
238
|
}
|
|
151
|
-
get
|
|
152
|
-
return
|
|
239
|
+
get currentChainId() {
|
|
240
|
+
return this._currentChainId;
|
|
153
241
|
}
|
|
154
242
|
set currentChainId(value) {
|
|
155
243
|
this._currentChainId = value;
|
|
@@ -160,23 +248,14 @@ class WalletConnect extends EthWalletConnector {
|
|
|
160
248
|
localStorage.removeItem(this.currentChainKey);
|
|
161
249
|
}
|
|
162
250
|
}
|
|
163
|
-
get
|
|
164
|
-
return this.
|
|
165
|
-
}
|
|
166
|
-
set hasSwitchedNetwork(value) {
|
|
167
|
-
this._hasSwitchedNetwork = value;
|
|
168
|
-
if (value) {
|
|
169
|
-
localStorage.setItem(this.swicthedNetworkKey, value.toString());
|
|
170
|
-
}
|
|
171
|
-
else {
|
|
172
|
-
localStorage.removeItem(this.swicthedNetworkKey);
|
|
173
|
-
}
|
|
251
|
+
get sessionTopicKey() {
|
|
252
|
+
return sessionTopicKey(this.key);
|
|
174
253
|
}
|
|
175
|
-
get
|
|
176
|
-
return this.
|
|
254
|
+
get activeAccountKey() {
|
|
255
|
+
return activeAccountKey(this.key);
|
|
177
256
|
}
|
|
178
|
-
|
|
179
|
-
return
|
|
257
|
+
get currentChainKey() {
|
|
258
|
+
return currentChainKey(this.key);
|
|
180
259
|
}
|
|
181
260
|
setupEventListeners() {
|
|
182
261
|
if (!WalletConnect.provider) {
|
|
@@ -201,7 +280,6 @@ class WalletConnect extends EthWalletConnector {
|
|
|
201
280
|
}
|
|
202
281
|
this.currentChainId = chainId;
|
|
203
282
|
this.emit('chainChange', { chain: String(chainId) });
|
|
204
|
-
this.hasSwitchedNetwork = true;
|
|
205
283
|
// When a user switches network from their wallet, we need the provider to change network
|
|
206
284
|
// such that any future calls to `getNetwork` will return the correct network
|
|
207
285
|
this.switchNetwork({ networkChainId: chainId });
|
|
@@ -230,92 +308,6 @@ class WalletConnect extends EthWalletConnector {
|
|
|
230
308
|
WalletConnect.provider.off('session_event', this.sessionEventHandler);
|
|
231
309
|
WalletConnect.provider.off('session_delete', this.sessionDeleteHandler);
|
|
232
310
|
}
|
|
233
|
-
getWalletClient(chainId) {
|
|
234
|
-
if (!WalletConnect.provider) {
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
return createWalletClient({
|
|
238
|
-
account: this.getActiveAccount(),
|
|
239
|
-
chain: chainsMap[chainId !== null && chainId !== void 0 ? chainId : String(this.currentChainId)],
|
|
240
|
-
transport: custom(WalletConnect.provider),
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
getAddress(opts) {
|
|
244
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
245
|
-
var _a, _b;
|
|
246
|
-
const activeAccount = this.getActiveAccount();
|
|
247
|
-
if (activeAccount === null || activeAccount === void 0 ? void 0 : activeAccount.address) {
|
|
248
|
-
return activeAccount.address;
|
|
249
|
-
}
|
|
250
|
-
if (!WalletConnect.provider || !((_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.signer.uri)) {
|
|
251
|
-
logger.debug('No WC2 provider found, re-initializing...');
|
|
252
|
-
yield this.endSession();
|
|
253
|
-
yield this.init();
|
|
254
|
-
// sleep 1 s to wait for connect call to finish
|
|
255
|
-
// the connect call isn't await-ed because it only resolves once
|
|
256
|
-
// the connection is established, but we need to wait for it to
|
|
257
|
-
// finish setting up the connection URI and making it available
|
|
258
|
-
// on the provider
|
|
259
|
-
yield sleep(1000);
|
|
260
|
-
if (!WalletConnect.provider || !((_b = WalletConnect.provider) === null || _b === void 0 ? void 0 : _b.signer.uri)) {
|
|
261
|
-
logger.debug('No WC2 provider found, escaping and throwing error');
|
|
262
|
-
throw new DynamicError('No provider found');
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
const metadata = getWalletBookWallet(this.walletBook, this.key, this.walletFallback);
|
|
266
|
-
performPlatformSpecificConnectionMethod(WalletConnect.provider.signer.uri, metadata, {
|
|
267
|
-
onDesktopUri: opts === null || opts === void 0 ? void 0 : opts.onDesktopUri,
|
|
268
|
-
onDisplayUri: opts === null || opts === void 0 ? void 0 : opts.onDisplayUri,
|
|
269
|
-
}, this.deepLinkPreference);
|
|
270
|
-
return new Promise((resolve, reject) => {
|
|
271
|
-
if (!WalletConnect.provider) {
|
|
272
|
-
reject(new DynamicError('No provider found'));
|
|
273
|
-
return;
|
|
274
|
-
}
|
|
275
|
-
const onFail = () => {
|
|
276
|
-
const error = new DynamicError('Connection rejected. Please try again.');
|
|
277
|
-
error.code = 'connection_rejected';
|
|
278
|
-
if (WalletConnect.provider) {
|
|
279
|
-
WalletConnect.provider.signer.uri = undefined;
|
|
280
|
-
// this is needed for mobile to work when using universal links.
|
|
281
|
-
// if the user cancels the connection, we need to re-initialize the provider
|
|
282
|
-
// so that the async work is done ahead of time, before the user tries to connect again,
|
|
283
|
-
// otherwise they will trigger the iOS bug where they are redirected to the app store
|
|
284
|
-
this.init();
|
|
285
|
-
}
|
|
286
|
-
reject(error);
|
|
287
|
-
// We must clean up the onConnect and onFail listeners
|
|
288
|
-
// whenever the connection attempt either succeeds or fails
|
|
289
|
-
cleanupListeners();
|
|
290
|
-
};
|
|
291
|
-
const onConnect = () => {
|
|
292
|
-
var _a;
|
|
293
|
-
const session = (_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.session;
|
|
294
|
-
if (!session) {
|
|
295
|
-
reject(new DynamicError('No session found'));
|
|
296
|
-
return;
|
|
297
|
-
}
|
|
298
|
-
this.setSession(session);
|
|
299
|
-
this.setWCActiveAccount(session.namespaces.eip155.accounts[0].split(':')[2]);
|
|
300
|
-
this.getNetwork().then((chainId) => {
|
|
301
|
-
var _a;
|
|
302
|
-
this.currentChainId = chainId;
|
|
303
|
-
resolve((_a = this.getActiveAccount()) === null || _a === void 0 ? void 0 : _a.address);
|
|
304
|
-
});
|
|
305
|
-
// We must clean up the onConnect and onFail listeners
|
|
306
|
-
// whenever the connection attempt either succeeds or fails
|
|
307
|
-
cleanupListeners();
|
|
308
|
-
};
|
|
309
|
-
const cleanupListeners = () => {
|
|
310
|
-
var _a;
|
|
311
|
-
ee.off('walletconnect_connection_failed', onFail);
|
|
312
|
-
(_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.off('connect', onConnect);
|
|
313
|
-
};
|
|
314
|
-
ee.on('walletconnect_connection_failed', onFail);
|
|
315
|
-
WalletConnect.provider.on('connect', onConnect);
|
|
316
|
-
});
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
311
|
/**
|
|
320
312
|
* WalletConnect V2 will fail to send the sign message request if the chainId
|
|
321
313
|
* is not the same as the one in the session. This method will wait for the
|
|
@@ -348,9 +340,8 @@ class WalletConnect extends EthWalletConnector {
|
|
|
348
340
|
if (!this.session) {
|
|
349
341
|
return;
|
|
350
342
|
}
|
|
351
|
-
const metadata = getWalletBookWallet(this.walletBook, this.key);
|
|
352
343
|
const deepLink = getDeepLink({
|
|
353
|
-
metadata,
|
|
344
|
+
deepLinks: this.metadata.deepLinks,
|
|
354
345
|
mode: 'regular',
|
|
355
346
|
preference: this.deepLinkPreference,
|
|
356
347
|
uri: (_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.signer.uri,
|
|
@@ -376,7 +367,7 @@ class WalletConnect extends EthWalletConnector {
|
|
|
376
367
|
if (!activeAccount) {
|
|
377
368
|
return;
|
|
378
369
|
}
|
|
379
|
-
const walletClient = yield this.
|
|
370
|
+
const walletClient = yield this.getWalletClient();
|
|
380
371
|
return walletClient.signMessage({
|
|
381
372
|
account: activeAccount,
|
|
382
373
|
message: messageToSign,
|
|
@@ -408,7 +399,6 @@ class WalletConnect extends EthWalletConnector {
|
|
|
408
399
|
var _a;
|
|
409
400
|
this.clearActiveAccount();
|
|
410
401
|
this.clearSession();
|
|
411
|
-
this.hasSwitchedNetwork = false;
|
|
412
402
|
this.currentChainId = undefined;
|
|
413
403
|
if (!((_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.session)) {
|
|
414
404
|
return;
|
|
@@ -454,27 +444,18 @@ class WalletConnect extends EthWalletConnector {
|
|
|
454
444
|
if (this.switchNetworkOnlyFromWallet) {
|
|
455
445
|
throw new DynamicError('Network switching is only supported through the wallet');
|
|
456
446
|
}
|
|
457
|
-
|
|
458
|
-
throw new DynamicError('Network switching not supported');
|
|
459
|
-
}
|
|
460
|
-
const walletClient = yield this.getWalletClientFromInitializedProvider();
|
|
461
|
-
if (this.isMetaMask()) {
|
|
462
|
-
const deepLink = this.getDeepLink();
|
|
463
|
-
if (deepLink) {
|
|
464
|
-
window.location.href = deepLink;
|
|
465
|
-
}
|
|
466
|
-
}
|
|
447
|
+
const walletClient = yield this.getWalletClient();
|
|
467
448
|
yield _super.providerSwitchNetwork.call(this, { network, provider: walletClient });
|
|
468
449
|
this.currentChainId = network.chainId;
|
|
469
|
-
this.hasSwitchedNetwork = true;
|
|
470
450
|
this.emit('chainChange', { chain: String(network.chainId) });
|
|
471
451
|
});
|
|
472
452
|
}
|
|
473
453
|
getConnectedAccounts() {
|
|
474
454
|
return __awaiter(this, void 0, void 0, function* () {
|
|
475
455
|
if (this.isInitialized === false) {
|
|
456
|
+
// TODO why not just call init()?
|
|
476
457
|
yield this.initProvider();
|
|
477
|
-
this.refreshSession();
|
|
458
|
+
yield this.refreshSession();
|
|
478
459
|
this.isInitialized = true;
|
|
479
460
|
}
|
|
480
461
|
const activeAccount = this.getActiveAccount();
|
|
@@ -484,21 +465,11 @@ class WalletConnect extends EthWalletConnector {
|
|
|
484
465
|
return [activeAccount.address];
|
|
485
466
|
});
|
|
486
467
|
}
|
|
487
|
-
isMetaMask() {
|
|
488
|
-
var _a, _b, _c, _d, _e;
|
|
489
|
-
return ((_e = (_d = (_c = (_b = (_a = this.session) === null || _a === void 0 ? void 0 : _a.peer) === null || _b === void 0 ? void 0 : _b.metadata) === null || _c === void 0 ? void 0 : _c.name) === null || _d === void 0 ? void 0 : _d.toLowerCase().startsWith('metamask')) !== null && _e !== void 0 ? _e : false);
|
|
490
|
-
}
|
|
491
468
|
getSupportedNetworks() {
|
|
492
469
|
return __awaiter(this, void 0, void 0, function* () {
|
|
493
470
|
var _a;
|
|
494
471
|
yield this.initProvider();
|
|
495
|
-
this.refreshSession();
|
|
496
|
-
if (this.isMetaMask()) {
|
|
497
|
-
if (this.hasSwitchedNetwork) {
|
|
498
|
-
return [String(this.currentChainId)];
|
|
499
|
-
}
|
|
500
|
-
return this.evmNetworks.map((network) => network.chainId.toString());
|
|
501
|
-
}
|
|
472
|
+
yield this.refreshSession();
|
|
502
473
|
if (!this.session) {
|
|
503
474
|
return [];
|
|
504
475
|
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
'use client'
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
-
|
|
6
|
-
var walletBook = require('@dynamic-labs/wallet-book');
|
|
7
|
-
var InjectedWalletBase = require('./InjectedWalletBase.cjs');
|
|
8
|
-
|
|
9
|
-
class UnknownInjected extends InjectedWalletBase {
|
|
10
|
-
constructor(props) {
|
|
11
|
-
super(props);
|
|
12
|
-
this.name = 'Unknown';
|
|
13
|
-
this.overrideKey = 'unknown';
|
|
14
|
-
this.wallet = walletBook.findWalletBookWallet(this.walletBook, this.key);
|
|
15
|
-
}
|
|
16
|
-
isInstalledOnBrowser() {
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
exports.UnknownInjected = UnknownInjected;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { EthWalletConnectorOpts } from '@dynamic-labs/ethereum-core';
|
|
2
|
-
import InjectedWalletBase from './InjectedWalletBase';
|
|
3
|
-
export declare class UnknownInjected extends InjectedWalletBase {
|
|
4
|
-
name: string;
|
|
5
|
-
overrideKey: string;
|
|
6
|
-
constructor(props: EthWalletConnectorOpts);
|
|
7
|
-
isInstalledOnBrowser(): boolean;
|
|
8
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
'use client'
|
|
2
|
-
import { findWalletBookWallet } from '@dynamic-labs/wallet-book';
|
|
3
|
-
import InjectedWalletBase from './InjectedWalletBase.js';
|
|
4
|
-
|
|
5
|
-
class UnknownInjected extends InjectedWalletBase {
|
|
6
|
-
constructor(props) {
|
|
7
|
-
super(props);
|
|
8
|
-
this.name = 'Unknown';
|
|
9
|
-
this.overrideKey = 'unknown';
|
|
10
|
-
this.wallet = findWalletBookWallet(this.walletBook, this.key);
|
|
11
|
-
}
|
|
12
|
-
isInstalledOnBrowser() {
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export { UnknownInjected };
|
package/src/utils/isString.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const isString: (value: unknown) => value is string;
|
package/src/utils/last.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const last: <T = unknown>(array: T[]) => T | undefined;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
'use client'
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
-
|
|
6
|
-
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
7
|
-
|
|
8
|
-
const parseIntSafe = (value, radix = 10) => {
|
|
9
|
-
try {
|
|
10
|
-
const int = parseInt(String(value), radix);
|
|
11
|
-
if (isNaN(int)) {
|
|
12
|
-
walletConnectorCore.logger.error(`Error parsing ${value}`);
|
|
13
|
-
return undefined;
|
|
14
|
-
}
|
|
15
|
-
return int;
|
|
16
|
-
}
|
|
17
|
-
catch (e) {
|
|
18
|
-
walletConnectorCore.logger.error(`Error parsing ${value} to int: ${e}`);
|
|
19
|
-
}
|
|
20
|
-
return undefined;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
exports.parseIntSafe = parseIntSafe;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const parseIntSafe: (value: string | number, radix?: number) => number | undefined;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
'use client'
|
|
2
|
-
import { logger } from '@dynamic-labs/wallet-connector-core';
|
|
3
|
-
|
|
4
|
-
const parseIntSafe = (value, radix = 10) => {
|
|
5
|
-
try {
|
|
6
|
-
const int = parseInt(String(value), radix);
|
|
7
|
-
if (isNaN(int)) {
|
|
8
|
-
logger.error(`Error parsing ${value}`);
|
|
9
|
-
return undefined;
|
|
10
|
-
}
|
|
11
|
-
return int;
|
|
12
|
-
}
|
|
13
|
-
catch (e) {
|
|
14
|
-
logger.error(`Error parsing ${value} to int: ${e}`);
|
|
15
|
-
}
|
|
16
|
-
return undefined;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export { parseIntSafe };
|