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

fix: make into_transaction fallible #4710

Merged
merged 3 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
41 changes: 22 additions & 19 deletions crates/rpc/rpc-types/src/eth/transaction/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,45 @@ pub enum TypedTransactionRequest {
}

impl TypedTransactionRequest {
/// coverts a typed transaction request into a primitive transaction
pub fn into_transaction(self) -> Transaction {
match self {
/// Converts a typed transaction request into a primitive transaction.
///
/// Returns `None` if any of the following are true:
/// - `nonce` is greater than [`u64::MAX`]
Copy link
Collaborator

Choose a reason for hiding this comment

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

just checked geth reference and nonce on rpc methods is U64,

I think we can also change this in our types?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Member Author

@DaniPopes DaniPopes Sep 21, 2023

Choose a reason for hiding this comment

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

Is there a reason we're using ruint for these txrequest anyway? I don't see any serde derives or whatever

Copy link
Collaborator

Choose a reason for hiding this comment

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

there are two steps here

TransactionRequest -> TypedTransactionRequest -> Transaction

arguably this could also be one step, but having a typed request is still useful

/// - `gas_limit` is greater than [`u64::MAX`]
/// - `value` is greater than [`u128::MAX`]
pub fn into_transaction(self) -> Option<Transaction> {
Some(match self {
TypedTransactionRequest::Legacy(tx) => Transaction::Legacy(TxLegacy {
chain_id: tx.chain_id,
nonce: u64::from_be_bytes(tx.nonce.to_be_bytes()),
gas_price: u128::from_be_bytes(tx.gas_price.to_be_bytes()),
gas_limit: u64::from_be_bytes(tx.gas_limit.to_be_bytes()),
nonce: tx.nonce.try_into().ok()?,
gas_price: tx.gas_price.to(),
gas_limit: tx.gas_limit.try_into().ok()?,
to: tx.kind.into(),
value: u128::from_be_bytes(tx.value.to_be_bytes()),
value: tx.value.try_into().ok()?,
input: tx.input,
}),
TypedTransactionRequest::EIP2930(tx) => Transaction::Eip2930(TxEip2930 {
chain_id: tx.chain_id,
nonce: u64::from_be_bytes(tx.nonce.to_be_bytes()),
gas_price: u128::from_be_bytes(tx.gas_price.to_be_bytes()),
gas_limit: u64::from_be_bytes(tx.gas_limit.to_be_bytes()),
nonce: tx.nonce.try_into().ok()?,
gas_price: tx.gas_price.to(),
gas_limit: tx.gas_limit.try_into().ok()?,
to: tx.kind.into(),
value: u128::from_be_bytes(tx.value.to_be_bytes()),
value: tx.value.try_into().ok()?,
input: tx.input,
access_list: tx.access_list,
}),
TypedTransactionRequest::EIP1559(tx) => Transaction::Eip1559(TxEip1559 {
chain_id: tx.chain_id,
nonce: u64::from_be_bytes(tx.nonce.to_be_bytes()),
max_fee_per_gas: u128::from_be_bytes(tx.max_fee_per_gas.to_be_bytes()),
gas_limit: u64::from_be_bytes(tx.gas_limit.to_be_bytes()),
nonce: tx.nonce.try_into().ok()?,
max_fee_per_gas: tx.max_fee_per_gas.to(),
gas_limit: tx.gas_limit.try_into().ok()?,
to: tx.kind.into(),
value: u128::from_be_bytes(tx.value.to_be_bytes()),
value: tx.value.try_into().ok()?,
input: tx.input,
access_list: tx.access_list,
max_priority_fee_per_gas: u128::from_be_bytes(
tx.max_priority_fee_per_gas.to_be_bytes(),
),
max_priority_fee_per_gas: tx.max_priority_fee_per_gas.to(),
}),
}
})
}
}

Expand Down
3 changes: 2 additions & 1 deletion crates/rpc/rpc/src/eth/api/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ impl<Provider, Pool, Network> EthApi<Provider, Pool, Network> {

pub(crate) async fn sign_typed_data(&self, data: Value, account: Address) -> EthResult<Bytes> {
let signer = self.find_signer(&account)?;
let data = serde_json::from_value::<TypedData>(data).map_err(|_| SignError::TypedData)?;
let data =
serde_json::from_value::<TypedData>(data).map_err(|_| SignError::InvalidTypedData)?;
let signature = signer.sign_typed_data(account, &data)?;
let bytes = hex::encode(signature.to_bytes()).as_bytes().into();
Ok(bytes)
Expand Down
7 changes: 5 additions & 2 deletions crates/rpc/rpc/src/eth/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,11 @@ pub enum SignError {
NoAccount,
/// TypedData has invalid format.
#[error("Given typed data is not valid")]
TypedData,
/// No chainid
InvalidTypedData,
/// Invalid transaction request in `sign_transaction`.
#[error("Invalid transaction request")]
InvalidTransactionRequest,
/// No chain ID was given.
#[error("No chainid")]
NoChainId,
}
Expand Down
7 changes: 5 additions & 2 deletions crates/rpc/rpc/src/eth/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ impl DevSigner {
fn get_key(&self, account: Address) -> Result<&SecretKey> {
self.accounts.get(&account).ok_or(SignError::NoAccount)
}

fn sign_hash(&self, hash: H256, account: Address) -> Result<Signature> {
let secret = self.get_key(account)?;
let signature = sign_message(H256::from_slice(secret.as_ref()), hash);
signature.map_err(|_| SignError::CouldNotSign)
}
}

#[async_trait::async_trait]
impl EthSigner for DevSigner {
fn accounts(&self) -> Vec<Address> {
Expand All @@ -77,15 +79,16 @@ impl EthSigner for DevSigner {
address: &Address,
) -> Result<TransactionSigned> {
// convert to primitive transaction
let transaction = request.into_transaction();
let transaction = request.into_transaction().ok_or(SignError::InvalidTransactionRequest)?;
let tx_signature_hash = transaction.signature_hash();
let signature = self.sign_hash(tx_signature_hash, *address)?;

Ok(TransactionSigned::from_transaction_and_signature(transaction, signature))
}

fn sign_typed_data(&self, address: Address, payload: &TypedData) -> Result<Signature> {
let encoded: H256 = payload.encode_eip712().map_err(|_| SignError::TypedData)?.into();
let encoded: H256 =
payload.encode_eip712().map_err(|_| SignError::InvalidTypedData)?.into();
self.sign_hash(encoded, address)
}
}
Expand Down
Loading