From ebf52934e4091a8a861c5f8cc5665a0cfcbcf780 Mon Sep 17 00:00:00 2001 From: joshieDo <93316087+joshieDo@users.noreply.github.com> Date: Mon, 13 Dec 2021 19:39:24 +0200 Subject: [PATCH] fix handling empty tx parameters (#686) --- ethers-signers/src/trezor/app.rs | 14 ++++++++++++++ ethers-signers/src/trezor/types.rs | 26 +++++++++++++------------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/ethers-signers/src/trezor/app.rs b/ethers-signers/src/trezor/app.rs index 8e299ea6..2d7a0455 100644 --- a/ethers-signers/src/trezor/app.rs +++ b/ethers-signers/src/trezor/app.rs @@ -261,6 +261,20 @@ mod tests { let tx = trezor.sign_transaction(&tx_req).await.unwrap(); } + #[tokio::test] + #[ignore] + async fn test_sign_empty_txes() { + let trezor = TrezorEthereum::new(DerivationType::TrezorLive(0), 1).await.unwrap(); + { + let tx_req = Eip1559TransactionRequest::new().into(); + let tx = trezor.sign_transaction(&tx_req).await.unwrap(); + } + { + let tx_req = TransactionRequest::new().into(); + let tx = trezor.sign_transaction(&tx_req).await.unwrap(); + } + } + #[tokio::test] #[ignore] async fn test_sign_eip1559_tx() { diff --git a/ethers-signers/src/trezor/types.rs b/ethers-signers/src/trezor/types.rs index 2942d496..a3ec935c 100644 --- a/ethers-signers/src/trezor/types.rs +++ b/ethers-signers/src/trezor/types.rs @@ -4,7 +4,7 @@ use std::fmt; use thiserror::Error; -use ethers_core::types::{transaction::eip2718::TypedTransaction, NameOrAddress, U256}; +use ethers_core::types::{transaction::eip2718::TypedTransaction, NameOrAddress, H160, U256}; use trezor_client::client::AccessListItem as Trezor_AccessListItem; #[derive(Clone, Debug)] @@ -72,16 +72,16 @@ impl TrezorTransaction { } pub fn load(tx: &TypedTransaction) -> Result { - let to: String = match tx.to().ok_or(TrezorError::DataError)? { + let to: String = match tx.to().unwrap_or(&NameOrAddress::Address(H160::from(&[0; 20]))) { NameOrAddress::Name(_) => unimplemented!(), NameOrAddress::Address(value) => format!("0x{}", hex::encode(value)), }; - let nonce = Self::to_trimmed_big_endian(tx.nonce().ok_or(TrezorError::DataError)?); - let gas = Self::to_trimmed_big_endian(tx.gas().ok_or(TrezorError::DataError)?); - let gas_price = Self::to_trimmed_big_endian(&tx.gas_price().ok_or(TrezorError::DataError)?); - let value = Self::to_trimmed_big_endian(tx.value().ok_or(TrezorError::DataError)?); - let data = tx.data().ok_or(TrezorError::DataError)?.to_vec(); + let nonce = tx.nonce().map_or(vec![], Self::to_trimmed_big_endian); + let gas = tx.gas().map_or(vec![], Self::to_trimmed_big_endian); + let gas_price = tx.gas_price().map_or(vec![], |v| Self::to_trimmed_big_endian(&v)); + let value = tx.value().map_or(vec![], Self::to_trimmed_big_endian); + let data = tx.data().map_or(vec![], |v| v.to_vec()); match tx { TypedTransaction::Eip2930(_) | TypedTransaction::Legacy(_) => Ok(Self { @@ -96,12 +96,12 @@ impl TrezorTransaction { access_list: vec![], }), TypedTransaction::Eip1559(eip1559_tx) => { - let max_fee_per_gas = Self::to_trimmed_big_endian( - &eip1559_tx.max_fee_per_gas.ok_or(TrezorError::DataError)?, - ); - let max_priority_fee_per_gas = Self::to_trimmed_big_endian( - &eip1559_tx.max_priority_fee_per_gas.ok_or(TrezorError::DataError)?, - ); + let max_fee_per_gas = + eip1559_tx.max_fee_per_gas.map_or(vec![], |v| Self::to_trimmed_big_endian(&v)); + + let max_priority_fee_per_gas = eip1559_tx + .max_priority_fee_per_gas + .map_or(vec![], |v| Self::to_trimmed_big_endian(&v)); let mut access_list: Vec = Vec::new(); for item in &eip1559_tx.access_list.0 {