/
lib.rs
333 lines (303 loc) · 13.9 KB
/
lib.rs
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
use anchor_lang::prelude::*;
pub use context::*;
pub use error::*;
pub use message::*;
pub use state::*;
pub mod context;
pub mod error;
pub mod message;
pub mod state;
declare_id!("Scaffo1dingHe11oWor1d1111111111111111111111");
#[program]
/// # Hello World (Scaffolding Example #1)
///
/// A Cross-Chain Hello World application. This contract uses Wormhole's
/// generic messaging to send an arbitrary message to registered emitters on
/// foreign networks.
///
/// ## Program Instructions
/// * [`initialize`](initialize)
/// * [`register_emitter`](register_emitter)
/// * [`send_message`](send_message)
/// * [`receive_message`](receive_message)
///
/// ## Program Accounts
/// * [Config]
/// * [ForeignEmitter]
/// * [Received]
/// * [WormholeEmitter]
pub mod hello_world {
use super::*;
use anchor_lang::solana_program;
use wormhole_anchor_sdk::wormhole;
/// This instruction initializes the program config, which is meant
/// to store data useful for other instructions. The config specifies
/// an owner (e.g. multisig) and should be read-only for every instruction
/// in this example. This owner will be checked for designated owner-only
/// instructions like [`register_emitter`](register_emitter).
///
/// # Arguments
///
/// * `ctx` - `Initialize` context
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
let config = &mut ctx.accounts.config;
// Set the owner of the config (effectively the owner of the program).
config.owner = ctx.accounts.owner.key();
// Set Wormhole related addresses.
{
let wormhole = &mut config.wormhole;
// wormhole::BridgeData (Wormhole's program data).
wormhole.bridge = ctx.accounts.wormhole_bridge.key();
// wormhole::FeeCollector (lamports collector for posting
// messages).
wormhole.fee_collector = ctx.accounts.wormhole_fee_collector.key();
// wormhole::SequenceTracker (tracks # of messages posted by this
// program).
wormhole.sequence = ctx.accounts.wormhole_sequence.key();
}
// Set default values for posting Wormhole messages.
//
// Zero means no batching.
config.batch_id = 0;
// Anchor IDL default coder cannot handle wormhole::Finality enum,
// so this value is stored as u8.
config.finality = wormhole::Finality::Confirmed as u8;
// Initialize our Wormhole emitter account. It is not required by the
// Wormhole program that there is an actual account associated with the
// emitter PDA. The emitter PDA is just a mechanism to have the program
// sign for the `wormhole::post_message` instruction.
//
// But for fun, we will store our emitter's bump for convenience.
ctx.accounts.wormhole_emitter.bump = *ctx
.bumps
.get("wormhole_emitter")
.ok_or(HelloWorldError::BumpNotFound)?;
// This scope shows the steps of how to post a message with the
// Wormhole program.
{
// If Wormhole requires a fee before posting a message, we need to
// transfer lamports to the fee collector. Otherwise
// `wormhole::post_message` will fail.
let fee = ctx.accounts.wormhole_bridge.fee();
if fee > 0 {
solana_program::program::invoke(
&solana_program::system_instruction::transfer(
&ctx.accounts.owner.key(),
&ctx.accounts.wormhole_fee_collector.key(),
fee,
),
&ctx.accounts.to_account_infos(),
)?;
}
// Invoke `wormhole::post_message`. We are sending a Wormhole
// message in the `initialize` instruction so the Wormhole program
// can create a SequenceTracker account for our emitter. We will
// deserialize this account for our `send_message` instruction so
// we can find the next sequence number. More details about this in
// `send_message`.
//
// `wormhole::post_message` requires two signers: one for the
// emitter and another for the wormhole message data. Both of these
// accounts are owned by this program.
//
// There are two ways to handle the wormhole message data account:
// 1. Using an extra keypair. You may to generate a keypair
// outside of this instruction and pass that keypair as an
// additional signer for the transaction. An integrator might
// use an extra keypair if the message can be "thrown away"
// (not easily retrievable without going back to this
// transaction hash to retrieve the message's pubkey).
// 2. Generate a PDA. If we want some way to deserialize the
// message data written by the Wormhole program, we can use an
// account with an address derived by this program so we can
// use the PDA to access and deserialize the message data.
//
// In our example, we use method #2.
let wormhole_emitter = &ctx.accounts.wormhole_emitter;
let config = &ctx.accounts.config;
// If anyone were to care about the first message this program
// emits, he can deserialize it to find the program with which
// the emitter PDA was derived.
let mut payload: Vec<u8> = Vec::new();
HelloWorldMessage::serialize(
&HelloWorldMessage::Alive {
program_id: *ctx.program_id,
},
&mut payload,
)?;
wormhole::post_message(
CpiContext::new_with_signer(
ctx.accounts.wormhole_program.to_account_info(),
wormhole::PostMessage {
config: ctx.accounts.wormhole_bridge.to_account_info(),
message: ctx.accounts.wormhole_message.to_account_info(),
emitter: wormhole_emitter.to_account_info(),
sequence: ctx.accounts.wormhole_sequence.to_account_info(),
payer: ctx.accounts.owner.to_account_info(),
fee_collector: ctx.accounts.wormhole_fee_collector.to_account_info(),
clock: ctx.accounts.clock.to_account_info(),
rent: ctx.accounts.rent.to_account_info(),
system_program: ctx.accounts.system_program.to_account_info(),
},
&[
&[
SEED_PREFIX_SENT,
&wormhole::INITIAL_SEQUENCE.to_le_bytes()[..],
&[*ctx
.bumps
.get("wormhole_message")
.ok_or(HelloWorldError::BumpNotFound)?],
],
&[wormhole::SEED_PREFIX_EMITTER, &[wormhole_emitter.bump]],
],
),
config.batch_id,
payload,
config.finality.try_into().unwrap(),
)?;
}
// Done.
Ok(())
}
/// This instruction registers a new foreign emitter (from another network)
/// and saves the emitter information in a ForeignEmitter account. This
/// instruction is owner-only, meaning that only the owner of the program
/// (defined in the [Config] account) can add and update emitters.
///
/// # Arguments
///
/// * `ctx` - `RegisterForeignEmitter` context
/// * `chain` - Wormhole Chain ID
/// * `address` - Wormhole Emitter Address
pub fn register_emitter(
ctx: Context<RegisterEmitter>,
chain: u16,
address: [u8; 32],
) -> Result<()> {
// Foreign emitter cannot share the same Wormhole Chain ID as the
// Solana Wormhole program's. And cannot register a zero address.
require!(
chain > 0 && chain != wormhole::CHAIN_ID_SOLANA && !address.iter().all(|&x| x == 0),
HelloWorldError::InvalidForeignEmitter,
);
// Save the emitter info into the ForeignEmitter account.
let emitter = &mut ctx.accounts.foreign_emitter;
emitter.chain = chain;
emitter.address = address;
// Done.
Ok(())
}
/// This instruction posts a Wormhole message of some arbitrary size
/// in the form of bytes ([Vec<u8>]). The message is encoded as
/// [HelloWorldMessage::Hello], which serializes a payload ID (1) before the message
/// specified in the instruction. Instead of using the native borsh
/// serialization of [Vec] length (little endian u32), length of the
/// message is encoded as big endian u16 (in EVM, bytes for numerics are
/// natively serialized as big endian).
///
/// See [HelloWorldMessage] enum for serialization implementation.
///
/// # Arguments
///
/// * `message` - Arbitrary message to send out
pub fn send_message(ctx: Context<SendMessage>, message: Vec<u8>) -> Result<()> {
// If Wormhole requires a fee before posting a message, we need to
// transfer lamports to the fee collector. Otherwise
// `wormhole::post_message` will fail.
let fee = ctx.accounts.wormhole_bridge.fee();
if fee > 0 {
solana_program::program::invoke(
&solana_program::system_instruction::transfer(
&ctx.accounts.payer.key(),
&ctx.accounts.wormhole_fee_collector.key(),
fee,
),
&ctx.accounts.to_account_infos(),
)?;
}
// Invoke `wormhole::post_message`.
//
// `wormhole::post_message` requires two signers: one for the emitter
// and another for the wormhole message data. Both of these accounts
// are owned by this program.
//
// There are two ways to handle the wormhole message data account:
// 1. Using an extra keypair. You may to generate a keypair outside
// of this instruction and pass that keypair as an additional
// signer for the transaction. An integrator might use an extra
// keypair if the message can be "thrown away" (not easily
// retrievable without going back to this transaction hash to
// retrieve the message's pubkey).
// 2. Generate a PDA. If we want some way to deserialize the message
// data written by the Wormhole program, we can use an account
// with an address derived by this program so we can use the PDA
// to access and deserialize the message data.
//
// In our example, we use method #2.
let wormhole_emitter = &ctx.accounts.wormhole_emitter;
let config = &ctx.accounts.config;
// There is only one type of message that this example uses to
// communicate with its foreign counterparts (payload ID == 1).
let payload: Vec<u8> = HelloWorldMessage::Hello { message }.try_to_vec()?;
wormhole::post_message(
CpiContext::new_with_signer(
ctx.accounts.wormhole_program.to_account_info(),
wormhole::PostMessage {
config: ctx.accounts.wormhole_bridge.to_account_info(),
message: ctx.accounts.wormhole_message.to_account_info(),
emitter: wormhole_emitter.to_account_info(),
sequence: ctx.accounts.wormhole_sequence.to_account_info(),
payer: ctx.accounts.payer.to_account_info(),
fee_collector: ctx.accounts.wormhole_fee_collector.to_account_info(),
clock: ctx.accounts.clock.to_account_info(),
rent: ctx.accounts.rent.to_account_info(),
system_program: ctx.accounts.system_program.to_account_info(),
},
&[
&[
SEED_PREFIX_SENT,
&ctx.accounts.wormhole_sequence.next_value().to_le_bytes()[..],
&[*ctx
.bumps
.get("wormhole_message")
.ok_or(HelloWorldError::BumpNotFound)?],
],
&[wormhole::SEED_PREFIX_EMITTER, &[wormhole_emitter.bump]],
],
),
config.batch_id,
payload,
config.finality.try_into().unwrap(),
)?;
// Done.
Ok(())
}
/// This instruction reads a posted verified Wormhole message and verifies
/// that the payload is of type [HelloWorldMessage::Hello] (payload ID == 1). HelloWorldMessage
/// data is stored in a [Received] account.
///
/// See [HelloWorldMessage] enum for deserialization implementation.
///
/// # Arguments
///
/// * `vaa_hash` - Keccak256 hash of verified Wormhole message
pub fn receive_message(ctx: Context<ReceiveMessage>, vaa_hash: [u8; 32]) -> Result<()> {
let posted_message = &ctx.accounts.posted;
if let HelloWorldMessage::Hello { message } = posted_message.data() {
// HelloWorldMessage cannot be larger than the maximum size of the account.
require!(
message.len() <= MESSAGE_MAX_LENGTH,
HelloWorldError::InvalidMessage,
);
// Save batch ID, keccak256 hash and message payload.
let received = &mut ctx.accounts.received;
received.batch_id = posted_message.batch_id();
received.wormhole_message_hash = vaa_hash;
received.message = message.clone();
// Done
Ok(())
} else {
Err(HelloWorldError::InvalidMessage.into())
}
}
}