Skip to content

Commit 5885dbc

Browse files
authored
Fix eth_signTransaction request and response (#7804)
* Fix eth_signTransaction request and response * fixup! Fix eth_signTransaction request and response * Hardcode test nonce and fee values * Fix test signed result
1 parent c0aac85 commit 5885dbc

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

crates/anvil/core/src/eth/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pub enum EthRequest {
142142
#[cfg_attr(feature = "serde", serde(rename = "eth_sign", alias = "personal_sign"))]
143143
EthSign(Address, Bytes),
144144

145-
#[cfg_attr(feature = "serde", serde(rename = "eth_signTransaction"))]
145+
#[cfg_attr(feature = "serde", serde(rename = "eth_signTransaction", with = "sequence"))]
146146
EthSignTransaction(Box<WithOtherFields<TransactionRequest>>),
147147

148148
/// Signs data via [EIP-712](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md).

crates/anvil/src/eth/api.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use crate::{
3232
ClientFork, LoggingManager, Miner, MiningMode, StorageInfo,
3333
};
3434
use alloy_dyn_abi::TypedData;
35+
use alloy_eips::eip2718::Encodable2718;
3536
use alloy_network::eip2718::Decodable2718;
3637
use alloy_primitives::{Address, Bytes, TxHash, TxKind, B256, B64, U256, U64};
3738
use alloy_rpc_types::{
@@ -862,10 +863,8 @@ impl EthApi {
862863

863864
let request = self.build_typed_tx_request(request, nonce)?;
864865

865-
let signer = self.get_signer(from).ok_or(BlockchainError::NoSignerAvailable)?;
866-
let signature =
867-
alloy_primitives::hex::encode(signer.sign_transaction(request, &from)?.as_bytes());
868-
Ok(format!("0x{signature}"))
866+
let signed_transaction = self.sign_request(&from, request)?.encoded_2718();
867+
Ok(alloy_primitives::hex::encode_prefixed(signed_transaction))
869868
}
870869

871870
/// Sends a transaction

crates/anvil/tests/it/sign.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,30 @@ async fn can_sign_typed_data_os() {
282282
);
283283
}
284284

285+
#[tokio::test(flavor = "multi_thread")]
286+
async fn can_sign_transaction() {
287+
let (api, handle) = spawn(NodeConfig::test()).await;
288+
289+
let accounts = handle.dev_wallets().collect::<Vec<_>>();
290+
let from = accounts[0].address();
291+
let to = accounts[1].address();
292+
293+
// craft the tx
294+
// specify the `from` field so that the client knows which account to use
295+
let tx = TransactionRequest::default()
296+
.nonce(10)
297+
.max_fee_per_gas(100)
298+
.max_priority_fee_per_gas(101)
299+
.to(to)
300+
.value(U256::from(1001u64))
301+
.from(from);
302+
let tx = WithOtherFields::new(tx);
303+
// sign it via the eth_signTransaction API
304+
let signed_tx = api.sign_transaction(tx).await.unwrap();
305+
306+
assert_eq!(signed_tx, "0x02f868827a690a65648252089470997970c51812dc3a010c7d01b50e0d17dc79c88203e980c082f4f6a0e4de88aefcf87ccb04466e60de66a83192e46aa26177d5ea35efbfd43fd0ecdca00e3148e0e8e0b9a6f9b329efd6e30c4a461920f3a27497be3dbefaba996601da");
307+
}
308+
285309
#[tokio::test(flavor = "multi_thread")]
286310
async fn rejects_different_chain_id() {
287311
let (_api, handle) = spawn(NodeConfig::test()).await;

0 commit comments

Comments
 (0)