forked from virjilakrum/ethereum-solana-wormhole-messaging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
240 lines (215 loc) · 9.34 KB
/
cli.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import fs from 'fs';
import { AptosAccount, AptosClient, Types } from "aptos";
import { Command } from 'commander';
import { AptosMessenger, EVMMessenger, SolanaMessenger } from './sdk';
import * as utils from './sdk/utils'; // Assuming utils is in a separate file named 'utils.ts'
import {
tryNativeToUint8Array,
generateSignAndSubmitEntryFunction,
hexToUint8Array,
parseSequenceFromLogAptos,
getEmitterAddressSolana,
getEmitterAddressEth,
} from '@certusone/wormhole-sdk';
import fetch from 'node-fetch';
import ethers from 'ethers';
const config = JSON.parse(fs.readFileSync('./xdapp.config.json').toString())
const program = new Command();
program
.command('generate-keys')
.description('Generate a new key pair')
.action(async () => {
const { privateKey, publicKey } = await utils.generateKeyPair();
console.log('Private Key:', privateKey.toString('hex'));
console.log('Public Key:', utils.compressPublicKey(publicKey));
});
program
.command('send-encrypted-message <network> <recipientPublicKey> <message>')
.description('Send an encrypted message')
.action(async (network, recipientPublicKey, message) => {
let messenger;
if (network === 'ethereum') {
messenger = new EVMMessenger(config.networks[network].rpc, config.networks[network].bridgeAddress, config.networks[network].deployedAddress);
} else if (network === 'solana') {
messenger = new SolanaMessenger(config.networks[network].rpc, config.networks[network].bridgeAddress);
} else {
console.error(`ERROR: Unsupported network ${network}`);
return;
}
const senderKeyPair = await utils.generateKeyPair(); // In practice, you'd load this from secure storage
const encryptedMessage = await utils.encrypt(Buffer.from(recipientPublicKey, 'hex'), message);
if (messenger.sendMessage) {
const result = await messenger.sendMessage(
utils.compressPublicKey(senderKeyPair.publicKey),
recipientPublicKey,
encryptedMessage,
/* other necessary params */
);
console.log('Message sent:', result);
} else {
console.error(`ERROR: sendMessage not supported on ${network}`);
}
});
program
.name('xMessenger')
.description("Cross chain messaging example project.")
.version("2.0.0");
// deploy <network>
program
.command('deploy')
.argument('<network>', 'name of the network in xdapp.config.json to deploy')
.action(async (network:string) => {
if(!config.networks[network]){
console.error(`ERROR: ${network} not found in xdapp.config.json`);
return;
}
console.log(`Deploying ${network}...`);
switch(config.networks[network].type){
case "aptos":
let aptosMessenger = new AptosMessenger(config.networks[network].rpc, config.networks[network].deployedAddress);
const txnHash = await aptosMessenger.deploy(config.networks[network].privateKey);
console.log(`Deployed @ ${txnHash}`);
break;
case "solana":
console.log("Not supported yet");
break;
case "evm":
let evmMesenger = new EVMMessenger(config.networks[network].rpc, config.networks[network].bridgeAddress);
await evmMesenger.deploy(network, config.networks[network].privateKey);
break;
}
console.log(`Deploy finished!`);
});
// register network
program
.command('register-network')
.argument("<network>", "The source network")
.argument("<network>", "The foreign network to be registered")
.action(async (src, target) => {
if(!config.networks[src]){
console.error(`ERROR: ${src} not found in xdapp.config.json`);
return;
}
switch(config.networks[src].type){
case "aptos":
let aptosMessenger = new AptosMessenger(config.networks[src].rpc, config.networks[src].deployedAddress);
let targetAddress = tryNativeToUint8Array(config.networks[target].deployedAddress, config.networks[target].wormholeChainId)
const payload = aptosMessenger.registerNetwork(config.networks[src].bridgeAddress, config.networks[target].wormholeChainId, targetAddress);
const tx = await generateSignAndSubmitEntryFunction(
aptosMessenger.client,
new AptosAccount(hexToUint8Array(config.networks[src].privateKey)),
payload
);
const res = await aptosMessenger.client.waitForTransactionWithResult(tx.hash);
console.log(`Registered @ ${tx.hash}`);
break;
case "solana":
console.log("Not supported yet");
break;
case "evm":
console.log("Not supported yet")
break;
}
});
// send msg
program
.command('send-msg')
.argument("<network>", "The source network")
.argument("<msg>", "The message to be emitted")
.action(async (src, message) => {
if(!config.networks[src]){
console.error(`ERROR: ${src} not found in xdapp.config.json`);
return;
}
switch(config.networks[src].type){
case "aptos":
let aptosMessenger = new AptosMessenger(config.networks[src].rpc, config.networks[src].deployedAddress);
const payload = aptosMessenger.sendMessage(message);
const tx = (await generateSignAndSubmitEntryFunction(
aptosMessenger.client,
new AptosAccount(hexToUint8Array(config.networks[src].privateKey)),
payload
) as Types.UserTransaction);
const res = await aptosMessenger.client.waitForTransactionWithResult(tx.hash);
console.log(`Sent Messagage @ ${tx.hash}`);
let seq = parseSequenceFromLogAptos(config.networks[src].bridgeAddress, tx);
let emitter = await aptosMessenger.getEmitterAddress();
console.log(await getEmitterAddressSolana(config.networks['solana'].deployedAddress));
console.log(
"Searching for: ",
`${config.wormhole.restAddress}/v1/signed_vaa/${config.networks[src].wormholeChainId}/${emitter}/${seq}`
);
let vaaBytes = undefined;
while (!vaaBytes) {
vaaBytes = (await (
await fetch(
`${config.wormhole.restAddress}/v1/signed_vaa/${config.networks[src].wormholeChainId}/${emitter}/${seq}`
)
).json()).vaaBytes;
await new Promise((r) => setTimeout(r, 1000)); // Poll guardiand every sec for message
}
console.log("VAA: ", vaaBytes);
break;
case "solana":
console.log("Not supported yet");
break;
case "evm":
console.log("Not supported yet");
break;
}
});
// submit msg
//AQAAAAABALtgDWjywCNRnmIfNU+IkWYUqbzl730v55syJzPmlCwvf3lz9V6Ax9p7ZhMX7kMufwqxNBCkDHlatY5mh5w0cuUAY5+tlgAAAAAAFgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAAAAAAAAAAAAaGVsbG8=
program
.command('submit-vaa')
.argument("<network>", "The source network")
.argument("<vaa>", "The message to be emitted")
.action(async (src, vaa) => {
if(!config.networks[src]){
console.error(`ERROR: ${src} not found in xdapp.config.json`);
return;
}
switch(config.networks[src].type){
case "aptos":
let aptosMessenger = new AptosMessenger(config.networks[src].rpc, config.networks[src].deployedAddress);
const payload = aptosMessenger.submitMessage(vaa);
const tx = (await generateSignAndSubmitEntryFunction(
aptosMessenger.client,
new AptosAccount(hexToUint8Array(config.networks[src].privateKey)),
payload
) as Types.UserTransaction);
const res = await aptosMessenger.client.waitForTransactionWithResult(tx.hash);
console.log(`Submitted @ ${tx.hash}`);
break;
case "solana":
console.log("Not supported yet");
break;
case "evm":
console.log("Not supported yet");
break;
}
});
// get msg
program
.command('get-msg')
.argument('<network>', 'The network to fetch the message from')
.action(async (network) => {
if(!config.networks[network]){
console.error(`ERROR: ${network} not found in xdapp.config.json`);
return;
}
console.log(`Fetching current message on ${network}...`);
switch(config.networks[network].type){
case "aptos":
let aptosMessenger = new AptosMessenger(config.networks[network].rpc, config.networks[network].deployedAddress);
console.log(await aptosMessenger.getMessage());
break;
case "solana":
console.log("Not supported yet");
break;
case "evm":
console.log("Not supported yet");
break;
}
});
program.parse();