Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr committed Mar 20, 2024
1 parent b8ead18 commit 19f0a39
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions crates/cheatcodes/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ fn create_wallet(private_key: &U256, label: Option<&str>, state: &mut Cheatcodes
.abi_encode())
}

fn encode_vrs(v: u8, r: U256, s: U256) -> Vec<u8> {
(U256::from(v), B256::from(r), B256::from(s)).abi_encode()
}

pub(super) fn sign(private_key: &U256, digest: &B256) -> Result {
// The `ecrecover` precompile does not use EIP-155. No chain ID is needed.
let wallet = parse_wallet(private_key)?;
Expand All @@ -166,11 +170,9 @@ pub(super) fn sign(private_key: &U256, digest: &B256) -> Result {

assert_eq!(recovered, wallet.address());

let v = U256::from(sig.v().y_parity_byte_non_eip155().unwrap_or(sig.v().y_parity_byte()));
let r = B256::from(sig.r());
let s = B256::from(sig.s());
let v = sig.v().y_parity_byte_non_eip155().unwrap_or(sig.v().y_parity_byte());

Ok((v, r, s).abi_encode())
Ok(encode_vrs(v, sig.r(), sig.s()))
}

pub(super) fn sign_with_wallet<DB: DatabaseExt>(
Expand Down Expand Up @@ -207,11 +209,7 @@ pub(super) fn sign_with_wallet<DB: DatabaseExt>(
let recovered = sig.recover(digest.to_ethers()).map_err(|err| fmt_err!("{err}"))?;
assert_eq!(recovered.to_alloy(), signer);

let v = U256::from(sig.v);
let r = B256::from(sig.r.to_alloy());
let s = B256::from(sig.s.to_alloy());

Ok((v, r, s).abi_encode())
Ok(encode_vrs(sig.v as u8, sig.r.to_alloy(), sig.s.to_alloy()))
}

pub(super) fn sign_p256(private_key: &U256, digest: &B256, _state: &mut Cheatcodes) -> Result {
Expand Down

0 comments on commit 19f0a39

Please sign in to comment.