Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement sign_transaction on Trezor #100

Merged
merged 2 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ mod receipt;
pub use receipt::{Receipt, ReceiptEnvelope, ReceiptWithBloom};

mod transaction;
pub use transaction::{TxEip1559, TxEip2930, TxEnvelope, TxKind, TxLegacy, TxType};
pub use transaction::{TxEip1559, TxEip2930, TxEnvelope, TxLegacy, TxType};

pub use alloy_network::TxKind;
20 changes: 18 additions & 2 deletions crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{ReceiptWithBloom, TxKind, TxType};
use crate::{TxKind, TxType};
use alloy_eips::eip2930::AccessList;
use alloy_network::{Signed, Transaction};
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, B256, U256};
Expand Down Expand Up @@ -231,7 +231,7 @@ impl Decodable for TxEip1559 {

impl Transaction for TxEip1559 {
type Signature = Signature;
type Receipt = ReceiptWithBloom;
// type Receipt = ReceiptWithBloom;

fn into_signed(self, signature: Signature) -> Signed<Self> {
let mut buf = vec![];
Expand Down Expand Up @@ -274,6 +274,14 @@ impl Transaction for TxEip1559 {
self.input = input;
}

fn to(&self) -> TxKind {
self.to
}

fn set_to(&mut self, to: TxKind) {
self.to = to;
}

fn value(&self) -> U256 {
self.value
}
Expand Down Expand Up @@ -305,6 +313,14 @@ impl Transaction for TxEip1559 {
fn set_gas_limit(&mut self, limit: u64) {
self.gas_limit = limit;
}

fn gas_price(&self) -> Option<U256> {
None
}

fn set_gas_price(&mut self, price: U256) {
let _ = price;
}
}

#[cfg(all(test, feature = "k256"))]
Expand Down
25 changes: 21 additions & 4 deletions crates/consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{transaction::TxKind, ReceiptWithBloom, TxType};
use crate::{TxKind, TxType};
use alloy_eips::eip2930::AccessList;
use alloy_network::{Signed, Transaction};
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, B256, U256};
Expand Down Expand Up @@ -192,7 +192,7 @@ impl Decodable for TxEip2930 {

impl Transaction for TxEip2930 {
type Signature = Signature;
type Receipt = ReceiptWithBloom;
// type Receipt = ReceiptWithBloom;

fn into_signed(self, signature: Signature) -> Signed<Self> {
let mut buf = vec![];
Expand Down Expand Up @@ -236,6 +236,14 @@ impl Transaction for TxEip2930 {
self.input = input;
}

fn to(&self) -> TxKind {
self.to
}

fn set_to(&mut self, to: TxKind) {
self.to = to;
}

fn value(&self) -> U256 {
self.value
}
Expand Down Expand Up @@ -267,13 +275,22 @@ impl Transaction for TxEip2930 {
fn set_gas_limit(&mut self, limit: u64) {
self.gas_limit = limit;
}

fn gas_price(&self) -> Option<U256> {
Some(U256::from(self.gas_price))
}

fn set_gas_price(&mut self, price: U256) {
if let Ok(price) = price.try_into() {
self.gas_price = price;
}
}
}

#[cfg(test)]
mod tests {

use super::TxEip2930;
use crate::{transaction::TxKind, TxEnvelope};
use crate::{TxEnvelope, TxKind};
use alloy_network::{Signed, Transaction};
use alloy_primitives::{Address, Bytes, Signature, U256};
use alloy_rlp::{Decodable, Encodable};
Expand Down
24 changes: 21 additions & 3 deletions crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{transaction::TxKind, ReceiptWithBloom};
use crate::TxKind;
use alloy_network::{Signed, Transaction};
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, B256, U256};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header, Result};
Expand Down Expand Up @@ -208,7 +208,7 @@ impl Decodable for TxLegacy {

impl Transaction for TxLegacy {
type Signature = Signature;
type Receipt = ReceiptWithBloom;
// type Receipt = ReceiptWithBloom;

fn into_signed(self, signature: Signature) -> Signed<Self> {
let mut buf = vec![];
Expand Down Expand Up @@ -250,6 +250,14 @@ impl Transaction for TxLegacy {
self.input = data;
}

fn to(&self) -> TxKind {
self.to
}

fn set_to(&mut self, to: TxKind) {
self.to = to;
}

fn value(&self) -> U256 {
self.value
}
Expand Down Expand Up @@ -281,14 +289,24 @@ impl Transaction for TxLegacy {
fn set_gas_limit(&mut self, gas_limit: u64) {
self.gas_limit = gas_limit;
}

fn gas_price(&self) -> Option<U256> {
Some(U256::from(self.gas_price))
}

fn set_gas_price(&mut self, price: U256) {
if let Ok(price) = price.try_into() {
self.gas_price = price;
}
}
}

#[cfg(test)]
mod tests {
#[test]
#[cfg(feature = "k256")]
fn recover_signer_legacy() {
use crate::transaction::{TxKind, TxLegacy};
use crate::{TxKind, TxLegacy};
use alloy_network::Transaction;
use alloy_primitives::{b256, hex, Address, Signature, B256, U256};

Expand Down
3 changes: 0 additions & 3 deletions crates/consensus/src/transaction/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
mod common;
pub use common::TxKind;

mod eip1559;
pub use eip1559::TxEip1559;

Expand Down
2 changes: 1 addition & 1 deletion crates/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod sealed;
pub use sealed::{Sealable, Sealed};

mod transaction;
pub use transaction::{Eip1559Transaction, Signed, Transaction};
pub use transaction::{Eip1559Transaction, Signed, Transaction, TxKind};

mod receipt;
pub use receipt::Receipt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl TxKind {

/// Calculates a heuristic for the in-memory size of this object.
#[inline]
pub(crate) const fn size(self) -> usize {
pub const fn size(self) -> usize {
std::mem::size_of::<Self>()
}
}
Expand Down
33 changes: 27 additions & 6 deletions crates/network/src/transaction/mod.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
use crate::Receipt;
use alloy_primitives::{Bytes, ChainId, Signature, B256, U256};
use alloy_rlp::{BufMut, Decodable, Encodable};
use alloy_rlp::{BufMut, Encodable};

mod common;
pub use common::TxKind;

mod signed;
pub use signed::Signed;

/// Represents a minimal EVM transaction.
pub trait Transaction: Encodable + Decodable + Send + Sync + 'static {
pub trait Transaction: std::any::Any + Encodable + Send + Sync + 'static {
/// The signature type for this transaction.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bleh do we need to explicitly declare the Any here? I guess it'll be always there for most types we impl transaction for, would add a note in the code on why it's needed

///
/// This is usually [`alloy_primitives::Signature`], however, it may be different for future
/// EIP-2718 transaction types, or in other networks. For example, in Optimism, the deposit
/// transaction signature is the unit type `()`.
type Signature;

/// The receipt type for this transaction.
type Receipt: Receipt;

/// Convert to a signed transaction by adding a signature and computing the
Comment on lines -17 to -19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll bring this back per Prestwich thinking right? in that case let's leave this commented out instead of removing

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know honestly, should receipts be tied to a specific transaction? Keeping this will make it impossible to have signers use dyn transaction because know that also has to be specified

/// hash.
fn into_signed(self, signature: Signature) -> Signed<Self, Self::Signature>
Expand Down Expand Up @@ -45,6 +44,11 @@ pub trait Transaction: Encodable + Decodable + Send + Sync + 'static {
/// Set `data`.
fn set_input(&mut self, data: Bytes);

/// Get `to`.
fn to(&self) -> TxKind;
/// Set `to`.
fn set_to(&mut self, to: TxKind);

/// Get `value`.
fn value(&self) -> U256;
/// Set `value`.
Expand All @@ -64,6 +68,23 @@ pub trait Transaction: Encodable + Decodable + Send + Sync + 'static {
fn gas_limit(&self) -> u64;
/// Set `gas_limit`.
fn set_gas_limit(&mut self, limit: u64);

/// Get `gas_price`.
fn gas_price(&self) -> Option<U256>;
/// Set `gas_price`.
fn set_gas_price(&mut self, price: U256);
}

// TODO: Remove in favor of dyn trait upcasting (1.76+)
#[doc(hidden)]
impl<S: 'static> dyn Transaction<Signature = S> {
pub fn __downcast_ref<T: std::any::Any>(&self) -> Option<&T> {
if std::any::Any::type_id(self) == std::any::TypeId::of::<T>() {
unsafe { Some(&*(self as *const _ as *const T)) }
} else {
None
}
}
}

/// Captures getters and setters common across EIP-1559 transactions across all networks
Expand Down
4 changes: 2 additions & 2 deletions crates/signer-ledger/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::types::{DerivationType, LedgerError, INS, P1, P1_FIRST, P2};
use alloy_primitives::{hex, Address, ChainId, B256};
use alloy_signer::{Result, SignableTx, Signature, Signer};
use alloy_signer::{Result, SignableTx, Signature, Signer, TransactionExt};
use async_trait::async_trait;
use coins_ledger::{
common::{APDUCommand, APDUData},
Expand Down Expand Up @@ -48,7 +48,7 @@ impl Signer for LedgerSigner {
}

#[inline]
async fn sign_transaction(&self, tx: &mut dyn SignableTx) -> Result<Signature> {
async fn sign_transaction(&self, tx: &mut SignableTx) -> Result<Signature> {
let chain_id = self.chain_id();
if let Some(chain_id) = chain_id {
tx.set_chain_id_checked(chain_id)?;
Expand Down
2 changes: 2 additions & 0 deletions crates/signer-trezor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ repository.workspace = true
exclude.workspace = true

[dependencies]
alloy-consensus.workspace = true
alloy-network.workspace = true
alloy-primitives.workspace = true
alloy-signer.workspace = true

Expand Down
Loading
Loading