From b8b7ad60ada5e808c11beba2810c108d46d849bb Mon Sep 17 00:00:00 2001 From: "Supernovahs.eth" <91280922+supernovahs@users.noreply.github.com> Date: Thu, 10 Aug 2023 20:36:49 +0530 Subject: [PATCH] Signature functions moved to rpc_types_compat (#4143) --- .../rpc-types-compat/src/transaction/mod.rs | 13 ++--- .../src/transaction/signature.rs | 51 ++++++++++++++++++ .../rpc/rpc-types/src/eth/transaction/mod.rs | 2 +- .../src/eth/transaction/signature.rs | 53 +------------------ 4 files changed, 58 insertions(+), 61 deletions(-) create mode 100644 crates/rpc/rpc-types-compat/src/transaction/signature.rs diff --git a/crates/rpc/rpc-types-compat/src/transaction/mod.rs b/crates/rpc/rpc-types-compat/src/transaction/mod.rs index 02026b04fb..9cda539ef1 100644 --- a/crates/rpc/rpc-types-compat/src/transaction/mod.rs +++ b/crates/rpc/rpc-types-compat/src/transaction/mod.rs @@ -1,12 +1,12 @@ //! Compatibility functions for rpc `Transaction` type. - +mod signature; use reth_primitives::{ AccessListItem, BlockNumber, Transaction as PrimitiveTransaction, TransactionKind as PrimitiveTransactionKind, TransactionSignedEcRecovered, TxType, H256, U128, U256, U64, }; -use reth_rpc_types::{Signature, Transaction}; - +use reth_rpc_types::Transaction; +use signature::from_primitive_signature; /// Create a new rpc transaction result for a mined transaction, using the given block hash, /// number, and tx index fields to populate the corresponding fields in the rpc result. /// @@ -96,11 +96,8 @@ fn fill( ), }; - let signature = Signature::from_primitive_signature( - *signed_tx.signature(), - signed_tx.tx_type(), - signed_tx.chain_id(), - ); + let signature = + from_primitive_signature(*signed_tx.signature(), signed_tx.tx_type(), signed_tx.chain_id()); Transaction { hash: signed_tx.hash(), diff --git a/crates/rpc/rpc-types-compat/src/transaction/signature.rs b/crates/rpc/rpc-types-compat/src/transaction/signature.rs new file mode 100644 index 0000000000..0e7fe5f587 --- /dev/null +++ b/crates/rpc/rpc-types-compat/src/transaction/signature.rs @@ -0,0 +1,51 @@ +use reth_primitives::{Signature as PrimitiveSignature, TxType, U256}; +use reth_rpc_types::{Parity, Signature}; + +/// Creates a new rpc signature from a legacy [primitive +/// signature](reth_primitives::Signature), using the give chain id to compute the signature's +/// recovery id. +/// +/// If the chain id is `Some`, the recovery id is computed according to [EIP-155](https://eips.ethereum.org/EIPS/eip-155). +pub(crate) fn from_legacy_primitive_signature( + signature: PrimitiveSignature, + chain_id: Option, +) -> Signature { + Signature { + r: signature.r, + s: signature.s, + v: U256::from(signature.v(chain_id)), + y_parity: None, + } +} + +/// Creates a new rpc signature from a non-legacy [primitive +/// signature](reth_primitives::Signature). This sets the `v` value to `0` or `1` depending on +/// the signature's `odd_y_parity`. +pub(crate) fn from_typed_primitive_signature(signature: PrimitiveSignature) -> Signature { + Signature { + r: signature.r, + s: signature.s, + v: U256::from(signature.odd_y_parity as u8), + y_parity: Some(Parity(signature.odd_y_parity)), + } +} + +/// Creates a new rpc signature from a legacy [primitive +/// signature](reth_primitives::Signature). +/// +/// The tx type is used to determine whether or not to use the `chain_id` to compute the +/// signature's recovery id. +/// +/// If the transaction is a legacy transaction, it will use the `chain_id` to compute the +/// signature's recovery id. If the transaction is a typed transaction, it will set the `v` +/// value to `0` or `1` depending on the signature's `odd_y_parity`. +pub(crate) fn from_primitive_signature( + signature: PrimitiveSignature, + tx_type: TxType, + chain_id: Option, +) -> Signature { + match tx_type { + TxType::Legacy => from_legacy_primitive_signature(signature, chain_id), + _ => from_typed_primitive_signature(signature), + } +} diff --git a/crates/rpc/rpc-types/src/eth/transaction/mod.rs b/crates/rpc/rpc-types/src/eth/transaction/mod.rs index 1e8b09aecb..6b6601af62 100644 --- a/crates/rpc/rpc-types/src/eth/transaction/mod.rs +++ b/crates/rpc/rpc-types/src/eth/transaction/mod.rs @@ -3,7 +3,7 @@ pub use receipt::TransactionReceipt; pub use request::TransactionRequest; use reth_primitives::{AccessListItem, Address, Bytes, H256, U128, U256, U64}; use serde::{Deserialize, Serialize}; -pub use signature::Signature; +pub use signature::{Parity, Signature}; pub use typed::*; mod common; diff --git a/crates/rpc/rpc-types/src/eth/transaction/signature.rs b/crates/rpc/rpc-types/src/eth/transaction/signature.rs index 3c31a12600..78a8f98733 100644 --- a/crates/rpc/rpc-types/src/eth/transaction/signature.rs +++ b/crates/rpc/rpc-types/src/eth/transaction/signature.rs @@ -1,5 +1,5 @@ //! Signature related RPC values -use reth_primitives::{Signature as PrimitiveSignature, TxType, U256}; +use reth_primitives::U256; use serde::{Deserialize, Serialize}; /// Container type for all signature fields in RPC @@ -23,57 +23,6 @@ pub struct Signature { pub y_parity: Option, } -impl Signature { - /// Creates a new rpc signature from a legacy [primitive - /// signature](reth_primitives::Signature), using the give chain id to compute the signature's - /// recovery id. - /// - /// If the chain id is `Some`, the recovery id is computed according to [EIP-155](https://eips.ethereum.org/EIPS/eip-155). - pub(crate) fn from_legacy_primitive_signature( - signature: PrimitiveSignature, - chain_id: Option, - ) -> Self { - Self { - r: signature.r, - s: signature.s, - v: U256::from(signature.v(chain_id)), - y_parity: None, - } - } - - /// Creates a new rpc signature from a non-legacy [primitive - /// signature](reth_primitives::Signature). This sets the `v` value to `0` or `1` depending on - /// the signature's `odd_y_parity`. - pub(crate) fn from_typed_primitive_signature(signature: PrimitiveSignature) -> Self { - Self { - r: signature.r, - s: signature.s, - v: U256::from(signature.odd_y_parity as u8), - y_parity: Some(Parity(signature.odd_y_parity)), - } - } - - /// Creates a new rpc signature from a legacy [primitive - /// signature](reth_primitives::Signature). - /// - /// The tx type is used to determine whether or not to use the `chain_id` to compute the - /// signature's recovery id. - /// - /// If the transaction is a legacy transaction, it will use the `chain_id` to compute the - /// signature's recovery id. If the transaction is a typed transaction, it will set the `v` - /// value to `0` or `1` depending on the signature's `odd_y_parity`. - pub fn from_primitive_signature( - signature: PrimitiveSignature, - tx_type: TxType, - chain_id: Option, - ) -> Self { - match tx_type { - TxType::Legacy => Signature::from_legacy_primitive_signature(signature, chain_id), - _ => Signature::from_typed_primitive_signature(signature), - } - } -} - /// Type that represents the signature parity byte, meant for use in RPC. /// /// This will be serialized as "0x0" if false, and "0x1" if true.