Skip to content

Commit

Permalink
fix: signature y parity deserialization (#934)
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat authored Sep 13, 2024
1 parent 51b0fdf commit 53c8645
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
29 changes: 26 additions & 3 deletions crates/utils/src/serialization.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ pub fn deserialize_signature(signature: Span<felt252>, 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)?
};

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,
}
)
}
Expand Down Expand Up @@ -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<felt252> = array![
signature_0.r.low.into(),
signature_0.r.high.into(),
Expand All @@ -209,14 +220,22 @@ 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<felt252> = array![
signature_2.r.low.into(),
signature_2.r.high.into(),
signature_2.s.low.into(),
signature_2.s.high.into(),
0x1
];

let signature_3_felt252_arr: Array<felt252> = array![
signature_3.r.low.into(),
signature_3.r.high.into(),
signature_3.s.low.into(),
signature_3.s.high.into(),
0x0
];

Expand All @@ -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);
}
}
11 changes: 10 additions & 1 deletion scripts/compute_rlp_encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: ",
),
);

Expand Down Expand Up @@ -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`,
Expand Down
11 changes: 11 additions & 0 deletions scripts/data/input_eip1559_y_parity_false.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"nonce": "0x1",
"maxPriorityFeePerGas": "0x64",
"maxFeePerGas": "0x3e8",
"gasLimit": "0x64c9",
"to": "0xcccccccccccccccccccccccccccccccccccccccc",
"value": "0x0",
"data": "0x00",
"accessList": [],
"chainId": "0x4b4b5254"
}

0 comments on commit 53c8645

Please sign in to comment.