From 53c8645ac859feeade5e505582d8fb70e71c3274 Mon Sep 17 00:00:00 2001 From: Mathieu <60658558+enitrat@users.noreply.github.com> Date: Fri, 13 Sep 2024 12:07:18 +0200 Subject: [PATCH] fix: signature y parity deserialization (#934) --- crates/utils/src/serialization.cairo | 29 +++++++++++++++++-- scripts/compute_rlp_encoding.ts | 11 ++++++- .../data/input_eip1559_y_parity_false.json | 11 +++++++ 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 scripts/data/input_eip1559_y_parity_false.json diff --git a/crates/utils/src/serialization.cairo b/crates/utils/src/serialization.cairo index c29b2f9f2..58c17b910 100644 --- a/crates/utils/src/serialization.cairo +++ b/crates/utils/src/serialization.cairo @@ -11,7 +11,9 @@ pub fn deserialize_signature(signature: Span, chain_id: u64) -> Option< let v: u128 = (*signature.at(4)).try_into()?; - let y_parity = if (v == 0 || v == 1) { + let odd_y_parity = if v == 0 { + false + } else if v == 1 { true } else { compute_y_parity(v, chain_id)? @@ -19,7 +21,9 @@ pub fn deserialize_signature(signature: Span, chain_id: u64) -> Option< Option::Some( Signature { - r: u256 { low: r_low, high: r_high }, s: u256 { low: s_low, high: s_high }, y_parity, + r: u256 { low: r_low, high: r_high }, + s: u256 { low: s_low, high: s_high }, + y_parity: odd_y_parity, } ) } @@ -196,6 +200,13 @@ mod tests { y_parity: true }; + // tx_type = 2 with false y parity - cf input_eip1559_y_parity_false.json + let signature_3 = Signature { + r: 0x782ce82b688abbf13a5e7536b27be67d2795a28b9d4bf819120c17630d88e609, + s: 0x43b90a3315977fe71c9b3687a89857544158e23c5045e7a5852bae03323c9898, + y_parity: false + }; + let signature_0_felt252_arr: Array = array![ signature_0.r.low.into(), signature_0.r.high.into(), @@ -209,7 +220,7 @@ mod tests { signature_1.r.high.into(), signature_1.s.low.into(), signature_1.s.high.into(), - 0x0 + 0x1 ]; let signature_2_felt252_arr: Array = array![ @@ -217,6 +228,14 @@ mod tests { signature_2.r.high.into(), signature_2.s.low.into(), signature_2.s.high.into(), + 0x1 + ]; + + let signature_3_felt252_arr: Array = array![ + signature_3.r.low.into(), + signature_3.r.high.into(), + signature_3.s.low.into(), + signature_3.s.high.into(), 0x0 ]; @@ -231,5 +250,9 @@ mod tests { let result: Signature = deserialize_signature(signature_2_felt252_arr.span(), CHAIN_ID) .unwrap(); assert_eq!(result, signature_2); + + let result: Signature = deserialize_signature(signature_3_felt252_arr.span(), CHAIN_ID) + .unwrap(); + assert_eq!(result, signature_3); } } diff --git a/scripts/compute_rlp_encoding.ts b/scripts/compute_rlp_encoding.ts index a9d00be58..692922397 100644 --- a/scripts/compute_rlp_encoding.ts +++ b/scripts/compute_rlp_encoding.ts @@ -36,7 +36,7 @@ const main = async () => { let tx_type = parseInt( await question( - "enter transaction, 0: legacy, 1: 2930, 2:1559, 3: inc_counter ", + "enter transaction, 0: legacy, 1: 2930, 2:1559, 3: inc_counter, 4: y_parity_false eip1559: ", ), ); @@ -68,6 +68,15 @@ const main = async () => { ), ); break; + case 4: + tx_type = 2; + tx = JSON.parse( + readFileSync( + "./scripts/data/input_eip1559_y_parity_false.json", + "utf-8", + ), + ); + break; default: throw new Error( `transaction type ${tx_type} isn't a valid transaction type`, diff --git a/scripts/data/input_eip1559_y_parity_false.json b/scripts/data/input_eip1559_y_parity_false.json new file mode 100644 index 000000000..16ac42ade --- /dev/null +++ b/scripts/data/input_eip1559_y_parity_false.json @@ -0,0 +1,11 @@ +{ + "nonce": "0x1", + "maxPriorityFeePerGas": "0x64", + "maxFeePerGas": "0x3e8", + "gasLimit": "0x64c9", + "to": "0xcccccccccccccccccccccccccccccccccccccccc", + "value": "0x0", + "data": "0x00", + "accessList": [], + "chainId": "0x4b4b5254" +}