Skip to content

Commit

Permalink
if let some else
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr committed Mar 20, 2024
1 parent 8687271 commit b8ead18
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions crates/cheatcodes/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,40 +178,40 @@ pub(super) fn sign_with_wallet<DB: DatabaseExt>(
signer: Option<Address>,
digest: &B256,
) -> Result {
if let Some(script_wallets) = &ccx.state.script_wallets {
let mut script_wallets = script_wallets.inner.lock();
let maybe_provided_sender = script_wallets.provided_sender;
let signers = script_wallets.multi_wallet.signers()?;

let signer = if let Some(signer) = signer {
signer
} else if let Some(provided_sender) = maybe_provided_sender {
provided_sender
} else if signers.len() == 1 {
*signers.keys().next().unwrap()
} else {
return Err("could not determine signer".into());
};
let Some(script_wallets) = &ccx.state.script_wallets else {
return Err("no wallets are available".into());
};

let mut script_wallets = script_wallets.inner.lock();
let maybe_provided_sender = script_wallets.provided_sender;
let signers = script_wallets.multi_wallet.signers()?;

let signer = if let Some(signer) = signer {
signer
} else if let Some(provided_sender) = maybe_provided_sender {
provided_sender
} else if signers.len() == 1 {
*signers.keys().next().unwrap()
} else {
return Err("could not determine signer".into());
};

let wallet = signers
.get(&signer)
.ok_or_else(|| fmt_err!("signer with address {signer} is not available"))?;
let wallet = signers
.get(&signer)
.ok_or_else(|| fmt_err!("signer with address {signer} is not available"))?;

let sig = RuntimeOrHandle::new()
.block_on(wallet.sign_hash(digest))
.map_err(|err| fmt_err!("{err}"))?;
let sig = RuntimeOrHandle::new()
.block_on(wallet.sign_hash(digest))
.map_err(|err| fmt_err!("{err}"))?;

let recovered = sig.recover(digest.to_ethers()).map_err(|err| fmt_err!("{err}"))?;
assert_eq!(recovered.to_alloy(), signer);
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());
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())
} else {
Err("no wallets are available".into())
}
Ok((v, r, s).abi_encode())
}

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

0 comments on commit b8ead18

Please sign in to comment.