From 434b80a61f491e443992abc08fc3d4d82e88cb7a Mon Sep 17 00:00:00 2001 From: ArtificialPB Date: Wed, 31 Jan 2024 22:34:45 +0100 Subject: [PATCH] fix(primitives): don't double-hex signature bytes in `Signature#to_hex_bytes` --- .../primitives/src/transaction/signature.rs | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/crates/primitives/src/transaction/signature.rs b/crates/primitives/src/transaction/signature.rs index d92a38f7a483..de1f09c7cab0 100644 --- a/crates/primitives/src/transaction/signature.rs +++ b/crates/primitives/src/transaction/signature.rs @@ -181,7 +181,7 @@ impl Signature { /// Turn this signature into its hex-encoded representation. pub fn to_hex_bytes(&self) -> Bytes { - crate::hex::encode(self.to_bytes()).into() + self.to_bytes().into() } /// Calculates a heuristic for the in-memory size of the [Signature]. @@ -194,7 +194,7 @@ impl Signature { #[cfg(test)] mod tests { use crate::{transaction::signature::SECP256K1N_HALF, Address, Signature, B256, U256}; - use alloy_primitives::hex; + use alloy_primitives::{hex, hex::FromHex, Bytes}; use bytes::BytesMut; use std::str::FromStr; @@ -315,6 +315,24 @@ mod tests { assert!(signature.size() >= 65); } + #[test] + fn test_to_hex_bytes() { + let signature = Signature { + r: U256::from_str( + "18515461264373351373200002665853028612451056578545711640558177340181847433846", + ) + .unwrap(), + s: U256::from_str( + "46948507304638947509940763649030358759909902576025900602547168820602576006531", + ) + .unwrap(), + odd_y_parity: false, + }; + + let expected = Bytes::from_hex("0x28ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa63627667cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d831b").unwrap(); + assert_eq!(signature.to_hex_bytes(), expected); + } + #[test] fn eip_2_reject_high_s_value() { // This pre-homestead transaction has a high `s` value and should be rejected by the