1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
//! Module implementing the `hash` subcommand for `keccak256` hashing data.
use crate::cmd;
use anyhow::Result;
use clap::Parser;
use ethdigest::Digest;
use hdwallet::{
account::Signature, message::EthereumMessage, transaction::Transaction, typeddata::TypedData,
};
use std::path::PathBuf;
#[derive(Debug, Parser)]
pub struct Options {
#[clap(subcommand)]
input: Input,
}
#[derive(Debug, Parser)]
enum Input {
/// Hash an Ethereum transaction.
Transaction {
/// Path to transaction to hash in JSON format.
#[clap(name = "TRANSACTION")]
transaction: PathBuf,
/// Signature for the transaction.
#[clap(short, long)]
signature: Option<Signature>,
},
/// Hash an Ethereum message.
Message {
/// Path to the message to hash in the "eth_sign" scheme. This message
/// will be prefixed with "\x19Ethereum Signed Message:\n" and its
/// length before hashing.
#[clap(name = "MESSAGE")]
message: PathBuf,
},
/// Hash EIP-712 typed data.
#[clap(name = "typeddata")]
TypedData {
/// Path to the EIP-712 typed data in JSON format.
#[clap(name = "TYPEDDATA")]
typed_data: PathBuf,
/// Only compute the struct hash of the main message without mixing in
/// the domain separator.
#[clap(short, long)]
message_hash: bool,
},
/// Hash raw data.
Data {
/// Path to the data to hash. Use `-` for standard in.
#[clap(name = "DATA")]
data: PathBuf,
},
}
pub fn run(options: Options) -> Result<()> {
let digest = match options.input {
Input::Transaction {
transaction,
signature,
} => {
let transaction =
serde_json::from_slice::<Transaction>(&cmd::read_input(&transaction)?)?;
match signature {
Some(signature) => Digest::of(transaction.encode(signature)),
None => transaction.signing_message(),
}
}
Input::Message { message } => {
let message = EthereumMessage(cmd::read_input(&message)?);
message.signing_message()
}
Input::TypedData {
typed_data,
message_hash,
} => {
let typed_data = serde_json::from_slice::<TypedData>(&cmd::read_input(&typed_data)?)?;
if message_hash {
typed_data.message_hash()
} else {
typed_data.signing_message()
}
}
Input::Data { data } => {
let data = cmd::read_input(&data)?;
Digest::of(data)
}
};
println!("{digest}");
Ok(())
}
|