@rango-dev/provider-walletconnect-2 0.1.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.
@@ -0,0 +1,155 @@
1
+ import { SignClient } from '@walletconnect/sign-client/dist/types/client';
2
+ import { SessionTypes } from '@walletconnect/types';
3
+ import { AccountId, ChainId } from 'caip';
4
+ import type { GenericSigner, SolanaTransaction } from 'rango-types';
5
+ import { SignerError, SignerErrorCode } from 'rango-types';
6
+ import { NAMESPACES, SolanaRPCMethods } from '../constants';
7
+ import {
8
+ SolanaWeb3Signer,
9
+ generalSolanaTransactionExecutor,
10
+ } from '@rango-dev/signer-solana';
11
+ import { PublicKey, Transaction, VersionedTransaction } from '@solana/web3.js';
12
+ import base58 from 'bs58';
13
+
14
+ const NAMESPACE_NAME = NAMESPACES.SOLANA;
15
+ class SOLANASigner implements GenericSigner<SolanaTransaction> {
16
+ private client: SignClient;
17
+ private session: SessionTypes.Struct;
18
+
19
+ constructor(client: SignClient, session: SessionTypes.Struct) {
20
+ this.client = client;
21
+ this.session = session;
22
+ }
23
+
24
+ public async signMessage(
25
+ msg: string,
26
+ address: string,
27
+ chainId: string | null
28
+ ): Promise<string> {
29
+ const requestedFor = this.isNetworkAndAccountExistInSession({
30
+ address,
31
+ chainId,
32
+ });
33
+
34
+ const caipChainId = new ChainId({
35
+ namespace: NAMESPACE_NAME,
36
+ reference: requestedFor.chainId,
37
+ });
38
+
39
+ try {
40
+ const message = base58.encode(new TextEncoder().encode(msg));
41
+ const pubkey = new PublicKey(address);
42
+ const { signature } = await this.client.request<{
43
+ signature: string;
44
+ }>({
45
+ topic: this.session.topic,
46
+ chainId: caipChainId.toString(),
47
+ request: {
48
+ method: SolanaRPCMethods.SIGN_MESSAGE,
49
+ params: {
50
+ message,
51
+ pubkey,
52
+ },
53
+ },
54
+ });
55
+
56
+ return signature;
57
+ } catch (error) {
58
+ throw new SignerError(SignerErrorCode.SIGN_TX_ERROR, undefined, error);
59
+ }
60
+ }
61
+
62
+ async signAndSendTx(
63
+ tx: SolanaTransaction,
64
+ address: string,
65
+ chainId: string | null
66
+ ): Promise<{ hash: string }> {
67
+ const requestedFor = this.isNetworkAndAccountExistInSession({
68
+ address,
69
+ chainId,
70
+ });
71
+ const DefaultSolanaSigner: SolanaWeb3Signer = async (
72
+ solanaWeb3Transaction: Transaction | VersionedTransaction
73
+ ) => {
74
+ const response: { signature: string } = await this.client.request({
75
+ topic: this.session.topic,
76
+ chainId: requestedFor.caipChainId,
77
+ request: {
78
+ method: SolanaRPCMethods.SIGN_TRANSACTION,
79
+ params: solanaWeb3Transaction,
80
+ },
81
+ });
82
+
83
+ const publicKey = new PublicKey(tx.from);
84
+ const sign = base58.decode(response.signature);
85
+
86
+ solanaWeb3Transaction.addSignature(publicKey, Buffer.from(sign));
87
+ const raw = solanaWeb3Transaction.serialize();
88
+ return raw;
89
+ };
90
+
91
+ const hash = await generalSolanaTransactionExecutor(
92
+ tx,
93
+ DefaultSolanaSigner
94
+ );
95
+ return { hash };
96
+ }
97
+
98
+ private isNetworkAndAccountExistInSession(requestedFor: {
99
+ address: string;
100
+ chainId: string | null;
101
+ }) {
102
+ const { address, chainId } = requestedFor;
103
+
104
+ // TODO: solana chain id in supported blockchains("mainnet-beta") is different from solana chain id is here ("5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp")
105
+ //# Solana Mainnet
106
+ // solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvd
107
+ // refrence: https://github.com/ChainAgnostic/namespaces/blob/main/solana/caip2.md
108
+
109
+ let solana_chain_id = chainId;
110
+ this.session.namespaces[NAMESPACE_NAME]?.accounts.map((account) => {
111
+ const sol_account = account.split(':');
112
+ if (sol_account[2] === address) solana_chain_id = sol_account[1];
113
+ });
114
+
115
+ if (!solana_chain_id) {
116
+ throw new Error(
117
+ 'You need to set your chain for signing message/transaction.'
118
+ );
119
+ }
120
+ const caipAddress = new AccountId({
121
+ chainId: {
122
+ namespace: NAMESPACE_NAME,
123
+ reference: solana_chain_id,
124
+ },
125
+ address,
126
+ });
127
+
128
+ const addresses = this.session.namespaces[NAMESPACE_NAME]?.accounts.map(
129
+ (address) => address
130
+ );
131
+ if (!addresses || !addresses.includes(caipAddress.toString())) {
132
+ console.warn(
133
+ 'Available adresses and requested address:',
134
+ addresses,
135
+ caipAddress.toString()
136
+ );
137
+ throw new Error(
138
+ `Your requested address doesn't exist on your wallect connect session. Please reconnect your wallet.`
139
+ );
140
+ }
141
+
142
+ const caipChainId = new ChainId({
143
+ namespace: NAMESPACE_NAME,
144
+ reference: solana_chain_id,
145
+ });
146
+
147
+ return {
148
+ chainId: solana_chain_id,
149
+ address,
150
+ caipChainId: caipChainId.toString(),
151
+ };
152
+ }
153
+ }
154
+
155
+ export default SOLANASigner;
package/src/types.ts ADDED
@@ -0,0 +1,25 @@
1
+ import type { SignClient } from '@walletconnect/sign-client/dist/types/client';
2
+ import type { ProposalTypes, SessionTypes } from '@walletconnect/types';
3
+ import type { BlockchainMeta, CosmosBlockchainMeta } from 'rango-types/lib';
4
+
5
+ export interface WCInstance {
6
+ client: SignClient;
7
+ session: SessionTypes.Struct | null;
8
+ request: (params: any) => Promise<string>;
9
+ }
10
+
11
+ export interface CreateSessionParams {
12
+ requiredNamespaces: ProposalTypes.RequiredNamespaces;
13
+ optionalNamespaces?: ProposalTypes.OptionalNamespaces;
14
+ pairingTopic?: string;
15
+ }
16
+
17
+ export interface ConnectParams {
18
+ network: string;
19
+ meta: BlockchainMeta[];
20
+ }
21
+
22
+ export interface CosmosMeta extends CosmosBlockchainMeta {
23
+ // forcing the chainId to be `string` only.
24
+ chainId: string;
25
+ }