Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- program: post only respects reduce only ([#1878](https://github.com/drift-labs/protocol-v2/pull/1878))
- program: add sequence id to exchange/mm oracle ([#1834](https://github.com/drift-labs/protocol-v2/pull/1834))
- program: perp position max margin ratio ([#1847](https://github.com/drift-labs/protocol-v2/pull/1847))
- program: add padding to swift messages ([#1845](https://github.com/drift-labs/protocol-v2/pull/1845))
- program: rm lp ([#1755](https://github.com/drift-labs/protocol-v2/pull/1755))

### Fixes
Expand Down
4 changes: 0 additions & 4 deletions programs/drift/src/instructions/keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,10 +760,6 @@ pub fn place_signed_msg_taker_order<'c: 'info, 'info>(
return Ok(());
}

if let Some(max_margin_ratio) = verified_message_and_signature.max_margin_ratio {
taker.update_perp_position_max_margin_ratio(market_index, max_margin_ratio)?;
}

// Dont place order if signed msg order already exists
let mut taker_order_id_to_use = taker.next_order_id;
let mut signed_msg_order_id =
Expand Down
2 changes: 0 additions & 2 deletions programs/drift/src/state/order_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,6 @@ pub struct SignedMsgOrderParamsMessage {
pub uuid: [u8; 8],
pub take_profit_order_params: Option<SignedMsgTriggerOrderParams>,
pub stop_loss_order_params: Option<SignedMsgTriggerOrderParams>,
pub max_margin_ratio: Option<u16>,
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone, Default, Eq, PartialEq, Debug)]
Expand All @@ -883,7 +882,6 @@ pub struct SignedMsgOrderParamsDelegateMessage {
pub uuid: [u8; 8],
pub take_profit_order_params: Option<SignedMsgTriggerOrderParams>,
pub stop_loss_order_params: Option<SignedMsgTriggerOrderParams>,
pub max_margin_ratio: Option<u16>,
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone, Default, Eq, PartialEq, Debug)]
Expand Down
108 changes: 39 additions & 69 deletions programs/drift/src/validation/sig_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ use solana_program::program_memory::sol_memcmp;
use solana_program::sysvar;
use std::convert::TryInto;

#[cfg(test)]
mod tests;

const ED25519_PROGRAM_INPUT_HEADER_LEN: usize = 2;

const SIGNATURE_LEN: u16 = 64;
Expand Down Expand Up @@ -48,7 +45,6 @@ pub struct Ed25519SignatureOffsets {
pub message_instruction_index: u16,
}

#[derive(Debug)]
pub struct VerifiedMessage {
pub signed_msg_order_params: OrderParams,
pub sub_account_id: Option<u16>,
Expand All @@ -57,77 +53,13 @@ pub struct VerifiedMessage {
pub uuid: [u8; 8],
pub take_profit_order_params: Option<SignedMsgTriggerOrderParams>,
pub stop_loss_order_params: Option<SignedMsgTriggerOrderParams>,
pub max_margin_ratio: Option<u16>,
pub signature: [u8; 64],
}

fn slice_eq(a: &[u8], b: &[u8]) -> bool {
a.len() == b.len() && sol_memcmp(a, b, a.len()) == 0
}

pub fn deserialize_into_verified_message(
payload: Vec<u8>,
signature: &[u8; 64],
is_delegate_signer: bool,
) -> Result<VerifiedMessage> {
if is_delegate_signer {
if payload.len() < 8 {
return Err(SignatureVerificationError::InvalidMessageDataSize.into());
}
let min_len: usize = std::mem::size_of::<SignedMsgOrderParamsDelegateMessage>();
let mut owned = payload;
if owned.len() < min_len {
owned.resize(min_len, 0);
}
let deserialized = SignedMsgOrderParamsDelegateMessage::deserialize(
&mut &owned[8..], // 8 byte manual discriminator
)
.map_err(|_| {
msg!("Invalid message encoding for is_delegate_signer = true");
SignatureVerificationError::InvalidMessageDataSize
})?;

return Ok(VerifiedMessage {
signed_msg_order_params: deserialized.signed_msg_order_params,
sub_account_id: None,
delegate_signed_taker_pubkey: Some(deserialized.taker_pubkey),
slot: deserialized.slot,
uuid: deserialized.uuid,
take_profit_order_params: deserialized.take_profit_order_params,
stop_loss_order_params: deserialized.stop_loss_order_params,
max_margin_ratio: deserialized.max_margin_ratio,
signature: *signature,
});
} else {
if payload.len() < 8 {
return Err(SignatureVerificationError::InvalidMessageDataSize.into());
}
let min_len: usize = std::mem::size_of::<SignedMsgOrderParamsMessage>();
let mut owned = payload;
if owned.len() < min_len {
owned.resize(min_len, 0);
}
let deserialized = SignedMsgOrderParamsMessage::deserialize(
&mut &owned[8..], // 8 byte manual discriminator
)
.map_err(|_| {
msg!("Invalid delegate message encoding for with is_delegate_signer = false");
SignatureVerificationError::InvalidMessageDataSize
})?;
return Ok(VerifiedMessage {
signed_msg_order_params: deserialized.signed_msg_order_params,
sub_account_id: Some(deserialized.sub_account_id),
delegate_signed_taker_pubkey: None,
slot: deserialized.slot,
uuid: deserialized.uuid,
take_profit_order_params: deserialized.take_profit_order_params,
stop_loss_order_params: deserialized.stop_loss_order_params,
max_margin_ratio: deserialized.max_margin_ratio,
signature: *signature,
});
}
}

/// Check Ed25519Program instruction data verifies the given msg
///
/// `ix` an Ed25519Program instruction [see](https://github.com/solana-labs/solana/blob/master/sdk/src/ed25519_instruction.rs))
Expand Down Expand Up @@ -300,7 +232,45 @@ pub fn verify_and_decode_ed25519_msg(
let payload =
hex::decode(payload).map_err(|_| SignatureVerificationError::InvalidMessageHex)?;

deserialize_into_verified_message(payload, signature, is_delegate_signer)
if is_delegate_signer {
let deserialized = SignedMsgOrderParamsDelegateMessage::deserialize(
&mut &payload[8..], // 8 byte manual discriminator
)
.map_err(|_| {
msg!("Invalid message encoding for is_delegate_signer = true");
SignatureVerificationError::InvalidMessageDataSize
})?;

return Ok(VerifiedMessage {
signed_msg_order_params: deserialized.signed_msg_order_params,
sub_account_id: None,
delegate_signed_taker_pubkey: Some(deserialized.taker_pubkey),
slot: deserialized.slot,
uuid: deserialized.uuid,
take_profit_order_params: deserialized.take_profit_order_params,
stop_loss_order_params: deserialized.stop_loss_order_params,
signature: *signature,
});
} else {
let deserialized = SignedMsgOrderParamsMessage::deserialize(
&mut &payload[8..], // 8 byte manual discriminator
)
.map_err(|_| {
msg!("Invalid delegate message encoding for with is_delegate_signer = false");
SignatureVerificationError::InvalidMessageDataSize
})?;

return Ok(VerifiedMessage {
signed_msg_order_params: deserialized.signed_msg_order_params,
sub_account_id: Some(deserialized.sub_account_id),
delegate_signed_taker_pubkey: None,
slot: deserialized.slot,
uuid: deserialized.uuid,
take_profit_order_params: deserialized.take_profit_order_params,
stop_loss_order_params: deserialized.stop_loss_order_params,
signature: *signature,
});
}
}

#[error_code]
Expand Down
Loading
Loading