@dynamic-labs/ethereum 1.3.0 → 1.4.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 +25 -0
- package/package.json +9 -9
- package/src/coinbase/coinbase.cjs +24 -5
- package/src/coinbase/coinbase.d.ts +2 -1
- package/src/coinbase/coinbase.js +24 -5
- package/src/ethProviderHelper.cjs +6 -1
- package/src/ethProviderHelper.d.ts +2 -1
- package/src/ethProviderHelper.js +6 -1
- package/src/index.cjs +3 -9
- package/src/index.js +3 -9
- package/src/injected/InjectedWalletBase.cjs +2 -2
- package/src/injected/InjectedWalletBase.d.ts +1 -1
- package/src/injected/InjectedWalletBase.js +2 -2
- package/src/walletConnect/walletConnect.cjs +2 -136
- package/src/walletConnect/walletConnect.d.ts +3 -42
- package/src/walletConnect/walletConnect.js +3 -133
- package/src/walletConnect/walletConnectV2.cjs +97 -61
- package/src/walletConnect/walletConnectV2.d.ts +17 -14
- package/src/walletConnect/walletConnectV2.js +96 -60
- package/src/walletConnect/client/client.cjs +0 -201
- package/src/walletConnect/client/client.js +0 -187
|
@@ -1,147 +1,17 @@
|
|
|
1
|
-
import { __rest
|
|
2
|
-
import WalletConnectProvider from '@walletconnect/ethereum-provider';
|
|
3
|
-
import { createWalletClient, custom } from 'viem';
|
|
4
|
-
import { getDeepLink } from '@dynamic-labs/wallet-connector-core';
|
|
5
|
-
import { getWalletBookWallet } from '@dynamic-labs/wallet-book';
|
|
6
|
-
import { isMobile, DynamicError } from '@dynamic-labs/utils';
|
|
1
|
+
import { __rest } from '../../_virtual/_tslib.js';
|
|
7
2
|
import { EthWalletConnector } from '../EthWalletConnector.js';
|
|
8
|
-
import { INFURA_ID } from '../constants.js';
|
|
9
|
-
import { initClient, setupWalletConnectEventListeners, teardownWalletConnectEventListeners, fetchWalletConnectEVMPublicAddress, signWalletConnectPersonalMessage, killWalletConnectSession } from './client/client.js';
|
|
10
3
|
|
|
11
4
|
class WalletConnect extends EthWalletConnector {
|
|
12
5
|
constructor(_a) {
|
|
13
|
-
var {
|
|
6
|
+
var { walletName } = _a, props = __rest(_a, ["walletName"]);
|
|
14
7
|
super(props);
|
|
15
8
|
this.supportedChains = ['EVM', 'ETH'];
|
|
16
9
|
this.connectedChain = 'EVM';
|
|
17
10
|
this.bridge = 'https://bridge.walletconnect.org';
|
|
18
|
-
this.canConnectViaQrCode = true;
|
|
19
|
-
this.isWalletConnect = true;
|
|
20
|
-
this.switchNetworkOnlyFromWallet = false;
|
|
21
11
|
this.name = walletName;
|
|
22
|
-
if (walletConnectV1Bridge) {
|
|
23
|
-
this.bridge = walletConnectV1Bridge;
|
|
24
|
-
}
|
|
25
|
-
this.deepLinkPreference = props.deepLinkPreference || 'native';
|
|
26
|
-
}
|
|
27
|
-
getClient() {
|
|
28
|
-
if (this.client) {
|
|
29
|
-
return this.client;
|
|
30
|
-
}
|
|
31
|
-
this.client = initClient(this.key, this.bridge, this.clientOptions);
|
|
32
|
-
return this.client;
|
|
33
|
-
}
|
|
34
|
-
supportsNetworkSwitching() {
|
|
35
|
-
if (this.connectedChain === 'EVM') {
|
|
36
|
-
return true;
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
const client = this.getClient();
|
|
40
|
-
return Boolean(client === null || client === void 0 ? void 0 : client.chainId);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
setupEventListeners() {
|
|
44
|
-
setupWalletConnectEventListeners(this, this.getClient());
|
|
45
|
-
}
|
|
46
|
-
teardownEventListeners() {
|
|
47
|
-
teardownWalletConnectEventListeners(this.getClient());
|
|
48
12
|
}
|
|
49
13
|
getWalletClient() {
|
|
50
|
-
|
|
51
|
-
return client
|
|
52
|
-
? createWalletClient({
|
|
53
|
-
transport: custom(new WalletConnectProvider({
|
|
54
|
-
connector: client,
|
|
55
|
-
infuraId: INFURA_ID,
|
|
56
|
-
rpc: this.evmNetworkRpcMap(),
|
|
57
|
-
})),
|
|
58
|
-
})
|
|
59
|
-
: undefined;
|
|
60
|
-
}
|
|
61
|
-
fetchPublicAddress(opts) {
|
|
62
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
63
|
-
return fetchWalletConnectEVMPublicAddress(getWalletBookWallet(this.walletBook, this.key), this.getClient(), this.deepLinkPreference, Object.assign(Object.assign({}, opts), { onConnect: (payload) => {
|
|
64
|
-
var _a, _b;
|
|
65
|
-
(_a = opts === null || opts === void 0 ? void 0 : opts.onConnect) === null || _a === void 0 ? void 0 : _a.call(opts, payload);
|
|
66
|
-
this.connectedChain = payload.params[0].chainId ? 'EVM' : 'SOL';
|
|
67
|
-
if ((_b = payload.params[0].accounts) === null || _b === void 0 ? void 0 : _b.length) {
|
|
68
|
-
this.emit('accountChange', {
|
|
69
|
-
accounts: payload.params[0].accounts,
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
} }));
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
getDeepLink() {
|
|
76
|
-
var _a;
|
|
77
|
-
const wallet = getWalletBookWallet(this.walletBook, this.key);
|
|
78
|
-
if (!isMobile() && !((_a = wallet.desktop) === null || _a === void 0 ? void 0 : _a.native)) {
|
|
79
|
-
return undefined;
|
|
80
|
-
}
|
|
81
|
-
return getDeepLink({
|
|
82
|
-
metadata: wallet,
|
|
83
|
-
mode: 'regular',
|
|
84
|
-
preference: this.deepLinkPreference,
|
|
85
|
-
uri: this.getClient().uri,
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
signMessage(messageToSign) {
|
|
89
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
90
|
-
return signWalletConnectPersonalMessage(messageToSign, getWalletBookWallet(this.walletBook, this.key), this.getClient(), this.deepLinkPreference,
|
|
91
|
-
// don't call getPublicClient until we really need to
|
|
92
|
-
() => __awaiter(this, void 0, void 0, function* () { return this.getPublicClient(); }));
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
endSession() {
|
|
96
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
97
|
-
killWalletConnectSession(this.getClient());
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
providerSwitchNetwork({ network, provider, }) {
|
|
101
|
-
const _super = Object.create(null, {
|
|
102
|
-
providerSwitchNetwork: { get: () => super.providerSwitchNetwork }
|
|
103
|
-
});
|
|
104
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
105
|
-
const client = this.getClient();
|
|
106
|
-
const currentNetworkId = yield this.getNetwork();
|
|
107
|
-
if (currentNetworkId && currentNetworkId === network.chainId) {
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
if (this.switchNetworkOnlyFromWallet !== undefined &&
|
|
111
|
-
this.switchNetworkOnlyFromWallet) {
|
|
112
|
-
throw new DynamicError('Network switching is only supported through the wallet');
|
|
113
|
-
}
|
|
114
|
-
if (!this.supportsNetworkSwitching()) {
|
|
115
|
-
throw new DynamicError('Network switching not supported');
|
|
116
|
-
}
|
|
117
|
-
if (!client) {
|
|
118
|
-
throw new DynamicError('Client not found');
|
|
119
|
-
}
|
|
120
|
-
if (isMobile()) {
|
|
121
|
-
const deepLink = getDeepLink({
|
|
122
|
-
metadata: getWalletBookWallet(this.walletBook, this.key),
|
|
123
|
-
mode: 'regular',
|
|
124
|
-
preference: this.deepLinkPreference,
|
|
125
|
-
uri: client.uri,
|
|
126
|
-
});
|
|
127
|
-
window.location.href = deepLink;
|
|
128
|
-
}
|
|
129
|
-
return _super.providerSwitchNetwork.call(this, { network, provider });
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
getConnectedAccounts() {
|
|
133
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
134
|
-
const client = this.getClient();
|
|
135
|
-
if (!client.connected)
|
|
136
|
-
return [];
|
|
137
|
-
return client.accounts;
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
getSession() {
|
|
141
|
-
var _a;
|
|
142
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
143
|
-
return (_a = this.client) === null || _a === void 0 ? void 0 : _a.session;
|
|
144
|
-
});
|
|
14
|
+
return undefined;
|
|
145
15
|
}
|
|
146
16
|
}
|
|
147
17
|
|
|
@@ -3,18 +3,20 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var _tslib = require('../../_virtual/_tslib.cjs');
|
|
6
|
-
var
|
|
6
|
+
var EthereumProvider = require('@walletconnect/ethereum-provider');
|
|
7
7
|
var EventEmitter = require('eventemitter3');
|
|
8
8
|
var viem = require('viem');
|
|
9
|
+
var accounts = require('viem/accounts');
|
|
9
10
|
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
10
11
|
var walletBook = require('@dynamic-labs/wallet-book');
|
|
11
12
|
var utils = require('@dynamic-labs/utils');
|
|
13
|
+
var viemUtils = require('@dynamic-labs/viem-utils');
|
|
12
14
|
var EthWalletConnector = require('../EthWalletConnector.cjs');
|
|
13
15
|
var parseIntSafe = require('../utils/parseIntSafe.cjs');
|
|
14
16
|
|
|
15
17
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
16
18
|
|
|
17
|
-
var
|
|
19
|
+
var EthereumProvider__default = /*#__PURE__*/_interopDefaultLegacy(EthereumProvider);
|
|
18
20
|
var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
|
|
19
21
|
|
|
20
22
|
const activeAccountKey = (walletName) => `dynamic-wc2-active-account-${walletName}`;
|
|
@@ -35,6 +37,8 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
35
37
|
// When trying to switch network for MetaMask, the switch promise gets stuck
|
|
36
38
|
// if the switch got trigged once already, so we need to keep track of that
|
|
37
39
|
this._hasSwitchedNetwork = false;
|
|
40
|
+
this.sessionEventHandler = () => { };
|
|
41
|
+
this.sessionDeleteHandler = () => { };
|
|
38
42
|
this.name = opts.walletName;
|
|
39
43
|
this.projectId = opts.projectId;
|
|
40
44
|
this.deepLinkPreference = opts.deepLinkPreference || 'native';
|
|
@@ -60,7 +64,7 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
60
64
|
}
|
|
61
65
|
const reorderedChains = this.preferredChains.filter((chain) => allChains.includes(chain));
|
|
62
66
|
const remainingChains = allChains.filter((chain) => !this.preferredChains.includes(chain));
|
|
63
|
-
return [...reorderedChains, ...remainingChains];
|
|
67
|
+
return [...reorderedChains, ...remainingChains].map((chain) => Number(chain.split(':')[1]));
|
|
64
68
|
}
|
|
65
69
|
initConnection() {
|
|
66
70
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
@@ -69,44 +73,51 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
69
73
|
throw new utils.DynamicError('No provider found (init connection)');
|
|
70
74
|
}
|
|
71
75
|
// this means there is already a connection in progress, so don't call connect again
|
|
72
|
-
if (provider === null || provider === void 0 ? void 0 : provider.uri) {
|
|
76
|
+
if (provider === null || provider === void 0 ? void 0 : provider.signer.uri) {
|
|
73
77
|
return;
|
|
74
78
|
}
|
|
75
|
-
|
|
76
|
-
eip155: {
|
|
77
|
-
chains: this.getMappedChainsByPreferredOrder(),
|
|
78
|
-
events: ['chainChanged', 'accountsChanged'],
|
|
79
|
-
methods: [
|
|
80
|
-
'eth_chainId',
|
|
81
|
-
'eth_signTypedData',
|
|
82
|
-
'eth_signTransaction',
|
|
83
|
-
'eth_sign',
|
|
84
|
-
'personal_sign',
|
|
85
|
-
'eth_sendTransaction',
|
|
86
|
-
'eth_signTypedData_v4',
|
|
87
|
-
'wallet_switchEthereumChain',
|
|
88
|
-
'wallet_addEthereumChain',
|
|
89
|
-
],
|
|
90
|
-
rpcMap: this.evmNetworkRpcMap(),
|
|
91
|
-
},
|
|
92
|
-
};
|
|
93
|
-
provider
|
|
94
|
-
.connect({
|
|
95
|
-
optionalNamespaces,
|
|
96
|
-
})
|
|
97
|
-
.catch((e) => {
|
|
79
|
+
provider.connect().catch((e) => {
|
|
98
80
|
walletConnectorCore.logger.error(e);
|
|
99
81
|
ee.emit('walletconnect_connection_failed', e);
|
|
100
82
|
});
|
|
101
83
|
});
|
|
102
84
|
}
|
|
103
|
-
|
|
85
|
+
createProvider() {
|
|
104
86
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
105
|
-
|
|
106
|
-
|
|
87
|
+
return EthereumProvider__default["default"].init({
|
|
88
|
+
events: ['chainChanged', 'accountsChanged'],
|
|
89
|
+
methods: [],
|
|
90
|
+
optionalChains: this.getMappedChainsByPreferredOrder(),
|
|
91
|
+
optionalMethods: [
|
|
92
|
+
'eth_chainId',
|
|
93
|
+
'eth_signTypedData',
|
|
94
|
+
'eth_signTransaction',
|
|
95
|
+
'eth_sign',
|
|
96
|
+
'personal_sign',
|
|
97
|
+
'eth_sendTransaction',
|
|
98
|
+
'eth_signTypedData_v4',
|
|
99
|
+
'wallet_switchEthereumChain',
|
|
100
|
+
'wallet_addEthereumChain',
|
|
101
|
+
],
|
|
107
102
|
projectId: this.projectId,
|
|
103
|
+
rpcMap: this.evmNetworkRpcMap(),
|
|
104
|
+
showQrModal: false,
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
getWalletClientFromInitializedProvider() {
|
|
109
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
110
|
+
const walletConnect = this.createProvider();
|
|
111
|
+
const walletClient = viem.createWalletClient({
|
|
112
|
+
account: this.activeAccount ? accounts.toAccount(this.activeAccount) : undefined,
|
|
113
|
+
transport: viem.custom(yield walletConnect),
|
|
108
114
|
});
|
|
109
|
-
|
|
115
|
+
return walletClient;
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
createInitProviderPromise() {
|
|
119
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
120
|
+
WalletConnectV2.provider = yield this.createProvider();
|
|
110
121
|
this.teardownEventListeners();
|
|
111
122
|
this.setupEventListeners();
|
|
112
123
|
});
|
|
@@ -182,7 +193,7 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
182
193
|
if (!WalletConnectV2.provider) {
|
|
183
194
|
return;
|
|
184
195
|
}
|
|
185
|
-
|
|
196
|
+
this.sessionEventHandler = ({ params, }) => {
|
|
186
197
|
walletConnectorCore.logger.debug('session_event was called', { params });
|
|
187
198
|
if (!params || !params.event) {
|
|
188
199
|
walletConnectorCore.logger.debug('session_event was called without params or params.event');
|
|
@@ -191,12 +202,17 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
191
202
|
const { name, data } = params.event;
|
|
192
203
|
if (name === 'chainChanged') {
|
|
193
204
|
const chainId = parseIntSafe.parseIntSafe(data);
|
|
205
|
+
if (chainId === this.currentChainId) {
|
|
206
|
+
walletConnectorCore.logger.debug(`ignoring chainChanged event with same chain id as current chain id: ${chainId}`);
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
194
209
|
if (chainId === undefined) {
|
|
195
210
|
walletConnectorCore.logger.debug(`received unexpected data for chainChanged: ${data} with type ${typeof data}}`);
|
|
196
211
|
return;
|
|
197
212
|
}
|
|
198
213
|
this.currentChainId = chainId;
|
|
199
214
|
this.emit('chainChange', { chain: String(chainId) });
|
|
215
|
+
this.hasSwitchedNetwork = true;
|
|
200
216
|
// When a user switches network from their wallet, we need the provider to change network
|
|
201
217
|
// such that any future calls to `getNetwork` will return the correct network
|
|
202
218
|
this.switchNetwork({ networkChainId: chainId });
|
|
@@ -210,24 +226,28 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
210
226
|
const account = data[0].split(':')[2];
|
|
211
227
|
this.setActiveAccount(account);
|
|
212
228
|
}
|
|
213
|
-
}
|
|
214
|
-
WalletConnectV2.provider.
|
|
229
|
+
};
|
|
230
|
+
WalletConnectV2.provider.on('session_event', this.sessionEventHandler);
|
|
231
|
+
this.sessionDeleteHandler = () => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
215
232
|
this.endSession();
|
|
216
233
|
this.emit('disconnect');
|
|
217
|
-
})
|
|
234
|
+
});
|
|
235
|
+
WalletConnectV2.provider.on('session_delete', this.sessionDeleteHandler);
|
|
218
236
|
}
|
|
219
237
|
teardownEventListeners() {
|
|
220
238
|
if (!WalletConnectV2.provider) {
|
|
221
239
|
return;
|
|
222
240
|
}
|
|
223
|
-
WalletConnectV2.provider.
|
|
224
|
-
WalletConnectV2.provider.
|
|
241
|
+
WalletConnectV2.provider.off('session_event', this.sessionEventHandler);
|
|
242
|
+
WalletConnectV2.provider.off('session_delete', this.sessionDeleteHandler);
|
|
225
243
|
}
|
|
226
|
-
getWalletClient() {
|
|
244
|
+
getWalletClient(chainId) {
|
|
227
245
|
if (!WalletConnectV2.provider) {
|
|
228
246
|
return;
|
|
229
247
|
}
|
|
230
248
|
return viem.createWalletClient({
|
|
249
|
+
account: this.activeAccount ? accounts.toAccount(this.activeAccount) : undefined,
|
|
250
|
+
chain: viemUtils.chainsMap[chainId !== null && chainId !== void 0 ? chainId : String(this.currentChainId)],
|
|
231
251
|
transport: viem.custom(WalletConnectV2.provider),
|
|
232
252
|
});
|
|
233
253
|
}
|
|
@@ -237,7 +257,7 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
237
257
|
if (this.activeAccount) {
|
|
238
258
|
return this.activeAccount;
|
|
239
259
|
}
|
|
240
|
-
if (!WalletConnectV2.provider || !((_a = WalletConnectV2.provider) === null || _a === void 0 ? void 0 : _a.uri)) {
|
|
260
|
+
if (!WalletConnectV2.provider || !((_a = WalletConnectV2.provider) === null || _a === void 0 ? void 0 : _a.signer.uri)) {
|
|
241
261
|
walletConnectorCore.logger.debug('No WC2 provider found, re-initializing...');
|
|
242
262
|
yield this.endSession();
|
|
243
263
|
yield this.init();
|
|
@@ -247,13 +267,13 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
247
267
|
// finish setting up the connection URI and making it available
|
|
248
268
|
// on the provider
|
|
249
269
|
yield new Promise((resolve) => setTimeout(resolve, 1000));
|
|
250
|
-
if (!WalletConnectV2.provider || !((_b = WalletConnectV2.provider) === null || _b === void 0 ? void 0 : _b.uri)) {
|
|
270
|
+
if (!WalletConnectV2.provider || !((_b = WalletConnectV2.provider) === null || _b === void 0 ? void 0 : _b.signer.uri)) {
|
|
251
271
|
walletConnectorCore.logger.debug('No WC2 provider found, escaping and throwing error');
|
|
252
272
|
throw new utils.DynamicError('No provider found');
|
|
253
273
|
}
|
|
254
274
|
}
|
|
255
275
|
const metadata = walletBook.getWalletBookWallet(this.walletBook, this.key);
|
|
256
|
-
walletConnectorCore.performPlatformSpecificConnectionMethod(WalletConnectV2.provider.uri, metadata, {
|
|
276
|
+
walletConnectorCore.performPlatformSpecificConnectionMethod(WalletConnectV2.provider.signer.uri, metadata, {
|
|
257
277
|
onDesktopUri: opts === null || opts === void 0 ? void 0 : opts.onDesktopUri,
|
|
258
278
|
onDisplayUri: opts === null || opts === void 0 ? void 0 : opts.onDisplayUri,
|
|
259
279
|
}, this.deepLinkPreference);
|
|
@@ -266,7 +286,7 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
266
286
|
const error = new utils.DynamicError('Connection rejected. Please try again.');
|
|
267
287
|
error.code = 'connection_rejected';
|
|
268
288
|
if (WalletConnectV2.provider) {
|
|
269
|
-
WalletConnectV2.provider.uri = undefined;
|
|
289
|
+
WalletConnectV2.provider.signer.uri = undefined;
|
|
270
290
|
// this is needed for mobile to work when using universal links.
|
|
271
291
|
// if the user cancels the connection, we need to re-initialize the provider
|
|
272
292
|
// so that the async work is done ahead of time, before the user tries to connect again,
|
|
@@ -275,13 +295,19 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
275
295
|
}
|
|
276
296
|
reject(error);
|
|
277
297
|
});
|
|
278
|
-
WalletConnectV2.provider.on('connect', (
|
|
298
|
+
WalletConnectV2.provider.on('connect', () => {
|
|
299
|
+
var _a;
|
|
300
|
+
const session = (_a = WalletConnectV2.provider) === null || _a === void 0 ? void 0 : _a.session;
|
|
279
301
|
if (!session) {
|
|
280
302
|
reject(new utils.DynamicError('No session found'));
|
|
303
|
+
return;
|
|
281
304
|
}
|
|
282
305
|
this.setSession(session);
|
|
283
306
|
this.setActiveAccount(session.namespaces.eip155.accounts[0].split(':')[2]);
|
|
284
|
-
|
|
307
|
+
this.getNetwork().then((chainId) => {
|
|
308
|
+
this.currentChainId = chainId;
|
|
309
|
+
resolve(this.activeAccount);
|
|
310
|
+
});
|
|
285
311
|
});
|
|
286
312
|
});
|
|
287
313
|
});
|
|
@@ -299,10 +325,12 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
299
325
|
*/
|
|
300
326
|
waitForSignMessage(signMessageFn, messageToSign) {
|
|
301
327
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
302
|
-
const raceConditionPromise = new Promise((resolve) => {
|
|
328
|
+
const raceConditionPromise = new Promise((resolve, reject) => {
|
|
303
329
|
// Create listener for chain change event
|
|
304
330
|
this.on('chainChange', () => resolve({ success: false }));
|
|
305
|
-
signMessageFn(messageToSign)
|
|
331
|
+
signMessageFn(messageToSign)
|
|
332
|
+
.then((result) => resolve({ signedMessage: result, success: true }))
|
|
333
|
+
.catch(reject);
|
|
306
334
|
});
|
|
307
335
|
const signedMessageResult = yield raceConditionPromise;
|
|
308
336
|
if (signedMessageResult.success === false) {
|
|
@@ -321,7 +349,7 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
321
349
|
metadata,
|
|
322
350
|
mode: 'regular',
|
|
323
351
|
preference: this.deepLinkPreference,
|
|
324
|
-
uri: (_a = WalletConnectV2.provider) === null || _a === void 0 ? void 0 : _a.uri,
|
|
352
|
+
uri: (_a = WalletConnectV2.provider) === null || _a === void 0 ? void 0 : _a.signer.uri,
|
|
325
353
|
});
|
|
326
354
|
if (!deepLink) {
|
|
327
355
|
return;
|
|
@@ -335,10 +363,6 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
335
363
|
if (!this.session) {
|
|
336
364
|
throw new utils.DynamicError('no session');
|
|
337
365
|
}
|
|
338
|
-
const web3Provider = this.getWalletClient();
|
|
339
|
-
if (!web3Provider) {
|
|
340
|
-
throw new utils.DynamicError('No WalletConnect provider found to handle signing');
|
|
341
|
-
}
|
|
342
366
|
const deepLink = this.getDeepLink();
|
|
343
367
|
if (utils.isMobile() && deepLink) {
|
|
344
368
|
window.location.href = deepLink;
|
|
@@ -348,7 +372,8 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
348
372
|
if (!activeAccount) {
|
|
349
373
|
return;
|
|
350
374
|
}
|
|
351
|
-
|
|
375
|
+
const walletClient = yield this.getWalletClientFromInitializedProvider();
|
|
376
|
+
return walletClient.signMessage({
|
|
352
377
|
account: activeAccount,
|
|
353
378
|
message: messageToSign,
|
|
354
379
|
});
|
|
@@ -407,11 +432,17 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
407
432
|
return _super.getNetwork.call(this);
|
|
408
433
|
});
|
|
409
434
|
}
|
|
410
|
-
providerSwitchNetwork({ network,
|
|
435
|
+
providerSwitchNetwork({ network, }) {
|
|
411
436
|
const _super = Object.create(null, {
|
|
412
437
|
providerSwitchNetwork: { get: () => super.providerSwitchNetwork }
|
|
413
438
|
});
|
|
414
439
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
440
|
+
const supportedNetworks = yield this.getSupportedNetworks();
|
|
441
|
+
if (!(supportedNetworks === null || supportedNetworks === void 0 ? void 0 : supportedNetworks.includes(network.chainId.toString()))) {
|
|
442
|
+
const error = new utils.DynamicError('Network switching is not available at this time. The user should manually switch network in their wallet');
|
|
443
|
+
error.code = 'network_switching_only_available_in_wallet';
|
|
444
|
+
throw error;
|
|
445
|
+
}
|
|
415
446
|
const currentNetworkId = yield this.getNetwork();
|
|
416
447
|
if (currentNetworkId && currentNetworkId === network.chainId) {
|
|
417
448
|
return;
|
|
@@ -422,10 +453,14 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
422
453
|
if (!this.supportsNetworkSwitching()) {
|
|
423
454
|
throw new utils.DynamicError('Network switching not supported');
|
|
424
455
|
}
|
|
425
|
-
|
|
426
|
-
|
|
456
|
+
const walletClient = yield this.getWalletClientFromInitializedProvider();
|
|
457
|
+
if (this.isMetaMask()) {
|
|
458
|
+
const deepLink = this.getDeepLink();
|
|
459
|
+
if (deepLink) {
|
|
460
|
+
window.location.href = deepLink;
|
|
461
|
+
}
|
|
427
462
|
}
|
|
428
|
-
yield _super.providerSwitchNetwork.call(this, { network, provider });
|
|
463
|
+
yield _super.providerSwitchNetwork.call(this, { network, provider: walletClient });
|
|
429
464
|
this.currentChainId = network.chainId;
|
|
430
465
|
this.hasSwitchedNetwork = true;
|
|
431
466
|
this.emit('chainChange', { chain: String(network.chainId) });
|
|
@@ -451,13 +486,14 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
451
486
|
getSupportedNetworks() {
|
|
452
487
|
var _a;
|
|
453
488
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
454
|
-
// MM allows you to switch to any network the first time, even if it's not enabled in MM
|
|
455
|
-
// so we should consider all networks as supported if network switching hasn't been triggered yet
|
|
456
|
-
if (this.isMetaMask() && !this.hasSwitchedNetwork) {
|
|
457
|
-
return this.evmNetworks.map((network) => network.chainId.toString());
|
|
458
|
-
}
|
|
459
489
|
yield this.initProvider();
|
|
460
490
|
this.refreshSession();
|
|
491
|
+
if (this.isMetaMask()) {
|
|
492
|
+
if (this.hasSwitchedNetwork) {
|
|
493
|
+
return [String(this.currentChainId)];
|
|
494
|
+
}
|
|
495
|
+
return this.evmNetworks.map((network) => network.chainId.toString());
|
|
496
|
+
}
|
|
461
497
|
if (!this.session) {
|
|
462
498
|
return [];
|
|
463
499
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { SessionTypes } from '@walletconnect/types';
|
|
2
|
-
import { Hex
|
|
2
|
+
import { Hex } from 'viem';
|
|
3
3
|
import { Chain, FetchPublicAddressOpts, DeepLinkVariant } from '@dynamic-labs/wallet-connector-core';
|
|
4
4
|
import { EvmNetwork } from '@dynamic-labs/types';
|
|
5
5
|
import { EthWalletConnector, EthWalletConnectorOpts } from '../EthWalletConnector';
|
|
@@ -29,6 +29,8 @@ export declare class WalletConnectV2 extends EthWalletConnector {
|
|
|
29
29
|
private getMappedChains;
|
|
30
30
|
private getMappedChainsByPreferredOrder;
|
|
31
31
|
private initConnection;
|
|
32
|
+
private createProvider;
|
|
33
|
+
private getWalletClientFromInitializedProvider;
|
|
32
34
|
private createInitProviderPromise;
|
|
33
35
|
private initProvider;
|
|
34
36
|
private refreshSession;
|
|
@@ -42,10 +44,12 @@ export declare class WalletConnectV2 extends EthWalletConnector {
|
|
|
42
44
|
private set hasSwitchedNetwork(value);
|
|
43
45
|
private get hasSwitchedNetwork();
|
|
44
46
|
supportsNetworkSwitching(): boolean;
|
|
47
|
+
private sessionEventHandler;
|
|
48
|
+
private sessionDeleteHandler;
|
|
45
49
|
setupEventListeners(): void;
|
|
46
50
|
teardownEventListeners(): void;
|
|
47
|
-
getWalletClient(): {
|
|
48
|
-
account: undefined;
|
|
51
|
+
getWalletClient(chainId?: string): {
|
|
52
|
+
account: import("viem").JsonRpcAccount | undefined;
|
|
49
53
|
batch?: {
|
|
50
54
|
multicall?: boolean | {
|
|
51
55
|
batchSize?: number | undefined;
|
|
@@ -53,7 +57,7 @@ export declare class WalletConnectV2 extends EthWalletConnector {
|
|
|
53
57
|
} | undefined;
|
|
54
58
|
} | undefined;
|
|
55
59
|
cacheTime: number;
|
|
56
|
-
chain:
|
|
60
|
+
chain: import("viem").Chain;
|
|
57
61
|
key: string;
|
|
58
62
|
name: string;
|
|
59
63
|
pollingInterval: number;
|
|
@@ -62,20 +66,20 @@ export declare class WalletConnectV2 extends EthWalletConnector {
|
|
|
62
66
|
type: string;
|
|
63
67
|
uid: string;
|
|
64
68
|
addChain: (args: import("viem").AddChainParameters) => Promise<void>;
|
|
65
|
-
deployContract: <const TAbi extends import("viem").Abi | readonly unknown[], TChainOverride extends import("viem").Chain | undefined = undefined>(args: import("viem").DeployContractParameters<TAbi,
|
|
69
|
+
deployContract: <const TAbi extends import("viem").Abi | readonly unknown[], TChainOverride extends import("viem").Chain | undefined = undefined>(args: import("viem").DeployContractParameters<TAbi, import("viem").Chain, import("viem").JsonRpcAccount | undefined, TChainOverride>) => Promise<`0x${string}`>;
|
|
66
70
|
getAddresses: () => Promise<import("viem").GetAddressesReturnType>;
|
|
67
71
|
getChainId: () => Promise<number>;
|
|
68
72
|
getPermissions: () => Promise<import("viem").GetPermissionsReturnType>;
|
|
69
|
-
prepareTransactionRequest: <TChainOverride_1 extends import("viem").Chain | undefined = undefined>(args: import("viem").PrepareTransactionRequestParameters<
|
|
73
|
+
prepareTransactionRequest: <TChainOverride_1 extends import("viem").Chain | undefined = undefined>(args: import("viem").PrepareTransactionRequestParameters<import("viem").Chain, import("viem").JsonRpcAccount | undefined, TChainOverride_1>) => Promise<import("viem").PrepareTransactionRequestReturnType>;
|
|
70
74
|
requestAddresses: () => Promise<import("viem").RequestAddressesReturnType>;
|
|
71
75
|
requestPermissions: (args: {
|
|
72
76
|
[x: string]: Record<string, any>;
|
|
73
77
|
eth_accounts: Record<string, any>;
|
|
74
78
|
}) => Promise<import("viem").RequestPermissionsReturnType>;
|
|
75
79
|
sendRawTransaction: (args: import("node_modules/viem/_types/actions/wallet/sendRawTransaction").SendRawTransactionParameters) => Promise<`0x${string}`>;
|
|
76
|
-
sendTransaction: <TChainOverride_2 extends import("viem").Chain | undefined = undefined>(args: import("viem").SendTransactionParameters<
|
|
77
|
-
signMessage: (args: import("viem").SignMessageParameters<undefined>) => Promise<`0x${string}`>;
|
|
78
|
-
signTransaction: <TChainOverride_3 extends import("viem").Chain | undefined = undefined>(args: import("node_modules/viem/_types/actions/wallet/signTransaction").SignTransactionParameters<
|
|
80
|
+
sendTransaction: <TChainOverride_2 extends import("viem").Chain | undefined = undefined>(args: import("viem").SendTransactionParameters<import("viem").Chain, import("viem").JsonRpcAccount | undefined, TChainOverride_2>) => Promise<`0x${string}`>;
|
|
81
|
+
signMessage: (args: import("viem").SignMessageParameters<import("viem").JsonRpcAccount | undefined>) => Promise<`0x${string}`>;
|
|
82
|
+
signTransaction: <TChainOverride_3 extends import("viem").Chain | undefined = undefined>(args: import("node_modules/viem/_types/actions/wallet/signTransaction").SignTransactionParameters<import("viem").Chain, import("viem").JsonRpcAccount | undefined, TChainOverride_3>) => Promise<`0x${string}`>;
|
|
79
83
|
signTypedData: <const TTypedData extends {
|
|
80
84
|
[x: string]: readonly import("viem").TypedDataParameter[];
|
|
81
85
|
[x: `string[${string}]`]: undefined;
|
|
@@ -283,10 +287,10 @@ export declare class WalletConnectV2 extends EthWalletConnector {
|
|
|
283
287
|
uint256?: undefined;
|
|
284
288
|
} | {
|
|
285
289
|
[key: string]: unknown;
|
|
286
|
-
}, TPrimaryType extends string>(args: import("viem").SignTypedDataParameters<TTypedData, TPrimaryType, undefined>) => Promise<`0x${string}`>;
|
|
290
|
+
}, TPrimaryType extends string>(args: import("viem").SignTypedDataParameters<TTypedData, TPrimaryType, import("viem").JsonRpcAccount | undefined>) => Promise<`0x${string}`>;
|
|
287
291
|
switchChain: (args: import("viem").SwitchChainParameters) => Promise<void>;
|
|
288
292
|
watchAsset: (args: import("viem").WatchAssetParams) => Promise<boolean>;
|
|
289
|
-
writeContract: <const TAbi_1 extends import("viem").Abi | readonly unknown[], TFunctionName extends string, TChainOverride_4 extends import("viem").Chain | undefined = undefined>(args: import("viem").WriteContractParameters<TAbi_1, TFunctionName,
|
|
293
|
+
writeContract: <const TAbi_1 extends import("viem").Abi | readonly unknown[], TFunctionName extends string, TChainOverride_4 extends import("viem").Chain | undefined = undefined>(args: import("viem").WriteContractParameters<TAbi_1, TFunctionName, import("viem").Chain, import("viem").JsonRpcAccount | undefined, TChainOverride_4>) => Promise<`0x${string}`>;
|
|
290
294
|
extend: <const client extends {
|
|
291
295
|
[x: string]: unknown;
|
|
292
296
|
account?: undefined;
|
|
@@ -300,7 +304,7 @@ export declare class WalletConnectV2 extends EthWalletConnector {
|
|
|
300
304
|
transport?: undefined;
|
|
301
305
|
type?: undefined;
|
|
302
306
|
uid?: undefined;
|
|
303
|
-
} & Partial<Pick<import("viem").PublicActions, "getChainId" | "prepareTransactionRequest" | "sendRawTransaction" | "call" | "createContractEventFilter" | "createEventFilter" | "estimateContractGas" | "estimateGas" | "getBlock" | "getBlockNumber" | "getContractEvents" | "getEnsText" | "getFilterChanges" | "getGasPrice" | "getLogs" | "getTransaction" | "getTransactionCount" | "getTransactionReceipt" | "readContract" | "simulateContract" | "uninstallFilter" | "watchBlockNumber" | "watchContractEvent"> & Pick<import("viem").WalletActions, "sendTransaction" | "writeContract">>>(fn: (client: import("viem").Client<import("viem").CustomTransport,
|
|
307
|
+
} & Partial<Pick<import("viem").PublicActions, "getChainId" | "prepareTransactionRequest" | "sendRawTransaction" | "call" | "createContractEventFilter" | "createEventFilter" | "estimateContractGas" | "estimateGas" | "getBlock" | "getBlockNumber" | "getContractEvents" | "getEnsText" | "getFilterChanges" | "getGasPrice" | "getLogs" | "getTransaction" | "getTransactionCount" | "getTransactionReceipt" | "readContract" | "simulateContract" | "uninstallFilter" | "watchBlockNumber" | "watchContractEvent"> & Pick<import("viem").WalletActions, "sendTransaction" | "writeContract">>>(fn: (client: import("viem").Client<import("viem").CustomTransport, import("viem").Chain, import("viem").JsonRpcAccount | undefined, import("viem").WalletRpcSchema, import("viem").WalletActions<import("viem").Chain, import("viem").JsonRpcAccount | undefined>>) => client) => import("viem").Client<import("viem").CustomTransport, import("viem").Chain, import("viem").JsonRpcAccount | undefined, import("viem").WalletRpcSchema, { [K in keyof client]: client[K]; } & import("viem").WalletActions<import("viem").Chain, import("viem").JsonRpcAccount | undefined>>;
|
|
304
308
|
} | undefined;
|
|
305
309
|
fetchPublicAddress(opts?: FetchPublicAddressOpts): Promise<string | undefined>;
|
|
306
310
|
/**
|
|
@@ -323,9 +327,8 @@ export declare class WalletConnectV2 extends EthWalletConnector {
|
|
|
323
327
|
private setSession;
|
|
324
328
|
endSession(): Promise<void>;
|
|
325
329
|
getNetwork(): Promise<number | undefined>;
|
|
326
|
-
providerSwitchNetwork({ network,
|
|
330
|
+
providerSwitchNetwork({ network, }: {
|
|
327
331
|
network: EvmNetwork;
|
|
328
|
-
provider: WalletClient;
|
|
329
332
|
}): Promise<void>;
|
|
330
333
|
getConnectedAccounts(): Promise<string[]>;
|
|
331
334
|
private isMetaMask;
|