Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cast] Fix cast wallet verify #7215

Merged
merged 4 commits into from
Feb 23, 2024
Merged
Changes from all commits
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
29 changes: 24 additions & 5 deletions crates/cast/bin/cmd/wallet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloy_primitives::{Address, Signature, B256};
use alloy_primitives::{Address, Signature};
use alloy_signer::{
coins_bip39::{English, Mnemonic},
LocalWallet, MnemonicBuilder, Signer as AlloySigner,
Expand All @@ -12,7 +12,7 @@ use foundry_config::Config;
use foundry_wallets::{RawWalletOpts, WalletOpts, WalletSigner};
use rand::thread_rng;
use serde_json::json;
use std::{path::Path, str::FromStr};
use std::path::Path;
use yansi::Paint;

pub mod vanity;
Expand Down Expand Up @@ -273,9 +273,8 @@ impl WalletSubcommands {
println!("0x{sig}");
}
WalletSubcommands::Verify { message, signature, address } => {
let recovered_address =
signature.recover_address_from_prehash(&B256::from_str(&message)?)?;
if recovered_address == address {
let recovered_address = Self::recover_address_from_message(&message, &signature)?;
if address == recovered_address {
println!("Validation succeeded. Address {address} signed this message.");
} else {
println!("Validation failed. Address {address} did not sign this message.");
Expand Down Expand Up @@ -355,6 +354,11 @@ flag to set your key via:
Ok(())
}

/// Recovers an address from the specified message and signature
fn recover_address_from_message(message: &str, signature: &Signature) -> Result<Address> {
Ok(signature.recover_address_from_msg(message)?)
}

fn hex_str_to_bytes(s: &str) -> Result<Vec<u8>> {
Ok(match s.strip_prefix("0x") {
Some(data) => hex::decode(data).wrap_err("Could not decode 0x-prefixed string.")?,
Expand All @@ -365,6 +369,10 @@ flag to set your key via:

#[cfg(test)]
mod tests {
use std::str::FromStr;

use alloy_primitives::address;

use super::*;

#[test]
Expand Down Expand Up @@ -393,6 +401,17 @@ mod tests {
}
}

#[test]
fn can_verify_signed_hex_message() {
let message = "hello";
let signature = Signature::from_str("f2dd00eac33840c04b6fc8a5ec8c4a47eff63575c2bc7312ecb269383de0c668045309c423484c8d097df306e690c653f8e1ec92f7f6f45d1f517027771c3e801c").unwrap();
let address = address!("28A4F420a619974a2393365BCe5a7b560078Cc13");
let recovered_address =
WalletSubcommands::recover_address_from_message(message, &signature);
assert!(recovered_address.is_ok());
assert_eq!(address, recovered_address.unwrap());
}

#[test]
fn can_parse_wallet_sign_data() {
let args = WalletSubcommands::parse_from(["foundry-cli", "sign", "--data", "{ ... }"]);
Expand Down
Loading