fix: contract creation on trezor (#695)

* fix contract creation on trezor

* handle when _to_ param is an empty string

* return TrezorError when receiving an ENS name

* fix typo
This commit is contained in:
joshieDo 2021-12-15 19:52:06 +02:00 committed by GitHub
parent 6cbdc89187
commit 5bf3f73548
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 6 deletions

View File

@ -264,13 +264,34 @@ mod tests {
#[tokio::test]
#[ignore]
async fn test_sign_empty_txes() {
// Contract creation (empty `to`), requires data.
// To test without the data field, we need to specify a `to` address.
let trezor = TrezorEthereum::new(DerivationType::TrezorLive(0), 1).await.unwrap();
{
let tx_req = Eip1559TransactionRequest::new().into();
let tx_req = Eip1559TransactionRequest::new()
.to("2ed7afa17473e17ac59908f088b4371d28585476".parse::<Address>().unwrap())
.into();
let tx = trezor.sign_transaction(&tx_req).await.unwrap();
}
{
let tx_req = TransactionRequest::new().into();
let tx_req = TransactionRequest::new()
.to("2ed7afa17473e17ac59908f088b4371d28585476".parse::<Address>().unwrap())
.into();
let tx = trezor.sign_transaction(&tx_req).await.unwrap();
}
let data = hex::decode("095ea7b30000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap();
// Contract creation (empty `to`, with data) should show on the trezor device as:
// ` "0 Wei ETH
// ` new contract?"
let trezor = TrezorEthereum::new(DerivationType::TrezorLive(0), 1).await.unwrap();
{
let tx_req = Eip1559TransactionRequest::new().data(data.clone()).into();
let tx = trezor.sign_transaction(&tx_req).await.unwrap();
}
{
let tx_req = TransactionRequest::new().data(data.clone()).into();
let tx = trezor.sign_transaction(&tx_req).await.unwrap();
}
}

View File

@ -4,7 +4,7 @@
use std::fmt;
use thiserror::Error;
use ethers_core::types::{transaction::eip2718::TypedTransaction, NameOrAddress, H160, U256};
use ethers_core::types::{transaction::eip2718::TypedTransaction, NameOrAddress, U256};
use trezor_client::client::AccessListItem as Trezor_AccessListItem;
#[derive(Clone, Debug)]
@ -49,6 +49,8 @@ pub enum TrezorError {
/// Error when signing EIP712 struct with not compatible Trezor ETH app
#[error("Trezor ethereum app requires at least version: {0:?}")]
UnsupportedFirmwareVersion(String),
#[error("Does not support ENS.")]
NoENSSupport,
}
/// Trezor Transaction Struct
@ -72,9 +74,13 @@ impl TrezorTransaction {
}
pub fn load(tx: &TypedTransaction) -> Result<Self, TrezorError> {
let to: String = match tx.to().unwrap_or(&NameOrAddress::Address(H160::from(&[0; 20]))) {
NameOrAddress::Name(_) => unimplemented!(),
let to: String = match tx.to() {
Some(v) => match v {
NameOrAddress::Name(_) => return Err(TrezorError::NoENSSupport),
NameOrAddress::Address(value) => format!("0x{}", hex::encode(value)),
},
// Contract Creation
None => "".to_string(),
};
let nonce = tx.nonce().map_or(vec![], Self::to_trimmed_big_endian);