@dynamic-labs/ethereum 4.0.0-alpha.50 → 4.0.0-alpha.52
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 +10 -0
- package/package.cjs +1 -1
- package/package.js +1 -1
- package/package.json +9 -9
- package/src/utils/normalizeRpcError/normalizeRpcError.cjs +33 -2
- package/src/utils/normalizeRpcError/normalizeRpcError.js +33 -2
- package/src/walletConnect/walletConnect.cjs +171 -194
- package/src/walletConnect/walletConnect.d.ts +7677 -21
- package/src/walletConnect/walletConnect.js +172 -195
package/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
|
|
2
|
+
## [4.0.0-alpha.52](https://github.com/dynamic-labs/dynamic-auth/compare/v4.0.0-alpha.51...v4.0.0-alpha.52) (2025-01-02)
|
|
3
|
+
|
|
4
|
+
## [4.0.0-alpha.51](https://github.com/dynamic-labs/dynamic-auth/compare/v4.0.0-alpha.50...v4.0.0-alpha.51) (2024-12-30)
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
### Bug Fixes
|
|
8
|
+
|
|
9
|
+
* changing copy of linking same wallet ([#7489](https://github.com/dynamic-labs/dynamic-auth/issues/7489)) ([e7d6b95](https://github.com/dynamic-labs/dynamic-auth/commit/e7d6b957043eaf8c06a4e8ef480ec9b027abdb5e))
|
|
10
|
+
* normalize user reject error from Rainbow and BitGet ([#7721](https://github.com/dynamic-labs/dynamic-auth/issues/7721)) ([78a2e41](https://github.com/dynamic-labs/dynamic-auth/commit/78a2e41e235743f0c652be746a8bee74d4e06629))
|
|
11
|
+
|
|
2
12
|
## [4.0.0-alpha.50](https://github.com/dynamic-labs/dynamic-auth/compare/v4.0.0-alpha.49...v4.0.0-alpha.50) (2024-12-28)
|
|
3
13
|
|
|
4
14
|
|
package/package.cjs
CHANGED
package/package.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dynamic-labs/ethereum",
|
|
3
|
-
"version": "4.0.0-alpha.
|
|
3
|
+
"version": "4.0.0-alpha.52",
|
|
4
4
|
"description": "A React SDK for implementing wallet web3 authentication and authorization to your website.",
|
|
5
5
|
"author": "Dynamic Labs, Inc.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,14 +24,14 @@
|
|
|
24
24
|
"eventemitter3": "5.0.1",
|
|
25
25
|
"buffer": "6.0.3",
|
|
26
26
|
"@metamask/sdk": "0.30.1",
|
|
27
|
-
"@dynamic-labs/assert-package-version": "4.0.0-alpha.
|
|
28
|
-
"@dynamic-labs/embedded-wallet-evm": "4.0.0-alpha.
|
|
29
|
-
"@dynamic-labs/ethereum-core": "4.0.0-alpha.
|
|
30
|
-
"@dynamic-labs/logger": "4.0.0-alpha.
|
|
31
|
-
"@dynamic-labs/types": "4.0.0-alpha.
|
|
32
|
-
"@dynamic-labs/utils": "4.0.0-alpha.
|
|
33
|
-
"@dynamic-labs/wallet-book": "4.0.0-alpha.
|
|
34
|
-
"@dynamic-labs/wallet-connector-core": "4.0.0-alpha.
|
|
27
|
+
"@dynamic-labs/assert-package-version": "4.0.0-alpha.52",
|
|
28
|
+
"@dynamic-labs/embedded-wallet-evm": "4.0.0-alpha.52",
|
|
29
|
+
"@dynamic-labs/ethereum-core": "4.0.0-alpha.52",
|
|
30
|
+
"@dynamic-labs/logger": "4.0.0-alpha.52",
|
|
31
|
+
"@dynamic-labs/types": "4.0.0-alpha.52",
|
|
32
|
+
"@dynamic-labs/utils": "4.0.0-alpha.52",
|
|
33
|
+
"@dynamic-labs/wallet-book": "4.0.0-alpha.52",
|
|
34
|
+
"@dynamic-labs/wallet-connector-core": "4.0.0-alpha.52"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
37
|
"viem": "^2.7.12"
|
|
@@ -11,10 +11,41 @@ var viem = require('viem');
|
|
|
11
11
|
*/
|
|
12
12
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
13
13
|
const normalizeRpcError = (err) => {
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
/**
|
|
15
|
+
* When the error already comply with the EIP-1193 standard, we don't need to normalize it
|
|
16
|
+
*/
|
|
17
|
+
if (err.code === 4001) {
|
|
18
|
+
throw err;
|
|
16
19
|
}
|
|
20
|
+
let mappedError = null;
|
|
21
|
+
try {
|
|
22
|
+
mappedError = mapRpcError(err);
|
|
23
|
+
}
|
|
24
|
+
catch (e) {
|
|
25
|
+
// ignore errors when mapping
|
|
26
|
+
}
|
|
27
|
+
if (mappedError) {
|
|
28
|
+
throw mappedError;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* If no error is mapped, we rethrow the original error
|
|
32
|
+
*/
|
|
17
33
|
throw err;
|
|
18
34
|
};
|
|
35
|
+
const mapRpcError = (err) => {
|
|
36
|
+
/**
|
|
37
|
+
* Checks for user rejection error message
|
|
38
|
+
* Rainbow Extension will return a message like "User rejected the request"
|
|
39
|
+
* BitGet will return a message like "user reject this request"
|
|
40
|
+
* Keplr will return a message that matches "Request rejected"
|
|
41
|
+
*/
|
|
42
|
+
if (typeof err.message === 'string' &&
|
|
43
|
+
(err.message.includes('User rejected the request') ||
|
|
44
|
+
err.message.includes('user reject this request') ||
|
|
45
|
+
err.message === 'Request rejected')) {
|
|
46
|
+
return new viem.UserRejectedRequestError(err);
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
};
|
|
19
50
|
|
|
20
51
|
exports.normalizeRpcError = normalizeRpcError;
|
|
@@ -7,10 +7,41 @@ import { UserRejectedRequestError } from 'viem';
|
|
|
7
7
|
*/
|
|
8
8
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
9
|
const normalizeRpcError = (err) => {
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
/**
|
|
11
|
+
* When the error already comply with the EIP-1193 standard, we don't need to normalize it
|
|
12
|
+
*/
|
|
13
|
+
if (err.code === 4001) {
|
|
14
|
+
throw err;
|
|
12
15
|
}
|
|
16
|
+
let mappedError = null;
|
|
17
|
+
try {
|
|
18
|
+
mappedError = mapRpcError(err);
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
// ignore errors when mapping
|
|
22
|
+
}
|
|
23
|
+
if (mappedError) {
|
|
24
|
+
throw mappedError;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* If no error is mapped, we rethrow the original error
|
|
28
|
+
*/
|
|
13
29
|
throw err;
|
|
14
30
|
};
|
|
31
|
+
const mapRpcError = (err) => {
|
|
32
|
+
/**
|
|
33
|
+
* Checks for user rejection error message
|
|
34
|
+
* Rainbow Extension will return a message like "User rejected the request"
|
|
35
|
+
* BitGet will return a message like "user reject this request"
|
|
36
|
+
* Keplr will return a message that matches "Request rejected"
|
|
37
|
+
*/
|
|
38
|
+
if (typeof err.message === 'string' &&
|
|
39
|
+
(err.message.includes('User rejected the request') ||
|
|
40
|
+
err.message.includes('user reject this request') ||
|
|
41
|
+
err.message === 'Request rejected')) {
|
|
42
|
+
return new UserRejectedRequestError(err);
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
};
|
|
15
46
|
|
|
16
47
|
export { normalizeRpcError };
|
|
@@ -18,47 +18,154 @@ var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
|
|
|
18
18
|
|
|
19
19
|
const activeAccountKey = (walletName) => `dynamic-wc2-active-account-${walletName}`;
|
|
20
20
|
const sessionTopicKey = (walletName) => `dynamic-wc2-session-topic-${walletName}`;
|
|
21
|
-
const swicthedNetworkKey = (walletName) => `dynamic-wc2-switched-network-${walletName}`;
|
|
22
21
|
const currentChainKey = (walletName) => `dynamic-wc2-current-chain-${walletName}`;
|
|
23
22
|
const ee = new EventEmitter__default["default"]();
|
|
24
23
|
class WalletConnect extends ethereumCore.EthereumWalletConnector {
|
|
24
|
+
supportsNetworkSwitching() {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
25
27
|
constructor(opts) {
|
|
26
|
-
var _a;
|
|
27
28
|
super(opts);
|
|
28
29
|
this.isInitialized = false;
|
|
29
30
|
this.canConnectViaQrCode = true;
|
|
30
31
|
this.isWalletConnect = true;
|
|
31
32
|
this.preferredChains = [];
|
|
32
|
-
// When trying to switch network for MetaMask, the switch promise gets stuck
|
|
33
|
-
// if the switch got trigged once already, so we need to keep track of that
|
|
34
|
-
this._hasSwitchedNetwork = false;
|
|
35
33
|
this.sessionEventHandler = () => { };
|
|
36
34
|
this.sessionDeleteHandler = () => { };
|
|
37
35
|
this.name = opts.walletName;
|
|
38
36
|
this.projectId = opts.projectId;
|
|
39
37
|
this.deepLinkPreference = opts.deepLinkPreference || 'native';
|
|
40
38
|
this.preferredChains = opts.walletConnectPreferredChains || [];
|
|
41
|
-
this.hasSwitchedNetwork =
|
|
42
|
-
(_a = Boolean(localStorage.getItem(this.swicthedNetworkKey))) !== null && _a !== void 0 ? _a : false;
|
|
43
39
|
const lsCurrentChain = localStorage.getItem(this.currentChainKey);
|
|
44
40
|
this.currentChainId = lsCurrentChain
|
|
45
41
|
? utils.parseIntSafe(lsCurrentChain)
|
|
46
42
|
: undefined;
|
|
47
43
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
+
});
|
|
134
|
+
}
|
|
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
|
+
});
|
|
53
142
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return
|
|
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
|
+
});
|
|
59
165
|
}
|
|
60
166
|
initConnection() {
|
|
61
167
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
168
|
+
walletConnectorCore.logger.debug('initConnection was called', this.name);
|
|
62
169
|
const { provider } = WalletConnect;
|
|
63
170
|
if (!provider) {
|
|
64
171
|
throw new utils.DynamicError('No provider found (init connection)');
|
|
@@ -75,6 +182,7 @@ class WalletConnect extends ethereumCore.EthereumWalletConnector {
|
|
|
75
182
|
}
|
|
76
183
|
createProvider() {
|
|
77
184
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
185
|
+
walletConnectorCore.logger.debug('createProvider was called', this.name);
|
|
78
186
|
return EthereumProvider__default["default"].init({
|
|
79
187
|
events: ['chainChanged', 'accountsChanged'],
|
|
80
188
|
methods: [],
|
|
@@ -96,65 +204,49 @@ class WalletConnect extends ethereumCore.EthereumWalletConnector {
|
|
|
96
204
|
});
|
|
97
205
|
});
|
|
98
206
|
}
|
|
99
|
-
|
|
100
|
-
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
101
|
-
const walletConnect = this.createProvider();
|
|
102
|
-
const walletClient = viem.createWalletClient({
|
|
103
|
-
account: this.getActiveAccount(),
|
|
104
|
-
transport: viem.custom(yield walletConnect),
|
|
105
|
-
});
|
|
106
|
-
return walletClient;
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
createInitProviderPromise() {
|
|
110
|
-
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
111
|
-
WalletConnect.provider = yield this.createProvider();
|
|
112
|
-
this.teardownEventListeners();
|
|
113
|
-
this.setupEventListeners();
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
// We need to add a gate to this method since we will be calling it asynchronously
|
|
117
|
-
// from different places (such as setShowAuthFlow), which means there's a chance for
|
|
118
|
-
// a race condition to happen where createInitProviderPromise is called multiple times
|
|
119
|
-
initProvider() {
|
|
207
|
+
refreshSession() {
|
|
120
208
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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));
|
|
125
216
|
}
|
|
126
|
-
yield this.initializePromise;
|
|
127
217
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
((_d = (_c = WalletConnect.provider) === null || _c === void 0 ? void 0 : _c.session) === null || _d === void 0 ? void 0 : _d.topic)) {
|
|
135
|
-
this.session = WalletConnect.provider.session;
|
|
136
|
-
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 });
|
|
137
224
|
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
init() {
|
|
141
|
-
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
142
|
-
yield this.initProvider();
|
|
143
|
-
yield this.initConnection();
|
|
144
|
-
this.isInitialized = true;
|
|
145
225
|
});
|
|
146
226
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
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]));
|
|
152
232
|
}
|
|
153
|
-
|
|
154
|
-
|
|
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;
|
|
155
247
|
}
|
|
156
|
-
get
|
|
157
|
-
return
|
|
248
|
+
get currentChainId() {
|
|
249
|
+
return this._currentChainId;
|
|
158
250
|
}
|
|
159
251
|
set currentChainId(value) {
|
|
160
252
|
this._currentChainId = value;
|
|
@@ -165,23 +257,14 @@ class WalletConnect extends ethereumCore.EthereumWalletConnector {
|
|
|
165
257
|
localStorage.removeItem(this.currentChainKey);
|
|
166
258
|
}
|
|
167
259
|
}
|
|
168
|
-
get
|
|
169
|
-
return this.
|
|
170
|
-
}
|
|
171
|
-
set hasSwitchedNetwork(value) {
|
|
172
|
-
this._hasSwitchedNetwork = value;
|
|
173
|
-
if (value) {
|
|
174
|
-
localStorage.setItem(this.swicthedNetworkKey, value.toString());
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
localStorage.removeItem(this.swicthedNetworkKey);
|
|
178
|
-
}
|
|
260
|
+
get sessionTopicKey() {
|
|
261
|
+
return sessionTopicKey(this.key);
|
|
179
262
|
}
|
|
180
|
-
get
|
|
181
|
-
return this.
|
|
263
|
+
get activeAccountKey() {
|
|
264
|
+
return activeAccountKey(this.key);
|
|
182
265
|
}
|
|
183
|
-
|
|
184
|
-
return
|
|
266
|
+
get currentChainKey() {
|
|
267
|
+
return currentChainKey(this.key);
|
|
185
268
|
}
|
|
186
269
|
setupEventListeners() {
|
|
187
270
|
if (!WalletConnect.provider) {
|
|
@@ -206,7 +289,6 @@ class WalletConnect extends ethereumCore.EthereumWalletConnector {
|
|
|
206
289
|
}
|
|
207
290
|
this.currentChainId = chainId;
|
|
208
291
|
this.emit('chainChange', { chain: String(chainId) });
|
|
209
|
-
this.hasSwitchedNetwork = true;
|
|
210
292
|
// When a user switches network from their wallet, we need the provider to change network
|
|
211
293
|
// such that any future calls to `getNetwork` will return the correct network
|
|
212
294
|
this.switchNetwork({ networkChainId: chainId });
|
|
@@ -235,91 +317,6 @@ class WalletConnect extends ethereumCore.EthereumWalletConnector {
|
|
|
235
317
|
WalletConnect.provider.off('session_event', this.sessionEventHandler);
|
|
236
318
|
WalletConnect.provider.off('session_delete', this.sessionDeleteHandler);
|
|
237
319
|
}
|
|
238
|
-
getWalletClient(chainId) {
|
|
239
|
-
if (!WalletConnect.provider) {
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
return viem.createWalletClient({
|
|
243
|
-
account: this.getActiveAccount(),
|
|
244
|
-
chain: ethereumCore.chainsMap[chainId !== null && chainId !== void 0 ? chainId : String(this.currentChainId)],
|
|
245
|
-
transport: viem.custom(WalletConnect.provider),
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
getAddress(opts) {
|
|
249
|
-
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
250
|
-
var _a, _b;
|
|
251
|
-
const activeAccount = this.getActiveAccount();
|
|
252
|
-
if (activeAccount === null || activeAccount === void 0 ? void 0 : activeAccount.address) {
|
|
253
|
-
return activeAccount.address;
|
|
254
|
-
}
|
|
255
|
-
if (!WalletConnect.provider || !((_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.signer.uri)) {
|
|
256
|
-
walletConnectorCore.logger.debug('No WC2 provider found, re-initializing...');
|
|
257
|
-
yield this.endSession();
|
|
258
|
-
yield this.init();
|
|
259
|
-
// sleep 1 s to wait for connect call to finish
|
|
260
|
-
// the connect call isn't await-ed because it only resolves once
|
|
261
|
-
// the connection is established, but we need to wait for it to
|
|
262
|
-
// finish setting up the connection URI and making it available
|
|
263
|
-
// on the provider
|
|
264
|
-
yield utils.sleep(1000);
|
|
265
|
-
if (!WalletConnect.provider || !((_b = WalletConnect.provider) === null || _b === void 0 ? void 0 : _b.signer.uri)) {
|
|
266
|
-
walletConnectorCore.logger.debug('No WC2 provider found, escaping and throwing error');
|
|
267
|
-
throw new utils.DynamicError('No provider found');
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
walletConnectorCore.performPlatformSpecificConnectionMethod(WalletConnect.provider.signer.uri, this.metadata.deepLinks, {
|
|
271
|
-
onDesktopUri: opts === null || opts === void 0 ? void 0 : opts.onDesktopUri,
|
|
272
|
-
onDisplayUri: opts === null || opts === void 0 ? void 0 : opts.onDisplayUri,
|
|
273
|
-
}, this.deepLinkPreference);
|
|
274
|
-
return new Promise((resolve, reject) => {
|
|
275
|
-
if (!WalletConnect.provider) {
|
|
276
|
-
reject(new utils.DynamicError('No provider found'));
|
|
277
|
-
return;
|
|
278
|
-
}
|
|
279
|
-
const onFail = () => {
|
|
280
|
-
const error = new utils.DynamicError('Connection rejected. Please try again.');
|
|
281
|
-
error.code = 'connection_rejected';
|
|
282
|
-
if (WalletConnect.provider) {
|
|
283
|
-
WalletConnect.provider.signer.uri = undefined;
|
|
284
|
-
// this is needed for mobile to work when using universal links.
|
|
285
|
-
// if the user cancels the connection, we need to re-initialize the provider
|
|
286
|
-
// so that the async work is done ahead of time, before the user tries to connect again,
|
|
287
|
-
// otherwise they will trigger the iOS bug where they are redirected to the app store
|
|
288
|
-
this.init();
|
|
289
|
-
}
|
|
290
|
-
reject(error);
|
|
291
|
-
// We must clean up the onConnect and onFail listeners
|
|
292
|
-
// whenever the connection attempt either succeeds or fails
|
|
293
|
-
cleanupListeners();
|
|
294
|
-
};
|
|
295
|
-
const onConnect = () => {
|
|
296
|
-
var _a;
|
|
297
|
-
const session = (_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.session;
|
|
298
|
-
if (!session) {
|
|
299
|
-
reject(new utils.DynamicError('No session found'));
|
|
300
|
-
return;
|
|
301
|
-
}
|
|
302
|
-
this.setSession(session);
|
|
303
|
-
this.setWCActiveAccount(session.namespaces.eip155.accounts[0].split(':')[2]);
|
|
304
|
-
this.getNetwork().then((chainId) => {
|
|
305
|
-
var _a;
|
|
306
|
-
this.currentChainId = chainId;
|
|
307
|
-
resolve((_a = this.getActiveAccount()) === null || _a === void 0 ? void 0 : _a.address);
|
|
308
|
-
});
|
|
309
|
-
// We must clean up the onConnect and onFail listeners
|
|
310
|
-
// whenever the connection attempt either succeeds or fails
|
|
311
|
-
cleanupListeners();
|
|
312
|
-
};
|
|
313
|
-
const cleanupListeners = () => {
|
|
314
|
-
var _a;
|
|
315
|
-
ee.off('walletconnect_connection_failed', onFail);
|
|
316
|
-
(_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.off('connect', onConnect);
|
|
317
|
-
};
|
|
318
|
-
ee.on('walletconnect_connection_failed', onFail);
|
|
319
|
-
WalletConnect.provider.on('connect', onConnect);
|
|
320
|
-
});
|
|
321
|
-
});
|
|
322
|
-
}
|
|
323
320
|
/**
|
|
324
321
|
* WalletConnect V2 will fail to send the sign message request if the chainId
|
|
325
322
|
* is not the same as the one in the session. This method will wait for the
|
|
@@ -379,7 +376,7 @@ class WalletConnect extends ethereumCore.EthereumWalletConnector {
|
|
|
379
376
|
if (!activeAccount) {
|
|
380
377
|
return;
|
|
381
378
|
}
|
|
382
|
-
const walletClient = yield this.
|
|
379
|
+
const walletClient = yield this.getWalletClient();
|
|
383
380
|
return walletClient.signMessage({
|
|
384
381
|
account: activeAccount,
|
|
385
382
|
message: messageToSign,
|
|
@@ -411,7 +408,6 @@ class WalletConnect extends ethereumCore.EthereumWalletConnector {
|
|
|
411
408
|
var _a;
|
|
412
409
|
this.clearActiveAccount();
|
|
413
410
|
this.clearSession();
|
|
414
|
-
this.hasSwitchedNetwork = false;
|
|
415
411
|
this.currentChainId = undefined;
|
|
416
412
|
if (!((_a = WalletConnect.provider) === null || _a === void 0 ? void 0 : _a.session)) {
|
|
417
413
|
return;
|
|
@@ -457,27 +453,18 @@ class WalletConnect extends ethereumCore.EthereumWalletConnector {
|
|
|
457
453
|
if (this.switchNetworkOnlyFromWallet) {
|
|
458
454
|
throw new utils.DynamicError('Network switching is only supported through the wallet');
|
|
459
455
|
}
|
|
460
|
-
|
|
461
|
-
throw new utils.DynamicError('Network switching not supported');
|
|
462
|
-
}
|
|
463
|
-
const walletClient = yield this.getWalletClientFromInitializedProvider();
|
|
464
|
-
if (this.isMetaMask()) {
|
|
465
|
-
const deepLink = this.getDeepLink();
|
|
466
|
-
if (deepLink) {
|
|
467
|
-
window.location.href = deepLink;
|
|
468
|
-
}
|
|
469
|
-
}
|
|
456
|
+
const walletClient = yield this.getWalletClient();
|
|
470
457
|
yield _super.providerSwitchNetwork.call(this, { network, provider: walletClient });
|
|
471
458
|
this.currentChainId = network.chainId;
|
|
472
|
-
this.hasSwitchedNetwork = true;
|
|
473
459
|
this.emit('chainChange', { chain: String(network.chainId) });
|
|
474
460
|
});
|
|
475
461
|
}
|
|
476
462
|
getConnectedAccounts() {
|
|
477
463
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
478
464
|
if (this.isInitialized === false) {
|
|
465
|
+
// TODO why not just call init()?
|
|
479
466
|
yield this.initProvider();
|
|
480
|
-
this.refreshSession();
|
|
467
|
+
yield this.refreshSession();
|
|
481
468
|
this.isInitialized = true;
|
|
482
469
|
}
|
|
483
470
|
const activeAccount = this.getActiveAccount();
|
|
@@ -487,21 +474,11 @@ class WalletConnect extends ethereumCore.EthereumWalletConnector {
|
|
|
487
474
|
return [activeAccount.address];
|
|
488
475
|
});
|
|
489
476
|
}
|
|
490
|
-
isMetaMask() {
|
|
491
|
-
var _a, _b, _c, _d, _e;
|
|
492
|
-
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);
|
|
493
|
-
}
|
|
494
477
|
getSupportedNetworks() {
|
|
495
478
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
496
479
|
var _a;
|
|
497
480
|
yield this.initProvider();
|
|
498
|
-
this.refreshSession();
|
|
499
|
-
if (this.isMetaMask()) {
|
|
500
|
-
if (this.hasSwitchedNetwork) {
|
|
501
|
-
return [String(this.currentChainId)];
|
|
502
|
-
}
|
|
503
|
-
return this.evmNetworks.map((network) => network.chainId.toString());
|
|
504
|
-
}
|
|
481
|
+
yield this.refreshSession();
|
|
505
482
|
if (!this.session) {
|
|
506
483
|
return [];
|
|
507
484
|
}
|