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