@dynamic-labs/ethereum 2.0.0-alpha.3 → 2.0.0-alpha.30
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 +394 -0
- package/package.json +11 -12
- package/src/EthWalletConnector.cjs +1 -20
- package/src/EthWalletConnector.d.ts +9 -10
- package/src/EthWalletConnector.js +3 -22
- package/src/coinbase/client/client.cjs +14 -14
- package/src/coinbase/client/client.d.ts +5 -4
- package/src/coinbase/client/client.js +13 -13
- package/src/coinbase/client/types.d.ts +6 -3
- package/src/coinbase/coinbase.cjs +30 -14
- package/src/coinbase/coinbase.d.ts +20 -18
- package/src/coinbase/coinbase.js +31 -15
- package/src/ethProviderHelper.cjs +10 -5
- package/src/ethProviderHelper.d.ts +4 -3
- package/src/ethProviderHelper.js +10 -5
- package/src/index.cjs +7 -13
- package/src/index.d.ts +1 -0
- package/src/index.js +7 -13
- package/src/injected/ExodusEvm.cjs +1 -0
- package/src/injected/ExodusEvm.d.ts +1 -0
- package/src/injected/ExodusEvm.js +1 -0
- package/src/injected/InjectedWalletBase.cjs +27 -18
- package/src/injected/InjectedWalletBase.d.ts +2 -2
- package/src/injected/InjectedWalletBase.js +27 -18
- package/src/injected/PhantomEvm.cjs +4 -3
- package/src/injected/PhantomEvm.d.ts +2 -1
- package/src/injected/PhantomEvm.js +4 -3
- package/src/injected/Zerion.cjs +38 -0
- package/src/injected/Zerion.d.ts +10 -0
- package/src/injected/Zerion.js +34 -0
- package/src/injected/fetchInjectedWalletConnectors.cjs +25 -0
- package/src/injected/fetchInjectedWalletConnectors.d.ts +0 -1
- package/src/injected/fetchInjectedWalletConnectors.js +25 -0
- package/src/walletConnect/fetchWalletConnectWallets.cjs +14 -33
- package/src/walletConnect/fetchWalletConnectWallets.d.ts +2 -5
- package/src/walletConnect/fetchWalletConnectWallets.js +14 -33
- package/src/walletConnect/index.d.ts +0 -1
- package/src/walletConnect/walletConnect.cjs +450 -78
- package/src/walletConnect/walletConnect.d.ts +315 -31
- package/src/walletConnect/walletConnect.js +451 -80
- package/src/walletConnect/client/client.cjs +0 -201
- package/src/walletConnect/client/client.d.ts +0 -17
- package/src/walletConnect/client/client.js +0 -187
- package/src/walletConnect/client/index.d.ts +0 -1
- package/src/walletConnect/client/types.d.ts +0 -4
- package/src/walletConnect/walletConnectV2.cjs +0 -475
- package/src/walletConnect/walletConnectV2.d.ts +0 -333
- package/src/walletConnect/walletConnectV2.js +0 -466
|
@@ -1,146 +1,517 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import { __awaiter } from '../../_virtual/_tslib.js';
|
|
2
|
+
import EthereumProvider from '@walletconnect/ethereum-provider';
|
|
3
|
+
import EventEmitter from 'eventemitter3';
|
|
3
4
|
import { createWalletClient, custom } from 'viem';
|
|
4
|
-
import {
|
|
5
|
+
import { toAccount } from 'viem/accounts';
|
|
6
|
+
import { logger, performPlatformSpecificConnectionMethod, getDeepLink } from '@dynamic-labs/wallet-connector-core';
|
|
5
7
|
import { getWalletBookWallet } from '@dynamic-labs/wallet-book';
|
|
6
|
-
import {
|
|
8
|
+
import { DynamicError, sleep, isMobile } from '@dynamic-labs/utils';
|
|
9
|
+
import { chainsMap } from '@dynamic-labs/viem-utils';
|
|
7
10
|
import { EthWalletConnector } from '../EthWalletConnector.js';
|
|
8
|
-
import {
|
|
9
|
-
import { initClient, setupWalletConnectEventListeners, teardownWalletConnectEventListeners, fetchWalletConnectEVMPublicAddress, signWalletConnectPersonalMessage, killWalletConnectSession } from './client/client.js';
|
|
11
|
+
import { parseIntSafe } from '../utils/parseIntSafe.js';
|
|
10
12
|
|
|
13
|
+
const activeAccountKey = (walletName) => `dynamic-wc2-active-account-${walletName}`;
|
|
14
|
+
const sessionTopicKey = (walletName) => `dynamic-wc2-session-topic-${walletName}`;
|
|
15
|
+
const swicthedNetworkKey = (walletName) => `dynamic-wc2-switched-network-${walletName}`;
|
|
16
|
+
const currentChainKey = (walletName) => `dynamic-wc2-current-chain-${walletName}`;
|
|
17
|
+
const ee = new EventEmitter();
|
|
11
18
|
class WalletConnect extends EthWalletConnector {
|
|
12
|
-
constructor(
|
|
13
|
-
var
|
|
14
|
-
super(
|
|
19
|
+
constructor(opts) {
|
|
20
|
+
var _a;
|
|
21
|
+
super(opts);
|
|
15
22
|
this.supportedChains = ['EVM', 'ETH'];
|
|
16
23
|
this.connectedChain = 'EVM';
|
|
17
|
-
this.
|
|
24
|
+
this.isInitialized = false;
|
|
18
25
|
this.canConnectViaQrCode = true;
|
|
19
26
|
this.isWalletConnect = true;
|
|
20
|
-
this.
|
|
21
|
-
|
|
22
|
-
if
|
|
23
|
-
|
|
27
|
+
this.preferredChains = [];
|
|
28
|
+
// When trying to switch network for MetaMask, the switch promise gets stuck
|
|
29
|
+
// if the switch got trigged once already, so we need to keep track of that
|
|
30
|
+
this._hasSwitchedNetwork = false;
|
|
31
|
+
this.sessionEventHandler = () => { };
|
|
32
|
+
this.sessionDeleteHandler = () => { };
|
|
33
|
+
this.name = opts.walletName;
|
|
34
|
+
this.projectId = opts.projectId;
|
|
35
|
+
this.deepLinkPreference = opts.deepLinkPreference || 'native';
|
|
36
|
+
this.preferredChains = opts.walletConnectPreferredChains || [];
|
|
37
|
+
this.hasSwitchedNetwork =
|
|
38
|
+
(_a = Boolean(localStorage.getItem(this.swicthedNetworkKey))) !== null && _a !== void 0 ? _a : false;
|
|
39
|
+
const lsCurrentChain = localStorage.getItem(this.currentChainKey);
|
|
40
|
+
this.currentChainId = lsCurrentChain
|
|
41
|
+
? parseIntSafe(lsCurrentChain)
|
|
42
|
+
: undefined;
|
|
43
|
+
}
|
|
44
|
+
getMappedChains() {
|
|
45
|
+
return (this.evmNetworks
|
|
46
|
+
// Filters out palm that crashes Trust Wallet
|
|
47
|
+
.filter((network) => network.chainId !== 11297108109)
|
|
48
|
+
.map((network) => `eip155:${network.chainId}`));
|
|
49
|
+
}
|
|
50
|
+
getMappedChainsByPreferredOrder() {
|
|
51
|
+
const allChains = this.getMappedChains();
|
|
52
|
+
const reorderedChains = this.preferredChains.filter((chain) => allChains.includes(chain));
|
|
53
|
+
const remainingChains = allChains.filter((chain) => !this.preferredChains.includes(chain));
|
|
54
|
+
return [...reorderedChains, ...remainingChains].map((chain) => Number(chain.split(':')[1]));
|
|
55
|
+
}
|
|
56
|
+
initConnection() {
|
|
57
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
58
|
+
const { provider } = WalletConnect;
|
|
59
|
+
if (!provider) {
|
|
60
|
+
throw new DynamicError('No provider found (init connection)');
|
|
61
|
+
}
|
|
62
|
+
// this means there is already a connection in progress, so don't call connect again
|
|
63
|
+
if (provider === null || provider === void 0 ? void 0 : provider.signer.uri) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
provider.connect().catch((e) => {
|
|
67
|
+
logger.error(e);
|
|
68
|
+
ee.emit('walletconnect_connection_failed', e);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
createProvider() {
|
|
73
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
74
|
+
return EthereumProvider.init({
|
|
75
|
+
events: ['chainChanged', 'accountsChanged'],
|
|
76
|
+
methods: [],
|
|
77
|
+
optionalChains: this.getMappedChainsByPreferredOrder(),
|
|
78
|
+
optionalMethods: [
|
|
79
|
+
'eth_chainId',
|
|
80
|
+
'eth_signTypedData',
|
|
81
|
+
'eth_signTransaction',
|
|
82
|
+
'eth_sign',
|
|
83
|
+
'personal_sign',
|
|
84
|
+
'eth_sendTransaction',
|
|
85
|
+
'eth_signTypedData_v4',
|
|
86
|
+
'wallet_switchEthereumChain',
|
|
87
|
+
'wallet_addEthereumChain',
|
|
88
|
+
],
|
|
89
|
+
projectId: this.projectId,
|
|
90
|
+
rpcMap: this.evmNetworkRpcMap(),
|
|
91
|
+
showQrModal: false,
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
getWalletClientFromInitializedProvider() {
|
|
96
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
97
|
+
const walletConnect = this.createProvider();
|
|
98
|
+
const walletClient = createWalletClient({
|
|
99
|
+
account: this.activeAccount ? toAccount(this.activeAccount) : undefined,
|
|
100
|
+
transport: custom(yield walletConnect),
|
|
101
|
+
});
|
|
102
|
+
return walletClient;
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
createInitProviderPromise() {
|
|
106
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
107
|
+
WalletConnect.provider = yield this.createProvider();
|
|
108
|
+
this.teardownEventListeners();
|
|
109
|
+
this.setupEventListeners();
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
// We need to add a gate to this method since we will be calling it asynchronously
|
|
113
|
+
// from different places (such as setShowAuthFlow), which means there's a chance for
|
|
114
|
+
// a race condition to happen where createInitProviderPromise is called multiple times
|
|
115
|
+
initProvider() {
|
|
116
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
117
|
+
const { provider } = WalletConnect;
|
|
118
|
+
if (!provider) {
|
|
119
|
+
if (this.initializePromise === undefined) {
|
|
120
|
+
this.initializePromise = this.createInitProviderPromise();
|
|
121
|
+
}
|
|
122
|
+
yield this.initializePromise;
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
refreshSession() {
|
|
127
|
+
var _a, _b, _c, _d, _e;
|
|
128
|
+
if ((_b = (_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.session) === null || _b === void 0 ? void 0 : _b.topic) {
|
|
129
|
+
if (localStorage.getItem(this.sessionTopicKey) ===
|
|
130
|
+
((_d = (_c = WalletConnect.provider) === null || _c === void 0 ? void 0 : _c.session) === null || _d === void 0 ? void 0 : _d.topic)) {
|
|
131
|
+
this.session = WalletConnect.provider.session;
|
|
132
|
+
this.activeAccount = ((_e = localStorage.getItem(this.activeAccountKey)) !== null && _e !== void 0 ? _e : undefined);
|
|
133
|
+
}
|
|
24
134
|
}
|
|
25
|
-
this.deepLinkPreference = props.deepLinkPreference || 'native';
|
|
26
135
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
136
|
+
init() {
|
|
137
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
138
|
+
yield this.initProvider();
|
|
139
|
+
yield this.initConnection();
|
|
140
|
+
this.isInitialized = true;
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
get sessionTopicKey() {
|
|
144
|
+
return sessionTopicKey(this.key);
|
|
145
|
+
}
|
|
146
|
+
get activeAccountKey() {
|
|
147
|
+
return activeAccountKey(this.key);
|
|
148
|
+
}
|
|
149
|
+
get swicthedNetworkKey() {
|
|
150
|
+
return swicthedNetworkKey(this.key);
|
|
151
|
+
}
|
|
152
|
+
get currentChainKey() {
|
|
153
|
+
return currentChainKey(this.key);
|
|
154
|
+
}
|
|
155
|
+
set currentChainId(value) {
|
|
156
|
+
this._currentChainId = value;
|
|
157
|
+
if (value) {
|
|
158
|
+
localStorage.setItem(this.currentChainKey, value.toString());
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
localStorage.removeItem(this.currentChainKey);
|
|
30
162
|
}
|
|
31
|
-
this.client = initClient(this.key, this.bridge, this.clientOptions);
|
|
32
|
-
return this.client;
|
|
33
163
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
164
|
+
get currentChainId() {
|
|
165
|
+
return this._currentChainId;
|
|
166
|
+
}
|
|
167
|
+
set hasSwitchedNetwork(value) {
|
|
168
|
+
this._hasSwitchedNetwork = value;
|
|
169
|
+
if (value) {
|
|
170
|
+
localStorage.setItem(this.swicthedNetworkKey, value.toString());
|
|
37
171
|
}
|
|
38
172
|
else {
|
|
39
|
-
|
|
40
|
-
return Boolean(client === null || client === void 0 ? void 0 : client.chainId);
|
|
173
|
+
localStorage.removeItem(this.swicthedNetworkKey);
|
|
41
174
|
}
|
|
42
175
|
}
|
|
176
|
+
get hasSwitchedNetwork() {
|
|
177
|
+
return this._hasSwitchedNetwork;
|
|
178
|
+
}
|
|
179
|
+
supportsNetworkSwitching() {
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
43
182
|
setupEventListeners() {
|
|
44
|
-
|
|
183
|
+
if (!WalletConnect.provider) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
this.sessionEventHandler = ({ params, }) => {
|
|
187
|
+
logger.debug('session_event was called', { params });
|
|
188
|
+
if (!params || !params.event) {
|
|
189
|
+
logger.debug('session_event was called without params or params.event');
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
const { name, data } = params.event;
|
|
193
|
+
if (name === 'chainChanged') {
|
|
194
|
+
const chainId = parseIntSafe(data);
|
|
195
|
+
if (chainId === this.currentChainId) {
|
|
196
|
+
logger.debug(`ignoring chainChanged event with same chain id as current chain id: ${chainId}`);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (chainId === undefined) {
|
|
200
|
+
logger.debug(`received unexpected data for chainChanged: ${data} with type ${typeof data}}`);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
this.currentChainId = chainId;
|
|
204
|
+
this.emit('chainChange', { chain: String(chainId) });
|
|
205
|
+
this.hasSwitchedNetwork = true;
|
|
206
|
+
// When a user switches network from their wallet, we need the provider to change network
|
|
207
|
+
// such that any future calls to `getNetwork` will return the correct network
|
|
208
|
+
this.switchNetwork({ networkChainId: chainId });
|
|
209
|
+
}
|
|
210
|
+
else if (name === 'accountsChanged') {
|
|
211
|
+
if (!Array.isArray(data)) {
|
|
212
|
+
logger.debug(`received unexpected data for accountsChanged: ${data} with type ${typeof data}}`);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
// eslint-disable-next-line prefer-destructuring
|
|
216
|
+
const account = data[0].split(':')[2];
|
|
217
|
+
this.setActiveAccount(account);
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
WalletConnect.provider.on('session_event', this.sessionEventHandler);
|
|
221
|
+
this.sessionDeleteHandler = () => __awaiter(this, void 0, void 0, function* () {
|
|
222
|
+
this.endSession();
|
|
223
|
+
this.emit('disconnect');
|
|
224
|
+
});
|
|
225
|
+
WalletConnect.provider.on('session_delete', this.sessionDeleteHandler);
|
|
45
226
|
}
|
|
46
227
|
teardownEventListeners() {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
:
|
|
228
|
+
if (!WalletConnect.provider) {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
WalletConnect.provider.off('session_event', this.sessionEventHandler);
|
|
232
|
+
WalletConnect.provider.off('session_delete', this.sessionDeleteHandler);
|
|
233
|
+
}
|
|
234
|
+
getWalletClient(chainId) {
|
|
235
|
+
if (!WalletConnect.provider) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
return createWalletClient({
|
|
239
|
+
account: this.activeAccount ? toAccount(this.activeAccount) : undefined,
|
|
240
|
+
chain: chainsMap[chainId !== null && chainId !== void 0 ? chainId : String(this.currentChainId)],
|
|
241
|
+
transport: custom(WalletConnect.provider),
|
|
242
|
+
});
|
|
60
243
|
}
|
|
61
|
-
|
|
244
|
+
getAddress(opts) {
|
|
245
|
+
var _a, _b;
|
|
62
246
|
return __awaiter(this, void 0, void 0, function* () {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
247
|
+
if (this.activeAccount) {
|
|
248
|
+
return this.activeAccount;
|
|
249
|
+
}
|
|
250
|
+
if (!WalletConnect.provider || !((_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.signer.uri)) {
|
|
251
|
+
logger.debug('No WC2 provider found, re-initializing...');
|
|
252
|
+
yield this.endSession();
|
|
253
|
+
yield this.init();
|
|
254
|
+
// sleep 1 s to wait for connect call to finish
|
|
255
|
+
// the connect call isn't await-ed because it only resolves once
|
|
256
|
+
// the connection is established, but we need to wait for it to
|
|
257
|
+
// finish setting up the connection URI and making it available
|
|
258
|
+
// on the provider
|
|
259
|
+
yield sleep(1000);
|
|
260
|
+
if (!WalletConnect.provider || !((_b = WalletConnect.provider) === null || _b === void 0 ? void 0 : _b.signer.uri)) {
|
|
261
|
+
logger.debug('No WC2 provider found, escaping and throwing error');
|
|
262
|
+
throw new DynamicError('No provider found');
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
const metadata = getWalletBookWallet(this.walletBook, this.key, this.walletFallback);
|
|
266
|
+
performPlatformSpecificConnectionMethod(WalletConnect.provider.signer.uri, metadata, {
|
|
267
|
+
onDesktopUri: opts === null || opts === void 0 ? void 0 : opts.onDesktopUri,
|
|
268
|
+
onDisplayUri: opts === null || opts === void 0 ? void 0 : opts.onDisplayUri,
|
|
269
|
+
}, this.deepLinkPreference);
|
|
270
|
+
return new Promise((resolve, reject) => {
|
|
271
|
+
if (!WalletConnect.provider) {
|
|
272
|
+
reject(new DynamicError('No provider found'));
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
const onFail = () => {
|
|
276
|
+
const error = new DynamicError('Connection rejected. Please try again.');
|
|
277
|
+
error.code = 'connection_rejected';
|
|
278
|
+
if (WalletConnect.provider) {
|
|
279
|
+
WalletConnect.provider.signer.uri = undefined;
|
|
280
|
+
// this is needed for mobile to work when using universal links.
|
|
281
|
+
// if the user cancels the connection, we need to re-initialize the provider
|
|
282
|
+
// so that the async work is done ahead of time, before the user tries to connect again,
|
|
283
|
+
// otherwise they will trigger the iOS bug where they are redirected to the app store
|
|
284
|
+
this.init();
|
|
285
|
+
}
|
|
286
|
+
reject(error);
|
|
287
|
+
// We must clean up the onConnect and onFail listeners
|
|
288
|
+
// whenever the connection attempt either succeeds or fails
|
|
289
|
+
cleanupListeners();
|
|
290
|
+
};
|
|
291
|
+
const onConnect = () => {
|
|
292
|
+
var _a;
|
|
293
|
+
const session = (_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.session;
|
|
294
|
+
if (!session) {
|
|
295
|
+
reject(new DynamicError('No session found'));
|
|
296
|
+
return;
|
|
71
297
|
}
|
|
72
|
-
|
|
298
|
+
this.setSession(session);
|
|
299
|
+
this.setActiveAccount(session.namespaces.eip155.accounts[0].split(':')[2]);
|
|
300
|
+
this.getNetwork().then((chainId) => {
|
|
301
|
+
this.currentChainId = chainId;
|
|
302
|
+
resolve(this.activeAccount);
|
|
303
|
+
});
|
|
304
|
+
// We must clean up the onConnect and onFail listeners
|
|
305
|
+
// whenever the connection attempt either succeeds or fails
|
|
306
|
+
cleanupListeners();
|
|
307
|
+
};
|
|
308
|
+
const cleanupListeners = () => {
|
|
309
|
+
var _a;
|
|
310
|
+
ee.off('walletconnect_connection_failed', onFail);
|
|
311
|
+
(_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.off('connect', onConnect);
|
|
312
|
+
};
|
|
313
|
+
ee.on('walletconnect_connection_failed', onFail);
|
|
314
|
+
WalletConnect.provider.on('connect', onConnect);
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* WalletConnect V2 will fail to send the sign message request if the chainId
|
|
320
|
+
* is not the same as the one in the session. This method will wait for the
|
|
321
|
+
* chainId to change and then retry the sign message request.
|
|
322
|
+
*
|
|
323
|
+
* Otherwise it will just return the result of the sign message request.
|
|
324
|
+
*
|
|
325
|
+
* @param signMessageFn - Function to sign message with provider
|
|
326
|
+
* @param messageToSign - Message to sign
|
|
327
|
+
* @returns
|
|
328
|
+
*/
|
|
329
|
+
waitForSignMessage(signMessageFn, messageToSign) {
|
|
330
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
331
|
+
const raceConditionPromise = new Promise((resolve, reject) => {
|
|
332
|
+
// Create listener for chain change event
|
|
333
|
+
this.on('chainChange', () => resolve({ success: false }));
|
|
334
|
+
signMessageFn(messageToSign)
|
|
335
|
+
.then((result) => resolve({ signedMessage: result, success: true }))
|
|
336
|
+
.catch(reject);
|
|
337
|
+
});
|
|
338
|
+
const signedMessageResult = yield raceConditionPromise;
|
|
339
|
+
if (signedMessageResult.success === false) {
|
|
340
|
+
return signMessageFn(messageToSign);
|
|
341
|
+
}
|
|
342
|
+
return signedMessageResult.signedMessage;
|
|
73
343
|
});
|
|
74
344
|
}
|
|
75
345
|
getDeepLink() {
|
|
76
346
|
var _a;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
return undefined;
|
|
347
|
+
if (!this.session) {
|
|
348
|
+
return;
|
|
80
349
|
}
|
|
81
|
-
|
|
82
|
-
|
|
350
|
+
const metadata = getWalletBookWallet(this.walletBook, this.key);
|
|
351
|
+
const deepLink = getDeepLink({
|
|
352
|
+
metadata,
|
|
83
353
|
mode: 'regular',
|
|
84
354
|
preference: this.deepLinkPreference,
|
|
85
|
-
uri:
|
|
355
|
+
uri: (_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.signer.uri,
|
|
86
356
|
});
|
|
357
|
+
if (!deepLink) {
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
// we need to include the session topic here because it helps the wallet
|
|
361
|
+
// auto redirect back to the dapp after signing
|
|
362
|
+
return `${deepLink}?sessionTopic=${this.session.topic}`;
|
|
87
363
|
}
|
|
88
364
|
signMessage(messageToSign) {
|
|
89
365
|
return __awaiter(this, void 0, void 0, function* () {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
366
|
+
if (!this.session) {
|
|
367
|
+
throw new DynamicError('no session');
|
|
368
|
+
}
|
|
369
|
+
const deepLink = this.getDeepLink();
|
|
370
|
+
if (isMobile() && deepLink) {
|
|
371
|
+
window.location.href = deepLink;
|
|
372
|
+
}
|
|
373
|
+
const signMessageFn = (messageToSign) => __awaiter(this, void 0, void 0, function* () {
|
|
374
|
+
const { activeAccount } = this;
|
|
375
|
+
if (!activeAccount) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
const walletClient = yield this.getWalletClientFromInitializedProvider();
|
|
379
|
+
return walletClient.signMessage({
|
|
380
|
+
account: activeAccount,
|
|
381
|
+
message: messageToSign,
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
const response = yield this.waitForSignMessage(signMessageFn, messageToSign);
|
|
385
|
+
return response;
|
|
93
386
|
});
|
|
94
387
|
}
|
|
388
|
+
clearActiveAccount() {
|
|
389
|
+
localStorage.removeItem(this.activeAccountKey);
|
|
390
|
+
this.activeAccount = undefined;
|
|
391
|
+
}
|
|
392
|
+
clearSession() {
|
|
393
|
+
localStorage.removeItem(this.sessionTopicKey);
|
|
394
|
+
this.session = undefined;
|
|
395
|
+
}
|
|
396
|
+
setActiveAccount(account) {
|
|
397
|
+
localStorage.setItem(this.activeAccountKey, account);
|
|
398
|
+
this.activeAccount = account;
|
|
399
|
+
this.emit('accountChange', { accounts: [account] });
|
|
400
|
+
}
|
|
401
|
+
setSession(session) {
|
|
402
|
+
localStorage.setItem(this.sessionTopicKey, session.topic);
|
|
403
|
+
this.session = session;
|
|
404
|
+
}
|
|
95
405
|
endSession() {
|
|
406
|
+
var _a;
|
|
96
407
|
return __awaiter(this, void 0, void 0, function* () {
|
|
97
|
-
|
|
408
|
+
this.clearActiveAccount();
|
|
409
|
+
this.clearSession();
|
|
410
|
+
this.hasSwitchedNetwork = false;
|
|
411
|
+
this.currentChainId = undefined;
|
|
412
|
+
if (!((_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.session)) {
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
try {
|
|
416
|
+
yield WalletConnect.provider.disconnect();
|
|
417
|
+
// We must unset provider on logout so that a new session can be established
|
|
418
|
+
// If we don't then the provider will still have the old session and will hang
|
|
419
|
+
WalletConnect.provider = undefined;
|
|
420
|
+
}
|
|
421
|
+
catch (e) {
|
|
422
|
+
logger.debug(e);
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
getNetwork() {
|
|
427
|
+
const _super = Object.create(null, {
|
|
428
|
+
getNetwork: { get: () => super.getNetwork }
|
|
429
|
+
});
|
|
430
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
431
|
+
if (this.currentChainId) {
|
|
432
|
+
return this.currentChainId;
|
|
433
|
+
}
|
|
434
|
+
yield this.initProvider();
|
|
435
|
+
return _super.getNetwork.call(this);
|
|
98
436
|
});
|
|
99
437
|
}
|
|
100
|
-
providerSwitchNetwork({ network,
|
|
438
|
+
providerSwitchNetwork({ network, }) {
|
|
101
439
|
const _super = Object.create(null, {
|
|
102
440
|
providerSwitchNetwork: { get: () => super.providerSwitchNetwork }
|
|
103
441
|
});
|
|
104
442
|
return __awaiter(this, void 0, void 0, function* () {
|
|
105
|
-
const
|
|
443
|
+
const supportedNetworks = yield this.getSupportedNetworks();
|
|
444
|
+
if (!(supportedNetworks === null || supportedNetworks === void 0 ? void 0 : supportedNetworks.includes(network.chainId.toString()))) {
|
|
445
|
+
const error = new DynamicError('Network switching is not available at this time. The user should manually switch network in their wallet');
|
|
446
|
+
error.code = 'network_switching_only_available_in_wallet';
|
|
447
|
+
throw error;
|
|
448
|
+
}
|
|
106
449
|
const currentNetworkId = yield this.getNetwork();
|
|
107
450
|
if (currentNetworkId && currentNetworkId === network.chainId) {
|
|
108
451
|
return;
|
|
109
452
|
}
|
|
110
|
-
if (this.switchNetworkOnlyFromWallet
|
|
111
|
-
this.switchNetworkOnlyFromWallet) {
|
|
453
|
+
if (this.switchNetworkOnlyFromWallet) {
|
|
112
454
|
throw new DynamicError('Network switching is only supported through the wallet');
|
|
113
455
|
}
|
|
114
456
|
if (!this.supportsNetworkSwitching()) {
|
|
115
457
|
throw new DynamicError('Network switching not supported');
|
|
116
458
|
}
|
|
117
|
-
|
|
118
|
-
|
|
459
|
+
const walletClient = yield this.getWalletClientFromInitializedProvider();
|
|
460
|
+
if (this.isMetaMask()) {
|
|
461
|
+
const deepLink = this.getDeepLink();
|
|
462
|
+
if (deepLink) {
|
|
463
|
+
window.location.href = deepLink;
|
|
464
|
+
}
|
|
119
465
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
preference: this.deepLinkPreference,
|
|
125
|
-
uri: client.uri,
|
|
126
|
-
});
|
|
127
|
-
window.location.href = deepLink;
|
|
128
|
-
}
|
|
129
|
-
return _super.providerSwitchNetwork.call(this, { network, provider });
|
|
466
|
+
yield _super.providerSwitchNetwork.call(this, { network, provider: walletClient });
|
|
467
|
+
this.currentChainId = network.chainId;
|
|
468
|
+
this.hasSwitchedNetwork = true;
|
|
469
|
+
this.emit('chainChange', { chain: String(network.chainId) });
|
|
130
470
|
});
|
|
131
471
|
}
|
|
132
472
|
getConnectedAccounts() {
|
|
133
473
|
return __awaiter(this, void 0, void 0, function* () {
|
|
134
|
-
|
|
135
|
-
|
|
474
|
+
if (this.isInitialized === false) {
|
|
475
|
+
yield this.initProvider();
|
|
476
|
+
this.refreshSession();
|
|
477
|
+
this.isInitialized = true;
|
|
478
|
+
}
|
|
479
|
+
if (!this.activeAccount) {
|
|
136
480
|
return [];
|
|
137
|
-
|
|
481
|
+
}
|
|
482
|
+
return [this.activeAccount];
|
|
138
483
|
});
|
|
139
484
|
}
|
|
140
|
-
|
|
485
|
+
isMetaMask() {
|
|
486
|
+
var _a, _b, _c, _d, _e;
|
|
487
|
+
return ((_e = (_d = (_c = (_b = (_a = this.session) === null || _a === void 0 ? void 0 : _a.peer) === null || _b === void 0 ? void 0 : _b.metadata) === null || _c === void 0 ? void 0 : _c.name) === null || _d === void 0 ? void 0 : _d.toLowerCase().startsWith('metamask')) !== null && _e !== void 0 ? _e : false);
|
|
488
|
+
}
|
|
489
|
+
getSupportedNetworks() {
|
|
141
490
|
var _a;
|
|
142
491
|
return __awaiter(this, void 0, void 0, function* () {
|
|
143
|
-
|
|
492
|
+
yield this.initProvider();
|
|
493
|
+
this.refreshSession();
|
|
494
|
+
if (this.isMetaMask()) {
|
|
495
|
+
if (this.hasSwitchedNetwork) {
|
|
496
|
+
return [String(this.currentChainId)];
|
|
497
|
+
}
|
|
498
|
+
return this.evmNetworks.map((network) => network.chainId.toString());
|
|
499
|
+
}
|
|
500
|
+
if (!this.session) {
|
|
501
|
+
return [];
|
|
502
|
+
}
|
|
503
|
+
const chains = [];
|
|
504
|
+
// Some wallet (i.e ZenGo) use namespaces.account to list supported chains
|
|
505
|
+
// while others use keys within the namespaces object
|
|
506
|
+
Object.keys(this.session.namespaces).forEach((key) => {
|
|
507
|
+
if (key.startsWith('eip155:')) {
|
|
508
|
+
chains.push(key.split(':')[1]);
|
|
509
|
+
}
|
|
510
|
+
});
|
|
511
|
+
(_a = this.session.namespaces.eip155) === null || _a === void 0 ? void 0 : _a.accounts.forEach((account) => chains.push(account.split(':')[1]));
|
|
512
|
+
return chains.length
|
|
513
|
+
? chains
|
|
514
|
+
: this.evmNetworks.map((network) => network.chainId.toString());
|
|
144
515
|
});
|
|
145
516
|
}
|
|
146
517
|
}
|