-
Notifications
You must be signed in to change notification settings - Fork 370
/
Copy pathhelpers.ts
172 lines (150 loc) · 4.4 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import {
Connection,
PublicKey,
SystemProgram,
Transaction,
VersionedTransaction,
clusterApiUrl,
} from "@solana/web3.js";
import UniversalProvider from "@walletconnect/universal-provider/dist/types/UniversalProvider";
import bs58 from "bs58";
import nacl from "tweetnacl";
export enum SolanaChains {
MainnetBeta = "5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
Devnet = "EtWTRABZaYq6iMfeYKouRu166VU2xqa1",
}
export function verifyTransactionSignature(
address: string,
signature: string,
tx: Transaction
) {
return nacl.sign.detached.verify(
new Uint8Array(tx.serializeMessage()),
bs58.decode(signature),
bs58.decode(address)
);
}
export function verifyMessageSignature(
address: string,
signature: string,
message: string
) {
return nacl.sign.detached.verify(
bs58.decode(message),
bs58.decode(signature),
bs58.decode(address)
);
}
const isVersionedTransaction = (
transaction: Transaction | VersionedTransaction
): transaction is VersionedTransaction => "version" in transaction;
export const getProviderUrl = (chainId: string) => {
return `https://rpc.walletconnect.com/v1/?chainId=${chainId}&projectId=${
import.meta.env.VITE_PROJECT_ID
}`;
};
export const signMessage = async (
msg: string,
provider: UniversalProvider,
address: string
) => {
const senderPublicKey = new PublicKey(address);
const message = bs58.encode(new TextEncoder().encode(msg));
try {
const result = await provider!.request<{ signature: string }>({
method: "solana_signMessage",
params: {
pubkey: senderPublicKey.toBase58(),
message,
},
});
const valid = verifyMessageSignature(
senderPublicKey.toBase58(),
result.signature,
message
);
return {
method: "solana_signMessage",
address,
valid,
result: result.signature,
};
//eslint-disable-next-line
} catch (error: any) {
throw new Error(error);
}
};
export const sendTransaction = async (
to: string,
amount: number,
provider: UniversalProvider,
address: string
) => {
const isTestnet = provider.session!.namespaces.solana.chains?.includes(
`solana:${SolanaChains.Devnet}`
);
const senderPublicKey = new PublicKey(address);
const connection = new Connection(
isTestnet
? clusterApiUrl("testnet")
: getProviderUrl(`solana:${SolanaChains.MainnetBeta}`)
);
const { blockhash } = await connection.getLatestBlockhash();
const transaction: Transaction | VersionedTransaction = new Transaction({
feePayer: senderPublicKey,
recentBlockhash: blockhash,
}).add(
SystemProgram.transfer({
fromPubkey: senderPublicKey,
toPubkey: new PublicKey(to),
lamports: amount,
})
);
let rawTransaction: string;
let legacyTransaction: Transaction | VersionedTransaction | undefined;
if (isVersionedTransaction(transaction)) {
// V0 transactions are serialized and passed in the `transaction` property
rawTransaction = Buffer.from(transaction.serialize()).toString("base64");
if (transaction.version === "legacy") {
// For backwards-compatible, legacy transactions are spread in the params
legacyTransaction = Transaction.from(transaction.serialize());
}
} else {
rawTransaction = transaction
.serialize({
requireAllSignatures: false,
verifySignatures: false,
})
.toString("base64");
legacyTransaction = transaction;
}
try {
const result = await provider!.request<{ signature: string }>({
method: "solana_signTransaction",
params: {
// Passing ...legacyTransaction is deprecated.
// All new clients should rely on the `transaction` parameter.
// The future versions will stop passing ...legacyTransaction.
...legacyTransaction,
// New base64-encoded serialized transaction request parameter
transaction: rawTransaction,
},
});
// We only need `Buffer.from` here to satisfy the `Buffer` param type for `addSignature`.
// The resulting `UInt8Array` is equivalent to just `bs58.decode(...)`.
transaction.addSignature(
senderPublicKey,
Buffer.from(bs58.decode(result.signature))
);
const valid = transaction.verifySignatures();
return {
method: "solana_signTransaction",
address,
valid,
result: result.signature,
};
// eslint-disable-next-line
} catch (error: any) {
throw new Error(error);
}
};