-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathauthorize_nonce_account.rs
54 lines (46 loc) · 1.57 KB
/
authorize_nonce_account.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
use pinocchio::{
account_view::AccountView,
instruction::{AccountMeta, Instruction, Signer},
program::invoke_signed,
Address, ProgramResult,
};
/// Change the entity authorized to execute nonce instructions on the account.
///
/// The [`Address`] parameter identifies the entity to authorize.
///
/// ### Accounts:
/// 0. `[WRITE]` Nonce account
/// 1. `[SIGNER]` Nonce authority
pub struct AuthorizeNonceAccount<'a, 'b> {
/// Nonce account.
pub account: &'a AccountView,
/// Nonce authority.
pub authority: &'a AccountView,
/// New entity authorized to execute nonce instructions on the account.
pub new_authority: &'b Address,
}
impl AuthorizeNonceAccount<'_, '_> {
#[inline(always)]
pub fn invoke(&self) -> ProgramResult {
self.invoke_signed(&[])
}
pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
// account metadata
let account_metas: [AccountMeta; 2] = [
AccountMeta::writable(self.account.key()),
AccountMeta::readonly_signer(self.authority.key()),
];
// instruction data
// - [0..4 ]: instruction discriminator
// - [4..12]: lamports
let mut instruction_data = [0; 36];
instruction_data[0] = 7;
instruction_data[4..36].copy_from_slice(self.new_authority);
let instruction = Instruction {
program_id: &crate::ID,
accounts: &account_metas,
data: &instruction_data,
};
invoke_signed(&instruction, &[self.account, self.authority], signers)
}
}