Skip to content

Commit 701642b

Browse files
committed
Fix eth_signTransaction request and response
1 parent 26e6e57 commit 701642b

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-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: 4 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,9 @@ 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 =
867+
alloy_primitives::hex::encode(self.sign_request(&from, request)?.encoded_2718());
868+
Ok(format!("0x{signed_transaction}"))
869869
}
870870

871871
/// Sends a transaction

crates/anvil/tests/it/sign.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,24 @@ 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().to(to).value(U256::from(1001u64)).from(from);
296+
let tx = WithOtherFields::new(tx);
297+
// broadcast it via the eth_sendTransaction API
298+
let signed_tx = api.sign_transaction(tx).await.unwrap();
299+
300+
assert_eq!(signed_tx, "0x02f86c827a69808084773594008252089470997970c51812dc3a010c7d01b50e0d17dc79c88203e980c082f4f6a05b8c3e67a3cecdad35d8be49df75c6bb07f720dbd9742a4240d3cdee97653e36a02c946c4a6ce6e66803ff22af7b7d1c39307339ca122fa16a0ef63ace382e8d56");
301+
}
302+
285303
#[tokio::test(flavor = "multi_thread")]
286304
async fn rejects_different_chain_id() {
287305
let (_api, handle) = spawn(NodeConfig::test()).await;

0 commit comments

Comments
 (0)