-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlib.rs
More file actions
174 lines (146 loc) · 5.22 KB
/
lib.rs
File metadata and controls
174 lines (146 loc) · 5.22 KB
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
#![allow(unexpected_cfgs)]
#[cfg(any(test, feature = "test-helpers"))]
pub mod test_helpers;
use borsh::{BorshDeserialize, BorshSerialize};
use light_macros::pubkey;
use light_sdk::{
account::sha::LightAccount,
address::v1::derive_address,
constants::ADDRESS_TREE_V1,
cpi::{
v1::{CpiAccounts, LightSystemProgramCpi},
CpiSigner, InvokeLightSystemProgram, LightCpiInstruction,
},
derive_light_cpi_signer,
error::LightSdkError,
instruction::{account_meta::CompressedAccountMeta, PackedAddressTreeInfo, ValidityProof},
LightDiscriminator,
};
use solana_program::{
account_info::AccountInfo, entrypoint, program_error::ProgramError, pubkey::Pubkey,
};
pub const ID: Pubkey = pubkey!("2m6LXA7E6kMSkK6QHq2WCznD6kvhDcVFqEKpETKAQxYe");
pub const LIGHT_CPI_SIGNER: CpiSigner =
derive_light_cpi_signer!("2m6LXA7E6kMSkK6QHq2WCznD6kvhDcVFqEKpETKAQxYe");
entrypoint!(process_instruction);
#[repr(u8)]
#[derive(Debug)]
pub enum InstructionType {
Create = 0,
Update = 1,
}
impl TryFrom<u8> for InstructionType {
type Error = LightSdkError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(InstructionType::Create),
1 => Ok(InstructionType::Update),
_ => panic!("Invalid instruction discriminator."),
}
}
}
#[derive(
Debug, Default, Clone, BorshSerialize, BorshDeserialize, LightDiscriminator,
)]
pub struct MyCompressedAccount {
pub owner: Pubkey,
pub message: String,
}
#[derive(BorshSerialize, BorshDeserialize)]
pub struct CreateInstructionData {
pub proof: ValidityProof,
pub address_tree_info: PackedAddressTreeInfo,
pub output_state_tree_index: u8,
pub message: String,
}
#[derive(BorshSerialize, BorshDeserialize)]
pub struct UpdateInstructionData {
pub proof: ValidityProof,
pub account_meta: CompressedAccountMeta,
pub current_message: String,
pub new_message: String,
}
pub fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> Result<(), ProgramError> {
if program_id != &ID {
return Err(ProgramError::IncorrectProgramId);
}
if instruction_data.is_empty() {
return Err(ProgramError::InvalidInstructionData);
}
let discriminator = InstructionType::try_from(instruction_data[0])
.map_err(|_| ProgramError::InvalidInstructionData)?;
match discriminator {
InstructionType::Create => {
let instruction_data =
CreateInstructionData::try_from_slice(&instruction_data[1..])
.map_err(|_| ProgramError::InvalidInstructionData)?;
create(accounts, instruction_data)
}
InstructionType::Update => {
let instruction_data =
UpdateInstructionData::try_from_slice(&instruction_data[1..])
.map_err(|_| ProgramError::InvalidInstructionData)?;
update(accounts, instruction_data)
}
}
}
pub fn create(
accounts: &[AccountInfo],
instruction_data: CreateInstructionData,
) -> Result<(), ProgramError> {
let signer = accounts.first().ok_or(ProgramError::NotEnoughAccountKeys)?;
let light_cpi_accounts = CpiAccounts::new(signer, &accounts[1..], LIGHT_CPI_SIGNER);
let address_tree_pubkey = instruction_data
.address_tree_info
.get_tree_pubkey(&light_cpi_accounts)
.map_err(|_| ProgramError::NotEnoughAccountKeys)?;
if address_tree_pubkey.to_bytes() != ADDRESS_TREE_V1 {
solana_program::msg!("Invalid address tree");
return Err(ProgramError::InvalidAccountData);
}
let (address, address_seed) = derive_address(
&[b"message", signer.key.as_ref()],
&address_tree_pubkey,
&ID,
);
let new_address_params = instruction_data
.address_tree_info
.into_new_address_params_packed(address_seed);
let mut my_compressed_account = LightAccount::<MyCompressedAccount>::new_init(
&ID,
Some(address),
instruction_data.output_state_tree_index,
);
my_compressed_account.owner = *signer.key;
my_compressed_account.message = instruction_data.message;
LightSystemProgramCpi::new_cpi(LIGHT_CPI_SIGNER, instruction_data.proof)
.with_light_account(my_compressed_account)?
.with_new_addresses(&[new_address_params])
.invoke(light_cpi_accounts)?;
Ok(())
}
pub fn update(
accounts: &[AccountInfo],
instruction_data: UpdateInstructionData,
) -> Result<(), ProgramError> {
let signer = accounts.first().ok_or(ProgramError::NotEnoughAccountKeys)?;
let light_cpi_accounts = CpiAccounts::new(signer, &accounts[1..], LIGHT_CPI_SIGNER);
let mut my_compressed_account = LightAccount::<MyCompressedAccount>::new_mut(
&ID,
&instruction_data.account_meta,
MyCompressedAccount {
owner: *signer.key,
message: instruction_data.current_message,
},
)?;
// Update the account data with new message
my_compressed_account.account.message = instruction_data.new_message;
LightSystemProgramCpi::new_cpi(LIGHT_CPI_SIGNER, instruction_data.proof)
.with_light_account(my_compressed_account)?
.invoke(light_cpi_accounts)?;
Ok(())
}