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
use {
    anchor_lang::{
        prelude::*,
        solana_program::system_program
    },
    crate::state::*,
    std::mem::size_of
};

#[derive(Accounts)]
#[instruction(
    name: String,
    value: Pubkey,
    pointer_bump: u8,
    proof_bump: u8,
)]
pub struct CreatePointer<'info> {
    #[account(
        mut, 
        seeds = [
            SEED_INDEX, 
            index.owner.key().as_ref(), 
            index.namespace.as_ref()
        ],
        bump = index.bump, 
        has_one = owner,
    )]
    pub index: Account<'info, Index>,

    #[account(mut)]
    pub owner: Signer<'info>,

    #[account(mut)]
    pub payer: Signer<'info>,

    #[account(
        init,
        seeds = [
            SEED_POINTER,
            index.key().as_ref(),
            name.as_bytes(),
        ],
        bump = pointer_bump,
        payer = payer,
        space = 8 + size_of::<Pointer>(),
    )]
    pub pointer: Account<'info, Pointer>,

    #[account(
        init,
        seeds = [
            SEED_PROOF,
            index.key().as_ref(),
            value.as_ref(),
        ],
        bump = proof_bump,
        payer = payer,
        space = 8 + size_of::<Proof>(),
    )]
    pub proof: Account<'info, Proof>,
    
    #[account(address = system_program::ID)]
    pub system_program: Program<'info, System>,
}

pub fn handler(
    ctx: Context<CreatePointer>,
    name: String,
    value: Pubkey,
    pointer_bump: u8,
    proof_bump: u8,
) -> ProgramResult {
    // Get accounts.
    let index = &mut ctx.accounts.index;
    let pointer = &mut ctx.accounts.pointer;
    let proof = &mut ctx.accounts.proof;

    // Initialize pointer account.
    pointer.name = name.clone();
    pointer.value = value;
    pointer.bump = pointer_bump;

    // Initialize proof account.
    proof.name = pointer.name.clone(); 
    proof.bump = proof_bump;

    // Increment index counter.
    index.count += 1;
    
    return Ok(());
}