@dynamic-labs/ethereum 4.9.0 → 4.9.1-preview.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 +8 -0
- package/package.cjs +1 -1
- package/package.js +1 -1
- package/package.json +9 -10
- package/src/EthereumWalletConnectors.cjs +2 -1
- package/src/EthereumWalletConnectors.js +2 -1
- package/src/injected/InjectedWalletBase.cjs +2 -2
- package/src/injected/InjectedWalletBase.js +2 -2
- package/src/metaMask/MetaMaskConnector.cjs +3 -0
- package/src/metaMask/MetaMaskConnector.d.ts +1 -0
- package/src/metaMask/MetaMaskConnector.js +3 -0
- package/src/walletConnect/WalletConnectConnector/WalletConnectConnector.cjs +289 -0
- package/src/walletConnect/{walletConnect.d.ts → WalletConnectConnector/WalletConnectConnector.d.ts} +14 -53
- package/src/walletConnect/WalletConnectConnector/WalletConnectConnector.js +285 -0
- package/src/walletConnect/WalletConnectConnector/index.d.ts +1 -0
- package/src/walletConnect/WalletConnectProvider/WalletConnectProvider.cjs +197 -0
- package/src/walletConnect/WalletConnectProvider/WalletConnectProvider.d.ts +64 -0
- package/src/walletConnect/WalletConnectProvider/WalletConnectProvider.js +189 -0
- package/src/walletConnect/WalletConnectProvider/index.d.ts +1 -0
- package/src/walletConnect/index.d.ts +1 -1
- package/src/walletConnect/utils/fetchWalletConnectWallets.cjs +2 -2
- package/src/walletConnect/utils/fetchWalletConnectWallets.js +2 -2
- package/src/walletConnect/utils/getWalletConnectConnector.cjs +2 -2
- package/src/walletConnect/utils/getWalletConnectConnector.js +2 -2
- package/src/walletConnect/walletConnect.cjs +0 -504
- package/src/walletConnect/walletConnect.js +0 -495
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../../_virtual/_tslib.js';
|
|
3
|
+
import { createWalletClient, custom } from 'viem';
|
|
4
|
+
import { toAccount } from 'viem/accounts';
|
|
5
|
+
import { EthereumWalletConnector, chainsMap } from '@dynamic-labs/ethereum-core';
|
|
6
|
+
import { parseIntSafe, DynamicError, isMobile, PlatformService } from '@dynamic-labs/utils';
|
|
7
|
+
import { logger, getDeepLink } from '@dynamic-labs/wallet-connector-core';
|
|
8
|
+
import { normalizeRpcError } from '../../utils/normalizeRpcError/normalizeRpcError.js';
|
|
9
|
+
import { WalletConnectProvider } from '../WalletConnectProvider/WalletConnectProvider.js';
|
|
10
|
+
|
|
11
|
+
const WC_CURRENT_CHAIN_KEY = 'dynamic-wc2-current-chain';
|
|
12
|
+
class WalletConnectConnector extends EthereumWalletConnector {
|
|
13
|
+
constructor(opts) {
|
|
14
|
+
super(opts);
|
|
15
|
+
this.canConnectViaQrCode = true;
|
|
16
|
+
this.isWalletConnect = true;
|
|
17
|
+
this.name = opts.walletName;
|
|
18
|
+
this.deepLinkPreference = opts.deepLinkPreference || 'native';
|
|
19
|
+
const storedChainId = localStorage.getItem(WC_CURRENT_CHAIN_KEY);
|
|
20
|
+
if (storedChainId) {
|
|
21
|
+
this.currentChainId = parseIntSafe(storedChainId);
|
|
22
|
+
}
|
|
23
|
+
if (!opts.projectId) {
|
|
24
|
+
throw new DynamicError('WalletConnect project ID is required');
|
|
25
|
+
}
|
|
26
|
+
// set provider props generic to all wallets
|
|
27
|
+
WalletConnectProvider.projectId = opts.projectId;
|
|
28
|
+
WalletConnectProvider.enabledNetworks = opts.evmNetworks;
|
|
29
|
+
WalletConnectProvider.preferredChains =
|
|
30
|
+
opts.walletConnectPreferredChains || [];
|
|
31
|
+
WalletConnectProvider.evmNetworkRpcMap = this.evmNetworkRpcMap();
|
|
32
|
+
}
|
|
33
|
+
init() {
|
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
logger.logVerboseTroubleshootingMessage('[WalletConnect] init called', {
|
|
36
|
+
isInitialized: WalletConnectProvider.isInitialized,
|
|
37
|
+
});
|
|
38
|
+
// we should only init the provider once as soon as possible
|
|
39
|
+
// the connection is established when a wallet is selected (with getAddress)
|
|
40
|
+
if (WalletConnectProvider.isInitialized) {
|
|
41
|
+
logger.debug('[WalletConnect] init - already initialized - skipping');
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
logger.debug('[WalletConnect] init');
|
|
45
|
+
this.walletConnectorEventsEmitter.emit('connectorInitStarted', 'walletconnect');
|
|
46
|
+
yield WalletConnectProvider.init();
|
|
47
|
+
const provider = WalletConnectProvider.getProvider();
|
|
48
|
+
if (!provider) {
|
|
49
|
+
throw new DynamicError('WalletConnectProvider is not initialized');
|
|
50
|
+
}
|
|
51
|
+
this.setupWCEventListeners();
|
|
52
|
+
this.walletConnectorEventsEmitter.emit('connectorInitCompleted', 'walletconnect');
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
setupWCEventListeners() {
|
|
56
|
+
logger.debug('[WalletConnect] setupWCEventListeners');
|
|
57
|
+
WalletConnectProvider.teardownEventListeners();
|
|
58
|
+
WalletConnectProvider.setupEventListeners({
|
|
59
|
+
onAccountChanged: (account) => {
|
|
60
|
+
logger.debug('[WalletConnect] onAccountChanged', { account });
|
|
61
|
+
this.emit('accountChange', { accounts: [account] });
|
|
62
|
+
},
|
|
63
|
+
onChainChanged: (chainId) => {
|
|
64
|
+
logger.debug('[WalletConnect] onChainChange', { chainId });
|
|
65
|
+
if (chainId === this.currentChainId) {
|
|
66
|
+
logger.debug(`[WalletConnect] onChainChange - ignoring chainChanged event with same chain id as current chain id: ${chainId}`);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
this.currentChainId = chainId;
|
|
70
|
+
this.emit('chainChange', { chain: String(chainId) });
|
|
71
|
+
},
|
|
72
|
+
onDisconnect: () => {
|
|
73
|
+
logger.debug('[WalletConnect] onDisconnect');
|
|
74
|
+
this.endSession();
|
|
75
|
+
this.emit('disconnect');
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
endSession() {
|
|
80
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
+
logger.debug('[WalletConnect] endSession');
|
|
82
|
+
this.currentChainId = undefined;
|
|
83
|
+
yield WalletConnectProvider.disconnect();
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
getAddress(opts) {
|
|
87
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
88
|
+
logger.debug('[WalletConnect] getAddress', opts);
|
|
89
|
+
if (!WalletConnectProvider.isInitialized) {
|
|
90
|
+
logger.debug('[WalletConnect] getAddress - WalletConnectProvider is not initialized');
|
|
91
|
+
throw new DynamicError('WalletConnectProvider is not initialized');
|
|
92
|
+
}
|
|
93
|
+
logger.debug('[WalletConnect] getAddress - connecting to WalletConnect');
|
|
94
|
+
const addresses = yield WalletConnectProvider.connect({
|
|
95
|
+
connectionOpts: opts,
|
|
96
|
+
deepLinkPreference: this.deepLinkPreference,
|
|
97
|
+
deepLinks: this.metadata.deepLinks,
|
|
98
|
+
});
|
|
99
|
+
logger.debug('[WalletConnect] getAddress - connection result', addresses);
|
|
100
|
+
const address = addresses === null || addresses === void 0 ? void 0 : addresses[0];
|
|
101
|
+
return address;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
getWalletClient(chainId) {
|
|
105
|
+
logger.logVerboseTroubleshootingMessage('[WalletConnect] getWalletClient was called - chainId', chainId);
|
|
106
|
+
const provider = WalletConnectProvider.getProvider();
|
|
107
|
+
if (!provider) {
|
|
108
|
+
logger.debug('[WalletConnect] getWalletClient - provider is not initialized');
|
|
109
|
+
throw new DynamicError('WalletConnectProvider is not initialized');
|
|
110
|
+
}
|
|
111
|
+
const walletClient = createWalletClient({
|
|
112
|
+
account: this.getActiveAccount(),
|
|
113
|
+
chain: chainsMap[chainId !== null && chainId !== void 0 ? chainId : String(this.currentChainId)],
|
|
114
|
+
transport: custom({
|
|
115
|
+
request: (args) => {
|
|
116
|
+
this.deepLinkIfApplicable(args.method);
|
|
117
|
+
return provider.request(args).catch(normalizeRpcError);
|
|
118
|
+
},
|
|
119
|
+
}),
|
|
120
|
+
});
|
|
121
|
+
return walletClient;
|
|
122
|
+
}
|
|
123
|
+
deepLinkIfApplicable(method) {
|
|
124
|
+
const methodsThatRequireDeepLink = [
|
|
125
|
+
'personal_sign',
|
|
126
|
+
'eth_sendTransaction',
|
|
127
|
+
'eth_signTypedData_v4',
|
|
128
|
+
];
|
|
129
|
+
const deepLink = this.getDeepLink();
|
|
130
|
+
if (isMobile() && deepLink && methodsThatRequireDeepLink.includes(method)) {
|
|
131
|
+
PlatformService.openURL(deepLink);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
signMessage(messageToSign) {
|
|
135
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
136
|
+
logger.logVerboseTroubleshootingMessage('[WalletConnect] signMessage', messageToSign);
|
|
137
|
+
const activeAccount = this.getActiveAccount();
|
|
138
|
+
logger.logVerboseTroubleshootingMessage('[WalletConnect] signMessage - activeAccount', activeAccount);
|
|
139
|
+
if (!activeAccount) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const walletClient = yield this.getWalletClient();
|
|
143
|
+
return walletClient.signMessage({
|
|
144
|
+
account: activeAccount,
|
|
145
|
+
message: messageToSign,
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
getConnectedAccounts() {
|
|
150
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
151
|
+
const activeAccount = this.getActiveAccount();
|
|
152
|
+
logger.logVerboseTroubleshootingMessage('[WalletConnect] getConnectedAccounts - activeAccount', activeAccount);
|
|
153
|
+
return activeAccount ? [activeAccount.address] : [];
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
getActiveAccount() {
|
|
157
|
+
var _a;
|
|
158
|
+
const provider = WalletConnectProvider.getProvider();
|
|
159
|
+
const connectedAccount = (_a = provider === null || provider === void 0 ? void 0 : provider.accounts) === null || _a === void 0 ? void 0 : _a[0];
|
|
160
|
+
logger.logVerboseTroubleshootingMessage('[WalletConnect] getActiveAccount - connectedAccount', connectedAccount);
|
|
161
|
+
if (!connectedAccount) {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
return toAccount(connectedAccount);
|
|
165
|
+
}
|
|
166
|
+
get currentChainId() {
|
|
167
|
+
const lsCurrentChain = localStorage.getItem(WC_CURRENT_CHAIN_KEY);
|
|
168
|
+
try {
|
|
169
|
+
return lsCurrentChain ? parseIntSafe(lsCurrentChain) : undefined;
|
|
170
|
+
}
|
|
171
|
+
catch (e) {
|
|
172
|
+
logger.debug('[WalletConnect] getCurrentChainId - error', e);
|
|
173
|
+
return undefined;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
set currentChainId(value) {
|
|
177
|
+
if (value) {
|
|
178
|
+
localStorage.setItem(WC_CURRENT_CHAIN_KEY, value.toString());
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
localStorage.removeItem(WC_CURRENT_CHAIN_KEY);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
getActiveChain() {
|
|
185
|
+
if (!this.currentChainId) {
|
|
186
|
+
return undefined;
|
|
187
|
+
}
|
|
188
|
+
return chainsMap[this.currentChainId];
|
|
189
|
+
}
|
|
190
|
+
getNetwork() {
|
|
191
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
192
|
+
logger.logVerboseTroubleshootingMessage('[WalletConnect] getNetwork');
|
|
193
|
+
const provider = WalletConnectProvider.getProvider();
|
|
194
|
+
if (provider === null || provider === void 0 ? void 0 : provider.chainId) {
|
|
195
|
+
const network = provider.chainId;
|
|
196
|
+
this.currentChainId = network;
|
|
197
|
+
logger.logVerboseTroubleshootingMessage('[WalletConnect] getNetwork - provider network', network);
|
|
198
|
+
return network;
|
|
199
|
+
}
|
|
200
|
+
logger.logVerboseTroubleshootingMessage('[WalletConnect] getNetwork - no provider found, returning current chain id', {
|
|
201
|
+
currentChainId: this.currentChainId,
|
|
202
|
+
});
|
|
203
|
+
return this.currentChainId;
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
providerSwitchNetwork(_a) {
|
|
207
|
+
const _super = Object.create(null, {
|
|
208
|
+
providerSwitchNetwork: { get: () => super.providerSwitchNetwork }
|
|
209
|
+
});
|
|
210
|
+
return __awaiter(this, arguments, void 0, function* ({ network, }) {
|
|
211
|
+
logger.logVerboseTroubleshootingMessage('[WalletConnect] providerSwitchNetwork - network', {
|
|
212
|
+
network,
|
|
213
|
+
switchNetworkOnlyFromWallet: this.switchNetworkOnlyFromWallet,
|
|
214
|
+
});
|
|
215
|
+
const currentNetworkId = yield this.getNetwork();
|
|
216
|
+
logger.logVerboseTroubleshootingMessage('[WalletConnect] providerSwitchNetwork - currentNetworkId', currentNetworkId);
|
|
217
|
+
if (currentNetworkId && currentNetworkId === network.chainId) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (this.switchNetworkOnlyFromWallet) {
|
|
221
|
+
throw new DynamicError('Network switching is only supported through the wallet');
|
|
222
|
+
}
|
|
223
|
+
const walletClient = yield this.getWalletClient();
|
|
224
|
+
logger.logVerboseTroubleshootingMessage('[WalletConnect] providerSwitchNetwork - will switch network');
|
|
225
|
+
yield _super.providerSwitchNetwork.call(this, { network, provider: walletClient });
|
|
226
|
+
this.currentChainId = network.chainId;
|
|
227
|
+
logger.logVerboseTroubleshootingMessage('[WalletConnect] providerSwitchNetwork - switched network', network.chainId);
|
|
228
|
+
this.emit('chainChange', { chain: String(network.chainId) });
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
supportsNetworkSwitching() {
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
getSupportedNetworks() {
|
|
235
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
236
|
+
var _a;
|
|
237
|
+
const provider = WalletConnectProvider.getProvider();
|
|
238
|
+
if (!(provider === null || provider === void 0 ? void 0 : provider.session)) {
|
|
239
|
+
return [];
|
|
240
|
+
}
|
|
241
|
+
const chains = [];
|
|
242
|
+
// Some wallet (i.e ZenGo) use namespaces.account to list supported chains
|
|
243
|
+
// while others use keys within the namespaces object
|
|
244
|
+
Object.keys(provider === null || provider === void 0 ? void 0 : provider.session.namespaces).forEach((key) => {
|
|
245
|
+
if (key.startsWith('eip155:')) {
|
|
246
|
+
chains.push(key.split(':')[1]);
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
(_a = provider === null || provider === void 0 ? void 0 : provider.session.namespaces.eip155) === null || _a === void 0 ? void 0 : _a.accounts.forEach((account) => chains.push(account.split(':')[1]));
|
|
250
|
+
return chains.length
|
|
251
|
+
? chains
|
|
252
|
+
: this.evmNetworks.map((network) => network.chainId.toString());
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
getDeepLink() {
|
|
256
|
+
var _a;
|
|
257
|
+
const provider = WalletConnectProvider.getProvider();
|
|
258
|
+
logger.debug('[WalletConnect] getDeepLink', {
|
|
259
|
+
hasSession: Boolean(provider === null || provider === void 0 ? void 0 : provider.session),
|
|
260
|
+
topic: (_a = provider === null || provider === void 0 ? void 0 : provider.session) === null || _a === void 0 ? void 0 : _a.topic,
|
|
261
|
+
uri: provider === null || provider === void 0 ? void 0 : provider.signer.uri,
|
|
262
|
+
});
|
|
263
|
+
if (!(provider === null || provider === void 0 ? void 0 : provider.session)) {
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
const deepLink = getDeepLink({
|
|
267
|
+
deepLinks: this.metadata.deepLinks,
|
|
268
|
+
mode: 'regular',
|
|
269
|
+
preference: this.deepLinkPreference,
|
|
270
|
+
uri: provider.signer.uri,
|
|
271
|
+
});
|
|
272
|
+
logger.logVerboseTroubleshootingMessage('[WalletConnect] getDeepLink - deepLink', deepLink);
|
|
273
|
+
if (!deepLink) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
// we need to include the session topic here because it helps the wallet
|
|
277
|
+
// auto redirect back to the dapp after signing
|
|
278
|
+
return `${deepLink}?sessionTopic=${provider.session.topic}`;
|
|
279
|
+
}
|
|
280
|
+
getConnectionUri() {
|
|
281
|
+
return WalletConnectProvider.getConnectionUri();
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export { WalletConnectConnector };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { WalletConnectConnector } from './WalletConnectConnector';
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../../_virtual/_tslib.cjs');
|
|
7
|
+
var EthereumProvider = require('@walletconnect/ethereum-provider');
|
|
8
|
+
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
9
|
+
var utils = require('@dynamic-labs/utils');
|
|
10
|
+
var logger = require('../../utils/logger.cjs');
|
|
11
|
+
|
|
12
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
13
|
+
|
|
14
|
+
var EthereumProvider__default = /*#__PURE__*/_interopDefaultLegacy(EthereumProvider);
|
|
15
|
+
|
|
16
|
+
var _a;
|
|
17
|
+
class WalletConnectProvider {
|
|
18
|
+
constructor() {
|
|
19
|
+
throw new Error('WalletConnectProvider is not instantiable');
|
|
20
|
+
}
|
|
21
|
+
static getMappedChainsByPreferredOrder() {
|
|
22
|
+
const allChains = _a.enabledNetworks.map((network) => `eip155:${network.chainId}`);
|
|
23
|
+
const reorderedChains = _a.preferredChains.filter((chain) => allChains.includes(chain));
|
|
24
|
+
const remainingChains = allChains.filter((chain) => !_a.preferredChains.includes(chain));
|
|
25
|
+
return [...reorderedChains, ...remainingChains].map((chain) => Number(chain.split(':')[1]));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
_a = WalletConnectProvider;
|
|
29
|
+
WalletConnectProvider.isInitialized = false;
|
|
30
|
+
WalletConnectProvider.enabledNetworks = [];
|
|
31
|
+
WalletConnectProvider.preferredChains = [];
|
|
32
|
+
WalletConnectProvider.evmNetworkRpcMap = {};
|
|
33
|
+
WalletConnectProvider.eventListenersSetup = false;
|
|
34
|
+
WalletConnectProvider.accountChangedHandler = () => { };
|
|
35
|
+
WalletConnectProvider.chainChangedHandler = () => { };
|
|
36
|
+
WalletConnectProvider.disconnectHandler = () => { };
|
|
37
|
+
/**
|
|
38
|
+
* Initializes the provider. This method should only be called once.
|
|
39
|
+
* Does not start a connection.
|
|
40
|
+
*/
|
|
41
|
+
WalletConnectProvider.init = (...args_1) => _tslib.__awaiter(void 0, [...args_1], void 0, function* ({ storePrefix = 'dynamic-wc2', } = {}) {
|
|
42
|
+
logger.logger.debug('[WalletConnectProvider] init', {
|
|
43
|
+
isInitialized: _a.isInitialized,
|
|
44
|
+
});
|
|
45
|
+
if (_a.isInitialized) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
_a.isInitialized = true;
|
|
49
|
+
logger.logger.debug('[WalletConnectProvider] initializing');
|
|
50
|
+
_a.provider = yield EthereumProvider__default["default"].init({
|
|
51
|
+
customStoragePrefix: storePrefix,
|
|
52
|
+
disableProviderPing: true,
|
|
53
|
+
optionalChains: _a.getMappedChainsByPreferredOrder(),
|
|
54
|
+
optionalEvents: ['chainChanged', 'accountsChanged'],
|
|
55
|
+
optionalMethods: [
|
|
56
|
+
'eth_chainId',
|
|
57
|
+
'eth_signTypedData',
|
|
58
|
+
'eth_signTransaction',
|
|
59
|
+
'eth_sign',
|
|
60
|
+
'personal_sign',
|
|
61
|
+
'eth_sendTransaction',
|
|
62
|
+
'eth_signTypedData_v4',
|
|
63
|
+
'wallet_switchEthereumChain',
|
|
64
|
+
'wallet_addEthereumChain',
|
|
65
|
+
],
|
|
66
|
+
projectId: _a.projectId,
|
|
67
|
+
rpcMap: _a.evmNetworkRpcMap,
|
|
68
|
+
showQrModal: false,
|
|
69
|
+
});
|
|
70
|
+
logger.logger.debug('[WalletConnectProvider] initialized');
|
|
71
|
+
});
|
|
72
|
+
/**
|
|
73
|
+
* Connects to a wallet. This method should be called whenever a new wallet connection is needed.
|
|
74
|
+
* If the wallet is already connected when the page is refreshed, this method does not need to be called.
|
|
75
|
+
*/
|
|
76
|
+
WalletConnectProvider.connect = (_b) => _tslib.__awaiter(void 0, [_b], void 0, function* ({ deepLinks, deepLinkPreference, connectionOpts, }) {
|
|
77
|
+
const handleDisplayURI = (uri) => {
|
|
78
|
+
var _b;
|
|
79
|
+
logger.logger.debug('[WalletConnectProvider] handleDisplayURI', uri);
|
|
80
|
+
_a.connectionUri = uri;
|
|
81
|
+
walletConnectorCore.performPlatformSpecificConnectionMethod(_a.connectionUri, deepLinks, {
|
|
82
|
+
onDesktopUri: connectionOpts === null || connectionOpts === void 0 ? void 0 : connectionOpts.onDesktopUri,
|
|
83
|
+
onDisplayUri: connectionOpts === null || connectionOpts === void 0 ? void 0 : connectionOpts.onDisplayUri,
|
|
84
|
+
}, deepLinkPreference);
|
|
85
|
+
logger.logger.debug('[WalletConnectProvider] removing display_uri event listener');
|
|
86
|
+
(_b = _a.provider) === null || _b === void 0 ? void 0 : _b.off('display_uri', handleDisplayURI);
|
|
87
|
+
};
|
|
88
|
+
if (!_a.provider) {
|
|
89
|
+
throw new utils.DynamicError('WalletConnectProvider is not initialized');
|
|
90
|
+
}
|
|
91
|
+
// this is in case the user just cancels the deeplink prompt (i.e. in mobile/Safari)
|
|
92
|
+
// in this case, the connection is not rejected, so the "enable" promise is just pending
|
|
93
|
+
// so on retry, we should just use the same uri to handle that promise
|
|
94
|
+
if (_a.connectionUri) {
|
|
95
|
+
handleDisplayURI(_a.connectionUri);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
logger.logger.debug('[WalletConnectProvider] adding display_uri event listener');
|
|
99
|
+
_a.provider.on('display_uri', handleDisplayURI);
|
|
100
|
+
try {
|
|
101
|
+
const result = yield _a.provider.enable();
|
|
102
|
+
logger.logger.debug('[WalletConnectProvider] connected to WalletConnect', result);
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
logger.logger.error('[WalletConnectProvider] Failed to connect to WalletConnect', error);
|
|
107
|
+
const customError = new utils.DynamicError('Connection rejected. Please try again.');
|
|
108
|
+
customError.code = 'connection_rejected';
|
|
109
|
+
throw customError;
|
|
110
|
+
}
|
|
111
|
+
finally {
|
|
112
|
+
// Reset the connection URI after it's been consumed
|
|
113
|
+
_a.connectionUri = undefined;
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
/**
|
|
117
|
+
* Disconnects from a wallet. This method should be called whenever we need to disconnect from a wallet.
|
|
118
|
+
* It will kill the connection, but not the provider.
|
|
119
|
+
*/
|
|
120
|
+
WalletConnectProvider.disconnect = () => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
121
|
+
if (!_a.provider) {
|
|
122
|
+
logger.logger.debug('[WalletConnectProvider] disconnect - provider is not initialized');
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
_a.connectionUri = undefined;
|
|
126
|
+
logger.logger.debug('[WalletConnectProvider] disconnecting from WalletConnect');
|
|
127
|
+
try {
|
|
128
|
+
yield _a.provider.disconnect();
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
logger.logger.error('[WalletConnectProvider] Failed to disconnect from WalletConnect', error);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
/**
|
|
135
|
+
* Returns the EthereumProvider instance.
|
|
136
|
+
*/
|
|
137
|
+
WalletConnectProvider.getProvider = () => _a.provider;
|
|
138
|
+
WalletConnectProvider.getConnectionUri = () => _a.connectionUri;
|
|
139
|
+
WalletConnectProvider.handleChainChangedEvent = (chain, onChainChanged) => {
|
|
140
|
+
logger.logger.debug('[WalletConnectProvider] handling chain change event', {
|
|
141
|
+
chain,
|
|
142
|
+
});
|
|
143
|
+
const chainId = utils.parseIntSafe(chain);
|
|
144
|
+
if (!chainId) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
onChainChanged === null || onChainChanged === void 0 ? void 0 : onChainChanged(chainId);
|
|
148
|
+
};
|
|
149
|
+
WalletConnectProvider.handleAccountChangedEvent = (accounts, onAccountChanged) => {
|
|
150
|
+
logger.logger.debug('[WalletConnectProvider] handling account change event', {
|
|
151
|
+
accounts,
|
|
152
|
+
});
|
|
153
|
+
const [account] = accounts;
|
|
154
|
+
const address = account.includes(':') ? account.split(':').pop() : account;
|
|
155
|
+
if (!address) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
onAccountChanged === null || onAccountChanged === void 0 ? void 0 : onAccountChanged(address);
|
|
159
|
+
};
|
|
160
|
+
/**
|
|
161
|
+
* Sets up event listeners for the provider.
|
|
162
|
+
*/
|
|
163
|
+
WalletConnectProvider.setupEventListeners = ({ onChainChanged, onAccountChanged, onDisconnect, }) => {
|
|
164
|
+
if (!_a.provider ||
|
|
165
|
+
_a.eventListenersSetup) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
_a.chainChangedHandler = (chainId) => {
|
|
169
|
+
_a.handleChainChangedEvent(chainId, onChainChanged);
|
|
170
|
+
};
|
|
171
|
+
_a.accountChangedHandler = (account) => {
|
|
172
|
+
_a.handleAccountChangedEvent(account, onAccountChanged);
|
|
173
|
+
};
|
|
174
|
+
_a.disconnectHandler = () => {
|
|
175
|
+
logger.logger.debug('[WalletConnectProvider] handling disconnect event');
|
|
176
|
+
onDisconnect === null || onDisconnect === void 0 ? void 0 : onDisconnect();
|
|
177
|
+
};
|
|
178
|
+
_a.provider.on('accountsChanged', _a.accountChangedHandler);
|
|
179
|
+
_a.provider.on('chainChanged', _a.chainChangedHandler);
|
|
180
|
+
_a.provider.on('disconnect', _a.disconnectHandler);
|
|
181
|
+
_a.eventListenersSetup = true;
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* Tears down event listeners for the provider.
|
|
185
|
+
*/
|
|
186
|
+
WalletConnectProvider.teardownEventListeners = () => {
|
|
187
|
+
if (!_a.provider ||
|
|
188
|
+
!_a.eventListenersSetup) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
_a.provider.off('accountsChanged', _a.accountChangedHandler);
|
|
192
|
+
_a.provider.off('chainChanged', _a.chainChangedHandler);
|
|
193
|
+
_a.provider.off('disconnect', _a.disconnectHandler);
|
|
194
|
+
_a.eventListenersSetup = false;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
exports.WalletConnectProvider = WalletConnectProvider;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import EthereumProvider from '@walletconnect/ethereum-provider';
|
|
2
|
+
import { ProviderAccounts } from 'node_modules/@walletconnect/ethereum-provider/dist/types/types';
|
|
3
|
+
import { DeepLinkVariant, GetAddressOpts, WalletDeepLinks } from '@dynamic-labs/wallet-connector-core';
|
|
4
|
+
import { GenericNetwork } from '@dynamic-labs/types';
|
|
5
|
+
type ProviderInitOpts = {
|
|
6
|
+
storePrefix?: string;
|
|
7
|
+
};
|
|
8
|
+
export declare class WalletConnectProvider {
|
|
9
|
+
static isInitialized: boolean;
|
|
10
|
+
static projectId: string;
|
|
11
|
+
static enabledNetworks: GenericNetwork[];
|
|
12
|
+
static preferredChains: `eip155:${number}`[];
|
|
13
|
+
static evmNetworkRpcMap: Record<string, string>;
|
|
14
|
+
private static provider;
|
|
15
|
+
/**
|
|
16
|
+
* The connection URI for the current connection.
|
|
17
|
+
*/
|
|
18
|
+
private static connectionUri;
|
|
19
|
+
private static eventListenersSetup;
|
|
20
|
+
private static accountChangedHandler;
|
|
21
|
+
private static chainChangedHandler;
|
|
22
|
+
private static disconnectHandler;
|
|
23
|
+
private constructor();
|
|
24
|
+
/**
|
|
25
|
+
* Initializes the provider. This method should only be called once.
|
|
26
|
+
* Does not start a connection.
|
|
27
|
+
*/
|
|
28
|
+
static init: ({ storePrefix, }?: ProviderInitOpts) => Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Connects to a wallet. This method should be called whenever a new wallet connection is needed.
|
|
31
|
+
* If the wallet is already connected when the page is refreshed, this method does not need to be called.
|
|
32
|
+
*/
|
|
33
|
+
static connect: ({ deepLinks, deepLinkPreference, connectionOpts, }: {
|
|
34
|
+
deepLinks?: WalletDeepLinks;
|
|
35
|
+
deepLinkPreference: DeepLinkVariant;
|
|
36
|
+
connectionOpts?: GetAddressOpts;
|
|
37
|
+
}) => Promise<ProviderAccounts | undefined>;
|
|
38
|
+
/**
|
|
39
|
+
* Disconnects from a wallet. This method should be called whenever we need to disconnect from a wallet.
|
|
40
|
+
* It will kill the connection, but not the provider.
|
|
41
|
+
*/
|
|
42
|
+
static disconnect: () => Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Returns the EthereumProvider instance.
|
|
45
|
+
*/
|
|
46
|
+
static getProvider: () => EthereumProvider | undefined;
|
|
47
|
+
static getConnectionUri: () => string | undefined;
|
|
48
|
+
private static handleChainChangedEvent;
|
|
49
|
+
private static handleAccountChangedEvent;
|
|
50
|
+
/**
|
|
51
|
+
* Sets up event listeners for the provider.
|
|
52
|
+
*/
|
|
53
|
+
static setupEventListeners: ({ onChainChanged, onAccountChanged, onDisconnect, }: {
|
|
54
|
+
onChainChanged?: (chainId: number) => void;
|
|
55
|
+
onAccountChanged?: (account: string) => void;
|
|
56
|
+
onDisconnect?: () => void;
|
|
57
|
+
}) => void;
|
|
58
|
+
/**
|
|
59
|
+
* Tears down event listeners for the provider.
|
|
60
|
+
*/
|
|
61
|
+
static teardownEventListeners: () => void;
|
|
62
|
+
private static getMappedChainsByPreferredOrder;
|
|
63
|
+
}
|
|
64
|
+
export {};
|