@rango-dev/provider-walletconnect-2 0.15.0 → 0.16.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 +1 -18
- package/dist/constants.d.ts +1 -5
- package/dist/constants.d.ts.map +1 -1
- package/dist/helpers.d.ts +0 -3
- 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 +2 -10
- package/dist/session.d.ts.map +1 -1
- package/dist/signers/evm.d.ts +0 -1
- package/dist/signers/evm.d.ts.map +1 -1
- package/package.json +13 -14
- package/src/constants.ts +3 -12
- package/src/helpers.ts +3 -93
- package/src/index.ts +22 -58
- package/src/session.ts +5 -103
- package/src/signers/evm.ts +1 -38
package/dist/session.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { ConnectParams, CreateSessionParams, WCInstance } 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/lib';
|
|
5
4
|
export declare function getLastSession(client: SignClient): SessionTypes.Struct;
|
|
6
5
|
/**
|
|
7
6
|
*
|
|
@@ -32,10 +31,7 @@ export declare function tryGetPairing(client: SignClient): PairingTypes.Struct |
|
|
|
32
31
|
* Try to restore the session first, if couldn't, create a new session by showing a modal.
|
|
33
32
|
*
|
|
34
33
|
*/
|
|
35
|
-
export declare function tryConnect(client: SignClient, params: ConnectParams): Promise<
|
|
36
|
-
session: SessionTypes.Struct;
|
|
37
|
-
isNew: boolean;
|
|
38
|
-
}>;
|
|
34
|
+
export declare function tryConnect(client: SignClient, params: ConnectParams): Promise<SessionTypes.Struct>;
|
|
39
35
|
/**
|
|
40
36
|
* Wallet connect is a multichain protocol and we can not determine the connected wallet
|
|
41
37
|
* supports which wallet, `extend`ing session doesn't work during a bug in their utils packages.
|
|
@@ -54,7 +50,7 @@ export declare function cleanupSingleSession(client: SignClient, topic: string):
|
|
|
54
50
|
*
|
|
55
51
|
*/
|
|
56
52
|
export declare function disconnectSessions(client: SignClient): Promise<void[]>;
|
|
57
|
-
export declare function getAccountsFromSession(session: SessionTypes.Struct
|
|
53
|
+
export declare function getAccountsFromSession(session: SessionTypes.Struct): {
|
|
58
54
|
accounts: string[];
|
|
59
55
|
chainId: string;
|
|
60
56
|
}[];
|
|
@@ -64,8 +60,4 @@ export declare function getAccountsFromEvent(event: SignClientTypes.BaseEventArg
|
|
|
64
60
|
accounts: string[];
|
|
65
61
|
chainId: string;
|
|
66
62
|
}[];
|
|
67
|
-
export declare function updateSessionAccounts(instance: any, requestedNetwork: string, currentNetwork: string, meta: BlockchainMeta[]): Promise<void>;
|
|
68
|
-
export declare function needSessionRecreateOnSwitchNetwork(instance: any): boolean;
|
|
69
|
-
export declare function persistCurrentChainId(instance: any, chainId: string): any;
|
|
70
|
-
export declare function getPersistedChainId(instance: any): Promise<any>;
|
|
71
63
|
//# 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,UAAU,EAAE,MAAM,SAAS,CAAC;AAC9E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8CAA8C,CAAC;AAC/E,OAAO,KAAK,EACV,YAAY,EACZ,YAAY,EACZ,eAAe,EAChB,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAC9E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8CAA8C,CAAC;AAC/E,OAAO,KAAK,EACV,YAAY,EACZ,YAAY,EACZ,eAAe,EAChB,MAAM,sBAAsB,CAAC;AAe9B,wBAAgB,cAAc,CAAC,MAAM,EAAE,UAAU,uBAEhD;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,MAAM,EAAE,UAAU,EAClB,OAAO,EAAE,YAAY,CAAC,MAAM,GAC3B,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,UAAU,EAClB,MAAM,EAAE,aAAa,GACpB,OAAO,CAAC,YAAY,CAAC,MAAM,CAAC,CAkC9B;AAED;;;;GAIG;AACH,wBAAsB,6BAA6B,CACjD,QAAQ,EAAE,UAAU,EACpB,MAAM,EAAE,aAAa,GACpB,OAAO,CAAC,YAAY,CAAC,MAAM,CAAC,CA0B9B;AAoBD;;;;GAIG;AACH,wBAAsB,oBAAoB,CAAC,MAAM,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,iBAiB3E;AAED;;;;GAIG;AACH,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,UAAU,mBAwB1D;AAED,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,YAAY,CAAC,MAAM;;;IAkBlE;AAED,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,eAAe,CAAC,aAAa,CAAC;IACnC,UAAU,EAAE,YAAY,CAAC,UAAU,CAAC;CACrC,CAAC;;;IAeH"}
|
package/dist/signers/evm.d.ts
CHANGED
|
@@ -6,7 +6,6 @@ declare class EVMSigner implements GenericSigner<EvmTransaction> {
|
|
|
6
6
|
private client;
|
|
7
7
|
private session;
|
|
8
8
|
constructor(client: SignClient, session: SessionTypes.Struct);
|
|
9
|
-
static buildTx(evmTx: EvmTransaction, disableV2?: boolean): {};
|
|
10
9
|
signMessage(msg: string, address: string, chainId: string | null): Promise<string>;
|
|
11
10
|
signAndSendTx(tx: EvmTransaction, address: string, chainId: string | null): Promise<{
|
|
12
11
|
hash: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"evm.d.ts","sourceRoot":"","sources":["../../src/signers/evm.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8CAA8C,CAAC;AAC/E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAK/D,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC;AAMjD,cAAM,SAAU,YAAW,aAAa,CAAC,cAAc,CAAC;IACtD,OAAO,CAAC,MAAM,CAAa;IAC3B,OAAO,CAAC,OAAO,CAAsB;gBAEzB,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,YAAY,CAAC,MAAM;
|
|
1
|
+
{"version":3,"file":"evm.d.ts","sourceRoot":"","sources":["../../src/signers/evm.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8CAA8C,CAAC;AAC/E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAK/D,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC;AAMjD,cAAM,SAAU,YAAW,aAAa,CAAC,cAAc,CAAC;IACtD,OAAO,CAAC,MAAM,CAAa;IAC3B,OAAO,CAAC,OAAO,CAAsB;gBAEzB,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,YAAY,CAAC,MAAM;IAK/C,WAAW,CACtB,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,GAAG,IAAI,GACrB,OAAO,CAAC,MAAM,CAAC;IAqCZ,aAAa,CACjB,EAAE,EAAE,cAAc,EAClB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,GAAG,IAAI,GACrB,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IAsB5B,OAAO,CAAC,iCAAiC;CAwD1C;AAED,eAAe,SAAS,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rango-dev/provider-walletconnect-2",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
7
|
"main": "./dist/index.js",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": "./dist/index.js"
|
|
@@ -15,7 +15,6 @@
|
|
|
15
15
|
],
|
|
16
16
|
"scripts": {
|
|
17
17
|
"build": "node ../../scripts/build/command.mjs --path wallets/provider-walletconnect-2",
|
|
18
|
-
"ts-check": "tsc --declaration --emitDeclarationOnly -p ./tsconfig.json",
|
|
19
18
|
"clean": "rimraf dist",
|
|
20
19
|
"format": "prettier --write '{.,src}/**/*.{ts,tsx}'",
|
|
21
20
|
"lint": "eslint \"**/*.{ts,tsx}\" --ignore-path ../../.eslintignore"
|
|
@@ -24,22 +23,22 @@
|
|
|
24
23
|
"@cosmjs/launchpad": "^0.27.1",
|
|
25
24
|
"@keplr-wallet/cosmos": "^0.9.12",
|
|
26
25
|
"@keplr-wallet/simple-fetch": "^0.12.14",
|
|
27
|
-
"@rango-dev/signer-cosmos": "^0.
|
|
28
|
-
"@rango-dev/signer-evm": "^0.
|
|
29
|
-
"@rango-dev/signer-solana": "^0.
|
|
30
|
-
"@rango-dev/wallets-shared": "^0.
|
|
31
|
-
"@solana/web3.js": "1.67.2",
|
|
32
|
-
"@walletconnect/encoding": "1.0.2",
|
|
33
|
-
"@walletconnect/modal": "2.6.1",
|
|
34
|
-
"@walletconnect/sign-client": "2.9.1",
|
|
35
|
-
"@walletconnect/utils": "2.9.1",
|
|
26
|
+
"@rango-dev/signer-cosmos": "^0.23.0",
|
|
27
|
+
"@rango-dev/signer-evm": "^0.23.0",
|
|
28
|
+
"@rango-dev/signer-solana": "^0.23.0",
|
|
29
|
+
"@rango-dev/wallets-shared": "^0.23.0",
|
|
30
|
+
"@solana/web3.js": "^1.67.2",
|
|
31
|
+
"@walletconnect/encoding": "^1.0.2",
|
|
32
|
+
"@walletconnect/modal": "^2.6.1",
|
|
33
|
+
"@walletconnect/sign-client": "^2.9.1",
|
|
34
|
+
"@walletconnect/utils": "^2.9.1",
|
|
36
35
|
"bs58": "^5.0.0",
|
|
37
36
|
"caip": "^1.1.0",
|
|
38
37
|
"cosmos-wallet": "^1.2.0",
|
|
39
|
-
"rango-types": "^0.1.
|
|
38
|
+
"rango-types": "^0.1.47"
|
|
40
39
|
},
|
|
41
40
|
"devDependencies": {
|
|
42
|
-
"@walletconnect/types": "2.9.1"
|
|
41
|
+
"@walletconnect/types": "^2.9.1"
|
|
43
42
|
},
|
|
44
43
|
"publishConfig": {
|
|
45
44
|
"access": "public"
|
package/src/constants.ts
CHANGED
|
@@ -13,8 +13,6 @@ export enum NAMESPACES {
|
|
|
13
13
|
MULTIVERSX = 'multiversx',
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
export const CHAIN_ID_STORAGE = 'wc@2:client//namespaces';
|
|
17
|
-
|
|
18
16
|
// Refrence: https://docs.walletconnect.com/2.0/advanced/rpc-reference/solana-rpc
|
|
19
17
|
export enum SolanaRPCMethods {
|
|
20
18
|
GET_ACCOUNTS = 'solana_getAccounts',
|
|
@@ -38,9 +36,6 @@ export enum EthereumRPCMethods {
|
|
|
38
36
|
SIGN_TRANSACTION = 'eth_signTransaction',
|
|
39
37
|
SEND_TRANSACTION = 'eth_sendTransaction',
|
|
40
38
|
SEND_RAW_TRANSACTION = 'eth_sendRawTransaction',
|
|
41
|
-
SWITCH_CHAIN = 'wallet_switchEthereumChain',
|
|
42
|
-
ADD_CHAIN = 'wallet_addEthereumChain',
|
|
43
|
-
GET_CHAIN = 'eth_chainId',
|
|
44
39
|
}
|
|
45
40
|
|
|
46
41
|
export enum StarknetRPCMethods {
|
|
@@ -62,16 +57,11 @@ export const DEFAULT_ETHEREUM_METHODS = [
|
|
|
62
57
|
EthereumRPCMethods.PERSONAL_SIGN,
|
|
63
58
|
EthereumRPCMethods.SEND_TRANSACTION,
|
|
64
59
|
EthereumRPCMethods.SIGN_TRANSACTION,
|
|
65
|
-
EthereumRPCMethods.SWITCH_CHAIN,
|
|
66
|
-
EthereumRPCMethods.ADD_CHAIN,
|
|
67
|
-
EthereumRPCMethods.GET_CHAIN,
|
|
68
60
|
];
|
|
69
|
-
|
|
70
61
|
export const DEFAULT_SOLANA_METHODS = [
|
|
71
62
|
SolanaRPCMethods.SIGN_TRANSACTION,
|
|
72
63
|
SolanaRPCMethods.SIGN_MESSAGE,
|
|
73
64
|
];
|
|
74
|
-
|
|
75
65
|
export const DEFAULT_COSMOS_METHODS = [
|
|
76
66
|
CosmosRPCMethods.GET_ACCOUNTS,
|
|
77
67
|
CosmosRPCMethods.SIGN_AMINO,
|
|
@@ -83,9 +73,10 @@ export const DEFAULT_SOLANA_CHAIN_ID = '4sGjMW1sUnHzSxGspuhpqLDx6wiyjNtZ';
|
|
|
83
73
|
|
|
84
74
|
export const DEFAULT_APP_METADATA = {
|
|
85
75
|
name: 'Rango Exchange',
|
|
86
|
-
description:
|
|
76
|
+
description:
|
|
77
|
+
'Easiest DEX UX with best price to exchange all coins on all blockchains.',
|
|
87
78
|
url: 'https://app.rango.exchange/',
|
|
88
|
-
icons: ['https://
|
|
79
|
+
icons: ['https://avatars.githubusercontent.com/u/37784886'],
|
|
89
80
|
};
|
|
90
81
|
|
|
91
82
|
export const RELAY_URL = 'wss://relay.walletconnect.com';
|
package/src/helpers.ts
CHANGED
|
@@ -3,18 +3,10 @@ import type { WalletState } from '@rango-dev/wallets-shared';
|
|
|
3
3
|
import type { ProposalTypes } from '@walletconnect/types';
|
|
4
4
|
import type { BlockchainMeta } from 'rango-types';
|
|
5
5
|
|
|
6
|
-
import {
|
|
7
|
-
convertEvmBlockchainMetaToEvmChainInfo,
|
|
8
|
-
isEvmAddress,
|
|
9
|
-
Networks,
|
|
10
|
-
} from '@rango-dev/wallets-shared';
|
|
6
|
+
import { Networks } from '@rango-dev/wallets-shared';
|
|
11
7
|
import { WalletConnectModal } from '@walletconnect/modal';
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
cosmosBlockchains,
|
|
15
|
-
evmBlockchains,
|
|
16
|
-
isEvmBlockchain,
|
|
17
|
-
} from 'rango-types';
|
|
8
|
+
import { ChainId } from 'caip';
|
|
9
|
+
import { cosmosBlockchains, evmBlockchains } from 'rango-types';
|
|
18
10
|
|
|
19
11
|
import {
|
|
20
12
|
DEFAULT_COSMOS_METHODS,
|
|
@@ -22,7 +14,6 @@ import {
|
|
|
22
14
|
DEFAULT_ETHEREUM_METHODS,
|
|
23
15
|
DEFAULT_SOLANA_CHAIN_ID,
|
|
24
16
|
DEFAULT_SOLANA_METHODS,
|
|
25
|
-
EthereumRPCMethods,
|
|
26
17
|
NAMESPACES,
|
|
27
18
|
} from './constants';
|
|
28
19
|
import { getLastSession } from './session';
|
|
@@ -201,84 +192,3 @@ export function getChainIdByNetworkName(
|
|
|
201
192
|
|
|
202
193
|
return chainId;
|
|
203
194
|
}
|
|
204
|
-
|
|
205
|
-
export async function switchOrAddEvmChain(
|
|
206
|
-
meta: BlockchainMeta[],
|
|
207
|
-
requestedNetwork: string,
|
|
208
|
-
currentNetwork: string,
|
|
209
|
-
instance: any
|
|
210
|
-
) {
|
|
211
|
-
const evmBlockchains = meta.filter(isEvmBlockchain);
|
|
212
|
-
const evmNetworksChainInfo =
|
|
213
|
-
convertEvmBlockchainMetaToEvmChainInfo(evmBlockchains);
|
|
214
|
-
const targetChain = evmNetworksChainInfo[requestedNetwork];
|
|
215
|
-
const targetBlockchain = meta.find(
|
|
216
|
-
(blockchain: BlockchainMeta) => blockchain.name === requestedNetwork
|
|
217
|
-
);
|
|
218
|
-
const chainIdInHex = targetBlockchain?.chainId;
|
|
219
|
-
|
|
220
|
-
const currentChainId = getChainIdByNetworkName(currentNetwork, meta);
|
|
221
|
-
const currentChainEip = ChainId.format({
|
|
222
|
-
namespace: NAMESPACES.ETHEREUM,
|
|
223
|
-
reference: String(currentChainId),
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
const session = instance.session;
|
|
227
|
-
|
|
228
|
-
try {
|
|
229
|
-
await instance.client.request({
|
|
230
|
-
topic: session.topic,
|
|
231
|
-
request: {
|
|
232
|
-
method: EthereumRPCMethods.SWITCH_CHAIN,
|
|
233
|
-
params: [
|
|
234
|
-
{
|
|
235
|
-
chainId: chainIdInHex,
|
|
236
|
-
},
|
|
237
|
-
],
|
|
238
|
-
},
|
|
239
|
-
// It's required to pass current chain, otherwise it won't work
|
|
240
|
-
chainId: currentChainEip,
|
|
241
|
-
});
|
|
242
|
-
} catch (err: any) {
|
|
243
|
-
const addChainError = 4902;
|
|
244
|
-
if (
|
|
245
|
-
err?.code === addChainError ||
|
|
246
|
-
err?.message?.includes(String(addChainError))
|
|
247
|
-
) {
|
|
248
|
-
await instance.client.request({
|
|
249
|
-
topic: session.topic,
|
|
250
|
-
request: {
|
|
251
|
-
method: EthereumRPCMethods.ADD_CHAIN,
|
|
252
|
-
params: [targetChain],
|
|
253
|
-
},
|
|
254
|
-
// It's required to pass current chain, otherwise it won't work
|
|
255
|
-
chainId: currentChainEip,
|
|
256
|
-
});
|
|
257
|
-
} else {
|
|
258
|
-
throw err;
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
export function getCurrentEvmAccountAddress(instance: any) {
|
|
264
|
-
return instance.session.namespaces.eip155.accounts
|
|
265
|
-
?.map((account: string) => {
|
|
266
|
-
return new AccountId(account).address;
|
|
267
|
-
})
|
|
268
|
-
?.filter((address: string) => isEvmAddress(address))?.[0];
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
export function getEvmAccount(
|
|
272
|
-
network: string,
|
|
273
|
-
address: string,
|
|
274
|
-
meta: BlockchainMeta[]
|
|
275
|
-
) {
|
|
276
|
-
const currentChainId = getChainIdByNetworkName(network, meta);
|
|
277
|
-
return AccountId.format({
|
|
278
|
-
chainId: {
|
|
279
|
-
namespace: NAMESPACES.ETHEREUM,
|
|
280
|
-
reference: String(currentChainId),
|
|
281
|
-
},
|
|
282
|
-
address,
|
|
283
|
-
});
|
|
284
|
-
}
|
package/src/index.ts
CHANGED
|
@@ -12,9 +12,8 @@ import type {
|
|
|
12
12
|
import type { ISignClient } from '@walletconnect/types';
|
|
13
13
|
import type { BlockchainMeta, SignerFactory } from 'rango-types';
|
|
14
14
|
|
|
15
|
-
import {
|
|
15
|
+
import { WalletTypes } from '@rango-dev/wallets-shared';
|
|
16
16
|
import Client from '@walletconnect/sign-client';
|
|
17
|
-
import { AccountId, ChainId } from 'caip';
|
|
18
17
|
import { evmBlockchains } from 'rango-types';
|
|
19
18
|
|
|
20
19
|
import {
|
|
@@ -23,22 +22,14 @@ import {
|
|
|
23
22
|
EthereumEvents,
|
|
24
23
|
RELAY_URL,
|
|
25
24
|
} from './constants';
|
|
26
|
-
import {
|
|
27
|
-
createModalInstance,
|
|
28
|
-
simulateRequest,
|
|
29
|
-
switchOrAddEvmChain,
|
|
30
|
-
} from './helpers';
|
|
25
|
+
import { createModalInstance, simulateRequest } from './helpers';
|
|
31
26
|
import {
|
|
32
27
|
cleanupSingleSession,
|
|
33
28
|
disconnectSessions,
|
|
34
29
|
getAccountsFromEvent,
|
|
35
30
|
getAccountsFromSession,
|
|
36
|
-
getPersistedChainId,
|
|
37
|
-
needSessionRecreateOnSwitchNetwork,
|
|
38
|
-
persistCurrentChainId,
|
|
39
31
|
tryConnect,
|
|
40
32
|
trySwitchByCreatingNewSession,
|
|
41
|
-
updateSessionAccounts,
|
|
42
33
|
} from './session';
|
|
43
34
|
import signer from './signer';
|
|
44
35
|
|
|
@@ -60,15 +51,14 @@ export const config: WalletConfig = {
|
|
|
60
51
|
checkInstallation: false,
|
|
61
52
|
isAsyncInstance: true,
|
|
62
53
|
defaultNetwork: DEFAULT_NETWORK,
|
|
63
|
-
isAsyncSwitchNetwork: true,
|
|
64
54
|
};
|
|
65
55
|
|
|
66
56
|
export const getInstance: GetInstance = async (options) => {
|
|
67
57
|
const { currentProvider, getState, meta } = options;
|
|
68
58
|
|
|
69
59
|
/*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
60
|
+
*Create a new pair, if exists use the pair,
|
|
61
|
+
*Or use the already created one.
|
|
72
62
|
*/
|
|
73
63
|
let provider: ISignClient;
|
|
74
64
|
if (!currentProvider) {
|
|
@@ -77,6 +67,7 @@ export const getInstance: GetInstance = async (options) => {
|
|
|
77
67
|
'You need to set `WC_PROJECT_ID` in Wallet Connect provider.'
|
|
78
68
|
);
|
|
79
69
|
}
|
|
70
|
+
|
|
80
71
|
provider = await Client.init({
|
|
81
72
|
relayUrl: RELAY_URL,
|
|
82
73
|
projectId: envs.WC_PROJECT_ID,
|
|
@@ -100,29 +91,15 @@ export const connect: Connect = async ({ instance, network, meta }) => {
|
|
|
100
91
|
const requestedNetwork = network || DEFAULT_NETWORK;
|
|
101
92
|
|
|
102
93
|
// Try to restore the session first, if couldn't, create a new session by showing a modal.
|
|
103
|
-
const
|
|
94
|
+
const session = await tryConnect(client, {
|
|
104
95
|
network: requestedNetwork,
|
|
105
96
|
meta,
|
|
106
97
|
});
|
|
107
98
|
// Override the value (session).
|
|
108
99
|
instance.session = session;
|
|
109
|
-
|
|
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];
|
|
100
|
+
|
|
101
|
+
const accounts = getAccountsFromSession(session);
|
|
102
|
+
return accounts;
|
|
126
103
|
};
|
|
127
104
|
|
|
128
105
|
export const subscribe: Subscribe = ({
|
|
@@ -149,16 +126,13 @@ export const subscribe: Subscribe = ({
|
|
|
149
126
|
// Listen to events triggred by wallet. (e.g. accountsChanged and chainChanged)
|
|
150
127
|
client.on('session_event', (args) => {
|
|
151
128
|
if (args.params.event.name === EthereumEvents.ACCOUNTS_CHANGED) {
|
|
152
|
-
const accounts = args.params.event.data
|
|
153
|
-
|
|
154
|
-
});
|
|
155
|
-
const chainId = ChainId.parse(args.params.chainId).reference;
|
|
129
|
+
const accounts = args.params.event.data;
|
|
130
|
+
const chainId = args.params.chainId;
|
|
156
131
|
updateAccounts(accounts);
|
|
157
132
|
updateChainId(chainId);
|
|
158
133
|
} else if (args.params.event.name === EthereumEvents.CHAIN_CHANGED) {
|
|
159
|
-
const chainId = args.params.
|
|
134
|
+
const chainId = args.params.chainId;
|
|
160
135
|
updateChainId(chainId);
|
|
161
|
-
void persistCurrentChainId(instance, chainId);
|
|
162
136
|
} else {
|
|
163
137
|
console.log('[WC2] session_event not supported', args.params.event);
|
|
164
138
|
}
|
|
@@ -175,27 +149,17 @@ export const switchNetwork: SwitchNetwork = async ({
|
|
|
175
149
|
network,
|
|
176
150
|
instance,
|
|
177
151
|
meta,
|
|
178
|
-
getState,
|
|
179
152
|
}) => {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
network,
|
|
191
|
-
meta,
|
|
192
|
-
});
|
|
193
|
-
instance.session = session;
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
const currentNetwork = getState?.().network || Networks.ETHEREUM;
|
|
197
|
-
await updateSessionAccounts(instance, network, currentNetwork, meta);
|
|
198
|
-
await switchOrAddEvmChain(meta, network, currentNetwork, instance);
|
|
153
|
+
/**
|
|
154
|
+
* Wallet connect is a multichain protocol and we can not determine the connected wallet
|
|
155
|
+
* supports which wallet, `extend`ing session doesn't work during a bug in their utils packages.
|
|
156
|
+
* So we will try to make a new session with `network` that user needs to switch.
|
|
157
|
+
*/
|
|
158
|
+
const session = await trySwitchByCreatingNewSession(instance, {
|
|
159
|
+
network,
|
|
160
|
+
meta,
|
|
161
|
+
});
|
|
162
|
+
instance.session = session;
|
|
199
163
|
};
|
|
200
164
|
|
|
201
165
|
/**
|
package/src/session.ts
CHANGED
|
@@ -5,19 +5,16 @@ import type {
|
|
|
5
5
|
SessionTypes,
|
|
6
6
|
SignClientTypes,
|
|
7
7
|
} from '@walletconnect/types';
|
|
8
|
-
import type { BlockchainMeta } from 'rango-types/lib';
|
|
9
8
|
|
|
10
9
|
import { Networks, timeout } from '@rango-dev/wallets-shared';
|
|
11
10
|
import { getSdkError } from '@walletconnect/utils';
|
|
12
11
|
import { AccountId } from 'caip';
|
|
13
12
|
|
|
14
|
-
import {
|
|
13
|
+
import { PING_TIMEOUT } from './constants';
|
|
15
14
|
import {
|
|
16
15
|
generateOptionalNamespace,
|
|
17
16
|
generateRequiredNamespace,
|
|
18
17
|
getChainIdByNetworkName,
|
|
19
|
-
getCurrentEvmAccountAddress,
|
|
20
|
-
getEvmAccount,
|
|
21
18
|
getModal,
|
|
22
19
|
solanaChainIdToNetworkName,
|
|
23
20
|
} from './helpers';
|
|
@@ -133,7 +130,7 @@ export function tryGetPairing(
|
|
|
133
130
|
export async function tryConnect(
|
|
134
131
|
client: SignClient,
|
|
135
132
|
params: ConnectParams
|
|
136
|
-
): Promise<
|
|
133
|
+
): Promise<SessionTypes.Struct> {
|
|
137
134
|
const { network, meta } = params;
|
|
138
135
|
|
|
139
136
|
const requiredNamespaces = generateRequiredNamespace(meta, network);
|
|
@@ -148,7 +145,6 @@ export async function tryConnect(
|
|
|
148
145
|
}
|
|
149
146
|
|
|
150
147
|
// Check if the user has a session, if yes, restore the session and use it.
|
|
151
|
-
let isNew = false;
|
|
152
148
|
let session: SessionTypes.Struct | undefined;
|
|
153
149
|
const pairing = tryGetPairing(client);
|
|
154
150
|
if (pairing) {
|
|
@@ -165,10 +161,9 @@ export async function tryConnect(
|
|
|
165
161
|
requiredNamespaces,
|
|
166
162
|
optionalNamespaces,
|
|
167
163
|
});
|
|
168
|
-
isNew = true;
|
|
169
164
|
}
|
|
170
165
|
|
|
171
|
-
return
|
|
166
|
+
return session;
|
|
172
167
|
}
|
|
173
168
|
|
|
174
169
|
/**
|
|
@@ -280,10 +275,7 @@ export async function disconnectSessions(client: SignClient) {
|
|
|
280
275
|
return await Promise.all(allPromises);
|
|
281
276
|
}
|
|
282
277
|
|
|
283
|
-
export function getAccountsFromSession(
|
|
284
|
-
session: SessionTypes.Struct,
|
|
285
|
-
chainId?: string
|
|
286
|
-
) {
|
|
278
|
+
export function getAccountsFromSession(session: SessionTypes.Struct) {
|
|
287
279
|
const accounts = Object.values(session.namespaces)
|
|
288
280
|
.map((namespace) => namespace.accounts)
|
|
289
281
|
.flat()
|
|
@@ -298,21 +290,8 @@ export function getAccountsFromSession(
|
|
|
298
290
|
accounts: [address],
|
|
299
291
|
chainId: chain,
|
|
300
292
|
};
|
|
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
293
|
});
|
|
315
|
-
|
|
294
|
+
|
|
316
295
|
return accounts;
|
|
317
296
|
}
|
|
318
297
|
|
|
@@ -335,80 +314,3 @@ export function getAccountsFromEvent(
|
|
|
335
314
|
|
|
336
315
|
return accounts;
|
|
337
316
|
}
|
|
338
|
-
|
|
339
|
-
/*
|
|
340
|
-
* Before switch network, we need to update session namespace accounts
|
|
341
|
-
* to contain both current chain and target chain accoutns.
|
|
342
|
-
*/
|
|
343
|
-
export async function updateSessionAccounts(
|
|
344
|
-
instance: any,
|
|
345
|
-
requestedNetwork: string,
|
|
346
|
-
currentNetwork: string,
|
|
347
|
-
meta: BlockchainMeta[]
|
|
348
|
-
) {
|
|
349
|
-
const session = instance.session;
|
|
350
|
-
|
|
351
|
-
const namespaces = session.namespaces;
|
|
352
|
-
let needUpdateNamepspace = false;
|
|
353
|
-
const accounts = namespaces.eip155.accounts;
|
|
354
|
-
|
|
355
|
-
const currentAccountAddress = getCurrentEvmAccountAddress(instance);
|
|
356
|
-
const requestedAccount = getEvmAccount(
|
|
357
|
-
requestedNetwork,
|
|
358
|
-
currentAccountAddress,
|
|
359
|
-
meta
|
|
360
|
-
);
|
|
361
|
-
if (!accounts.includes(requestedAccount)) {
|
|
362
|
-
accounts.push(requestedAccount);
|
|
363
|
-
needUpdateNamepspace = true;
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
const currentAccount = getEvmAccount(
|
|
367
|
-
currentNetwork,
|
|
368
|
-
currentAccountAddress,
|
|
369
|
-
meta
|
|
370
|
-
);
|
|
371
|
-
if (!accounts.includes(currentAccount)) {
|
|
372
|
-
accounts.push(currentAccount);
|
|
373
|
-
needUpdateNamepspace = true;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
if (needUpdateNamepspace) {
|
|
377
|
-
const updatedNamespaces = {
|
|
378
|
-
...namespaces,
|
|
379
|
-
eip155: {
|
|
380
|
-
...namespaces.eip155,
|
|
381
|
-
accounts,
|
|
382
|
-
},
|
|
383
|
-
};
|
|
384
|
-
await instance.client.session
|
|
385
|
-
.update({
|
|
386
|
-
topic: session.topic,
|
|
387
|
-
namespaces: updatedNamespaces,
|
|
388
|
-
})
|
|
389
|
-
.catch((err: unknown) => {
|
|
390
|
-
console.log(err);
|
|
391
|
-
});
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
/*
|
|
396
|
-
* For some wallet e.g. Trust Wallet Mobile which doesn't support
|
|
397
|
-
* RPC method for switch network, we need to recreate session
|
|
398
|
-
*/
|
|
399
|
-
export function needSessionRecreateOnSwitchNetwork(instance: any): boolean {
|
|
400
|
-
const TRUST_WALLET_KEYWORD = 'trust';
|
|
401
|
-
const peerName = instance?.session?.peer?.metadata?.name;
|
|
402
|
-
return peerName?.toLowerCase()?.includes(TRUST_WALLET_KEYWORD);
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
export function persistCurrentChainId(instance: any, chainId: string) {
|
|
406
|
-
return instance.client.core.storage.setItem(CHAIN_ID_STORAGE, {
|
|
407
|
-
defaultChainId: parseInt(chainId),
|
|
408
|
-
});
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
export async function getPersistedChainId(instance: any) {
|
|
412
|
-
return (await instance.client.core.storage.getItem(CHAIN_ID_STORAGE))
|
|
413
|
-
?.defaultChainId;
|
|
414
|
-
}
|
package/src/signers/evm.ts
CHANGED
|
@@ -20,42 +20,6 @@ class EVMSigner implements GenericSigner<EvmTransaction> {
|
|
|
20
20
|
this.session = session;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
static buildTx(evmTx: EvmTransaction, disableV2 = false) {
|
|
24
|
-
let tx = {};
|
|
25
|
-
if (evmTx.from) {
|
|
26
|
-
tx = { ...tx, from: evmTx.from };
|
|
27
|
-
}
|
|
28
|
-
if (evmTx.to) {
|
|
29
|
-
tx = { ...tx, to: evmTx.to };
|
|
30
|
-
}
|
|
31
|
-
if (evmTx.data) {
|
|
32
|
-
tx = { ...tx, data: evmTx.data };
|
|
33
|
-
}
|
|
34
|
-
if (evmTx.value) {
|
|
35
|
-
tx = { ...tx, value: evmTx.value };
|
|
36
|
-
}
|
|
37
|
-
if (evmTx.nonce) {
|
|
38
|
-
tx = { ...tx, nonce: evmTx.nonce };
|
|
39
|
-
}
|
|
40
|
-
if (evmTx.gasLimit) {
|
|
41
|
-
tx = { ...tx, gasLimit: evmTx.gasLimit };
|
|
42
|
-
}
|
|
43
|
-
if (evmTx.gasPrice) {
|
|
44
|
-
const shift = 16;
|
|
45
|
-
tx = {
|
|
46
|
-
...tx,
|
|
47
|
-
gasPrice: '0x' + parseInt(evmTx.gasPrice).toString(shift),
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
if (evmTx.maxFeePerGas && !disableV2) {
|
|
51
|
-
tx = { ...tx, maxFeePerGas: evmTx.maxFeePerGas };
|
|
52
|
-
}
|
|
53
|
-
if (evmTx.maxPriorityFeePerGas && !disableV2) {
|
|
54
|
-
tx = { ...tx, maxPriorityFeePerGas: evmTx.maxPriorityFeePerGas };
|
|
55
|
-
}
|
|
56
|
-
return tx;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
23
|
public async signMessage(
|
|
60
24
|
msg: string,
|
|
61
25
|
address: string,
|
|
@@ -107,13 +71,12 @@ class EVMSigner implements GenericSigner<EvmTransaction> {
|
|
|
107
71
|
chainId,
|
|
108
72
|
});
|
|
109
73
|
try {
|
|
110
|
-
const transaction = EVMSigner.buildTx(tx);
|
|
111
74
|
const hash: string = await this.client.request({
|
|
112
75
|
topic: this.session.topic,
|
|
113
76
|
chainId: requestedFor.caipChainId,
|
|
114
77
|
request: {
|
|
115
78
|
method: EthereumRPCMethods.SEND_TRANSACTION,
|
|
116
|
-
params: [
|
|
79
|
+
params: [tx],
|
|
117
80
|
},
|
|
118
81
|
});
|
|
119
82
|
return {
|