Skip to content
Open
Changes from 1 commit
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
53 changes: 53 additions & 0 deletions crates/primitives/src/signature/sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,19 @@ impl Signature {
sig
}

/// Returns the byte-array representation of this signature with raw `y_parity`.
///
/// The first 32 bytes are the `r` value, the second 32 bytes the `s` value
/// and the final byte is the `y_parity` value as-is (0 or 1).
#[inline]
pub fn as_raw_bytes(&self) -> [u8; 65] {
let mut sig = [0u8; 65];
sig[..32].copy_from_slice(&self.r.to_be_bytes::<32>());
sig[32..64].copy_from_slice(&self.s.to_be_bytes::<32>());
sig[64] = self.y_parity as u8;
sig
}

/// Decode the signature from the ERC-2098 compact representation.
///
/// The first 32 bytes are the `r` value, and the next 32 bytes are the `s` value with `yParity`
Expand Down Expand Up @@ -657,6 +670,46 @@ mod tests {
assert_eq!(signature.as_bytes(), expected);
}

#[test]
fn as_raw_bytes() {
let signature = Signature::new(
U256::from_str(
"18515461264373351373200002665853028612451056578545711640558177340181847433846",
)
.unwrap(),
U256::from_str(
"46948507304638947509940763649030358759909902576025900602547168820602576006531",
)
.unwrap(),
false,
);

let expected = hex!(
"0x28ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa63627667cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d8300"
);
assert_eq!(signature.as_raw_bytes(), expected);
}

#[test]
fn as_raw_bytes_y_parity_true() {
let signature = Signature::new(
U256::from_str(
"18515461264373351373200002665853028612451056578545711640558177340181847433846",
)
.unwrap(),
U256::from_str(
"46948507304638947509940763649030358759909902576025900602547168820602576006531",
)
.unwrap(),
true,
);

let expected = hex!(
"0x28ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa63627667cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d8301"
);
assert_eq!(signature.as_raw_bytes(), expected);
}

#[test]
fn as_erc2098_y_false() {
let signature = Signature::new(
Expand Down
Loading