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
use crate::pda::PDA;

use anchor_lang::prelude::*;

use std::convert::TryFrom;

pub const SEED_AUTHORITY: &[u8] = b"authority";

/**
 * Authority
 */

#[account]
#[derive(Debug)]
pub struct Authority {
    pub bump: u8,
}

impl Authority {
    pub fn pda() -> PDA {
        Pubkey::find_program_address(&[SEED_AUTHORITY], &crate::ID)
    }
}

impl TryFrom<Vec<u8>> for Authority {
    type Error = Error;
    fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
        Authority::try_deserialize(&mut data.as_slice())
    }
}

/**
 * AuthorityAccount
 */

pub trait AuthorityAccount {
    fn init(&mut self, bump: u8) -> Result<()>;
}

impl AuthorityAccount for Account<'_, Authority> {
    fn init(&mut self, bump: u8) -> Result<()> {
        self.bump = bump;
        Ok(())
    }
}