@dynamic-labs/ethereum 1.0.0-alpha.9 → 1.0.1
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
CHANGED
|
@@ -1,4 +1,56 @@
|
|
|
1
1
|
|
|
2
|
+
### [1.0.1](https://github.com/dynamic-labs/DynamicAuth/compare/v1.0.0...v1.0.1) (2023-12-14)
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
### Bug Fixes
|
|
6
|
+
|
|
7
|
+
* update wagmi to v1.4.12 ([#4171](https://github.com/dynamic-labs/DynamicAuth/issues/4171)) ([a741989](https://github.com/dynamic-labs/DynamicAuth/commit/a74198978f4708e96b12743b2f1f372b23940020))
|
|
8
|
+
|
|
9
|
+
## [1.0.0](https://github.com/dynamic-labs/DynamicAuth/compare/v1.0.0-alpha.14...v1.0.0) (2023-12-12)
|
|
10
|
+
|
|
11
|
+
Changelog: https://docs.dynamic.xyz/changelog/v1
|
|
12
|
+
Upgrade guide: https://docs.dynamic.xyz/react-sdk/upgrade
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
## [1.0.0-alpha.14](https://github.com/dynamic-labs/DynamicAuth/compare/v1.0.0-alpha.13...v1.0.0-alpha.14) (2023-12-12)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Bug Fixes
|
|
19
|
+
|
|
20
|
+
* add user_media scope to instagram ([#4144](https://github.com/dynamic-labs/DynamicAuth/issues/4144)) ([57b9140](https://github.com/dynamic-labs/DynamicAuth/commit/57b91400cae7287157988cd6dfe7f30aeced10de))
|
|
21
|
+
* enable buy button onramp even with pregenerated wallets ([#4135](https://github.com/dynamic-labs/DynamicAuth/issues/4135)) ([fd94ecd](https://github.com/dynamic-labs/DynamicAuth/commit/fd94ecdcbe7190dc9abeb829cdcd933561afdd77))
|
|
22
|
+
* make the pin input fields responsive ([#4140](https://github.com/dynamic-labs/DynamicAuth/issues/4140)) ([4244744](https://github.com/dynamic-labs/DynamicAuth/commit/42447445ec32a3bca48a48d001c24dcd5b5cf004))
|
|
23
|
+
|
|
24
|
+
## [1.0.0-alpha.13](https://github.com/dynamic-labs/DynamicAuth/compare/v1.0.0-alpha.12...v1.0.0-alpha.13) (2023-12-12)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
### Bug Fixes
|
|
28
|
+
|
|
29
|
+
* embedded wallet connected flag not picking up ([#4128](https://github.com/dynamic-labs/DynamicAuth/issues/4128)) ([c6fbb1d](https://github.com/dynamic-labs/DynamicAuth/commit/c6fbb1de2027b186a4277a75545fd66d0d11ba95))
|
|
30
|
+
|
|
31
|
+
## [1.0.0-alpha.12](https://github.com/dynamic-labs/DynamicAuth/compare/v1.0.0-alpha.11...v1.0.0-alpha.12) (2023-12-11)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
### Features
|
|
35
|
+
|
|
36
|
+
* add useFunding hook ([#4098](https://github.com/dynamic-labs/DynamicAuth/issues/4098)) ([27424ab](https://github.com/dynamic-labs/DynamicAuth/commit/27424ab1a15202ee2b7c73c8a19f11e480e1794a))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
### Bug Fixes
|
|
40
|
+
|
|
41
|
+
* add primary color to the secure wallet text ([#4127](https://github.com/dynamic-labs/DynamicAuth/issues/4127)) ([7c642d0](https://github.com/dynamic-labs/DynamicAuth/commit/7c642d0385112289df52874bc5dd2eaa0e6cdf50))
|
|
42
|
+
|
|
43
|
+
## [1.0.0-alpha.11](https://github.com/dynamic-labs/DynamicAuth/compare/v1.0.0-alpha.10...v1.0.0-alpha.11) (2023-12-11)
|
|
44
|
+
|
|
45
|
+
## [1.0.0-alpha.10](https://github.com/dynamic-labs/DynamicAuth/compare/v1.0.0-alpha.9...v1.0.0-alpha.10) (2023-12-08)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
### Bug Fixes
|
|
49
|
+
|
|
50
|
+
* allow transaction error to read from viem error ([#4101](https://github.com/dynamic-labs/DynamicAuth/issues/4101)) ([0f46cbe](https://github.com/dynamic-labs/DynamicAuth/commit/0f46cbea1eb260c2efdeee9b62f669fb3cf4ab93))
|
|
51
|
+
* trigger claim flow for unclaimed embedded smart contract wallet signer ([#4108](https://github.com/dynamic-labs/DynamicAuth/issues/4108)) ([37c9013](https://github.com/dynamic-labs/DynamicAuth/commit/37c9013109f2d6a37bde041da9072c5f5fa2df67))
|
|
52
|
+
* WalletConnect v2 connector with non active chains ([#4085](https://github.com/dynamic-labs/DynamicAuth/issues/4085)) ([f69cbe9](https://github.com/dynamic-labs/DynamicAuth/commit/f69cbe96012563f71e89939c96f2a46926e7b394))
|
|
53
|
+
|
|
2
54
|
## [1.0.0-alpha.9](https://github.com/dynamic-labs/DynamicAuth/compare/v1.0.0-alpha.8...v1.0.0-alpha.9) (2023-12-07)
|
|
3
55
|
|
|
4
56
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dynamic-labs/ethereum",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/dynamic-labs/DynamicAuth.git",
|
|
@@ -32,12 +32,12 @@
|
|
|
32
32
|
"@walletconnect/universal-provider": "2.10.6",
|
|
33
33
|
"eventemitter3": "5.0.1",
|
|
34
34
|
"buffer": "6.0.3",
|
|
35
|
-
"@dynamic-labs/rpc-providers": "1.0.
|
|
36
|
-
"@dynamic-labs/turnkey": "1.0.
|
|
37
|
-
"@dynamic-labs/types": "1.0.
|
|
38
|
-
"@dynamic-labs/utils": "1.0.
|
|
39
|
-
"@dynamic-labs/wallet-book": "1.0.
|
|
40
|
-
"@dynamic-labs/wallet-connector-core": "1.0.
|
|
35
|
+
"@dynamic-labs/rpc-providers": "1.0.1",
|
|
36
|
+
"@dynamic-labs/turnkey": "1.0.1",
|
|
37
|
+
"@dynamic-labs/types": "1.0.1",
|
|
38
|
+
"@dynamic-labs/utils": "1.0.1",
|
|
39
|
+
"@dynamic-labs/wallet-book": "1.0.1",
|
|
40
|
+
"@dynamic-labs/wallet-connector-core": "1.0.1",
|
|
41
41
|
"stream": "0.0.2"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
@@ -149,7 +149,7 @@ class EthWalletConnector extends walletConnectorCore.WalletConnectorBase {
|
|
|
149
149
|
});
|
|
150
150
|
}
|
|
151
151
|
providerSwitchNetwork({ network, provider, }) {
|
|
152
|
-
var _a, _b, _c;
|
|
152
|
+
var _a, _b, _c, _d;
|
|
153
153
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
154
154
|
const { chainId } = network;
|
|
155
155
|
const currentNetworkId = yield this.getNetwork();
|
|
@@ -163,18 +163,19 @@ class EthWalletConnector extends walletConnectorCore.WalletConnectorBase {
|
|
|
163
163
|
return yield provider.switchChain(utils.getOrMapViemChain(network));
|
|
164
164
|
}
|
|
165
165
|
catch (error) {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
else if (error.code === 4902 ||
|
|
171
|
-
error.message.includes('Unrecognized chain') ||
|
|
166
|
+
// we need to check for unrecognized chain error first because it also contains 'rejected' in message
|
|
167
|
+
if (error.code === 4902 ||
|
|
168
|
+
((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('Unrecognized chain')) ||
|
|
172
169
|
// https://github.com/MetaMask/metamask-mobile/issues/3312#issuecomment-1065923294
|
|
173
|
-
((_c = (_b = error
|
|
170
|
+
((_c = (_b = error.data) === null || _b === void 0 ? void 0 : _b.orginalError) === null || _c === void 0 ? void 0 : _c.code) === 4902) {
|
|
174
171
|
// error code indicates the chain has not been added yet
|
|
175
172
|
// https://docs.metamask.io/guide/rpc-api.html#usage-with-wallet-switchethereumchain
|
|
176
173
|
return this.providerAddNetwork({ network, provider });
|
|
177
174
|
}
|
|
175
|
+
else if (((_d = error.message) === null || _d === void 0 ? void 0 : _d.includes('rejected')) ||
|
|
176
|
+
(typeof error === 'string' && error.includes('rejected'))) {
|
|
177
|
+
throw new utils.DynamicError("User rejected the wallet's request to switch network");
|
|
178
|
+
}
|
|
178
179
|
else {
|
|
179
180
|
throw error;
|
|
180
181
|
}
|
|
@@ -145,7 +145,7 @@ class EthWalletConnector extends WalletConnectorBase {
|
|
|
145
145
|
});
|
|
146
146
|
}
|
|
147
147
|
providerSwitchNetwork({ network, provider, }) {
|
|
148
|
-
var _a, _b, _c;
|
|
148
|
+
var _a, _b, _c, _d;
|
|
149
149
|
return __awaiter(this, void 0, void 0, function* () {
|
|
150
150
|
const { chainId } = network;
|
|
151
151
|
const currentNetworkId = yield this.getNetwork();
|
|
@@ -159,18 +159,19 @@ class EthWalletConnector extends WalletConnectorBase {
|
|
|
159
159
|
return yield provider.switchChain(getOrMapViemChain(network));
|
|
160
160
|
}
|
|
161
161
|
catch (error) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
}
|
|
166
|
-
else if (error.code === 4902 ||
|
|
167
|
-
error.message.includes('Unrecognized chain') ||
|
|
162
|
+
// we need to check for unrecognized chain error first because it also contains 'rejected' in message
|
|
163
|
+
if (error.code === 4902 ||
|
|
164
|
+
((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('Unrecognized chain')) ||
|
|
168
165
|
// https://github.com/MetaMask/metamask-mobile/issues/3312#issuecomment-1065923294
|
|
169
|
-
((_c = (_b = error
|
|
166
|
+
((_c = (_b = error.data) === null || _b === void 0 ? void 0 : _b.orginalError) === null || _c === void 0 ? void 0 : _c.code) === 4902) {
|
|
170
167
|
// error code indicates the chain has not been added yet
|
|
171
168
|
// https://docs.metamask.io/guide/rpc-api.html#usage-with-wallet-switchethereumchain
|
|
172
169
|
return this.providerAddNetwork({ network, provider });
|
|
173
170
|
}
|
|
171
|
+
else if (((_d = error.message) === null || _d === void 0 ? void 0 : _d.includes('rejected')) ||
|
|
172
|
+
(typeof error === 'string' && error.includes('rejected'))) {
|
|
173
|
+
throw new DynamicError("User rejected the wallet's request to switch network");
|
|
174
|
+
}
|
|
174
175
|
else {
|
|
175
176
|
throw error;
|
|
176
177
|
}
|
|
@@ -19,9 +19,12 @@ var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
|
|
|
19
19
|
|
|
20
20
|
const activeAccountKey = (walletName) => `dynamic-wc2-active-account-${walletName}`;
|
|
21
21
|
const sessionTopicKey = (walletName) => `dynamic-wc2-session-topic-${walletName}`;
|
|
22
|
+
const swicthedNetworkKey = (walletName) => `dynamic-wc2-switched-network-${walletName}`;
|
|
23
|
+
const currentChainKey = (walletName) => `dynamic-wc2-current-chain-${walletName}`;
|
|
22
24
|
const ee = new EventEmitter__default["default"]();
|
|
23
25
|
class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
24
26
|
constructor(opts) {
|
|
27
|
+
var _a;
|
|
25
28
|
super(opts);
|
|
26
29
|
this.supportedChains = ['EVM', 'ETH'];
|
|
27
30
|
this.connectedChain = 'EVM';
|
|
@@ -29,6 +32,9 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
29
32
|
this.canConnectViaQrCode = true;
|
|
30
33
|
this.isWalletConnect = true;
|
|
31
34
|
this.preferredChains = [];
|
|
35
|
+
// When trying to switch network for MetaMask, the switch promise gets stuck
|
|
36
|
+
// if the switch got trigged once already, so we need to keep track of that
|
|
37
|
+
this._hasSwitchedNetwork = false;
|
|
32
38
|
this.name = opts.walletName;
|
|
33
39
|
this.projectId = opts.projectId;
|
|
34
40
|
this.deepLinkPreference = opts.deepLinkPreference || 'native';
|
|
@@ -36,40 +42,17 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
36
42
|
if (!this.preferredChains.includes('eip155:1')) {
|
|
37
43
|
this.preferredChains.push('eip155:1');
|
|
38
44
|
}
|
|
45
|
+
this.hasSwitchedNetwork =
|
|
46
|
+
(_a = Boolean(localStorage.getItem(this.swicthedNetworkKey))) !== null && _a !== void 0 ? _a : false;
|
|
47
|
+
const lsCurrentChain = localStorage.getItem(this.currentChainKey);
|
|
48
|
+
this.currentChainId = lsCurrentChain
|
|
49
|
+
? parseIntSafe.parseIntSafe(lsCurrentChain)
|
|
50
|
+
: undefined;
|
|
39
51
|
}
|
|
40
|
-
|
|
41
|
-
const preferredNetwork = this.preferredChains.find((chain) => this.supportedChain(chain));
|
|
42
|
-
if (preferredNetwork) {
|
|
43
|
-
return preferredNetwork;
|
|
44
|
-
}
|
|
45
|
-
else if (this.findSupportedChains().length) {
|
|
46
|
-
return this.findSupportedChains()[0];
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
return 'eip155:1';
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
findSupportedChains() {
|
|
53
|
-
var _a;
|
|
54
|
-
try {
|
|
55
|
-
const wallet = walletBook.getWalletBookWallet(this.walletBook, this.key);
|
|
56
|
-
const supportedChains = (_a = this.evmNetworks) === null || _a === void 0 ? void 0 : _a.filter((network) => { var _a; return (_a = wallet === null || wallet === void 0 ? void 0 : wallet.chains) === null || _a === void 0 ? void 0 : _a.includes(`eip155:${network.chainId}`); }).map((network) => `eip155:${network.chainId}`);
|
|
57
|
-
return supportedChains;
|
|
58
|
-
}
|
|
59
|
-
catch (_b) {
|
|
60
|
-
// pass
|
|
61
|
-
}
|
|
62
|
-
return [];
|
|
63
|
-
}
|
|
64
|
-
supportedChain(chainId) {
|
|
65
|
-
var _a;
|
|
66
|
-
return (_a = this.findSupportedChains().filter((chain) => [`eip155:${chainId}`, chainId].includes(chain))) === null || _a === void 0 ? void 0 : _a[0];
|
|
67
|
-
}
|
|
68
|
-
optionalChains() {
|
|
52
|
+
getMappedChains() {
|
|
69
53
|
return (this.evmNetworks
|
|
70
|
-
// Filters out
|
|
71
|
-
.filter((network) => network.chainId !== 11297108109
|
|
72
|
-
`eip155:${network.chainId}` !== this.defaultChainId())
|
|
54
|
+
// Filters out palm that crashes Trust Wallet
|
|
55
|
+
.filter((network) => network.chainId !== 11297108109)
|
|
73
56
|
.map((network) => `eip155:${network.chainId}`));
|
|
74
57
|
}
|
|
75
58
|
initConnection() {
|
|
@@ -82,34 +65,26 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
82
65
|
if (provider === null || provider === void 0 ? void 0 : provider.uri) {
|
|
83
66
|
return;
|
|
84
67
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
}
|
|
68
|
+
const optionalNamespaces = {
|
|
69
|
+
eip155: {
|
|
70
|
+
chains: this.getMappedChains(),
|
|
71
|
+
events: ['chainChanged', 'accountsChanged'],
|
|
72
|
+
methods: [
|
|
73
|
+
'eth_chainId',
|
|
74
|
+
'eth_signTypedData',
|
|
75
|
+
'eth_signTransaction',
|
|
76
|
+
'eth_sign',
|
|
77
|
+
'personal_sign',
|
|
78
|
+
'eth_sendTransaction',
|
|
79
|
+
'eth_signTypedData_v4',
|
|
80
|
+
'wallet_switchEthereumChain',
|
|
81
|
+
'wallet_addEthereumChain',
|
|
82
|
+
],
|
|
83
|
+
rpcMap: this.evmNetworkRpcMap(),
|
|
84
|
+
},
|
|
85
|
+
};
|
|
103
86
|
provider
|
|
104
87
|
.connect({
|
|
105
|
-
namespaces: {
|
|
106
|
-
eip155: {
|
|
107
|
-
chains: [this.defaultChainId()],
|
|
108
|
-
events: ['chainChanged', 'accountsChanged'],
|
|
109
|
-
methods: ['personal_sign', 'eth_sendTransaction'],
|
|
110
|
-
rpcMap: this.evmNetworkRpcMap(),
|
|
111
|
-
},
|
|
112
|
-
},
|
|
113
88
|
optionalNamespaces,
|
|
114
89
|
})
|
|
115
90
|
.catch((e) => {
|
|
@@ -125,6 +100,8 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
125
100
|
projectId: this.projectId,
|
|
126
101
|
});
|
|
127
102
|
WalletConnectV2.provider = provider;
|
|
103
|
+
this.teardownEventListeners();
|
|
104
|
+
this.setupEventListeners();
|
|
128
105
|
});
|
|
129
106
|
}
|
|
130
107
|
initProvider() {
|
|
@@ -161,6 +138,36 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
161
138
|
get activeAccountKey() {
|
|
162
139
|
return activeAccountKey(this.key);
|
|
163
140
|
}
|
|
141
|
+
get swicthedNetworkKey() {
|
|
142
|
+
return swicthedNetworkKey(this.key);
|
|
143
|
+
}
|
|
144
|
+
get currentChainKey() {
|
|
145
|
+
return currentChainKey(this.key);
|
|
146
|
+
}
|
|
147
|
+
set currentChainId(value) {
|
|
148
|
+
this._currentChainId = value;
|
|
149
|
+
if (value) {
|
|
150
|
+
localStorage.setItem(this.currentChainKey, value.toString());
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
localStorage.removeItem(this.currentChainKey);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
get currentChainId() {
|
|
157
|
+
return this._currentChainId;
|
|
158
|
+
}
|
|
159
|
+
set hasSwitchedNetwork(value) {
|
|
160
|
+
this._hasSwitchedNetwork = value;
|
|
161
|
+
if (value) {
|
|
162
|
+
localStorage.setItem(this.swicthedNetworkKey, value.toString());
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
localStorage.removeItem(this.swicthedNetworkKey);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
get hasSwitchedNetwork() {
|
|
169
|
+
return this._hasSwitchedNetwork;
|
|
170
|
+
}
|
|
164
171
|
supportsNetworkSwitching() {
|
|
165
172
|
return true;
|
|
166
173
|
}
|
|
@@ -169,6 +176,7 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
169
176
|
return;
|
|
170
177
|
}
|
|
171
178
|
WalletConnectV2.provider.client.on('session_event', ({ params }) => {
|
|
179
|
+
walletConnectorCore.logger.debug('session_event was called', { params });
|
|
172
180
|
if (!params || !params.event) {
|
|
173
181
|
walletConnectorCore.logger.debug('session_event was called without params or params.event');
|
|
174
182
|
return;
|
|
@@ -180,6 +188,7 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
180
188
|
walletConnectorCore.logger.debug(`received unexpected data for chainChanged: ${data} with type ${typeof data}}`);
|
|
181
189
|
return;
|
|
182
190
|
}
|
|
191
|
+
this.currentChainId = chainId;
|
|
183
192
|
this.emit('chainChange', { chain: String(chainId) });
|
|
184
193
|
// When a user switches network from their wallet, we need the provider to change network
|
|
185
194
|
// such that any future calls to `getNetwork` will return the correct network
|
|
@@ -358,6 +367,8 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
358
367
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
359
368
|
this.clearActiveAccount();
|
|
360
369
|
this.clearSession();
|
|
370
|
+
this.hasSwitchedNetwork = false;
|
|
371
|
+
this.currentChainId = undefined;
|
|
361
372
|
if (!((_a = WalletConnectV2.provider) === null || _a === void 0 ? void 0 : _a.session)) {
|
|
362
373
|
return;
|
|
363
374
|
}
|
|
@@ -377,6 +388,9 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
377
388
|
getNetwork: { get: () => super.getNetwork }
|
|
378
389
|
});
|
|
379
390
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
391
|
+
if (this.currentChainId) {
|
|
392
|
+
return this.currentChainId;
|
|
393
|
+
}
|
|
380
394
|
yield this.initProvider();
|
|
381
395
|
return _super.getNetwork.call(this);
|
|
382
396
|
});
|
|
@@ -400,6 +414,8 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
400
414
|
throw new utils.DynamicError('Provider not found');
|
|
401
415
|
}
|
|
402
416
|
yield _super.providerSwitchNetwork.call(this, { network, provider });
|
|
417
|
+
this.currentChainId = network.chainId;
|
|
418
|
+
this.hasSwitchedNetwork = true;
|
|
403
419
|
this.emit('chainChange', { chain: String(network.chainId) });
|
|
404
420
|
});
|
|
405
421
|
}
|
|
@@ -417,7 +433,11 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
417
433
|
});
|
|
418
434
|
}
|
|
419
435
|
getSupportedNetworks() {
|
|
436
|
+
var _a;
|
|
420
437
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
438
|
+
if (!this.hasSwitchedNetwork) {
|
|
439
|
+
return undefined;
|
|
440
|
+
}
|
|
421
441
|
yield this.initProvider();
|
|
422
442
|
this.refreshSession();
|
|
423
443
|
if (!this.session) {
|
|
@@ -431,7 +451,7 @@ class WalletConnectV2 extends EthWalletConnector.EthWalletConnector {
|
|
|
431
451
|
chains.push(key.split(':')[1]);
|
|
432
452
|
}
|
|
433
453
|
});
|
|
434
|
-
this.session.namespaces.eip155.accounts.forEach((account) => chains.push(account.split(':')[1]));
|
|
454
|
+
(_a = this.session.namespaces.eip155) === null || _a === void 0 ? void 0 : _a.accounts.forEach((account) => chains.push(account.split(':')[1]));
|
|
435
455
|
return chains.length ? chains : undefined;
|
|
436
456
|
});
|
|
437
457
|
}
|
|
@@ -23,11 +23,10 @@ export declare class WalletConnectV2 extends EthWalletConnector {
|
|
|
23
23
|
private projectId?;
|
|
24
24
|
private deepLinkPreference;
|
|
25
25
|
private preferredChains;
|
|
26
|
+
private _currentChainId;
|
|
27
|
+
private _hasSwitchedNetwork;
|
|
26
28
|
constructor(opts: WalletConnectorV2Opts);
|
|
27
|
-
private
|
|
28
|
-
private findSupportedChains;
|
|
29
|
-
private supportedChain;
|
|
30
|
-
private optionalChains;
|
|
29
|
+
private getMappedChains;
|
|
31
30
|
private initConnection;
|
|
32
31
|
private createInitProviderPromise;
|
|
33
32
|
private initProvider;
|
|
@@ -35,6 +34,12 @@ export declare class WalletConnectV2 extends EthWalletConnector {
|
|
|
35
34
|
init(): Promise<void>;
|
|
36
35
|
get sessionTopicKey(): string;
|
|
37
36
|
get activeAccountKey(): string;
|
|
37
|
+
get swicthedNetworkKey(): string;
|
|
38
|
+
get currentChainKey(): string;
|
|
39
|
+
private set currentChainId(value);
|
|
40
|
+
private get currentChainId();
|
|
41
|
+
private set hasSwitchedNetwork(value);
|
|
42
|
+
private get hasSwitchedNetwork();
|
|
38
43
|
supportsNetworkSwitching(): boolean;
|
|
39
44
|
setupEventListeners(): void;
|
|
40
45
|
teardownEventListeners(): void;
|
|
@@ -10,9 +10,12 @@ import { parseIntSafe } from '../utils/parseIntSafe.js';
|
|
|
10
10
|
|
|
11
11
|
const activeAccountKey = (walletName) => `dynamic-wc2-active-account-${walletName}`;
|
|
12
12
|
const sessionTopicKey = (walletName) => `dynamic-wc2-session-topic-${walletName}`;
|
|
13
|
+
const swicthedNetworkKey = (walletName) => `dynamic-wc2-switched-network-${walletName}`;
|
|
14
|
+
const currentChainKey = (walletName) => `dynamic-wc2-current-chain-${walletName}`;
|
|
13
15
|
const ee = new EventEmitter();
|
|
14
16
|
class WalletConnectV2 extends EthWalletConnector {
|
|
15
17
|
constructor(opts) {
|
|
18
|
+
var _a;
|
|
16
19
|
super(opts);
|
|
17
20
|
this.supportedChains = ['EVM', 'ETH'];
|
|
18
21
|
this.connectedChain = 'EVM';
|
|
@@ -20,6 +23,9 @@ class WalletConnectV2 extends EthWalletConnector {
|
|
|
20
23
|
this.canConnectViaQrCode = true;
|
|
21
24
|
this.isWalletConnect = true;
|
|
22
25
|
this.preferredChains = [];
|
|
26
|
+
// When trying to switch network for MetaMask, the switch promise gets stuck
|
|
27
|
+
// if the switch got trigged once already, so we need to keep track of that
|
|
28
|
+
this._hasSwitchedNetwork = false;
|
|
23
29
|
this.name = opts.walletName;
|
|
24
30
|
this.projectId = opts.projectId;
|
|
25
31
|
this.deepLinkPreference = opts.deepLinkPreference || 'native';
|
|
@@ -27,40 +33,17 @@ class WalletConnectV2 extends EthWalletConnector {
|
|
|
27
33
|
if (!this.preferredChains.includes('eip155:1')) {
|
|
28
34
|
this.preferredChains.push('eip155:1');
|
|
29
35
|
}
|
|
36
|
+
this.hasSwitchedNetwork =
|
|
37
|
+
(_a = Boolean(localStorage.getItem(this.swicthedNetworkKey))) !== null && _a !== void 0 ? _a : false;
|
|
38
|
+
const lsCurrentChain = localStorage.getItem(this.currentChainKey);
|
|
39
|
+
this.currentChainId = lsCurrentChain
|
|
40
|
+
? parseIntSafe(lsCurrentChain)
|
|
41
|
+
: undefined;
|
|
30
42
|
}
|
|
31
|
-
|
|
32
|
-
const preferredNetwork = this.preferredChains.find((chain) => this.supportedChain(chain));
|
|
33
|
-
if (preferredNetwork) {
|
|
34
|
-
return preferredNetwork;
|
|
35
|
-
}
|
|
36
|
-
else if (this.findSupportedChains().length) {
|
|
37
|
-
return this.findSupportedChains()[0];
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
return 'eip155:1';
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
findSupportedChains() {
|
|
44
|
-
var _a;
|
|
45
|
-
try {
|
|
46
|
-
const wallet = getWalletBookWallet(this.walletBook, this.key);
|
|
47
|
-
const supportedChains = (_a = this.evmNetworks) === null || _a === void 0 ? void 0 : _a.filter((network) => { var _a; return (_a = wallet === null || wallet === void 0 ? void 0 : wallet.chains) === null || _a === void 0 ? void 0 : _a.includes(`eip155:${network.chainId}`); }).map((network) => `eip155:${network.chainId}`);
|
|
48
|
-
return supportedChains;
|
|
49
|
-
}
|
|
50
|
-
catch (_b) {
|
|
51
|
-
// pass
|
|
52
|
-
}
|
|
53
|
-
return [];
|
|
54
|
-
}
|
|
55
|
-
supportedChain(chainId) {
|
|
56
|
-
var _a;
|
|
57
|
-
return (_a = this.findSupportedChains().filter((chain) => [`eip155:${chainId}`, chainId].includes(chain))) === null || _a === void 0 ? void 0 : _a[0];
|
|
58
|
-
}
|
|
59
|
-
optionalChains() {
|
|
43
|
+
getMappedChains() {
|
|
60
44
|
return (this.evmNetworks
|
|
61
|
-
// Filters out
|
|
62
|
-
.filter((network) => network.chainId !== 11297108109
|
|
63
|
-
`eip155:${network.chainId}` !== this.defaultChainId())
|
|
45
|
+
// Filters out palm that crashes Trust Wallet
|
|
46
|
+
.filter((network) => network.chainId !== 11297108109)
|
|
64
47
|
.map((network) => `eip155:${network.chainId}`));
|
|
65
48
|
}
|
|
66
49
|
initConnection() {
|
|
@@ -73,34 +56,26 @@ class WalletConnectV2 extends EthWalletConnector {
|
|
|
73
56
|
if (provider === null || provider === void 0 ? void 0 : provider.uri) {
|
|
74
57
|
return;
|
|
75
58
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
}
|
|
59
|
+
const optionalNamespaces = {
|
|
60
|
+
eip155: {
|
|
61
|
+
chains: this.getMappedChains(),
|
|
62
|
+
events: ['chainChanged', 'accountsChanged'],
|
|
63
|
+
methods: [
|
|
64
|
+
'eth_chainId',
|
|
65
|
+
'eth_signTypedData',
|
|
66
|
+
'eth_signTransaction',
|
|
67
|
+
'eth_sign',
|
|
68
|
+
'personal_sign',
|
|
69
|
+
'eth_sendTransaction',
|
|
70
|
+
'eth_signTypedData_v4',
|
|
71
|
+
'wallet_switchEthereumChain',
|
|
72
|
+
'wallet_addEthereumChain',
|
|
73
|
+
],
|
|
74
|
+
rpcMap: this.evmNetworkRpcMap(),
|
|
75
|
+
},
|
|
76
|
+
};
|
|
94
77
|
provider
|
|
95
78
|
.connect({
|
|
96
|
-
namespaces: {
|
|
97
|
-
eip155: {
|
|
98
|
-
chains: [this.defaultChainId()],
|
|
99
|
-
events: ['chainChanged', 'accountsChanged'],
|
|
100
|
-
methods: ['personal_sign', 'eth_sendTransaction'],
|
|
101
|
-
rpcMap: this.evmNetworkRpcMap(),
|
|
102
|
-
},
|
|
103
|
-
},
|
|
104
79
|
optionalNamespaces,
|
|
105
80
|
})
|
|
106
81
|
.catch((e) => {
|
|
@@ -116,6 +91,8 @@ class WalletConnectV2 extends EthWalletConnector {
|
|
|
116
91
|
projectId: this.projectId,
|
|
117
92
|
});
|
|
118
93
|
WalletConnectV2.provider = provider;
|
|
94
|
+
this.teardownEventListeners();
|
|
95
|
+
this.setupEventListeners();
|
|
119
96
|
});
|
|
120
97
|
}
|
|
121
98
|
initProvider() {
|
|
@@ -152,6 +129,36 @@ class WalletConnectV2 extends EthWalletConnector {
|
|
|
152
129
|
get activeAccountKey() {
|
|
153
130
|
return activeAccountKey(this.key);
|
|
154
131
|
}
|
|
132
|
+
get swicthedNetworkKey() {
|
|
133
|
+
return swicthedNetworkKey(this.key);
|
|
134
|
+
}
|
|
135
|
+
get currentChainKey() {
|
|
136
|
+
return currentChainKey(this.key);
|
|
137
|
+
}
|
|
138
|
+
set currentChainId(value) {
|
|
139
|
+
this._currentChainId = value;
|
|
140
|
+
if (value) {
|
|
141
|
+
localStorage.setItem(this.currentChainKey, value.toString());
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
localStorage.removeItem(this.currentChainKey);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
get currentChainId() {
|
|
148
|
+
return this._currentChainId;
|
|
149
|
+
}
|
|
150
|
+
set hasSwitchedNetwork(value) {
|
|
151
|
+
this._hasSwitchedNetwork = value;
|
|
152
|
+
if (value) {
|
|
153
|
+
localStorage.setItem(this.swicthedNetworkKey, value.toString());
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
localStorage.removeItem(this.swicthedNetworkKey);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
get hasSwitchedNetwork() {
|
|
160
|
+
return this._hasSwitchedNetwork;
|
|
161
|
+
}
|
|
155
162
|
supportsNetworkSwitching() {
|
|
156
163
|
return true;
|
|
157
164
|
}
|
|
@@ -160,6 +167,7 @@ class WalletConnectV2 extends EthWalletConnector {
|
|
|
160
167
|
return;
|
|
161
168
|
}
|
|
162
169
|
WalletConnectV2.provider.client.on('session_event', ({ params }) => {
|
|
170
|
+
logger.debug('session_event was called', { params });
|
|
163
171
|
if (!params || !params.event) {
|
|
164
172
|
logger.debug('session_event was called without params or params.event');
|
|
165
173
|
return;
|
|
@@ -171,6 +179,7 @@ class WalletConnectV2 extends EthWalletConnector {
|
|
|
171
179
|
logger.debug(`received unexpected data for chainChanged: ${data} with type ${typeof data}}`);
|
|
172
180
|
return;
|
|
173
181
|
}
|
|
182
|
+
this.currentChainId = chainId;
|
|
174
183
|
this.emit('chainChange', { chain: String(chainId) });
|
|
175
184
|
// When a user switches network from their wallet, we need the provider to change network
|
|
176
185
|
// such that any future calls to `getNetwork` will return the correct network
|
|
@@ -349,6 +358,8 @@ class WalletConnectV2 extends EthWalletConnector {
|
|
|
349
358
|
return __awaiter(this, void 0, void 0, function* () {
|
|
350
359
|
this.clearActiveAccount();
|
|
351
360
|
this.clearSession();
|
|
361
|
+
this.hasSwitchedNetwork = false;
|
|
362
|
+
this.currentChainId = undefined;
|
|
352
363
|
if (!((_a = WalletConnectV2.provider) === null || _a === void 0 ? void 0 : _a.session)) {
|
|
353
364
|
return;
|
|
354
365
|
}
|
|
@@ -368,6 +379,9 @@ class WalletConnectV2 extends EthWalletConnector {
|
|
|
368
379
|
getNetwork: { get: () => super.getNetwork }
|
|
369
380
|
});
|
|
370
381
|
return __awaiter(this, void 0, void 0, function* () {
|
|
382
|
+
if (this.currentChainId) {
|
|
383
|
+
return this.currentChainId;
|
|
384
|
+
}
|
|
371
385
|
yield this.initProvider();
|
|
372
386
|
return _super.getNetwork.call(this);
|
|
373
387
|
});
|
|
@@ -391,6 +405,8 @@ class WalletConnectV2 extends EthWalletConnector {
|
|
|
391
405
|
throw new DynamicError('Provider not found');
|
|
392
406
|
}
|
|
393
407
|
yield _super.providerSwitchNetwork.call(this, { network, provider });
|
|
408
|
+
this.currentChainId = network.chainId;
|
|
409
|
+
this.hasSwitchedNetwork = true;
|
|
394
410
|
this.emit('chainChange', { chain: String(network.chainId) });
|
|
395
411
|
});
|
|
396
412
|
}
|
|
@@ -408,7 +424,11 @@ class WalletConnectV2 extends EthWalletConnector {
|
|
|
408
424
|
});
|
|
409
425
|
}
|
|
410
426
|
getSupportedNetworks() {
|
|
427
|
+
var _a;
|
|
411
428
|
return __awaiter(this, void 0, void 0, function* () {
|
|
429
|
+
if (!this.hasSwitchedNetwork) {
|
|
430
|
+
return undefined;
|
|
431
|
+
}
|
|
412
432
|
yield this.initProvider();
|
|
413
433
|
this.refreshSession();
|
|
414
434
|
if (!this.session) {
|
|
@@ -422,7 +442,7 @@ class WalletConnectV2 extends EthWalletConnector {
|
|
|
422
442
|
chains.push(key.split(':')[1]);
|
|
423
443
|
}
|
|
424
444
|
});
|
|
425
|
-
this.session.namespaces.eip155.accounts.forEach((account) => chains.push(account.split(':')[1]));
|
|
445
|
+
(_a = this.session.namespaces.eip155) === null || _a === void 0 ? void 0 : _a.accounts.forEach((account) => chains.push(account.split(':')[1]));
|
|
426
446
|
return chains.length ? chains : undefined;
|
|
427
447
|
});
|
|
428
448
|
}
|