Skip to content

Commit

Permalink
Merge pull request #180 from bitfinity-network/chore/accept-both-inpu…
Browse files Browse the repository at this point in the history
…t-and-data

chore:  accept both input and data for estimating gas
  • Loading branch information
itsyaasir authored Jul 8, 2024
2 parents fad7e9d + d7c7735 commit 44f9a98
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 23 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
75 changes: 73 additions & 2 deletions src/did/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<H160>,
Expand All @@ -27,7 +27,8 @@ pub struct EstimateGasRequest {
pub max_priority_fee_per_gas: Option<U256>,
pub gas: Option<U256>,
pub value: Option<U256>,
pub data: Option<Bytes>,
#[serde(default, alias = "data", skip_serializing_if = "Option::is_none")]
pub input: Option<Bytes>,
pub nonce: Option<U256>,
#[serde(rename = "chainId", default, skip_serializing_if = "Option::is_none")]
pub chain_id: Option<U256>,
Expand All @@ -38,3 +39,73 @@ pub struct EstimateGasRequest {
)]
pub access_list: Option<AccessList>,
}

#[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);
}
}
21 changes: 5 additions & 16 deletions src/eth-signer/src/ic_sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 2 additions & 4 deletions src/eth-signer/src/sign_strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,17 +272,15 @@ mod ic_sign {

/// Lazily compute the public key
async fn get_or_compute_pubkey(&self) -> Result<Vec<u8>, 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());
}

let new_pubkey = IcSigner
.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)
}
Expand Down

0 comments on commit 44f9a98

Please sign in to comment.