-
Notifications
You must be signed in to change notification settings - Fork 106
/
signs.rs
331 lines (289 loc) · 11.4 KB
/
signs.rs
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#![allow(missing_docs)]
use super::*;
use crate::{
error::{to_audius_program_error, AudiusProgramError},
processor::SENDER_SEED_PREFIX,
state::{SenderAccount, VoteMessage},
};
use borsh::BorshDeserialize;
use borsh::BorshSerialize;
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, instruction::Instruction,
program_error::ProgramError, program_pack::IsInitialized, pubkey::Pubkey, secp256k1_program,
sysvar,
};
use std::{
collections::{BTreeMap, BTreeSet},
convert::TryInto,
};
/// Attempts to retrieve all instructions before `index_current_instruction`
/// as secp recovery instructions, and asserts that it finds `necessary_instructions_count`
/// instructions.
/// Returns a vec of type (instruction, instruction_index)
pub fn get_secp_instructions(
index_current_instruction: u16,
necessary_instructions_count: usize,
instruction_info: &AccountInfo,
) -> Result<Vec<(Instruction, u16)>, AudiusProgramError> {
let mut secp_instructions: Vec<(Instruction, u16)> = Vec::new();
for ind in 0..index_current_instruction {
let instruction = sysvar::instructions::load_instruction_at(
ind as usize,
&instruction_info.data.borrow(),
)
.map_err(to_audius_program_error)?;
if instruction.program_id == secp256k1_program::id() {
secp_instructions.push((instruction, ind));
}
}
if secp_instructions.len() != necessary_instructions_count {
return Err(AudiusProgramError::Secp256InstructionMissing);
}
Ok(secp_instructions)
}
/// Returns the eth_addresses and operators associated with `signers`.
///
/// Ensures that there are no duplicate signers, that each signer
/// is owned by the program, and that each signer account can be derived
/// from it's known eth address.
pub fn get_and_verify_signer_metadata<'a>(
program_id: &Pubkey,
reward_manager_key: &Pubkey,
signers: Vec<&AccountInfo<'a>>,
) -> Result<(Vec<EthereumAddress>, BTreeSet<EthereumAddress>), ProgramError> {
let mut senders_eth_addresses: Vec<EthereumAddress> = Vec::new();
let mut operators = BTreeSet::<EthereumAddress>::new();
for signer in signers {
let signer_data = SenderAccount::try_from_slice(&signer.data.borrow())?;
if !signer_data.is_initialized() {
return Err(ProgramError::UninitializedAccount);
}
assert_owned_by(signer, program_id)?;
// Derive the Solana address corresponding to the
// signer's eth address
let (_, derived_signer, _) = find_derived_pair(
program_id,
reward_manager_key,
[
SENDER_SEED_PREFIX.as_ref(),
signer_data.eth_address.as_ref(),
]
.concat()
.as_ref(),
);
if derived_signer != *signer.key {
return Err(ProgramError::InvalidSeeds);
}
if senders_eth_addresses.contains(&signer_data.eth_address) {
return Err(AudiusProgramError::RepeatedSenders.into());
}
if !operators.insert(signer_data.operator) {
return Err(AudiusProgramError::OperatorCollision.into());
}
senders_eth_addresses.push(signer_data.eth_address);
}
Ok((senders_eth_addresses, operators))
}
/// Retrieves an eth signer from an secp instruction
pub fn get_signer_from_secp_instruction(secp_instruction_data: Vec<u8>) -> EthereumAddress {
let eth_address_offset = 12;
let instruction_signer =
secp_instruction_data[eth_address_offset..eth_address_offset + 20].to_vec();
let instruction_signer: EthereumAddress = instruction_signer.as_slice().try_into().unwrap();
instruction_signer
}
/// Secp256k1 signature offsets data
#[derive(Clone, Copy, Debug, Default, PartialEq, BorshDeserialize, BorshSerialize)]
pub struct SecpSignatureOffsets {
/// Offset of 64+1 bytes
pub signature_offset: u16,
/// Index of signature instruction in buffer
pub signature_instruction_index: u8,
/// Offset to eth_address of 20 bytes
pub eth_address_offset: u16,
/// Index of eth address instruction in buffer
pub eth_address_instruction_index: u8,
/// Offset to start of message data
pub message_data_offset: u16,
/// Size of message data
pub message_data_size: u16,
/// Index on message instruction in buffer
pub message_instruction_index: u8,
}
pub const SIGNATURE_OFFSETS_SERIALIZED_SIZE: usize = 11;
pub const DATA_START: usize = SIGNATURE_OFFSETS_SERIALIZED_SIZE + 1;
/// Validates the secp offsets struct is as expected -
/// the *_index fields must point to the `instruction_index`,
/// the *_offset fields must be known sizes,
/// and the message len must match the `expected_message_size`.
pub fn validate_secp_offsets(
secp_instruction_data: Vec<u8>,
instruction_index: u8,
) -> ProgramResult {
// First, ensure there is just a single offsets struct included
if secp_instruction_data[0] != 1 {
return Err(AudiusProgramError::SignatureVerificationFailed.into());
}
let start = 1;
let end = start + SIGNATURE_OFFSETS_SERIALIZED_SIZE;
let offsets = SecpSignatureOffsets::try_from_slice(&secp_instruction_data[start..end]).unwrap();
// Ensure indices match current instruction
let indices_match = offsets.signature_instruction_index == instruction_index
&& offsets.eth_address_instruction_index == instruction_index
&& offsets.message_instruction_index == instruction_index;
if !indices_match {
return Err(AudiusProgramError::SignatureVerificationFailed.into());
}
// Ensure offsets match expected values
// eth_address_offset = DATA_START (12)
if offsets.eth_address_offset != DATA_START as u16 {
return Err(AudiusProgramError::SignatureVerificationFailed.into());
}
// signature_offset = DATA_START + eth_pubkey.len (20) = 32
if offsets.signature_offset != 32 {
return Err(AudiusProgramError::SignatureVerificationFailed.into());
}
// message_data_offset = signature_offset + signature_arr.len (65) = 97
if offsets.message_data_offset != 97 {
return Err(AudiusProgramError::SignatureVerificationFailed.into());
}
Ok(())
}
// meta (12) + address (20) + signature (65) = 97
const MESSAGE_DATA_OFFSET: usize = 97;
/// Assert that the message contained in `secp_instruction_data`
/// matches `expected_message`.
pub fn check_message_from_secp_instruction(
secp_instruction_data: Vec<u8>,
expected_message: &[u8],
) -> Result<(), ProgramError> {
let message = secp_instruction_data[MESSAGE_DATA_OFFSET..].to_vec();
if message != *expected_message {
Err(AudiusProgramError::SignatureVerificationFailed.into())
} else {
Ok(())
}
}
/// Attempts to parse out a `vote_message` from `secp_instruction_data`.
pub fn get_vote_message_from_secp_instruction(
secp_instruction_data: Vec<u8>,
) -> Result<VoteMessage, ProgramError> {
let mut message = secp_instruction_data[MESSAGE_DATA_OFFSET..].to_vec();
while message.len() < 128 {
message.push(0);
}
message
.as_slice()
.try_into()
.map_err(|_| AudiusProgramError::SignatureVerificationFailed.into())
}
fn vec_into_checkmap(vec: &[EthereumAddress]) -> BTreeMap<EthereumAddress, bool> {
let mut map = BTreeMap::new();
for item in vec {
map.insert(*item, false);
}
map
}
/// Assert that `eth_signer` is in check_map, but hasn't previously
/// been checked.
fn check_signer(
checkmap: &mut BTreeMap<EthereumAddress, bool>,
eth_signer: &EthereumAddress,
) -> ProgramResult {
if let Some(val) = checkmap.get_mut(eth_signer) {
if !*val {
*val = true;
} else {
return Err(AudiusProgramError::SignCollision.into());
}
} else {
return Err(AudiusProgramError::WrongSigner.into());
}
Ok(())
}
/// Validates secp instructions for add or delete sender instructions.
pub fn validate_secp_add_delete_sender(
program_id: &Pubkey,
reward_manager: &Pubkey,
instruction_info: &AccountInfo,
expected_signers: Vec<&AccountInfo>,
extraction_depth: usize,
new_sender: EthereumAddress,
message_prefix: &str,
) -> ProgramResult {
// Ensure that `instructions_info` is indeed the instructions sysvar
if !sysvar::instructions::check_id(&instruction_info.key) {
return Err(AudiusProgramError::InstructionLoadError.into());
}
let index = sysvar::instructions::load_current_index(&instruction_info.data.borrow());
// Instruction can't be first in transaction
// because must follow after `new_secp256k1_instruction`
if index == 0 {
return Err(AudiusProgramError::Secp256InstructionMissing.into());
}
// Load previous instructions
let secp_instructions = get_secp_instructions(index, extraction_depth, instruction_info)?;
// Get the eth addresses associated with our expected_signers
let (senders_eth_addresses, _) =
get_and_verify_signer_metadata(program_id, reward_manager, expected_signers)?;
let mut checkmap = vec_into_checkmap(&senders_eth_addresses);
let expected_message = [
message_prefix.as_ref(),
reward_manager.as_ref(),
new_sender.as_ref(),
]
.concat();
// For each secp instruction, assert that the signer was expected and not duplicated
// and that the message is formatted correctly.
for (secp_instruction, index) in secp_instructions {
let eth_signer = get_signer_from_secp_instruction(secp_instruction.data.clone());
check_signer(&mut checkmap, ð_signer)?;
validate_secp_offsets(
secp_instruction.data.clone(),
index as u8,
)?;
check_message_from_secp_instruction(
secp_instruction.data.clone(),
expected_message.as_ref(),
)?;
}
Ok(())
}
/// Checks secp instruction for submit_attestation:
/// ensures the message is signed by `expected_signer`, and
/// returns the message.
pub fn validate_secp_submit_attestation(
instruction_info: &AccountInfo,
expected_signer: &EthereumAddress,
) -> Result<VoteMessage, ProgramError> {
// Ensure that `instructions_info` is indeed the instructions sysvar
if !sysvar::instructions::check_id(&instruction_info.key) {
return Err(AudiusProgramError::InstructionLoadError.into());
}
let index = sysvar::instructions::load_current_index(&instruction_info.data.borrow());
// Instruction can't be first in transaction
// because must follow after `new_secp256k1_instruction`
if index == 0 {
return Err(AudiusProgramError::Secp256InstructionMissing.into());
}
let secp_index = index - 1;
// Load previous instruction
let secp_instruction = sysvar::instructions::load_instruction_at(
secp_index as usize,
&instruction_info.data.borrow(),
)
.map_err(to_audius_program_error)?;
// Check that instruction is `new_secp256k1_instruction`
if secp_instruction.program_id != secp256k1_program::id() {
return Err(AudiusProgramError::Secp256InstructionMissing.into());
}
validate_secp_offsets(
secp_instruction.data.clone(),
secp_index.try_into().unwrap(),
)?;
let eth_signer = get_signer_from_secp_instruction(secp_instruction.data.clone());
if eth_signer != *expected_signer {
return Err(AudiusProgramError::WrongSigner.into());
}
get_vote_message_from_secp_instruction(secp_instruction.data)
}