@dynamic-labs/ethereum 4.8.1 → 4.8.2-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 +16 -0
- package/package.cjs +1 -1
- package/package.js +1 -1
- package/package.json +9 -10
- package/src/EthereumWalletConnectors.cjs +4 -3
- package/src/EthereumWalletConnectors.js +3 -2
- package/src/injected/InjectedWalletBase.cjs +2 -2
- package/src/injected/InjectedWalletBase.js +2 -2
- package/src/metaMask/MetaMaskConnector.cjs +1 -1
- package/src/metaMask/MetaMaskConnector.js +2 -2
- package/src/walletConnect/WalletConnectConnector/WalletConnectConnector.cjs +286 -0
- package/src/walletConnect/{walletConnect.d.ts → WalletConnectConnector/WalletConnectConnector.d.ts} +12 -53
- package/src/walletConnect/WalletConnectConnector/WalletConnectConnector.js +282 -0
- package/src/walletConnect/WalletConnectConnector/index.d.ts +1 -0
- package/src/walletConnect/WalletConnectProvider/WalletConnectProvider.cjs +169 -0
- package/src/walletConnect/WalletConnectProvider/WalletConnectProvider.d.ts +39 -0
- package/src/walletConnect/WalletConnectProvider/WalletConnectProvider.js +161 -0
- package/src/walletConnect/WalletConnectProvider/index.d.ts +1 -0
- package/src/walletConnect/index.d.ts +2 -2
- package/src/walletConnect/{fetchWalletConnectWallets.cjs → utils/fetchWalletConnectWallets.cjs} +2 -8
- package/src/walletConnect/{fetchWalletConnectWallets.d.ts → utils/fetchWalletConnectWallets.d.ts} +0 -1
- package/src/walletConnect/{fetchWalletConnectWallets.js → utils/fetchWalletConnectWallets.js} +3 -8
- package/src/walletConnect/utils/getWalletConnectConnector.cjs +14 -0
- package/src/walletConnect/utils/getWalletConnectConnector.d.ts +2 -0
- package/src/walletConnect/utils/getWalletConnectConnector.js +10 -0
- package/src/walletConnect/utils/index.d.ts +2 -0
- package/src/walletConnect/walletConnect.cjs +0 -504
- package/src/walletConnect/walletConnect.js +0 -495
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../../_virtual/_tslib.js';
|
|
3
|
+
import { createWalletClient, custom } from 'viem';
|
|
4
|
+
import { EthereumWalletConnector, chainsMap } from '@dynamic-labs/ethereum-core';
|
|
5
|
+
import { parseIntSafe, DynamicError, isMobile, PlatformService } from '@dynamic-labs/utils';
|
|
6
|
+
import { logger, getDeepLink } from '@dynamic-labs/wallet-connector-core';
|
|
7
|
+
import { normalizeRpcError } from '../../utils/normalizeRpcError/normalizeRpcError.js';
|
|
8
|
+
import { WalletConnectProvider } from '../WalletConnectProvider/WalletConnectProvider.js';
|
|
9
|
+
|
|
10
|
+
const WC_ACTIVE_ACCOUNT_KEY = 'dynamic-wc2-active-account';
|
|
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
|
+
// restore the active account and chain from local storage
|
|
20
|
+
const storedAddress = localStorage.getItem(WC_ACTIVE_ACCOUNT_KEY);
|
|
21
|
+
if (storedAddress) {
|
|
22
|
+
this.setActiveAccount(storedAddress);
|
|
23
|
+
}
|
|
24
|
+
const storedChainId = localStorage.getItem(WC_CURRENT_CHAIN_KEY);
|
|
25
|
+
if (storedChainId) {
|
|
26
|
+
this.currentChainId = parseIntSafe(storedChainId);
|
|
27
|
+
}
|
|
28
|
+
// set provider props generic to all wallets
|
|
29
|
+
WalletConnectProvider.projectId = opts.projectId;
|
|
30
|
+
WalletConnectProvider.enabledNetworks = opts.evmNetworks;
|
|
31
|
+
WalletConnectProvider.preferredChains =
|
|
32
|
+
opts.walletConnectPreferredChains || [];
|
|
33
|
+
WalletConnectProvider.evmNetworkRpcMap = this.evmNetworkRpcMap();
|
|
34
|
+
}
|
|
35
|
+
init() {
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
logger.logVerboseTroubleshootingMessage('[WalletConnect] init called', {
|
|
38
|
+
isInitialized: WalletConnectProvider.isInitialized,
|
|
39
|
+
});
|
|
40
|
+
// we should only init the provider once as soon as possible
|
|
41
|
+
// the connection is established when a wallet is selected (with getAddress)
|
|
42
|
+
if (WalletConnectProvider.isInitialized) {
|
|
43
|
+
logger.debug('[WalletConnect] init - already initialized - skipping');
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
logger.debug('[WalletConnect] init');
|
|
47
|
+
yield WalletConnectProvider.init();
|
|
48
|
+
const provider = WalletConnectProvider.getProvider();
|
|
49
|
+
if (!provider) {
|
|
50
|
+
throw new DynamicError('WalletConnectProvider is not initialized');
|
|
51
|
+
}
|
|
52
|
+
this.setupWCEventListeners();
|
|
53
|
+
this.walletConnectorEventsEmitter.emit('walletConnectInitialized');
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
setupWCEventListeners() {
|
|
57
|
+
logger.debug('[WalletConnect] setupWCEventListeners');
|
|
58
|
+
WalletConnectProvider.teardownEventListeners();
|
|
59
|
+
WalletConnectProvider.setupEventListeners({
|
|
60
|
+
onAccountChanged: (account) => {
|
|
61
|
+
logger.debug('[WalletConnect] onAccountChanged', { account });
|
|
62
|
+
this.setWCActiveAccount(account);
|
|
63
|
+
},
|
|
64
|
+
onChainChanged: (chainId) => {
|
|
65
|
+
logger.debug('[WalletConnect] onChainChange', { chainId });
|
|
66
|
+
if (chainId === this.currentChainId) {
|
|
67
|
+
logger.debug(`[WalletConnect] onChainChange - ignoring chainChanged event with same chain id as current chain id: ${chainId}`);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
this.currentChainId = chainId;
|
|
71
|
+
this.emit('chainChange', { chain: String(chainId) });
|
|
72
|
+
},
|
|
73
|
+
onDisconnect: () => {
|
|
74
|
+
logger.debug('[WalletConnect] onDisconnect');
|
|
75
|
+
this.endSession();
|
|
76
|
+
this.emit('disconnect');
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
endSession() {
|
|
81
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
logger.debug('[WalletConnect] endSession');
|
|
83
|
+
localStorage.removeItem(WC_ACTIVE_ACCOUNT_KEY);
|
|
84
|
+
this.setActiveAccount(undefined);
|
|
85
|
+
this.currentChainId = undefined;
|
|
86
|
+
yield WalletConnectProvider.disconnect();
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
getAddress(opts) {
|
|
90
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
91
|
+
logger.debug('[WalletConnect] getAddress', opts);
|
|
92
|
+
if (!WalletConnectProvider.isInitialized) {
|
|
93
|
+
logger.debug('[WalletConnect] getAddress - WalletConnectProvider is not initialized');
|
|
94
|
+
throw new DynamicError('WalletConnectProvider is not initialized');
|
|
95
|
+
}
|
|
96
|
+
logger.debug('[WalletConnect] getAddress - connecting to WalletConnect');
|
|
97
|
+
const addresses = yield WalletConnectProvider.connect({
|
|
98
|
+
connectionOpts: opts,
|
|
99
|
+
deepLinkPreference: this.deepLinkPreference,
|
|
100
|
+
deepLinks: this.metadata.deepLinks,
|
|
101
|
+
});
|
|
102
|
+
logger.debug('[WalletConnect] getAddress - connection result', addresses);
|
|
103
|
+
const address = addresses === null || addresses === void 0 ? void 0 : addresses[0];
|
|
104
|
+
this.setWCActiveAccount(address);
|
|
105
|
+
return address;
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
getWalletClient(chainId) {
|
|
109
|
+
logger.logVerboseTroubleshootingMessage('[WalletConnect] getWalletClient was called - chainId', chainId);
|
|
110
|
+
const provider = WalletConnectProvider.getProvider();
|
|
111
|
+
if (!provider) {
|
|
112
|
+
logger.debug('[WalletConnect] getWalletClient - provider is not initialized');
|
|
113
|
+
throw new DynamicError('WalletConnectProvider is not initialized');
|
|
114
|
+
}
|
|
115
|
+
const walletClient = createWalletClient({
|
|
116
|
+
account: this.getActiveAccount(),
|
|
117
|
+
chain: chainsMap[chainId !== null && chainId !== void 0 ? chainId : String(this.currentChainId)],
|
|
118
|
+
transport: custom({
|
|
119
|
+
request: (args) => {
|
|
120
|
+
this.deepLinkIfApplicable(args.method);
|
|
121
|
+
return provider.request(args).catch(normalizeRpcError);
|
|
122
|
+
},
|
|
123
|
+
}),
|
|
124
|
+
});
|
|
125
|
+
return walletClient;
|
|
126
|
+
}
|
|
127
|
+
deepLinkIfApplicable(method) {
|
|
128
|
+
const methodsThatRequireDeepLink = [
|
|
129
|
+
'personal_sign',
|
|
130
|
+
'eth_sendTransaction',
|
|
131
|
+
'eth_signTypedData_v4',
|
|
132
|
+
];
|
|
133
|
+
const deepLink = this.getDeepLink();
|
|
134
|
+
if (isMobile() && deepLink && methodsThatRequireDeepLink.includes(method)) {
|
|
135
|
+
PlatformService.openURL(deepLink);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
signMessage(messageToSign) {
|
|
139
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
140
|
+
logger.logVerboseTroubleshootingMessage('[WalletConnect] signMessage', messageToSign);
|
|
141
|
+
const activeAccount = this.getActiveAccount();
|
|
142
|
+
logger.logVerboseTroubleshootingMessage('[WalletConnect] signMessage - activeAccount', activeAccount);
|
|
143
|
+
if (!activeAccount) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const walletClient = yield this.getWalletClient();
|
|
147
|
+
return walletClient.signMessage({
|
|
148
|
+
account: activeAccount,
|
|
149
|
+
message: messageToSign,
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
getConnectedAccounts() {
|
|
154
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
155
|
+
const activeAccount = this.getActiveAccount();
|
|
156
|
+
logger.logVerboseTroubleshootingMessage('[WalletConnect] getConnectedAccounts - activeAccount', activeAccount);
|
|
157
|
+
return activeAccount ? [activeAccount.address] : [];
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
setWCActiveAccount(account) {
|
|
161
|
+
logger.logVerboseTroubleshootingMessage('[WalletConnect] setWCActiveAccount', account);
|
|
162
|
+
if (account) {
|
|
163
|
+
localStorage.setItem(WC_ACTIVE_ACCOUNT_KEY, account);
|
|
164
|
+
this.setActiveAccount(account);
|
|
165
|
+
this.emit('accountChange', { accounts: [account] });
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
localStorage.removeItem(WC_ACTIVE_ACCOUNT_KEY);
|
|
169
|
+
this.setActiveAccount(undefined);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
get currentChainId() {
|
|
173
|
+
const lsCurrentChain = localStorage.getItem(WC_CURRENT_CHAIN_KEY);
|
|
174
|
+
try {
|
|
175
|
+
return lsCurrentChain ? parseIntSafe(lsCurrentChain) : undefined;
|
|
176
|
+
}
|
|
177
|
+
catch (e) {
|
|
178
|
+
logger.debug('[WalletConnect] getCurrentChainId - error', e);
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
set currentChainId(value) {
|
|
183
|
+
if (value) {
|
|
184
|
+
localStorage.setItem(WC_CURRENT_CHAIN_KEY, value.toString());
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
localStorage.removeItem(WC_CURRENT_CHAIN_KEY);
|
|
188
|
+
}
|
|
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
|
+
}
|
|
281
|
+
|
|
282
|
+
export { WalletConnectConnector };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { WalletConnectConnector } from './WalletConnectConnector';
|
|
@@ -0,0 +1,169 @@
|
|
|
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 teardownEventListeners() {
|
|
22
|
+
if (!_a.provider ||
|
|
23
|
+
!_a.eventListenersSetup) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
_a.provider.off('accountsChanged', _a.accountChangedHandler);
|
|
27
|
+
_a.provider.off('chainChanged', _a.chainChangedHandler);
|
|
28
|
+
_a.provider.off('session_delete', _a.sessionDeleteHandler);
|
|
29
|
+
_a.eventListenersSetup = false;
|
|
30
|
+
}
|
|
31
|
+
static getMappedChainsByPreferredOrder() {
|
|
32
|
+
const allChains = _a.enabledNetworks.map((network) => `eip155:${network.chainId}`);
|
|
33
|
+
const reorderedChains = _a.preferredChains.filter((chain) => allChains.includes(chain));
|
|
34
|
+
const remainingChains = allChains.filter((chain) => !_a.preferredChains.includes(chain));
|
|
35
|
+
return [...reorderedChains, ...remainingChains].map((chain) => Number(chain.split(':')[1]));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
_a = WalletConnectProvider;
|
|
39
|
+
WalletConnectProvider.isInitialized = false;
|
|
40
|
+
WalletConnectProvider.enabledNetworks = [];
|
|
41
|
+
WalletConnectProvider.preferredChains = [];
|
|
42
|
+
WalletConnectProvider.evmNetworkRpcMap = {};
|
|
43
|
+
WalletConnectProvider.eventListenersSetup = false;
|
|
44
|
+
WalletConnectProvider.accountChangedHandler = () => { };
|
|
45
|
+
WalletConnectProvider.chainChangedHandler = () => { };
|
|
46
|
+
WalletConnectProvider.sessionDeleteHandler = () => { };
|
|
47
|
+
// this method should only be called once
|
|
48
|
+
// it initializes the provider, but does not starts a connection
|
|
49
|
+
WalletConnectProvider.init = (...args_1) => _tslib.__awaiter(void 0, [...args_1], void 0, function* ({ storePrefix = 'dynamic-wc2', } = {}) {
|
|
50
|
+
logger.logger.debug('[WalletConnectProvider] init', {
|
|
51
|
+
isInitialized: _a.isInitialized,
|
|
52
|
+
});
|
|
53
|
+
if (_a.isInitialized) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
_a.isInitialized = true;
|
|
57
|
+
logger.logger.debug('[WalletConnectProvider] initializing');
|
|
58
|
+
_a.provider = yield EthereumProvider__default["default"].init({
|
|
59
|
+
customStoragePrefix: storePrefix,
|
|
60
|
+
disableProviderPing: true,
|
|
61
|
+
optionalChains: _a.getMappedChainsByPreferredOrder(),
|
|
62
|
+
optionalEvents: ['chainChanged', 'accountsChanged'],
|
|
63
|
+
optionalMethods: [
|
|
64
|
+
'eth_chainId',
|
|
65
|
+
'eth_signTypedData',
|
|
66
|
+
'eth_signTransaction',
|
|
67
|
+
'eth_sign',
|
|
68
|
+
'personal_sign',
|
|
69
|
+
'eth_sendTransaction',
|
|
70
|
+
'eth_signTypedData_v4',
|
|
71
|
+
'wallet_switchEthereumChain',
|
|
72
|
+
'wallet_addEthereumChain',
|
|
73
|
+
],
|
|
74
|
+
projectId: _a.projectId,
|
|
75
|
+
rpcMap: _a.evmNetworkRpcMap,
|
|
76
|
+
showQrModal: false,
|
|
77
|
+
});
|
|
78
|
+
logger.logger.debug('[WalletConnectProvider] initialized');
|
|
79
|
+
});
|
|
80
|
+
// this method should be called whenever we need to connect to a wallet
|
|
81
|
+
// when the page is refreshed, if the wallet is already connected,
|
|
82
|
+
// this method does not need to be called
|
|
83
|
+
WalletConnectProvider.connect = (_b) => _tslib.__awaiter(void 0, [_b], void 0, function* ({ deepLinks, deepLinkPreference, connectionOpts, }) {
|
|
84
|
+
const handleDisplayURI = (uri) => {
|
|
85
|
+
var _b;
|
|
86
|
+
logger.logger.debug('[WalletConnectProvider] handleDisplayURI', uri);
|
|
87
|
+
_a.connectionUri = uri;
|
|
88
|
+
walletConnectorCore.performPlatformSpecificConnectionMethod(_a.connectionUri, deepLinks, {
|
|
89
|
+
onDesktopUri: connectionOpts === null || connectionOpts === void 0 ? void 0 : connectionOpts.onDesktopUri,
|
|
90
|
+
onDisplayUri: connectionOpts === null || connectionOpts === void 0 ? void 0 : connectionOpts.onDisplayUri,
|
|
91
|
+
}, deepLinkPreference);
|
|
92
|
+
logger.logger.debug('[WalletConnectProvider] removing display_uri event listener');
|
|
93
|
+
(_b = _a.provider) === null || _b === void 0 ? void 0 : _b.off('display_uri', handleDisplayURI);
|
|
94
|
+
};
|
|
95
|
+
if (!_a.provider) {
|
|
96
|
+
throw new utils.DynamicError('WalletConnectProvider is not initialized');
|
|
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
|
+
});
|
|
112
|
+
// this method should be called whenever we need to disconnect from a wallet
|
|
113
|
+
// it will kill the connection, but not the provider
|
|
114
|
+
WalletConnectProvider.disconnect = () => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
115
|
+
if (!_a.provider) {
|
|
116
|
+
logger.logger.debug('[WalletConnectProvider] disconnect - provider is not initialized');
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
logger.logger.debug('[WalletConnectProvider] disconnecting from WalletConnect');
|
|
120
|
+
try {
|
|
121
|
+
yield _a.provider.disconnect();
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
logger.logger.error('[WalletConnectProvider] Failed to disconnect from WalletConnect', error);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
WalletConnectProvider.getProvider = () => _a.provider;
|
|
128
|
+
WalletConnectProvider.handleChainChangedEvent = (chain, onChainChanged) => {
|
|
129
|
+
logger.logger.debug('[WalletConnectProvider] handling chain change event', {
|
|
130
|
+
chain,
|
|
131
|
+
});
|
|
132
|
+
const chainId = utils.parseIntSafe(chain);
|
|
133
|
+
if (!chainId) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
onChainChanged === null || onChainChanged === void 0 ? void 0 : onChainChanged(chainId);
|
|
137
|
+
};
|
|
138
|
+
WalletConnectProvider.handleAccountChangedEvent = (accounts, onAccountChanged) => {
|
|
139
|
+
logger.logger.debug('[WalletConnectProvider] handling account change event', {
|
|
140
|
+
accounts,
|
|
141
|
+
});
|
|
142
|
+
const [account] = accounts;
|
|
143
|
+
const address = account.includes(':') ? account.split(':').pop() : account;
|
|
144
|
+
if (!address) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
onAccountChanged === null || onAccountChanged === void 0 ? void 0 : onAccountChanged(address);
|
|
148
|
+
};
|
|
149
|
+
WalletConnectProvider.setupEventListeners = ({ onChainChanged, onAccountChanged, onDisconnect, }) => {
|
|
150
|
+
if (!_a.provider ||
|
|
151
|
+
_a.eventListenersSetup) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
_a.chainChangedHandler = (chainId) => {
|
|
155
|
+
_a.handleChainChangedEvent(chainId, onChainChanged);
|
|
156
|
+
};
|
|
157
|
+
_a.accountChangedHandler = (account) => {
|
|
158
|
+
_a.handleAccountChangedEvent(account, onAccountChanged);
|
|
159
|
+
};
|
|
160
|
+
_a.sessionDeleteHandler = () => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
161
|
+
onDisconnect === null || onDisconnect === void 0 ? void 0 : onDisconnect();
|
|
162
|
+
});
|
|
163
|
+
_a.provider.on('accountsChanged', _a.accountChangedHandler);
|
|
164
|
+
_a.provider.on('chainChanged', _a.chainChangedHandler);
|
|
165
|
+
_a.provider.on('session_delete', _a.sessionDeleteHandler);
|
|
166
|
+
_a.eventListenersSetup = true;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
exports.WalletConnectProvider = WalletConnectProvider;
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
static connectionUri: string | undefined;
|
|
16
|
+
private static eventListenersSetup;
|
|
17
|
+
private static accountChangedHandler;
|
|
18
|
+
private static chainChangedHandler;
|
|
19
|
+
private static sessionDeleteHandler;
|
|
20
|
+
private constructor();
|
|
21
|
+
static init: ({ storePrefix, }?: ProviderInitOpts) => Promise<void>;
|
|
22
|
+
static connect: ({ deepLinks, deepLinkPreference, connectionOpts, }: {
|
|
23
|
+
deepLinks?: WalletDeepLinks;
|
|
24
|
+
deepLinkPreference: DeepLinkVariant;
|
|
25
|
+
connectionOpts?: GetAddressOpts;
|
|
26
|
+
}) => Promise<ProviderAccounts>;
|
|
27
|
+
static disconnect: () => Promise<void>;
|
|
28
|
+
static getProvider: () => EthereumProvider | undefined;
|
|
29
|
+
private static handleChainChangedEvent;
|
|
30
|
+
private static handleAccountChangedEvent;
|
|
31
|
+
static setupEventListeners: ({ onChainChanged, onAccountChanged, onDisconnect, }: {
|
|
32
|
+
onChainChanged?: (chainId: number) => void;
|
|
33
|
+
onAccountChanged?: (account: string) => void;
|
|
34
|
+
onDisconnect?: () => void;
|
|
35
|
+
}) => void;
|
|
36
|
+
static teardownEventListeners(): void;
|
|
37
|
+
private static getMappedChainsByPreferredOrder;
|
|
38
|
+
}
|
|
39
|
+
export {};
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../../_virtual/_tslib.js';
|
|
3
|
+
import EthereumProvider from '@walletconnect/ethereum-provider';
|
|
4
|
+
import { performPlatformSpecificConnectionMethod } from '@dynamic-labs/wallet-connector-core';
|
|
5
|
+
import { parseIntSafe, DynamicError } from '@dynamic-labs/utils';
|
|
6
|
+
import { logger } from '../../utils/logger.js';
|
|
7
|
+
|
|
8
|
+
var _a;
|
|
9
|
+
class WalletConnectProvider {
|
|
10
|
+
constructor() {
|
|
11
|
+
throw new Error('WalletConnectProvider is not instantiable');
|
|
12
|
+
}
|
|
13
|
+
static teardownEventListeners() {
|
|
14
|
+
if (!_a.provider ||
|
|
15
|
+
!_a.eventListenersSetup) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
_a.provider.off('accountsChanged', _a.accountChangedHandler);
|
|
19
|
+
_a.provider.off('chainChanged', _a.chainChangedHandler);
|
|
20
|
+
_a.provider.off('session_delete', _a.sessionDeleteHandler);
|
|
21
|
+
_a.eventListenersSetup = false;
|
|
22
|
+
}
|
|
23
|
+
static getMappedChainsByPreferredOrder() {
|
|
24
|
+
const allChains = _a.enabledNetworks.map((network) => `eip155:${network.chainId}`);
|
|
25
|
+
const reorderedChains = _a.preferredChains.filter((chain) => allChains.includes(chain));
|
|
26
|
+
const remainingChains = allChains.filter((chain) => !_a.preferredChains.includes(chain));
|
|
27
|
+
return [...reorderedChains, ...remainingChains].map((chain) => Number(chain.split(':')[1]));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
_a = WalletConnectProvider;
|
|
31
|
+
WalletConnectProvider.isInitialized = false;
|
|
32
|
+
WalletConnectProvider.enabledNetworks = [];
|
|
33
|
+
WalletConnectProvider.preferredChains = [];
|
|
34
|
+
WalletConnectProvider.evmNetworkRpcMap = {};
|
|
35
|
+
WalletConnectProvider.eventListenersSetup = false;
|
|
36
|
+
WalletConnectProvider.accountChangedHandler = () => { };
|
|
37
|
+
WalletConnectProvider.chainChangedHandler = () => { };
|
|
38
|
+
WalletConnectProvider.sessionDeleteHandler = () => { };
|
|
39
|
+
// this method should only be called once
|
|
40
|
+
// it initializes the provider, but does not starts a connection
|
|
41
|
+
WalletConnectProvider.init = (...args_1) => __awaiter(void 0, [...args_1], void 0, function* ({ storePrefix = 'dynamic-wc2', } = {}) {
|
|
42
|
+
logger.debug('[WalletConnectProvider] init', {
|
|
43
|
+
isInitialized: _a.isInitialized,
|
|
44
|
+
});
|
|
45
|
+
if (_a.isInitialized) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
_a.isInitialized = true;
|
|
49
|
+
logger.debug('[WalletConnectProvider] initializing');
|
|
50
|
+
_a.provider = yield EthereumProvider.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.debug('[WalletConnectProvider] initialized');
|
|
71
|
+
});
|
|
72
|
+
// this method should be called whenever we need to connect to a wallet
|
|
73
|
+
// when the page is refreshed, if the wallet is already connected,
|
|
74
|
+
// this method does not need to be called
|
|
75
|
+
WalletConnectProvider.connect = (_b) => __awaiter(void 0, [_b], void 0, function* ({ deepLinks, deepLinkPreference, connectionOpts, }) {
|
|
76
|
+
const handleDisplayURI = (uri) => {
|
|
77
|
+
var _b;
|
|
78
|
+
logger.debug('[WalletConnectProvider] handleDisplayURI', uri);
|
|
79
|
+
_a.connectionUri = uri;
|
|
80
|
+
performPlatformSpecificConnectionMethod(_a.connectionUri, deepLinks, {
|
|
81
|
+
onDesktopUri: connectionOpts === null || connectionOpts === void 0 ? void 0 : connectionOpts.onDesktopUri,
|
|
82
|
+
onDisplayUri: connectionOpts === null || connectionOpts === void 0 ? void 0 : connectionOpts.onDisplayUri,
|
|
83
|
+
}, deepLinkPreference);
|
|
84
|
+
logger.debug('[WalletConnectProvider] removing display_uri event listener');
|
|
85
|
+
(_b = _a.provider) === null || _b === void 0 ? void 0 : _b.off('display_uri', handleDisplayURI);
|
|
86
|
+
};
|
|
87
|
+
if (!_a.provider) {
|
|
88
|
+
throw new DynamicError('WalletConnectProvider is not initialized');
|
|
89
|
+
}
|
|
90
|
+
logger.debug('[WalletConnectProvider] adding display_uri event listener');
|
|
91
|
+
_a.provider.on('display_uri', handleDisplayURI);
|
|
92
|
+
try {
|
|
93
|
+
const result = yield _a.provider.enable();
|
|
94
|
+
logger.debug('[WalletConnectProvider] connected to WalletConnect', result);
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
logger.error('[WalletConnectProvider] Failed to connect to WalletConnect', error);
|
|
99
|
+
const customError = new DynamicError('Connection rejected. Please try again.');
|
|
100
|
+
customError.code = 'connection_rejected';
|
|
101
|
+
throw customError;
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
// this method should be called whenever we need to disconnect from a wallet
|
|
105
|
+
// it will kill the connection, but not the provider
|
|
106
|
+
WalletConnectProvider.disconnect = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
107
|
+
if (!_a.provider) {
|
|
108
|
+
logger.debug('[WalletConnectProvider] disconnect - provider is not initialized');
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
logger.debug('[WalletConnectProvider] disconnecting from WalletConnect');
|
|
112
|
+
try {
|
|
113
|
+
yield _a.provider.disconnect();
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
logger.error('[WalletConnectProvider] Failed to disconnect from WalletConnect', error);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
WalletConnectProvider.getProvider = () => _a.provider;
|
|
120
|
+
WalletConnectProvider.handleChainChangedEvent = (chain, onChainChanged) => {
|
|
121
|
+
logger.debug('[WalletConnectProvider] handling chain change event', {
|
|
122
|
+
chain,
|
|
123
|
+
});
|
|
124
|
+
const chainId = parseIntSafe(chain);
|
|
125
|
+
if (!chainId) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
onChainChanged === null || onChainChanged === void 0 ? void 0 : onChainChanged(chainId);
|
|
129
|
+
};
|
|
130
|
+
WalletConnectProvider.handleAccountChangedEvent = (accounts, onAccountChanged) => {
|
|
131
|
+
logger.debug('[WalletConnectProvider] handling account change event', {
|
|
132
|
+
accounts,
|
|
133
|
+
});
|
|
134
|
+
const [account] = accounts;
|
|
135
|
+
const address = account.includes(':') ? account.split(':').pop() : account;
|
|
136
|
+
if (!address) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
onAccountChanged === null || onAccountChanged === void 0 ? void 0 : onAccountChanged(address);
|
|
140
|
+
};
|
|
141
|
+
WalletConnectProvider.setupEventListeners = ({ onChainChanged, onAccountChanged, onDisconnect, }) => {
|
|
142
|
+
if (!_a.provider ||
|
|
143
|
+
_a.eventListenersSetup) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
_a.chainChangedHandler = (chainId) => {
|
|
147
|
+
_a.handleChainChangedEvent(chainId, onChainChanged);
|
|
148
|
+
};
|
|
149
|
+
_a.accountChangedHandler = (account) => {
|
|
150
|
+
_a.handleAccountChangedEvent(account, onAccountChanged);
|
|
151
|
+
};
|
|
152
|
+
_a.sessionDeleteHandler = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
153
|
+
onDisconnect === null || onDisconnect === void 0 ? void 0 : onDisconnect();
|
|
154
|
+
});
|
|
155
|
+
_a.provider.on('accountsChanged', _a.accountChangedHandler);
|
|
156
|
+
_a.provider.on('chainChanged', _a.chainChangedHandler);
|
|
157
|
+
_a.provider.on('session_delete', _a.sessionDeleteHandler);
|
|
158
|
+
_a.eventListenersSetup = true;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
export { WalletConnectProvider };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { WalletConnectProvider } from './WalletConnectProvider';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export { fetchWalletConnectWallets } from './
|
|
1
|
+
export { WalletConnectConnector } from './WalletConnectConnector';
|
|
2
|
+
export { fetchWalletConnectWallets, getWalletConnectConnector } from './utils';
|