Skip to content

Commit 7f2cabe

Browse files
committed
fix(lazer): allow caller to verify instruction index and message offset
1 parent 41e7906 commit 7f2cabe

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

lazer/contracts/solana/programs/pyth-lazer-solana-contract/src/lib.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
mod signature;
22

33
use {
4-
crate::signature::VerifiedMessage,
54
anchor_lang::{
65
prelude::*, solana_program::pubkey::PUBKEY_BYTES, system_program, Discriminator,
76
},
@@ -189,11 +188,17 @@ pub mod pyth_lazer_solana_contract {
189188
/// - `ed25519_instruction_index` is the index of the `ed25519_program` instruction
190189
/// within the transaction. This instruction must precede the current instruction.
191190
/// - `signature_index` is the index of the signature within the inputs to the `ed25519_program`.
191+
/// - `message_instruction_index` is the index of the instruction that receives the message data
192+
/// within the transaction.
193+
/// - `message_offset` is the offset of the signed message within the
194+
/// input data for that instruction.
192195
pub fn verify_message(
193196
ctx: Context<VerifyMessage>,
194197
ed25519_instruction_index: u16,
195198
signature_index: u8,
196-
) -> Result<VerifiedMessage> {
199+
message_instruction_index: u16,
200+
message_offset: u16,
201+
) -> Result<()> {
197202
system_program::transfer(
198203
CpiContext::new(
199204
ctx.accounts.system_program.to_account_info(),
@@ -210,11 +215,15 @@ pub mod pyth_lazer_solana_contract {
210215
&ctx.accounts.instructions_sysvar,
211216
ed25519_instruction_index,
212217
signature_index,
218+
message_instruction_index,
219+
message_offset,
213220
)
214221
.map_err(|err| {
215222
msg!("signature verification error: {:?}", err);
216-
err.into()
217-
})
223+
err
224+
})?;
225+
226+
Ok(())
218227
}
219228
}
220229

lazer/contracts/solana/programs/pyth-lazer-solana-contract/src/signature.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,17 @@ impl From<SignatureVerificationError> for anchor_lang::error::Error {
154154
/// - `ed25519_instruction_index` is the index of the `ed25519_program` instruction
155155
/// within the transaction. This instruction must precede the current instruction.
156156
/// - `signature_index` is the index of the signature within the inputs to the `ed25519_program`.
157+
/// - `message_instruction_index` is the index of the instruction that receives the message data
158+
/// within the transaction.
159+
/// - `message_offset` is the offset of the signed message within the
160+
/// input data for that instruction.
157161
pub fn verify_message(
158162
storage: &Storage,
159163
instructions_sysvar: &AccountInfo,
160164
ed25519_instruction_index: u16,
161165
signature_index: u8,
166+
message_instruction_index: u16,
167+
message_offset: u16,
162168
) -> Result<VerifiedMessage, SignatureVerificationError> {
163169
const SOLANA_FORMAT_MAGIC_LE: u32 = 2182742457;
164170

@@ -200,6 +206,10 @@ pub fn verify_message(
200206
}
201207
let offsets = &args[usize::from(signature_index)];
202208

209+
if offsets.message_instruction_index != message_instruction_index {
210+
return Err(SignatureVerificationError::InvalidInstructionIndex);
211+
}
212+
203213
if offsets.signature_instruction_index != offsets.message_instruction_index
204214
|| offsets.public_key_instruction_index != offsets.message_instruction_index
205215
{
@@ -215,10 +225,15 @@ pub fn verify_message(
215225
)
216226
.map_err(SignatureVerificationError::LoadInstructionAtFailed)?;
217227

218-
let message_offset = offsets
228+
let derived_message_offset = offsets
219229
.signature_offset
220230
.checked_sub(MAGIC_LEN)
221231
.ok_or(SignatureVerificationError::MessageOffsetOverflow)?;
232+
233+
if message_offset != derived_message_offset {
234+
return Err(SignatureVerificationError::InvalidMessageOffset);
235+
}
236+
222237
let message_end_offset = offsets
223238
.message_data_offset
224239
.checked_add(offsets.message_data_size)

lazer/contracts/solana/programs/pyth-lazer-solana-contract/tests/test1.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ impl Setup {
148148
&pyth_lazer_solana_contract::instruction::VerifyMessage {
149149
ed25519_instruction_index: 0,
150150
signature_index: 0,
151+
message_instruction_index: instruction_index,
152+
message_offset,
151153
}
152154
.data(),
153155
vec![

0 commit comments

Comments
 (0)