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
extern crate proc_macro;
use quote;
use parse_macro_input;
/// Executes the given access control method before running the decorated
/// instruction handler. Any method in scope of the attribute can be invoked
/// with any arguments from the associated instruction handler.
///
/// # Example
///
/// ```ignore
/// use anchor_lang::prelude::*;
///
/// #[program]
/// mod errors {
/// use super::*;
///
/// #[access_control(Create::accounts(&ctx, bump_seed))]
/// pub fn create(ctx: Context<Create>, bump_seed: u8) -> Result<()> {
/// let my_account = &mut ctx.accounts.my_account;
/// my_account.bump_seed = bump_seed;
/// }
/// }
///
/// #[derive(Accounts)]
/// pub struct Create {
/// #[account(init)]
/// my_account: Account<'info, MyAccount>,
/// }
///
/// impl Create {
/// pub fn accounts(ctx: &Context<Create>, bump_seed: u8) -> Result<()> {
/// let seeds = &[ctx.accounts.my_account.to_account_info().key.as_ref(), &[bump_seed]];
/// Pubkey::create_program_address(seeds, ctx.program_id)
/// .map_err(|_| ErrorCode::InvalidNonce)?;
/// Ok(())
/// }
/// }
/// ```
///
/// This example demonstrates a useful pattern. Not only can you use
/// `#[access_control]` to ensure any invariants or preconditions hold prior to
/// executing an instruction, but also it can be used to finish any validation
/// on the `Accounts` struct, particularly when instruction arguments are
/// needed. Here, we use the given `bump_seed` to verify it creates a valid
/// program-derived address.