Skip to content

Commit

Permalink
feat: implement sign_transaction on Trezor
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Jan 5, 2024
1 parent 74e8207 commit 63b00a9
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 196 deletions.
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
21 changes: 15 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.
///
/// 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
/// 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,11 @@ 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);
}

/// Captures getters and setters common across EIP-1559 transactions across all networks
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

0 comments on commit 63b00a9

Please sign in to comment.