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
use borsh::maybestd::io::Write;
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use solana_program::borsh::try_from_slice_unchecked;
use solana_program::{
account_info::AccountInfo, program_error::ProgramError, program_pack::IsInitialized,
pubkey::Pubkey,
};
use spl_governance_tools::account::{get_account_data, AccountMaxSize};
use crate::{error::GovernanceError, PROGRAM_AUTHORITY_SEED};
use crate::state::enums::GovernanceAccountType;
use super::legacy::SignatoryRecordV1;
#[repr(C)]
#[derive(Clone, Debug, PartialEq, BorshDeserialize, BorshSerialize, BorshSchema)]
pub struct SignatoryRecordV2 {
pub account_type: GovernanceAccountType,
pub proposal: Pubkey,
pub signatory: Pubkey,
pub signed_off: bool,
pub reserved_v2: [u8; 8],
}
impl AccountMaxSize for SignatoryRecordV2 {}
impl IsInitialized for SignatoryRecordV2 {
fn is_initialized(&self) -> bool {
self.account_type == GovernanceAccountType::SignatoryRecordV2
}
}
impl SignatoryRecordV2 {
pub fn assert_can_sign_off(&self, signatory_info: &AccountInfo) -> Result<(), ProgramError> {
if self.signed_off {
return Err(GovernanceError::SignatoryAlreadySignedOff.into());
}
if !signatory_info.is_signer {
return Err(GovernanceError::SignatoryMustSign.into());
}
Ok(())
}
pub fn assert_can_remove_signatory(&self) -> Result<(), ProgramError> {
if self.signed_off {
return Err(GovernanceError::SignatoryAlreadySignedOff.into());
}
Ok(())
}
pub fn serialize<W: Write>(self, writer: &mut W) -> Result<(), ProgramError> {
if self.account_type == GovernanceAccountType::SignatoryRecordV2 {
BorshSerialize::serialize(&self, writer)?
} else if self.account_type == GovernanceAccountType::SignatoryRecordV1 {
if self.reserved_v2 != [0; 8] {
panic!("Extended data not supported by SignatoryRecordV1")
}
let signatory_record_data_v1 = SignatoryRecordV1 {
account_type: self.account_type,
proposal: self.proposal,
signatory: self.signatory,
signed_off: self.signed_off,
};
BorshSerialize::serialize(&signatory_record_data_v1, writer)?;
}
Ok(())
}
}
pub fn get_signatory_record_address_seeds<'a>(
proposal: &'a Pubkey,
signatory: &'a Pubkey,
) -> [&'a [u8]; 3] {
[
PROGRAM_AUTHORITY_SEED,
proposal.as_ref(),
signatory.as_ref(),
]
}
pub fn get_signatory_record_address<'a>(
program_id: &Pubkey,
proposal: &'a Pubkey,
signatory: &'a Pubkey,
) -> Pubkey {
Pubkey::find_program_address(
&get_signatory_record_address_seeds(proposal, signatory),
program_id,
)
.0
}
pub fn get_signatory_record_data(
program_id: &Pubkey,
signatory_record_info: &AccountInfo,
) -> Result<SignatoryRecordV2, ProgramError> {
let account_type: GovernanceAccountType =
try_from_slice_unchecked(&signatory_record_info.data.borrow())?;
if account_type == GovernanceAccountType::SignatoryRecordV1 {
let signatory_record_data_v1 =
get_account_data::<SignatoryRecordV1>(program_id, signatory_record_info)?;
return Ok(SignatoryRecordV2 {
account_type,
proposal: signatory_record_data_v1.proposal,
signatory: signatory_record_data_v1.signatory,
signed_off: signatory_record_data_v1.signed_off,
reserved_v2: [0; 8],
});
}
get_account_data::<SignatoryRecordV2>(program_id, signatory_record_info)
}
pub fn get_signatory_record_data_for_seeds(
program_id: &Pubkey,
signatory_record_info: &AccountInfo,
proposal: &Pubkey,
signatory: &Pubkey,
) -> Result<SignatoryRecordV2, ProgramError> {
let (signatory_record_address, _) = Pubkey::find_program_address(
&get_signatory_record_address_seeds(proposal, signatory),
program_id,
);
if signatory_record_address != *signatory_record_info.key {
return Err(GovernanceError::InvalidSignatoryAddress.into());
}
get_signatory_record_data(program_id, signatory_record_info)
}