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
use {
    crate::state::*,
    anchor_lang::prelude::*,
    hpl_utils::{reallocate, Default},
    std::cmp::max,
};

pub fn insert_public_info<'info>(
    label: String,
    value: String,
    public_info: &mut Account<'info, PublicInfo>,
    authority: &Signer<'info>,
    rent_sysvar: &Sysvar<'info, Rent>,
    system_program: &Program<'info, System>,
) -> Result<()> {
    if let Some(old) = public_info.info.get(&label) {
        let mut old_value_size: isize = 0;
        println!("{}", old_value_size);
        match old {
            Info::SingleValue { value } => {
                old_value_size = value.as_bytes().len() as isize;
            }
        }

        let new_value_size = value.as_bytes().len() as isize;
        let delta = new_value_size - old_value_size;
        if delta != 0 {
            reallocate(
                delta,
                public_info.to_account_info(),
                authority.to_account_info(),
                &rent_sysvar,
                &system_program,
            )?;
        }
    } else {
        reallocate(
            max(24, value.as_bytes().len() * 2) as isize
                + max(24, label.as_bytes().len() * 2) as isize,
            public_info.to_account_info(),
            authority.to_account_info(),
            &rent_sysvar,
            &system_program,
        )?;
    }

    public_info
        .info
        .insert(label, Info::SingleValue { value: value });

    Ok(())
}

#[derive(Accounts)]
#[instruction(_env: String)]
pub struct InitPublicInfo<'info> {
    /// Public info account
    #[account(
      init, payer = authority,
      space = PublicInfo::LEN,
      seeds = [b"public_info".as_ref(), _env.as_bytes(), crate::id().as_ref()],
      bump,
    )]
    pub public_info: Account<'info, PublicInfo>,

    /// The update authority of the program
    #[account(mut)]
    pub authority: Signer<'info>,

    /// Program data accout
    /// CHECK: This is not dangerous
    #[account()]
    pub program_data: AccountInfo<'info>,

    /// Program accounnt
    /// CHECK: This is not dangerous
    #[account(address = crate::id())]
    pub program: AccountInfo<'info>,

    /// The system program.
    pub system_program: Program<'info, System>,
}

pub fn init_public_info(ctx: Context<InitPublicInfo>, _env: String) -> Result<()> {
    let public_info = &mut ctx.accounts.public_info;
    public_info.set_defaults();
    public_info.bump = ctx.bumps["public_info"];
    Ok(())
}

#[derive(Accounts)]
#[instruction(_env: String)]
pub struct SetPublicInfo<'info> {
    /// Public info account
    #[account(
      mut,
      seeds = [b"public_info".as_ref(), _env.as_bytes(), crate::id().as_ref()],
      bump = public_info.bump,
    )]
    pub public_info: Account<'info, PublicInfo>,

    /// The update authority of the program
    #[account(mut)]
    pub authority: Signer<'info>,

    /// Program data accout
    /// CHECK: This is not dangerous
    #[account()]
    pub program_data: AccountInfo<'info>,

    /// Program accounnt
    /// CHECK: This is not dangerous
    #[account(address = crate::id())]
    pub program: AccountInfo<'info>,

    /// NATIVE RENT SYSVAR
    pub rent_sysvar: Sysvar<'info, Rent>,
    /// The system program.
    pub system_program: Program<'info, System>,
}
/// Structure representing the arguments for setting public information in the project.
///
/// # Fields
///
/// - `label`: The label of the public information to be set.
/// - `value`: The value of the public information to be set.
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug, PartialEq)]
pub struct SetPublicInfoArgs {
    pub label: String,
    pub value: String,
}

pub fn set_public_info(
    ctx: Context<SetPublicInfo>,
    _env: String,
    args: SetPublicInfoArgs,
) -> Result<()> {
    let public_info = &mut ctx.accounts.public_info;
    insert_public_info(
        args.label,
        args.value,
        public_info,
        &ctx.accounts.authority,
        &ctx.accounts.rent_sysvar,
        &ctx.accounts.system_program,
    )?;
    Ok(())
}

#[derive(Accounts)]
#[instruction(_env: String)]
pub struct SetAuthDriver<'info> {
    #[account(
      mut,
      seeds = [b"public_info".as_ref(), _env.as_bytes(), crate::id().as_ref()],
      bump = public_info.bump,
    )]
    pub public_info: Account<'info, PublicInfo>,
    #[account()]
    pub auth_driver: Signer<'info>,
    #[account(mut)]
    pub authority: Signer<'info>,
    /// CHECK: This is not dangerous
    #[account()]
    pub program_data: AccountInfo<'info>,
    /// CHECK: This is not dangerous
    #[account(address = crate::id())]
    pub program: AccountInfo<'info>,
    /// NATIVE RENT SYSVAR
    pub rent_sysvar: Sysvar<'info, Rent>,
    /// The system program.
    pub system_program: Program<'info, System>,
}

/// Structure representing the arguments for setting the authentication driver in the project.
///
/// # Fields
///
/// - `driver_offchain`: The auth off-chain URL.
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug, PartialEq)]
pub struct SetAuthDriverArgs {
    pub driver_offchain: String,
}

pub fn set_auth_driver(
    ctx: Context<SetAuthDriver>,
    _env: String,
    args: SetAuthDriverArgs,
) -> Result<()> {
    let public_info = &mut ctx.accounts.public_info;

    insert_public_info(
        "auth_driver_offchain".to_string(),
        args.driver_offchain,
        public_info,
        &ctx.accounts.authority,
        &ctx.accounts.rent_sysvar,
        &ctx.accounts.system_program,
    )?;

    insert_public_info(
        "auth_driver_key".to_string(),
        ctx.accounts.auth_driver.key().to_string(),
        public_info,
        &ctx.accounts.authority,
        &ctx.accounts.rent_sysvar,
        &ctx.accounts.system_program,
    )?;

    Ok(())
}