Skip to content

Commit c122c3f

Browse files
Akash ThotaAkash Thota
authored andcommitted
chore(feat): requseted changes for seperate current index and ix params functions
1 parent 7fcb8fd commit c122c3f

File tree

4 files changed

+85
-12
lines changed

4 files changed

+85
-12
lines changed

lang/src/signature_verification/ed25519.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,25 @@ use crate::prelude::*;
33
use crate::solana_program::instruction::Instruction;
44
use solana_sdk_ids::ed25519_program;
55

6+
/// Verifies an Ed25519 signature instruction assuming the signature, public key,
7+
/// and message bytes are embedded directly inside the instruction data (Solana's
8+
/// default encoding). Prefer [`verify_ed25519_ix_with_instruction_index`] when
9+
/// working with custom instructions that point at external instruction data.
610
pub fn verify_ed25519_ix(
711
ix: &Instruction,
812
pubkey: &[u8; 32],
913
msg: &[u8],
1014
sig: &[u8; 64],
15+
) -> Result<()> {
16+
verify_ed25519_ix_with_instruction_index(ix, u16::MAX, pubkey, msg, sig)
17+
}
18+
19+
pub fn verify_ed25519_ix_with_instruction_index(
20+
ix: &Instruction,
21+
instruction_index: u16,
22+
pubkey: &[u8; 32],
23+
msg: &[u8],
24+
sig: &[u8; 64],
1125
) -> Result<()> {
1226
require_keys_eq!(
1327
ix.program_id,
@@ -31,12 +45,12 @@ pub fn verify_ed25519_ix(
3145
expected.push(1u8); // num signatures
3246
expected.push(0u8); // padding
3347
expected.extend_from_slice(&sig_offset.to_le_bytes());
34-
expected.extend_from_slice(&(u16::MAX).to_le_bytes());
48+
expected.extend_from_slice(&instruction_index.to_le_bytes());
3549
expected.extend_from_slice(&pubkey_offset.to_le_bytes());
36-
expected.extend_from_slice(&(u16::MAX).to_le_bytes());
50+
expected.extend_from_slice(&instruction_index.to_le_bytes());
3751
expected.extend_from_slice(&msg_offset.to_le_bytes());
3852
expected.extend_from_slice(&msg_len.to_le_bytes());
39-
expected.extend_from_slice(&(u16::MAX).to_le_bytes());
53+
expected.extend_from_slice(&instruction_index.to_le_bytes());
4054

4155
expected.extend_from_slice(sig);
4256
expected.extend_from_slice(pubkey);
Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,50 @@
11
use crate::prelude::*;
22
use crate::solana_program::instruction::Instruction;
3-
use crate::solana_program::sysvar::instructions::load_instruction_at_checked;
3+
use crate::solana_program::sysvar::instructions::{
4+
load_current_index_checked, load_instruction_at_checked,
5+
};
6+
use core::convert::TryFrom;
47

58
mod ed25519;
69
mod secp256k1;
710

8-
pub use ed25519::verify_ed25519_ix;
9-
pub use secp256k1::verify_secp256k1_ix;
11+
pub use ed25519::{verify_ed25519_ix, verify_ed25519_ix_with_instruction_index};
12+
pub use secp256k1::{verify_secp256k1_ix, verify_secp256k1_ix_with_instruction_index};
1013

1114
/// Load an instruction from the Instructions sysvar at the given index.
1215
pub fn load_instruction(index: usize, ix_sysvar: &AccountInfo<'_>) -> Result<Instruction> {
1316
let ix = load_instruction_at_checked(index, ix_sysvar)
1417
.map_err(|_| error!(error::ErrorCode::ConstraintRaw))?;
1518
Ok(ix)
1619
}
20+
21+
/// Loads the instruction currently executing in this transaction and verifies it
22+
/// as an Ed25519 signature instruction.
23+
pub fn verify_current_ed25519_instruction(
24+
ix_sysvar: &AccountInfo<'_>,
25+
pubkey: &[u8; 32],
26+
msg: &[u8],
27+
sig: &[u8; 64],
28+
) -> Result<()> {
29+
let idx = load_current_index_checked(ix_sysvar)
30+
.map_err(|_| error!(error::ErrorCode::ConstraintRaw))?;
31+
let ix = load_instruction(idx as usize, ix_sysvar)?;
32+
verify_ed25519_ix_with_instruction_index(&ix, idx, pubkey, msg, sig)
33+
}
34+
35+
/// Loads the instruction currently executing in this transaction and verifies it
36+
/// as a Secp256k1 signature instruction.
37+
pub fn verify_current_secp256k1_instruction(
38+
ix_sysvar: &AccountInfo<'_>,
39+
eth_address: &[u8; 20],
40+
msg: &[u8],
41+
sig: &[u8; 64],
42+
recovery_id: u8,
43+
) -> Result<()> {
44+
let idx_u16 = load_current_index_checked(ix_sysvar)
45+
.map_err(|_| error!(error::ErrorCode::ConstraintRaw))?;
46+
let idx_u8 =
47+
u8::try_from(idx_u16).map_err(|_| error!(error::ErrorCode::InvalidNumericConversion))?;
48+
let ix = load_instruction(idx_u16 as usize, ix_sysvar)?;
49+
verify_secp256k1_ix_with_instruction_index(&ix, idx_u8, eth_address, msg, sig, recovery_id)
50+
}

lang/src/signature_verification/secp256k1.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,28 @@ use crate::prelude::*;
33
use crate::solana_program::instruction::Instruction;
44
use solana_sdk_ids::secp256k1_program;
55

6+
/// Verifies a Secp256k1 instruction created under the assumption that the
7+
/// signature, address, and message bytes all live inside the same instruction
8+
/// (i.e. the signature ix is placed at index `0`). Prefer
9+
/// [`verify_secp256k1_ix_with_instruction_index`] and pass the actual signature
10+
/// instruction index instead of relying on this default.
611
pub fn verify_secp256k1_ix(
712
ix: &Instruction,
813
eth_address: &[u8; 20],
914
msg: &[u8],
1015
sig: &[u8; 64],
1116
recovery_id: u8,
17+
) -> Result<()> {
18+
verify_secp256k1_ix_with_instruction_index(ix, 0, eth_address, msg, sig, recovery_id)
19+
}
20+
21+
pub fn verify_secp256k1_ix_with_instruction_index(
22+
ix: &Instruction,
23+
instruction_index: u8,
24+
eth_address: &[u8; 20],
25+
msg: &[u8],
26+
sig: &[u8; 64],
27+
recovery_id: u8,
1228
) -> Result<()> {
1329
require_keys_eq!(
1430
ix.program_id,
@@ -33,12 +49,12 @@ pub fn verify_secp256k1_ix(
3349

3450
expected.push(1u8); // num signatures
3551
expected.extend_from_slice(&sig_offset.to_le_bytes());
36-
expected.push(0u8); // sig ix idx
52+
expected.push(instruction_index); // sig ix idx
3753
expected.extend_from_slice(&eth_offset.to_le_bytes());
38-
expected.push(0u8); // eth ix idx
54+
expected.push(instruction_index); // eth ix idx
3955
expected.extend_from_slice(&msg_offset.to_le_bytes());
4056
expected.extend_from_slice(&msg_len.to_le_bytes());
41-
expected.push(0u8); // msg ix idx
57+
expected.push(instruction_index); // msg ix idx
4258

4359
expected.extend_from_slice(eth_address);
4460
expected.extend_from_slice(sig);

tests/signature-verification/programs/signature-verification-test/src/lib.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use anchor_lang::prelude::*;
22
use anchor_lang::signature_verification::{
3-
load_instruction, verify_ed25519_ix, verify_secp256k1_ix,
3+
load_instruction, verify_ed25519_ix_with_instruction_index,
4+
verify_secp256k1_ix_with_instruction_index,
45
};
56

67
declare_id!("9P8zSbNRQkwDrjCmqsHHcU1GTk5npaKYgKHroAkupbLG");
@@ -15,8 +16,9 @@ pub mod signature_verification_test {
1516
signature: [u8; 64],
1617
) -> Result<()> {
1718
let ix = load_instruction(0, &ctx.accounts.ix_sysvar)?;
18-
verify_ed25519_ix(
19+
verify_ed25519_ix_with_instruction_index(
1920
&ix,
21+
u16::MAX,
2022
&ctx.accounts.signer.key().to_bytes(),
2123
&message,
2224
&signature,
@@ -34,7 +36,14 @@ pub mod signature_verification_test {
3436
eth_address: [u8; 20],
3537
) -> Result<()> {
3638
let ix = load_instruction(0, &ctx.accounts.ix_sysvar)?;
37-
verify_secp256k1_ix(&ix, &eth_address, &message, &signature, recovery_id)?;
39+
verify_secp256k1_ix_with_instruction_index(
40+
&ix,
41+
0,
42+
&eth_address,
43+
&message,
44+
&signature,
45+
recovery_id,
46+
)?;
3847

3948
msg!("Secp256k1 signature verified successfully using custom helper!");
4049

0 commit comments

Comments
 (0)