Skip to content

Commit

Permalink
Add ability to sign arbitrary 0x-prefixed data to cast wallet sign (#…
Browse files Browse the repository at this point in the history
…4820)

* feat(cast-wallet-sign): add 0x-prefixed data read

* refactor(fmt): format files

* test(cast-wallet): rename cast wallet sign tests

* chore: add help message

---------

Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
Rubilmax and mattsse authored Apr 26, 2023
1 parent 8973b2b commit e9bc782
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
16 changes: 13 additions & 3 deletions cli/src/cmd/cast/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ pub enum WalletSubcommands {
},
#[clap(name = "sign", visible_alias = "s", about = "Sign a message.")]
Sign {
#[clap(help = "message to sign", value_name = "MESSAGE")]
#[clap(
help = "message to sign. Messages starting with 0x are expected to be hex encoded, which get decoded before being signed",
value_name = "MESSAGE"
)]
message: String,
#[clap(flatten)]
wallet: Wallet,
Expand Down Expand Up @@ -120,8 +123,15 @@ impl WalletSubcommands {
}
WalletSubcommands::Sign { message, wallet } => {
let wallet = wallet.signer(0).await?;
let sig = wallet.sign_message(message).await?;
println!("Signature: 0x{sig}");
let sig = match message.strip_prefix("0x") {
Some(data) => {
let data_bytes: Vec<u8> =
hex::decode(data).wrap_err("Could not decode 0x-prefixed string.")?;
wallet.sign_message(data_bytes).await?
}
None => wallet.sign_message(message).await?,
};
println!("0x{sig}");
}
WalletSubcommands::Verify { message, signature, address } => {
let pubkey: Address = address.parse().wrap_err("Invalid address")?;
Expand Down
26 changes: 26 additions & 0 deletions cli/tests/it/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,32 @@ casttest!(wallet_address_keystore_with_password_file, |_: TestProject, mut cmd:
assert!(out.contains("0xeC554aeAFE75601AaAb43Bd4621A22284dB566C2"));
});

// tests that `cast wallet sign` outputs the expected signature
casttest!(cast_wallet_sign_utf8_data, |_: TestProject, mut cmd: TestCommand| {
cmd.args([
"wallet",
"sign",
"--private-key",
"0x0000000000000000000000000000000000000000000000000000000000000001",
"test",
]);
let output = cmd.stdout_lossy();
assert_eq!(output.trim(), "0xfe28833983d6faa0715c7e8c3873c725ddab6fa5bf84d40e780676e463e6bea20fc6aea97dc273a98eb26b0914e224c8dd5c615ceaab69ddddcf9b0ae3de0e371c");
});

// tests that `cast wallet sign` outputs the expected signature, given a 0x-prefixed data
casttest!(cast_wallet_sign_hex_data, |_: TestProject, mut cmd: TestCommand| {
cmd.args([
"wallet",
"sign",
"--private-key",
"0x0000000000000000000000000000000000000000000000000000000000000001",
"0x0000000000000000000000000000000000000000000000000000000000000000",
]);
let output = cmd.stdout_lossy();
assert_eq!(output.trim(), "0x23a42ca5616ee730ff3735890c32fc7b9491a9f633faca9434797f2c845f5abf4d9ba23bd7edb8577acebaa3644dc5a4995296db420522bb40060f1693c33c9b1c");
});

// tests that `cast estimate` is working correctly.
casttest!(estimate_function_gas, |_: TestProject, mut cmd: TestCommand| {
let eth_rpc_url = next_http_rpc_endpoint();
Expand Down
2 changes: 1 addition & 1 deletion evm/src/executor/inspector/cheatcodes/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ fn parse_json(
if values.iter().any(|value| value.is_object()) {
return Err(error::encode_error(format!(
"You can only coerce values or arrays, not JSON objects. The key '{key}' returns an object",
)))
)));
}

if values.is_empty() {
Expand Down

0 comments on commit e9bc782

Please sign in to comment.