diff --git a/Cargo.toml b/Cargo.toml index f2f55aa7..745862d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,8 +26,10 @@ license = "MIT" repository = "https://github.com/bitfinity-network/bitfinity-evm-sdk" version = "0.27.0" + + [workspace.dependencies] -alloy-primitives = { version = "0.7", default-feures = false } +alloy-primitives = { version = "0.7", default-features = false } anyhow = "1.0" async-trait = "0.1" bincode = "1.3" diff --git a/src/did/src/gas.rs b/src/did/src/gas.rs index dfceaf2e..05c9f74d 100644 --- a/src/did/src/gas.rs +++ b/src/did/src/gas.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use crate::transaction::AccessList; use crate::{Bytes, H160, U256}; -#[derive(Debug, Clone, Default, Eq, PartialEq, Serialize, CandidType, Deserialize)] +#[derive(Debug, Clone, Default, Eq, PartialEq, CandidType, Deserialize, Serialize)] /// The `estimate_gas` method parameters pub struct EstimateGasRequest { pub from: Option, @@ -27,7 +27,8 @@ pub struct EstimateGasRequest { pub max_priority_fee_per_gas: Option, pub gas: Option, pub value: Option, - pub data: Option, + #[serde(default, alias = "data", skip_serializing_if = "Option::is_none")] + pub input: Option, pub nonce: Option, #[serde(rename = "chainId", default, skip_serializing_if = "Option::is_none")] pub chain_id: Option, @@ -38,3 +39,73 @@ pub struct EstimateGasRequest { )] pub access_list: Option, } + +#[cfg(test)] +mod tests { + use super::*; + use crate::test_utils::{test_candid_roundtrip, test_json_roundtrip}; + + #[test] + fn test_serde_roundtrip_with_data_field() { + let json = r#"{ + "from": "0x1234567890123456789012345678901234567890", + "to": "0x0987654321098765432109876543210987654321", + "gasPrice": "0x1234", + "value": "0x5678", + "data": "0xabcdef", + "nonce": "0x9", + "chainId": "0x1" + }"#; + + let request: EstimateGasRequest = serde_json::from_str(json).unwrap(); + test_json_roundtrip(&request); + } + + #[test] + fn test_serde_roundtrip_with_input_field() { + let json = r#"{ + "from": "0x1234567890123456789012345678901234567890", + "to": "0x0987654321098765432109876543210987654321", + "gasPrice": "0x1234", + "value": "0x5678", + "input": "0xabcdef", + "nonce": "0x9", + "chainId": "0x1" + }"#; + + let request: EstimateGasRequest = serde_json::from_str(json).unwrap(); + test_json_roundtrip(&request); + } + + #[test] + fn test_candid_roundtrip_with_data_field() { + let json = r#"{ + "from": "0x1234567890123456789012345678901234567890", + "to": "0x0987654321098765432109876543210987654321", + "gasPrice": "0x1234", + "value": "0x5678", + "data": "0xabcdef", + "nonce": "0x9", + "chainId": "0x1" + }"#; + + let request: EstimateGasRequest = serde_json::from_str(json).unwrap(); + test_candid_roundtrip(&request); + } + + #[test] + fn test_candid_roundtrip_with_input_field() { + let json = r#"{ + "from": "0x1234567890123456789012345678901234567890", + "to": "0x0987654321098765432109876543210987654321", + "gasPrice": "0x1234", + "value": "0x5678", + "input": "0xabcdef", + "nonce": "0x9", + "chainId": "0x1" + }"#; + + let request: EstimateGasRequest = serde_json::from_str(json).unwrap(); + test_candid_roundtrip(&request); + } +} diff --git a/src/eth-signer/src/ic_sign.rs b/src/eth-signer/src/ic_sign.rs index 1b4f5d55..d82e5797 100644 --- a/src/eth-signer/src/ic_sign.rs +++ b/src/eth-signer/src/ic_sign.rs @@ -3,11 +3,9 @@ use std::fmt; use candid::{CandidType, Principal}; use ethereum_types::U256; use ethers_core::k256::ecdsa::{self, RecoveryId, VerifyingKey}; -use ethers_core::k256::elliptic_curve::sec1::ToEncodedPoint; -use ethers_core::k256::PublicKey; use ethers_core::types::transaction::eip2718::TypedTransaction; use ethers_core::types::{Signature, SignatureError, H160}; -use ethers_core::utils::{self, public_key_to_address}; +use ethers_core::utils::public_key_to_address; use ic_canister::virtual_canister_call; use ic_exports::ic_cdk::api::call::RejectionCode; use ic_exports::ic_cdk::api::management_canister::ecdsa::{ @@ -97,21 +95,12 @@ impl IcSigner { let pub_key = VerifyingKey::from_sec1_bytes(pubkey).map_err(|_| IcSignerError::InvalidPublicKey)?; - let sec_signature = ecdsa::Signature::from_slice(&signature).map_err(|e| { - IcSignerError::Internal(format!( - "failed to parse ECDSA signature: {}", - e.to_string(), - )) + let sec_signature = ecdsa::Signature::from_slice(signature).map_err(|e| { + IcSignerError::Internal(format!("failed to parse ECDSA signature: {e}")) })?; - let recovery_id = RecoveryId::trial_recovery_from_prehash( - &pub_key, - &digest, - &sec_signature, - ) - .map_err(|e| { - IcSignerError::Internal(format!("failed to compute recovery ID: {}", e.to_string())) - })?; + let recovery_id = RecoveryId::trial_recovery_from_prehash(&pub_key, digest, &sec_signature) + .map_err(|e| IcSignerError::Internal(format!("failed to compute recovery ID: {e}")))?; Ok(recovery_id) } diff --git a/src/eth-signer/src/sign_strategy.rs b/src/eth-signer/src/sign_strategy.rs index e76b5b37..d673dd7e 100644 --- a/src/eth-signer/src/sign_strategy.rs +++ b/src/eth-signer/src/sign_strategy.rs @@ -272,9 +272,7 @@ mod ic_sign { /// Lazily compute the public key async fn get_or_compute_pubkey(&self) -> Result, TransactionSignerError> { - let mut cached_pubkey = self.cached_pubkey.borrow_mut(); - - if let Some(pubkey) = cached_pubkey.as_ref() { + if let Some(pubkey) = self.cached_pubkey.borrow().as_ref() { return Ok(pubkey.clone()); } @@ -282,7 +280,7 @@ mod ic_sign { .public_key(self.key_id.clone(), self.derivation_path.clone()) .await?; - *cached_pubkey = Some(new_pubkey.clone()); + *self.cached_pubkey.borrow_mut() = Some(new_pubkey.clone()); Ok(new_pubkey) }