@rango-dev/provider-walletconnect-2 0.20.1-next.2 → 0.21.1-next.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 +4 -0
- package/dist/helpers.d.ts +7 -1
- package/dist/helpers.d.ts.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +3 -3
- package/dist/session.d.ts +9 -18
- package/dist/session.d.ts.map +1 -1
- package/dist/signers/evm.d.ts.map +1 -1
- package/dist/types.d.ts +0 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +12 -11
- package/src/helpers.ts +37 -81
- package/src/index.ts +43 -46
- package/src/session.ts +44 -107
- package/src/signers/evm.ts +6 -0
- package/src/types.ts +0 -1
package/dist/session.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { ConnectParams, CreateSessionParams
|
|
1
|
+
import type { ConnectParams, CreateSessionParams } from './types';
|
|
2
2
|
import type { SignClient } from '@walletconnect/sign-client/dist/types/client';
|
|
3
3
|
import type { PairingTypes, SessionTypes, SignClientTypes } from '@walletconnect/types';
|
|
4
|
-
import type { BlockchainMeta } from 'rango-types
|
|
4
|
+
import type { BlockchainMeta } from 'rango-types';
|
|
5
5
|
export declare function getLastSession(client: SignClient): SessionTypes.Struct;
|
|
6
6
|
/**
|
|
7
7
|
*
|
|
@@ -9,7 +9,7 @@ export declare function getLastSession(client: SignClient): SessionTypes.Struct;
|
|
|
9
9
|
* If the wallet didn't respond during 10 seconds (PING_TIME), we assume the wallet isn't available and we need to create a new session.
|
|
10
10
|
*
|
|
11
11
|
*/
|
|
12
|
-
export declare function restoreSession(client: SignClient,
|
|
12
|
+
export declare function restoreSession(client: SignClient, pairingTopic: PairingTypes.Struct['topic']): Promise<SessionTypes.Struct | undefined>;
|
|
13
13
|
/**
|
|
14
14
|
*
|
|
15
15
|
* Getting a pair of required and optional namespaces then tries to show a modal and connect (pair)
|
|
@@ -32,16 +32,7 @@ export declare function tryGetPairing(client: SignClient): PairingTypes.Struct |
|
|
|
32
32
|
* Try to restore the session first, if couldn't, create a new session by showing a modal.
|
|
33
33
|
*
|
|
34
34
|
*/
|
|
35
|
-
export declare function tryConnect(client: SignClient, params: ConnectParams): Promise<
|
|
36
|
-
session: SessionTypes.Struct;
|
|
37
|
-
isNew: boolean;
|
|
38
|
-
}>;
|
|
39
|
-
/**
|
|
40
|
-
* Wallet connect is a multichain protocol and we can not determine the connected wallet
|
|
41
|
-
* supports which wallet, `extend`ing session doesn't work during a bug in their utils packages.
|
|
42
|
-
* So we will try to make a new session with `network` that user needs to switch.
|
|
43
|
-
*/
|
|
44
|
-
export declare function trySwitchByCreatingNewSession(instance: WCInstance, params: ConnectParams): Promise<SessionTypes.Struct>;
|
|
35
|
+
export declare function tryConnect(client: SignClient, params: ConnectParams): Promise<SessionTypes.Struct>;
|
|
45
36
|
/**
|
|
46
37
|
*
|
|
47
38
|
* Try to find sessions with a topic id and expire them.
|
|
@@ -54,8 +45,8 @@ export declare function cleanupSingleSession(client: SignClient, topic: string):
|
|
|
54
45
|
*
|
|
55
46
|
*/
|
|
56
47
|
export declare function disconnectSessions(client: SignClient): Promise<void[]>;
|
|
57
|
-
export declare function getAccountsFromSession(session: SessionTypes.Struct
|
|
58
|
-
|
|
48
|
+
export declare function getAccountsFromSession(session: SessionTypes.Struct): {
|
|
49
|
+
address: string;
|
|
59
50
|
chainId: string;
|
|
60
51
|
}[];
|
|
61
52
|
export declare function getAccountsFromEvent(event: SignClientTypes.BaseEventArgs<{
|
|
@@ -65,7 +56,7 @@ export declare function getAccountsFromEvent(event: SignClientTypes.BaseEventArg
|
|
|
65
56
|
chainId: string;
|
|
66
57
|
}[];
|
|
67
58
|
export declare function updateSessionAccounts(instance: any, requestedNetwork: string, currentNetwork: string, meta: BlockchainMeta[]): Promise<void>;
|
|
68
|
-
export declare function
|
|
69
|
-
export declare function persistCurrentChainId(
|
|
70
|
-
export declare function getPersistedChainId(
|
|
59
|
+
export declare function ignoreNamespaceMethods(instance: any): boolean;
|
|
60
|
+
export declare function persistCurrentChainId(client: SignClient, chainId?: string): Promise<void>;
|
|
61
|
+
export declare function getPersistedChainId(client: SignClient): Promise<string | undefined>;
|
|
71
62
|
//# sourceMappingURL=session.d.ts.map
|
package/dist/session.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,mBAAmB,EAAE,
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAClE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8CAA8C,CAAC;AAC/E,OAAO,KAAK,EACV,YAAY,EACZ,YAAY,EACZ,eAAe,EAChB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAelD,wBAAgB,cAAc,CAAC,MAAM,EAAE,UAAU,uBAEhD;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,MAAM,EAAE,UAAU,EAClB,YAAY,EAAE,YAAY,CAAC,MAAM,CAAC,OAAO,CAAC,GACzC,OAAO,CAAC,YAAY,CAAC,MAAM,GAAG,SAAS,CAAC,CAW1C;AAED;;;;;;;GAOG;AACH,wBAAsB,aAAa,CACjC,MAAM,EAAE,UAAU,EAClB,OAAO,EAAE,mBAAmB,GAC3B,OAAO,CAAC,YAAY,CAAC,MAAM,CAAC,CA+C9B;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAC3B,MAAM,EAAE,UAAU,GACjB,YAAY,CAAC,MAAM,GAAG,SAAS,CAMjC;AAED;;;;GAIG;AACH,wBAAsB,UAAU,CAC9B,MAAM,EAAE,UAA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CAAC;IA6B5B,OAAO,CAAC,iCAAiC;CAwD1C;AAED,eAAe,SAAS,CAAC"}
|
package/dist/types.d.ts
CHANGED
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8CAA8C,CAAC;AAC/E,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACxE,OAAO,KAAK,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAE5E,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,UAAU,CAAC;IACnB,OAAO,EAAE,YAAY,CAAC,MAAM,GAAG,IAAI,CAAC;IACpC,OAAO,EAAE,CAAC,MAAM,EAAE,GAAG,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;CAC3C;AAED,MAAM,WAAW,mBAAmB;IAClC,kBAAkB,EAAE,aAAa,CAAC,kBAAkB,CAAC;IACrD,kBAAkB,CAAC,EAAE,aAAa,CAAC,kBAAkB,CAAC;IACtD,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,aAAa;IAC5B,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8CAA8C,CAAC;AAC/E,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACxE,OAAO,KAAK,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAE5E,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,UAAU,CAAC;IACnB,OAAO,EAAE,YAAY,CAAC,MAAM,GAAG,IAAI,CAAC;IACpC,OAAO,EAAE,CAAC,MAAM,EAAE,GAAG,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;CAC3C;AAED,MAAM,WAAW,mBAAmB;IAClC,kBAAkB,EAAE,aAAa,CAAC,kBAAkB,CAAC;IACrD,kBAAkB,CAAC,EAAE,aAAa,CAAC,kBAAkB,CAAC;IACtD,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,cAAc,EAAE,CAAC;CACxB;AAED,MAAM,WAAW,UAAW,SAAQ,oBAAoB;IAEtD,OAAO,EAAE,MAAM,CAAC;CACjB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rango-dev/provider-walletconnect-2",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.1-next.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -24,22 +24,23 @@
|
|
|
24
24
|
"@cosmjs/launchpad": "^0.27.1",
|
|
25
25
|
"@keplr-wallet/cosmos": "^0.9.12",
|
|
26
26
|
"@keplr-wallet/simple-fetch": "^0.12.14",
|
|
27
|
-
"@rango-dev/
|
|
28
|
-
"@rango-dev/signer-
|
|
29
|
-
"@rango-dev/signer-
|
|
30
|
-
"@rango-dev/
|
|
31
|
-
"@
|
|
32
|
-
"@
|
|
33
|
-
"@walletconnect/
|
|
34
|
-
"@walletconnect/
|
|
35
|
-
"@walletconnect/
|
|
27
|
+
"@rango-dev/logging-core": "^0.1.1-next.0",
|
|
28
|
+
"@rango-dev/signer-cosmos": "^0.26.0",
|
|
29
|
+
"@rango-dev/signer-evm": "^0.26.0",
|
|
30
|
+
"@rango-dev/signer-solana": "^0.26.0",
|
|
31
|
+
"@rango-dev/wallets-shared": "^0.28.1-next.0",
|
|
32
|
+
"@solana/web3.js": "^1.67.2",
|
|
33
|
+
"@walletconnect/encoding": "^1.0.2",
|
|
34
|
+
"@walletconnect/modal": "^2.6.2",
|
|
35
|
+
"@walletconnect/sign-client": "^2.11.2",
|
|
36
|
+
"@walletconnect/utils": "^2.11.2",
|
|
36
37
|
"bs58": "^5.0.0",
|
|
37
38
|
"caip": "^1.1.0",
|
|
38
39
|
"cosmos-wallet": "^1.2.0",
|
|
39
40
|
"rango-types": "^0.1.59"
|
|
40
41
|
},
|
|
41
42
|
"devDependencies": {
|
|
42
|
-
"@walletconnect/types": "2.
|
|
43
|
+
"@walletconnect/types": "^2.11.2"
|
|
43
44
|
},
|
|
44
45
|
"publishConfig": {
|
|
45
46
|
"access": "public"
|
package/src/helpers.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { CosmosMeta } from './types';
|
|
2
1
|
import type { WalletState } from '@rango-dev/wallets-shared';
|
|
3
2
|
import type { ProposalTypes } from '@walletconnect/types';
|
|
4
3
|
import type { BlockchainMeta } from 'rango-types';
|
|
@@ -10,18 +9,12 @@ import {
|
|
|
10
9
|
} from '@rango-dev/wallets-shared';
|
|
11
10
|
import { WalletConnectModal } from '@walletconnect/modal';
|
|
12
11
|
import { AccountId, ChainId } from 'caip';
|
|
13
|
-
import {
|
|
14
|
-
cosmosBlockchains,
|
|
15
|
-
evmBlockchains,
|
|
16
|
-
isEvmBlockchain,
|
|
17
|
-
} from 'rango-types';
|
|
12
|
+
import { evmBlockchains, isEvmBlockchain } from 'rango-types';
|
|
18
13
|
|
|
19
14
|
import {
|
|
20
|
-
DEFAULT_COSMOS_METHODS,
|
|
21
15
|
DEFAULT_ETHEREUM_EVENTS,
|
|
22
16
|
DEFAULT_ETHEREUM_METHODS,
|
|
23
17
|
DEFAULT_SOLANA_CHAIN_ID,
|
|
24
|
-
DEFAULT_SOLANA_METHODS,
|
|
25
18
|
EthereumRPCMethods,
|
|
26
19
|
NAMESPACES,
|
|
27
20
|
} from './constants';
|
|
@@ -47,75 +40,21 @@ type FinalNamespaces = {
|
|
|
47
40
|
[key in NAMESPACES]?: ProposalTypes.BaseRequiredNamespace;
|
|
48
41
|
};
|
|
49
42
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const cosmos = cosmosBlockchains(meta);
|
|
56
|
-
|
|
57
|
-
const requiredEvmChain = evm.find((chain) => chain.name === network);
|
|
58
|
-
const requiredCosmosChain = cosmos.find((chain) => chain.name === network);
|
|
59
|
-
const requiredSolanaChain = network === Networks.SOLANA;
|
|
60
|
-
|
|
61
|
-
if (requiredEvmChain) {
|
|
62
|
-
return {
|
|
63
|
-
[NAMESPACES.ETHEREUM]: {
|
|
64
|
-
events: DEFAULT_ETHEREUM_EVENTS,
|
|
65
|
-
methods: DEFAULT_ETHEREUM_METHODS,
|
|
66
|
-
chains: [
|
|
67
|
-
new ChainId({
|
|
68
|
-
namespace: NAMESPACES.ETHEREUM,
|
|
69
|
-
reference: String(parseInt(requiredEvmChain.chainId)),
|
|
70
|
-
}).toString(),
|
|
71
|
-
],
|
|
72
|
-
},
|
|
73
|
-
};
|
|
74
|
-
} else if (!!requiredCosmosChain) {
|
|
75
|
-
return {
|
|
76
|
-
[NAMESPACES.COSMOS]: {
|
|
77
|
-
events: [],
|
|
78
|
-
methods: DEFAULT_COSMOS_METHODS,
|
|
79
|
-
chains: [
|
|
80
|
-
new ChainId({
|
|
81
|
-
namespace: NAMESPACES.COSMOS,
|
|
82
|
-
reference: requiredCosmosChain.chainId!,
|
|
83
|
-
}).toString(),
|
|
84
|
-
],
|
|
85
|
-
},
|
|
86
|
-
};
|
|
87
|
-
} else if (requiredSolanaChain) {
|
|
88
|
-
return {
|
|
89
|
-
[NAMESPACES.SOLANA]: {
|
|
90
|
-
events: [],
|
|
91
|
-
methods: DEFAULT_SOLANA_METHODS,
|
|
92
|
-
chains: [`solana:${DEFAULT_SOLANA_CHAIN_ID}`],
|
|
93
|
-
},
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return undefined;
|
|
98
|
-
}
|
|
99
|
-
|
|
43
|
+
/*
|
|
44
|
+
* Some wallets like 1inch (android) has problem when we pass cosmos chains in optional namespace
|
|
45
|
+
* Also some wallets like keplr mobile doesn't work when we don't pass cosmos chains in required namespace
|
|
46
|
+
* It seems to be a bug in their current implementation.
|
|
47
|
+
*/
|
|
100
48
|
export function generateOptionalNamespace(
|
|
101
49
|
meta: BlockchainMeta[]
|
|
102
50
|
): FinalNamespaces | undefined {
|
|
103
51
|
const evm = evmBlockchains(meta);
|
|
104
|
-
const cosmos = cosmosBlockchains(meta);
|
|
105
52
|
const evmChains = evm.map((chain) => {
|
|
106
53
|
return new ChainId({
|
|
107
54
|
namespace: NAMESPACES.ETHEREUM,
|
|
108
55
|
reference: String(parseInt(chain.chainId)),
|
|
109
56
|
}).toString();
|
|
110
57
|
});
|
|
111
|
-
const cosmosChains = cosmos
|
|
112
|
-
.filter((chain): chain is CosmosMeta => !!chain.chainId)
|
|
113
|
-
.map((chain) => {
|
|
114
|
-
return new ChainId({
|
|
115
|
-
namespace: NAMESPACES.COSMOS,
|
|
116
|
-
reference: chain.chainId,
|
|
117
|
-
}).toString();
|
|
118
|
-
});
|
|
119
58
|
|
|
120
59
|
const namespaces: FinalNamespaces = {
|
|
121
60
|
[NAMESPACES.ETHEREUM]: {
|
|
@@ -123,21 +62,7 @@ export function generateOptionalNamespace(
|
|
|
123
62
|
events: DEFAULT_ETHEREUM_EVENTS,
|
|
124
63
|
chains: evmChains,
|
|
125
64
|
},
|
|
126
|
-
[NAMESPACES.SOLANA]: {
|
|
127
|
-
methods: DEFAULT_SOLANA_METHODS,
|
|
128
|
-
events: [],
|
|
129
|
-
chains: [`solana:${DEFAULT_SOLANA_CHAIN_ID}`],
|
|
130
|
-
},
|
|
131
65
|
};
|
|
132
|
-
|
|
133
|
-
if (cosmosChains.length) {
|
|
134
|
-
namespaces[NAMESPACES.COSMOS] = {
|
|
135
|
-
methods: DEFAULT_COSMOS_METHODS,
|
|
136
|
-
events: [],
|
|
137
|
-
chains: cosmosChains,
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
|
|
141
66
|
return namespaces;
|
|
142
67
|
}
|
|
143
68
|
|
|
@@ -282,3 +207,34 @@ export function getEvmAccount(
|
|
|
282
207
|
address,
|
|
283
208
|
});
|
|
284
209
|
}
|
|
210
|
+
|
|
211
|
+
// It's enough to return only connected network for the EVM networks and ignore others
|
|
212
|
+
export function filterEvmAccounts(
|
|
213
|
+
accounts: {
|
|
214
|
+
address: string;
|
|
215
|
+
chainId: string;
|
|
216
|
+
}[],
|
|
217
|
+
currentChainId?: string
|
|
218
|
+
) {
|
|
219
|
+
let firstEvmAddress = false;
|
|
220
|
+
return accounts
|
|
221
|
+
.filter(({ address, chainId }) => {
|
|
222
|
+
const isEvm = isEvmAddress(address);
|
|
223
|
+
if (isEvm) {
|
|
224
|
+
if (currentChainId && chainId !== currentChainId) {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
if (!currentChainId) {
|
|
228
|
+
if (firstEvmAddress) {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
firstEvmAddress = true;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return true;
|
|
235
|
+
})
|
|
236
|
+
.map(({ address, chainId }) => ({
|
|
237
|
+
accounts: [address],
|
|
238
|
+
chainId: chainId,
|
|
239
|
+
}));
|
|
240
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
import type { ISignClient } from '@walletconnect/types';
|
|
13
13
|
import type { BlockchainMeta, SignerFactory } from 'rango-types';
|
|
14
14
|
|
|
15
|
+
import { debug, error as logError } from '@rango-dev/logging-core';
|
|
15
16
|
import { Networks, WalletTypes } from '@rango-dev/wallets-shared';
|
|
16
17
|
import Client from '@walletconnect/sign-client';
|
|
17
18
|
import { AccountId, ChainId } from 'caip';
|
|
@@ -21,10 +22,13 @@ import {
|
|
|
21
22
|
DEFAULT_APP_METADATA,
|
|
22
23
|
DEFAULT_NETWORK,
|
|
23
24
|
EthereumEvents,
|
|
25
|
+
EthereumRPCMethods,
|
|
26
|
+
NAMESPACES,
|
|
24
27
|
RELAY_URL,
|
|
25
28
|
} from './constants';
|
|
26
29
|
import {
|
|
27
30
|
createModalInstance,
|
|
31
|
+
filterEvmAccounts,
|
|
28
32
|
simulateRequest,
|
|
29
33
|
switchOrAddEvmChain,
|
|
30
34
|
} from './helpers';
|
|
@@ -34,10 +38,9 @@ import {
|
|
|
34
38
|
getAccountsFromEvent,
|
|
35
39
|
getAccountsFromSession,
|
|
36
40
|
getPersistedChainId,
|
|
37
|
-
|
|
41
|
+
ignoreNamespaceMethods,
|
|
38
42
|
persistCurrentChainId,
|
|
39
43
|
tryConnect,
|
|
40
|
-
trySwitchByCreatingNewSession,
|
|
41
44
|
updateSessionAccounts,
|
|
42
45
|
} from './session';
|
|
43
46
|
import signer from './signer';
|
|
@@ -94,35 +97,15 @@ export const getInstance: GetInstance = async (options) => {
|
|
|
94
97
|
};
|
|
95
98
|
};
|
|
96
99
|
|
|
97
|
-
export const connect: Connect = async ({ instance,
|
|
100
|
+
export const connect: Connect = async ({ instance, meta }) => {
|
|
98
101
|
const { client } = instance as WCInstance;
|
|
99
|
-
|
|
100
|
-
const requestedNetwork = network || DEFAULT_NETWORK;
|
|
101
|
-
|
|
102
102
|
// Try to restore the session first, if couldn't, create a new session by showing a modal.
|
|
103
|
-
const
|
|
104
|
-
network: requestedNetwork,
|
|
105
|
-
meta,
|
|
106
|
-
});
|
|
103
|
+
const session = await tryConnect(client, { meta });
|
|
107
104
|
// Override the value (session).
|
|
108
105
|
instance.session = session;
|
|
109
|
-
const currentChainId =
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const accounts = getAccountsFromSession(session, currentChainId);
|
|
113
|
-
/*
|
|
114
|
-
* TODO: we need to fix next lines to support multiple accounts
|
|
115
|
-
* for now, it will return the current evm account on the current chain
|
|
116
|
-
*/
|
|
117
|
-
if (!isNew) {
|
|
118
|
-
return {
|
|
119
|
-
chainId: String(currentChainId),
|
|
120
|
-
accounts: accounts?.[0].accounts,
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
const newChainId = accounts?.[accounts.length - 1].chainId;
|
|
124
|
-
void persistCurrentChainId(instance, newChainId);
|
|
125
|
-
return accounts?.[accounts.length - 1];
|
|
106
|
+
const currentChainId = await getPersistedChainId(client);
|
|
107
|
+
const accounts = getAccountsFromSession(session);
|
|
108
|
+
return filterEvmAccounts(accounts, currentChainId);
|
|
126
109
|
};
|
|
127
110
|
|
|
128
111
|
export const subscribe: Subscribe = ({
|
|
@@ -158,7 +141,7 @@ export const subscribe: Subscribe = ({
|
|
|
158
141
|
} else if (args.params.event.name === EthereumEvents.CHAIN_CHANGED) {
|
|
159
142
|
const chainId = args.params.event.data;
|
|
160
143
|
updateChainId(chainId);
|
|
161
|
-
void persistCurrentChainId(instance, chainId);
|
|
144
|
+
void persistCurrentChainId(instance.client, chainId);
|
|
162
145
|
} else {
|
|
163
146
|
console.log('[WC2] session_event not supported', args.params.event);
|
|
164
147
|
}
|
|
@@ -176,26 +159,40 @@ export const switchNetwork: SwitchNetwork = async ({
|
|
|
176
159
|
instance,
|
|
177
160
|
meta,
|
|
178
161
|
getState,
|
|
162
|
+
updateChainId,
|
|
179
163
|
}) => {
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
if (
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
164
|
+
const evm = evmBlockchains(meta);
|
|
165
|
+
const chainId = evm.find((chain) => chain.name === network)?.chainId;
|
|
166
|
+
if (!chainId) {
|
|
167
|
+
const error = new Error(`There is no match for ${chainId}`);
|
|
168
|
+
logError(error);
|
|
169
|
+
throw error;
|
|
170
|
+
}
|
|
171
|
+
const chaindIdStr = new ChainId({
|
|
172
|
+
namespace: NAMESPACES.ETHEREUM,
|
|
173
|
+
reference: String(parseInt(chainId)),
|
|
174
|
+
}).toString();
|
|
175
|
+
|
|
176
|
+
const session = instance.session;
|
|
177
|
+
const evmNamespace = session.namespaces[NAMESPACES.ETHEREUM];
|
|
178
|
+
const authorizedChains = evmNamespace?.chains || [];
|
|
179
|
+
const authorizedMethods = evmNamespace?.methods || [];
|
|
180
|
+
|
|
181
|
+
if (
|
|
182
|
+
authorizedMethods.includes(EthereumRPCMethods.SWITCH_CHAIN) &&
|
|
183
|
+
!ignoreNamespaceMethods(instance)
|
|
184
|
+
) {
|
|
185
|
+
const currentNetwork = getState?.().network || Networks.ETHEREUM;
|
|
186
|
+
await updateSessionAccounts(instance, network, currentNetwork, meta);
|
|
187
|
+
await switchOrAddEvmChain(meta, network, currentNetwork, instance);
|
|
188
|
+
} else if (authorizedChains.includes(chaindIdStr)) {
|
|
189
|
+
updateChainId(chainId);
|
|
194
190
|
return;
|
|
191
|
+
} else {
|
|
192
|
+
const error = new Error(`Chain ${chainId} is not configured.`);
|
|
193
|
+
logError(error);
|
|
194
|
+
throw error;
|
|
195
195
|
}
|
|
196
|
-
const currentNetwork = getState?.().network || Networks.ETHEREUM;
|
|
197
|
-
await updateSessionAccounts(instance, network, currentNetwork, meta);
|
|
198
|
-
await switchOrAddEvmChain(meta, network, currentNetwork, instance);
|
|
199
196
|
};
|
|
200
197
|
|
|
201
198
|
/**
|
|
@@ -211,7 +208,7 @@ export const disconnect: Disconnect = async ({ instance }) => {
|
|
|
211
208
|
const { client } = instance as WCInstance;
|
|
212
209
|
|
|
213
210
|
if (client) {
|
|
214
|
-
void disconnectSessions(client);
|
|
211
|
+
void disconnectSessions(client).catch((error) => debug(error));
|
|
215
212
|
}
|
|
216
213
|
};
|
|
217
214
|
|
package/src/session.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type { ConnectParams, CreateSessionParams
|
|
1
|
+
import type { ConnectParams, CreateSessionParams } from './types';
|
|
2
2
|
import type { SignClient } from '@walletconnect/sign-client/dist/types/client';
|
|
3
3
|
import type {
|
|
4
4
|
PairingTypes,
|
|
5
5
|
SessionTypes,
|
|
6
6
|
SignClientTypes,
|
|
7
7
|
} from '@walletconnect/types';
|
|
8
|
-
import type { BlockchainMeta } from 'rango-types
|
|
8
|
+
import type { BlockchainMeta } from 'rango-types';
|
|
9
9
|
|
|
10
10
|
import { Networks, timeout } from '@rango-dev/wallets-shared';
|
|
11
11
|
import { getSdkError } from '@walletconnect/utils';
|
|
@@ -14,8 +14,6 @@ import { AccountId } from 'caip';
|
|
|
14
14
|
import { CHAIN_ID_STORAGE, PING_TIMEOUT } from './constants';
|
|
15
15
|
import {
|
|
16
16
|
generateOptionalNamespace,
|
|
17
|
-
generateRequiredNamespace,
|
|
18
|
-
getChainIdByNetworkName,
|
|
19
17
|
getCurrentEvmAccountAddress,
|
|
20
18
|
getEvmAccount,
|
|
21
19
|
getModal,
|
|
@@ -34,11 +32,11 @@ export function getLastSession(client: SignClient) {
|
|
|
34
32
|
*/
|
|
35
33
|
export async function restoreSession(
|
|
36
34
|
client: SignClient,
|
|
37
|
-
|
|
35
|
+
pairingTopic: PairingTypes.Struct['topic']
|
|
38
36
|
): Promise<SessionTypes.Struct | undefined> {
|
|
39
37
|
await timeout(
|
|
40
38
|
client.ping({
|
|
41
|
-
topic:
|
|
39
|
+
topic: pairingTopic,
|
|
42
40
|
}),
|
|
43
41
|
PING_TIMEOUT
|
|
44
42
|
);
|
|
@@ -133,27 +131,18 @@ export function tryGetPairing(
|
|
|
133
131
|
export async function tryConnect(
|
|
134
132
|
client: SignClient,
|
|
135
133
|
params: ConnectParams
|
|
136
|
-
): Promise<
|
|
137
|
-
const {
|
|
138
|
-
|
|
139
|
-
const requiredNamespaces = generateRequiredNamespace(meta, network);
|
|
140
|
-
/**
|
|
141
|
-
* We try to get all of our supported chains as optional.
|
|
142
|
-
* Currently, it only works on Trust Wallet (Note: the response is buggy and only returns eip155 optional namespaces).
|
|
143
|
-
*/
|
|
144
|
-
const optionalNamespaces = generateOptionalNamespace(meta);
|
|
134
|
+
): Promise<SessionTypes.Struct> {
|
|
135
|
+
const { meta } = params;
|
|
145
136
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
137
|
+
// We try to get all of our supported chains as optional.
|
|
138
|
+
const optionalNamespaces = generateOptionalNamespace(meta);
|
|
149
139
|
|
|
150
140
|
// Check if the user has a session, if yes, restore the session and use it.
|
|
151
|
-
let isNew = false;
|
|
152
141
|
let session: SessionTypes.Struct | undefined;
|
|
153
142
|
const pairing = tryGetPairing(client);
|
|
154
143
|
if (pairing) {
|
|
155
144
|
try {
|
|
156
|
-
session = await restoreSession(client, pairing);
|
|
145
|
+
session = await restoreSession(client, pairing.topic);
|
|
157
146
|
} catch (e) {
|
|
158
147
|
await disconnectSessions(client);
|
|
159
148
|
}
|
|
@@ -162,67 +151,12 @@ export async function tryConnect(
|
|
|
162
151
|
// In case of connecting for the first time or session couldn't be restored, we will create a new session.
|
|
163
152
|
if (!session) {
|
|
164
153
|
session = await createSession(client, {
|
|
165
|
-
requiredNamespaces,
|
|
154
|
+
requiredNamespaces: {},
|
|
166
155
|
optionalNamespaces,
|
|
167
156
|
});
|
|
168
|
-
isNew = true;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return { session, isNew };
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Wallet connect is a multichain protocol and we can not determine the connected wallet
|
|
176
|
-
* supports which wallet, `extend`ing session doesn't work during a bug in their utils packages.
|
|
177
|
-
* So we will try to make a new session with `network` that user needs to switch.
|
|
178
|
-
*/
|
|
179
|
-
export async function trySwitchByCreatingNewSession(
|
|
180
|
-
instance: WCInstance,
|
|
181
|
-
params: ConnectParams
|
|
182
|
-
): Promise<SessionTypes.Struct> {
|
|
183
|
-
const { client, session } = instance;
|
|
184
|
-
const { network, meta } = params;
|
|
185
|
-
|
|
186
|
-
if (!session) {
|
|
187
|
-
throw new Error(
|
|
188
|
-
'For switching network, you need to have an active session!'
|
|
189
|
-
);
|
|
190
157
|
}
|
|
191
158
|
|
|
192
|
-
|
|
193
|
-
const chainId = getChainIdByNetworkName(network, params.meta) || network;
|
|
194
|
-
const requestedSession = getSessionByChainId(client, chainId);
|
|
195
|
-
if (requestedSession) {
|
|
196
|
-
return requestedSession;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// Creating a new session for requested network.
|
|
200
|
-
const requiredNamespaces = generateRequiredNamespace(meta, network);
|
|
201
|
-
if (!requiredNamespaces) {
|
|
202
|
-
throw new Error(`Couldn't generate requiredNamespaces for ${network}`);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
const createdSession = await createSession(client, { requiredNamespaces });
|
|
206
|
-
|
|
207
|
-
return createdSession;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
*
|
|
212
|
-
* Looking for a chainId in sessions and return the session if found.
|
|
213
|
-
*
|
|
214
|
-
*/
|
|
215
|
-
function getSessionByChainId(
|
|
216
|
-
client: WCInstance['client'],
|
|
217
|
-
chainId: string
|
|
218
|
-
): SessionTypes.Struct | undefined {
|
|
219
|
-
const sessions = client.session.getAll();
|
|
220
|
-
const requestedSession = sessions.find((session) => {
|
|
221
|
-
const accounts = getAccountsFromSession(session);
|
|
222
|
-
return accounts.find((account) => account.chainId === chainId);
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
return requestedSession;
|
|
159
|
+
return session;
|
|
226
160
|
}
|
|
227
161
|
|
|
228
162
|
/**
|
|
@@ -277,13 +211,13 @@ export async function disconnectSessions(client: SignClient) {
|
|
|
277
211
|
);
|
|
278
212
|
}
|
|
279
213
|
|
|
214
|
+
// reset the current chain id
|
|
215
|
+
void persistCurrentChainId(client, undefined);
|
|
216
|
+
|
|
280
217
|
return await Promise.all(allPromises);
|
|
281
218
|
}
|
|
282
219
|
|
|
283
|
-
export function getAccountsFromSession(
|
|
284
|
-
session: SessionTypes.Struct,
|
|
285
|
-
chainId?: string
|
|
286
|
-
) {
|
|
220
|
+
export function getAccountsFromSession(session: SessionTypes.Struct) {
|
|
287
221
|
const accounts = Object.values(session.namespaces)
|
|
288
222
|
.map((namespace) => namespace.accounts)
|
|
289
223
|
.flat()
|
|
@@ -295,24 +229,10 @@ export function getAccountsFromSession(
|
|
|
295
229
|
*/
|
|
296
230
|
const chain = solanaChainIdToNetworkName(chainId.reference);
|
|
297
231
|
return {
|
|
298
|
-
|
|
232
|
+
address,
|
|
299
233
|
chainId: chain,
|
|
300
234
|
};
|
|
301
|
-
})
|
|
302
|
-
// TODO: fix, ignore solana and cosmos for now
|
|
303
|
-
.filter((account) => account.chainId !== Networks.SOLANA);
|
|
304
|
-
// sort accounts, so connected chain is first item in array
|
|
305
|
-
if (!!chainId) {
|
|
306
|
-
accounts.sort((a, b) => {
|
|
307
|
-
if (a.chainId === chainId) {
|
|
308
|
-
return 1;
|
|
309
|
-
}
|
|
310
|
-
if (b.chainId === chainId) {
|
|
311
|
-
return -1;
|
|
312
|
-
}
|
|
313
|
-
return 0;
|
|
314
235
|
});
|
|
315
|
-
}
|
|
316
236
|
return accounts;
|
|
317
237
|
}
|
|
318
238
|
|
|
@@ -393,22 +313,39 @@ export async function updateSessionAccounts(
|
|
|
393
313
|
}
|
|
394
314
|
|
|
395
315
|
/*
|
|
396
|
-
*
|
|
397
|
-
*
|
|
316
|
+
* Certain wallets, such as Trust Wallet and 1inch, are providing incorrect methods in
|
|
317
|
+
* response to session proposal requests. These wallets do not support certain optional
|
|
318
|
+
* RPC methods like "wallet_xyz," but they include them in the response under the session namespace.
|
|
319
|
+
* For the time being, we should avoid their session namespace response.
|
|
320
|
+
* see also: https://github.com/trustwallet/wallet-core/issues/3588
|
|
398
321
|
*/
|
|
399
|
-
export function
|
|
400
|
-
const
|
|
322
|
+
export function ignoreNamespaceMethods(instance: any): boolean {
|
|
323
|
+
const WALLETS_WITH_WRONG_NAMESPACE_METHODS = ['trust', '1inch'];
|
|
401
324
|
const peerName = instance?.session?.peer?.metadata?.name;
|
|
402
|
-
return
|
|
325
|
+
return WALLETS_WITH_WRONG_NAMESPACE_METHODS.some((name) =>
|
|
326
|
+
peerName?.toLowerCase()?.includes(name)
|
|
327
|
+
);
|
|
403
328
|
}
|
|
404
329
|
|
|
405
|
-
export function persistCurrentChainId(
|
|
406
|
-
|
|
407
|
-
|
|
330
|
+
export async function persistCurrentChainId(
|
|
331
|
+
client: SignClient,
|
|
332
|
+
chainId?: string
|
|
333
|
+
) {
|
|
334
|
+
return client.core.storage.setItem(CHAIN_ID_STORAGE, {
|
|
335
|
+
defaultChainId: chainId ? parseInt(chainId) : '',
|
|
408
336
|
});
|
|
409
337
|
}
|
|
410
338
|
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
339
|
+
/*
|
|
340
|
+
* get the latest chain id from the storage,
|
|
341
|
+
* used for setting current chain id in session reconnect.
|
|
342
|
+
*/
|
|
343
|
+
export async function getPersistedChainId(client: SignClient) {
|
|
344
|
+
try {
|
|
345
|
+
const chainId = (await client.core.storage.getItem(CHAIN_ID_STORAGE))
|
|
346
|
+
?.defaultChainId;
|
|
347
|
+
return !!chainId ? String(chainId) : undefined;
|
|
348
|
+
} catch {
|
|
349
|
+
return undefined;
|
|
350
|
+
}
|
|
414
351
|
}
|