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
use {
crate::{
error::AssociatedTokenAccountError,
instruction::AssociatedTokenAccountInstruction,
tools::account::{create_pda_account, get_account_len},
*,
},
borsh::BorshDeserialize,
solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint::ProgramResult,
msg,
program::{invoke, invoke_signed},
program_error::ProgramError,
pubkey::Pubkey,
rent::Rent,
system_program,
sysvar::Sysvar,
},
safe_token_2022::{
extension::{ExtensionType, StateWithExtensions},
state::{Account, Mint},
},
};
#[derive(PartialEq)]
enum CreateMode {
Always,
Idempotent,
}
pub fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
input: &[u8],
) -> ProgramResult {
let instruction = if input.is_empty() {
AssociatedTokenAccountInstruction::Create
} else {
AssociatedTokenAccountInstruction::try_from_slice(input)
.map_err(|_| ProgramError::InvalidInstructionData)?
};
msg!("{:?}", instruction);
match instruction {
AssociatedTokenAccountInstruction::Create => {
process_create_associated_token_account(program_id, accounts, CreateMode::Always)
}
AssociatedTokenAccountInstruction::CreateIdempotent => {
process_create_associated_token_account(program_id, accounts, CreateMode::Idempotent)
}
AssociatedTokenAccountInstruction::RecoverNested => {
process_recover_nested(program_id, accounts)
}
}
}
fn process_create_associated_token_account(
program_id: &Pubkey,
accounts: &[AccountInfo],
create_mode: CreateMode,
) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
let funder_info = next_account_info(account_info_iter)?;
let associated_token_account_info = next_account_info(account_info_iter)?;
let wallet_account_info = next_account_info(account_info_iter)?;
let safe_token_mint_info = next_account_info(account_info_iter)?;
let system_program_info = next_account_info(account_info_iter)?;
let safe_token_program_info = next_account_info(account_info_iter)?;
let safe_token_program_id = safe_token_program_info.key;
let (associated_token_address, bump_seed) = get_associated_token_address_and_bump_seed_internal(
wallet_account_info.key,
safe_token_mint_info.key,
program_id,
safe_token_program_id,
);
if associated_token_address != *associated_token_account_info.key {
msg!("Error: Associated address does not match seed derivation");
return Err(ProgramError::InvalidSeeds);
}
if create_mode == CreateMode::Idempotent
&& associated_token_account_info.owner == safe_token_program_id
{
let ata_data = associated_token_account_info.data.borrow();
if let Ok(associated_token_account) = StateWithExtensions::<Account>::unpack(&ata_data) {
if associated_token_account.base.owner != *wallet_account_info.key {
let error = AssociatedTokenAccountError::InvalidOwner;
msg!("{}", error);
return Err(error.into());
}
if associated_token_account.base.mint != *safe_token_mint_info.key {
return Err(ProgramError::InvalidAccountData);
}
return Ok(());
}
}
if *associated_token_account_info.owner != system_program::id() {
return Err(ProgramError::IllegalOwner);
}
let rent = Rent::get()?;
let associated_token_account_signer_seeds: &[&[_]] = &[
&wallet_account_info.key.to_bytes(),
&safe_token_program_id.to_bytes(),
&safe_token_mint_info.key.to_bytes(),
&[bump_seed],
];
let account_len = get_account_len(
safe_token_mint_info,
safe_token_program_info,
&[ExtensionType::ImmutableOwner],
)?;
create_pda_account(
funder_info,
&rent,
account_len,
safe_token_program_id,
system_program_info,
associated_token_account_info,
associated_token_account_signer_seeds,
)?;
msg!("Initialize the associated token account");
invoke(
&safe_token_2022::instruction::initialize_immutable_owner(
safe_token_program_id,
associated_token_account_info.key,
)?,
&[
associated_token_account_info.clone(),
safe_token_program_info.clone(),
],
)?;
invoke(
&safe_token_2022::instruction::initialize_account3(
safe_token_program_id,
associated_token_account_info.key,
safe_token_mint_info.key,
wallet_account_info.key,
)?,
&[
associated_token_account_info.clone(),
safe_token_mint_info.clone(),
wallet_account_info.clone(),
safe_token_program_info.clone(),
],
)
}
pub fn process_recover_nested(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
let nested_associated_token_account_info = next_account_info(account_info_iter)?;
let nested_token_mint_info = next_account_info(account_info_iter)?;
let destination_associated_token_account_info = next_account_info(account_info_iter)?;
let owner_associated_token_account_info = next_account_info(account_info_iter)?;
let owner_token_mint_info = next_account_info(account_info_iter)?;
let wallet_account_info = next_account_info(account_info_iter)?;
let safe_token_program_info = next_account_info(account_info_iter)?;
let safe_token_program_id = safe_token_program_info.key;
let (owner_associated_token_address, bump_seed) =
get_associated_token_address_and_bump_seed_internal(
wallet_account_info.key,
owner_token_mint_info.key,
program_id,
safe_token_program_id,
);
if owner_associated_token_address != *owner_associated_token_account_info.key {
msg!("Error: Owner associated address does not match seed derivation");
return Err(ProgramError::InvalidSeeds);
}
let (nested_associated_token_address, _) = get_associated_token_address_and_bump_seed_internal(
owner_associated_token_account_info.key,
nested_token_mint_info.key,
program_id,
safe_token_program_id,
);
if nested_associated_token_address != *nested_associated_token_account_info.key {
msg!("Error: Nested associated address does not match seed derivation");
return Err(ProgramError::InvalidSeeds);
}
let (destination_associated_token_address, _) =
get_associated_token_address_and_bump_seed_internal(
wallet_account_info.key,
nested_token_mint_info.key,
program_id,
safe_token_program_id,
);
if destination_associated_token_address != *destination_associated_token_account_info.key {
msg!("Error: Destination associated address does not match seed derivation");
return Err(ProgramError::InvalidSeeds);
}
if !wallet_account_info.is_signer {
msg!("Wallet of the owner associated token account must sign");
return Err(ProgramError::MissingRequiredSignature);
}
if owner_token_mint_info.owner != safe_token_program_id {
msg!("Owner mint not owned by provided token program");
return Err(ProgramError::IllegalOwner);
}
let (amount, decimals) = {
if owner_associated_token_account_info.owner != safe_token_program_id {
msg!("Owner associated token account not owned by provided token program, recreate the owner associated token account first");
return Err(ProgramError::IllegalOwner);
}
let owner_account_data = owner_associated_token_account_info.data.borrow();
let owner_account = StateWithExtensions::<Account>::unpack(&owner_account_data)?;
if owner_account.base.owner != *wallet_account_info.key {
msg!("Owner associated token account not owned by provided wallet");
return Err(AssociatedTokenAccountError::InvalidOwner.into());
}
if nested_associated_token_account_info.owner != safe_token_program_id {
msg!("Nested associated token account not owned by provided token program");
return Err(ProgramError::IllegalOwner);
}
let nested_account_data = nested_associated_token_account_info.data.borrow();
let nested_account = StateWithExtensions::<Account>::unpack(&nested_account_data)?;
if nested_account.base.owner != *owner_associated_token_account_info.key {
msg!("Nested associated token account not owned by provided associated token account");
return Err(AssociatedTokenAccountError::InvalidOwner.into());
}
let amount = nested_account.base.amount;
if nested_token_mint_info.owner != safe_token_program_id {
msg!("Nested mint account not owned by provided token program");
return Err(ProgramError::IllegalOwner);
}
let nested_mint_data = nested_token_mint_info.data.borrow();
let nested_mint = StateWithExtensions::<Mint>::unpack(&nested_mint_data)?;
let decimals = nested_mint.base.decimals;
(amount, decimals)
};
let owner_associated_token_account_signer_seeds: &[&[_]] = &[
&wallet_account_info.key.to_bytes(),
&safe_token_program_id.to_bytes(),
&owner_token_mint_info.key.to_bytes(),
&[bump_seed],
];
invoke_signed(
&safe_token_2022::instruction::transfer_checked(
safe_token_program_id,
nested_associated_token_account_info.key,
nested_token_mint_info.key,
destination_associated_token_account_info.key,
owner_associated_token_account_info.key,
&[],
amount,
decimals,
)?,
&[
nested_associated_token_account_info.clone(),
nested_token_mint_info.clone(),
destination_associated_token_account_info.clone(),
owner_associated_token_account_info.clone(),
safe_token_program_info.clone(),
],
&[owner_associated_token_account_signer_seeds],
)?;
invoke_signed(
&safe_token_2022::instruction::close_account(
safe_token_program_id,
nested_associated_token_account_info.key,
wallet_account_info.key,
owner_associated_token_account_info.key,
&[],
)?,
&[
nested_associated_token_account_info.clone(),
wallet_account_info.clone(),
owner_associated_token_account_info.clone(),
safe_token_program_info.clone(),
],
&[owner_associated_token_account_signer_seeds],
)
}