forked from Asterizm-Protocol/asterizm-contracts-sol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
388 lines (327 loc) · 11.3 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
use anchor_lang::prelude::*;
declare_id!("As34Rx7ZJKc6JETxaYSZ7fHUkmhhBLhLzjMdTXJTuXDS");
declare_program!(asterizm_initializer);
declare_program!(asterizm_relayer);
mod models;
pub use models::*;
#[program]
pub mod asterizm_client {
use super::*;
use anchor_lang::solana_program::hash::hash;
use anchor_lang::solana_program::program::invoke_signed;
use anchor_lang::solana_program::system_instruction;
use anchor_lang::solana_program::sysvar::instructions::get_instruction_relative;
pub fn initialize(
ctx: Context<Initialize>,
local_chain_id: u64,
manager: Pubkey,
) -> Result<()> {
ctx.accounts.settings_account.is_initialized = true;
ctx.accounts.settings_account.manager = manager;
ctx.accounts.settings_account.local_chain_id = local_chain_id;
ctx.accounts.settings_account.bump = ctx.bumps.settings_account;
emit!(CreateClientSettingsEvent {
address: ctx.accounts.settings_account.key(),
manager,
local_chain_id,
});
Ok(())
}
pub fn update_settings(ctx: Context<UpdateSettings>, manager: Pubkey) -> Result<()> {
ctx.accounts.settings_account.manager = manager;
emit!(UpdateClientSettingsEvent {
address: ctx.accounts.settings_account.key(),
manager,
});
Ok(())
}
pub fn create_client(
ctx: Context<CreateClientAccount>,
user_address: Pubkey,
relay_owner: Pubkey,
notify_transfer_sending_result: bool,
disable_hash_validation: bool,
) -> Result<()> {
ctx.accounts.client_account.is_initialized = true;
ctx.accounts.client_account.user_address = user_address;
ctx.accounts.client_account.relay_owner = relay_owner;
ctx.accounts.client_account.notify_transfer_sending_result = notify_transfer_sending_result;
ctx.accounts.client_account.disable_hash_validation = disable_hash_validation;
ctx.accounts.client_account.bump = ctx.bumps.client_account;
emit!(ClientCreatedEvent {
address: ctx.accounts.client_account.key(),
user_address,
relay_owner,
notify_transfer_sending_result,
disable_hash_validation,
});
Ok(())
}
pub fn update_client(
ctx: Context<UpdateClientAccount>,
user_address: Pubkey,
relay_owner: Pubkey,
notify_transfer_sending_result: bool,
disable_hash_validation: bool,
) -> Result<()> {
ctx.accounts.client_account.relay_owner = relay_owner;
ctx.accounts.client_account.notify_transfer_sending_result = notify_transfer_sending_result;
ctx.accounts.client_account.disable_hash_validation = disable_hash_validation;
emit!(ClientUpdatedEvent {
address: ctx.accounts.client_account.key(),
user_address,
relay_owner,
notify_transfer_sending_result,
disable_hash_validation,
});
Ok(())
}
pub fn create_client_trusted_address(
ctx: Context<CreateClientTrustedAddress>,
user_address: Pubkey,
chain_id: u64,
address: Pubkey,
) -> Result<()> {
ctx.accounts.trusted_address.is_initialized = true;
ctx.accounts.trusted_address.user_address = user_address;
ctx.accounts.trusted_address.address = address;
ctx.accounts.trusted_address.chain_id = chain_id;
ctx.accounts.trusted_address.bump = ctx.bumps.trusted_address;
emit!(ClientTrustedAddressCreatedEvent {
address,
user_address,
chain_id,
});
Ok(())
}
pub fn remove_client_trusted_address(
_ctx: Context<RemoveClientTrustedAddress>,
user_address: Pubkey,
chain_id: u64,
) -> Result<()> {
emit!(ClientTrustedAddressRemovedEvent {
user_address,
chain_id,
});
Ok(())
}
pub fn create_client_sender(
ctx: Context<CreateClientSender>,
user_address: Pubkey,
address: Pubkey,
) -> Result<()> {
ctx.accounts.sender.is_initialized = true;
ctx.accounts.sender.user_address = user_address;
ctx.accounts.sender.address = address;
ctx.accounts.sender.bump = ctx.bumps.sender;
emit!(ClientSenderCreatedEvent {
address,
user_address,
});
Ok(())
}
pub fn remove_client_sender(
_ctx: Context<RemoveClientSender>,
user_address: Pubkey,
address: Pubkey,
) -> Result<()> {
emit!(ClientSenderRemovedEvent {
address,
user_address,
});
Ok(())
}
pub fn init_send_message(
ctx: Context<InitSendMessage>,
user_address: Pubkey,
dst_chain_id: u64,
payload: Vec<u8>,
tx_id: u128,
) -> Result<()> {
ctx.accounts.client_account.tx_id += 1;
let message = InitMessage {
src_chain_id: ctx.accounts.settings_account.local_chain_id,
src_address: user_address,
dst_chain_id,
dst_address: ctx.accounts.trusted_address.address,
tx_id,
payload: payload.clone(),
};
let buffer = serialize_init_message_eth(message);
let transfer_hash = if ctx.accounts.chain_account.chain_type == 1
|| ctx.accounts.chain_account.chain_type == 4
{
hash(&buffer).to_bytes()
} else {
build_crosschain_hash(&buffer)
};
// Create Settings Account
let (outgoing_transfer_account, outgoing_transfer_bump) = Pubkey::find_program_address(
&[
br"outgoing_transfer",
&user_address.to_bytes(),
&transfer_hash,
],
&ID,
);
let outgoing_transfer_account_signer_seeds: &[&[_]] = &[
br"outgoing_transfer",
&user_address.to_bytes(),
&transfer_hash,
&[outgoing_transfer_bump],
];
if outgoing_transfer_account != ctx.accounts.transfer_account.key() {
return Err(ProgramError::InvalidArgument.into());
}
invoke_signed(
&system_instruction::create_account(
&ctx.accounts.signer.key(),
&ctx.accounts.transfer_account.key(),
1.max(ctx.accounts.rent.minimum_balance(TRANSFER_ACCOUNT_LEN + 8)),
(TRANSFER_ACCOUNT_LEN + 8) as u64,
&ID,
),
&[
ctx.accounts.signer.to_account_info(),
ctx.accounts.transfer_account.clone(),
ctx.accounts.rent.to_account_info(),
ctx.accounts.system_program.to_account_info(),
],
&[outgoing_transfer_account_signer_seeds],
)?;
// Init Settings Account
let settings_account_data = TransferAccount {
success_receive: true,
success_execute: false,
bump: outgoing_transfer_bump,
};
settings_account_data
.try_serialize(&mut *ctx.accounts.transfer_account.try_borrow_mut_data()?)?;
emit!(InitiateTransferEvent {
dst_chain_id,
trusted_address: ctx.accounts.trusted_address.address,
id: tx_id,
transfer_hash,
payload,
});
Ok(())
}
pub fn send_message(
ctx: Context<SendMessage>,
user_address: Pubkey,
dst_chain_id: u64,
tx_id: u128,
transfer_hash: [u8; 32],
value: u64,
) -> Result<()> {
asterizm_initializer::cpi::send_message(
ctx.accounts.into(),
ctx.accounts.client_account.relay_owner,
dst_chain_id,
user_address,
ctx.accounts.trusted_address.address,
tx_id,
transfer_hash,
ctx.accounts.client_account.notify_transfer_sending_result,
value,
)?;
ctx.accounts.transfer_account.success_execute = true;
Ok(())
}
pub fn resend_message(
ctx: Context<ResendMessage>,
user_address: Pubkey,
transfer_hash: [u8; 32],
value: u64,
) -> Result<()> {
asterizm_initializer::cpi::resend_message(
ctx.accounts.into(),
ctx.accounts.client_account.relay_owner,
user_address,
transfer_hash,
value,
)?;
Ok(())
}
pub fn init_receive_message(
ctx: Context<InitReceiveMessage>,
_dst_address: Pubkey,
src_address: Pubkey,
src_chain_id: u64,
tx_id: u128,
transfer_hash: [u8; 32],
) -> Result<()> {
let current_ix =
get_instruction_relative(0, &ctx.accounts.instruction_sysvar_account).unwrap();
// Restrict Who can call Receive message via CPI
if current_ix.program_id != asterizm_relayer::ID {
msg!("Only Relayer can call Client init Receive message instruction");
return Err(ProgramError::IncorrectProgramId.into());
}
ctx.accounts.transfer_account.success_receive = true;
ctx.accounts.transfer_account.bump = ctx.bumps.transfer_account;
emit!(PayloadReceivedEvent {
src_chain_id,
src_address,
tx_id,
transfer_hash
});
Ok(())
}
pub fn receive_message(
ctx: Context<ReceiveMessage>,
dst_address: Pubkey,
tx_id: u128,
src_chain_id: u64,
src_address: Pubkey,
transfer_hash: [u8; 32],
payload: Vec<u8>,
) -> Result<()> {
if ctx.accounts.transfer_account.success_execute == true {
return Err(ProgramError::AccountAlreadyInitialized.into());
}
let message = InitMessage {
src_chain_id,
src_address,
dst_chain_id: ctx.accounts.settings_account.local_chain_id,
dst_address,
tx_id,
payload: payload.clone(),
};
let buffer = serialize_init_message_eth(message);
let calculated_transfer_hash = if ctx.accounts.chain_account.chain_type == 1
|| ctx.accounts.chain_account.chain_type == 4
{
hash(&buffer).to_bytes()
} else {
build_crosschain_hash(&buffer)
};
if !ctx.accounts.client_account.disable_hash_validation {
if calculated_transfer_hash != transfer_hash {
return Err(ProgramError::InvalidArgument.into());
}
}
ctx.accounts.transfer_account.success_execute = true;
Ok(())
}
pub fn transfer_sending_result(
ctx: Context<TransferSendingResult>,
dst_address: Pubkey,
transfer_hash: [u8; 32],
status_code: u8,
) -> Result<()> {
let current_ix =
get_instruction_relative(0, &ctx.accounts.instruction_sysvar_account).unwrap();
// Restrict Who can call Receive message via CPI
if current_ix.program_id != asterizm_relayer::ID {
msg!("Only Relayer can call Client transfer sending result instruction");
return Err(ProgramError::IncorrectProgramId.into());
}
emit!(TransferSendingResultEvent {
dst_address,
transfer_hash,
status_code,
});
Ok(())
}
}