Skip to content

Commit

Permalink
chore: add candid/serde der and ser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
itsyaasir committed Jul 8, 2024
1 parent 5da52a5 commit d7c7735
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 22 deletions.
74 changes: 72 additions & 2 deletions src/did/src/gas.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use candid::CandidType;
use serde::Deserialize;
use serde::{Deserialize, Serialize};

use crate::transaction::AccessList;
use crate::{Bytes, H160, U256};

#[derive(Debug, Clone, Default, Eq, PartialEq, CandidType, Deserialize)]
#[derive(Debug, Clone, Default, Eq, PartialEq, CandidType, Deserialize, Serialize)]
/// The `estimate_gas` method parameters
pub struct EstimateGasRequest {
pub from: Option<H160>,
Expand Down Expand Up @@ -39,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 d7c7735

Please sign in to comment.