Skip to content

Commit

Permalink
Merge pull request #1416 from drift-labs/feat/swift-msg-hex
Browse files Browse the repository at this point in the history
Feat/swift msg hex
  • Loading branch information
LukasDeco authored Jan 8, 2025
2 parents c43913c + 1b08618 commit 7894238
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
11 changes: 7 additions & 4 deletions programs/drift/src/validation/sig_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ fn check_ed25519_data(data: &[u8], pubkey: &[u8], msg: &[u8], sig: &[u8]) -> Res
// https://github.com/solana-labs/solana-web3.js/blob/master/src/ed25519-program.ts#L33

// "Deserializing" byte slices

let num_signatures = &[data[0]]; // Byte 0
let padding = &[data[1]]; // Byte 1
let signature_offset = &data[2..=3]; // Bytes 2,3
Expand All @@ -84,7 +83,6 @@ fn check_ed25519_data(data: &[u8], pubkey: &[u8], msg: &[u8], sig: &[u8]) -> Res
let data_msg = &data[112..]; // Bytes 112..end

// Expected values

let exp_public_key_offset: u16 = 16; // 2*u8 + 7*u16
let exp_signature_offset: u16 = exp_public_key_offset + pubkey.len() as u16;
let exp_message_data_offset: u16 = exp_signature_offset + sig.len() as u16;
Expand Down Expand Up @@ -224,7 +222,7 @@ pub fn verify_ed25519_msg(
.ok_or(SignatureVerificationError::MessageOffsetOverflow)?;
&msg[start..end]
};
let mut payload = {
let payload = {
let start = usize::from(
expected_message_data_offset
.checked_sub(message_offset)
Expand Down Expand Up @@ -255,8 +253,11 @@ pub fn verify_ed25519_msg(
&msg[start..end].try_into().unwrap()
};

let payload =
hex::decode(payload).map_err(|_| SignatureVerificationError::InvalidMessageHex)?;
Ok(VerifiedMessage {
swift_order_params_message: SwiftOrderParamsMessage::deserialize(&mut payload).unwrap(),
swift_order_params_message: SwiftOrderParamsMessage::deserialize(&mut payload.as_slice())
.unwrap(),
signature: *signature,
})
}
Expand All @@ -282,4 +283,6 @@ pub enum SignatureVerificationError {
InvalidInstructionIndex,
#[msg("message offset overflow")]
MessageOffsetOverflow,
#[msg("invalid message hex")]
InvalidMessageHex,
}
Binary file modified sdk/bun.lockb
Binary file not shown.
4 changes: 3 additions & 1 deletion sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5888,13 +5888,15 @@ export class DriftClient {
return this.signMessage(takerOrderParamsMessage);
}

// encode the swift order for use in program Ix/signing
public encodeSwiftOrderParamsMessage(
orderParamsMessage: SwiftOrderParamsMessage
): Buffer {
return this.program.coder.types.encode(
const borshBuf = this.program.coder.types.encode(
'SwiftOrderParamsMessage',
orderParamsMessage
);
return Buffer.from(borshBuf.toString('hex'));
}

public decodeSwiftOrderParamsMessage(
Expand Down
8 changes: 4 additions & 4 deletions sdk/src/idl/drift.json
Original file line number Diff line number Diff line change
Expand Up @@ -11807,6 +11807,9 @@
},
{
"name": "MessageOffsetOverflow"
},
{
"name": "InvalidMessageHex"
}
]
}
Expand Down Expand Up @@ -14473,8 +14476,5 @@
"name": "PythLazerMessagePriceFeedMismatch",
"msg": "Pyth lazer message does not correspond to correct fed id"
}
],
"metadata": {
"address": "dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH"
}
]
}
Empty file modified test-scripts/single-anchor-test.sh
100644 → 100755
Empty file.

0 comments on commit 7894238

Please sign in to comment.