Skip to content

Commit

Permalink
Test ec_recover using SDK + fuel-crypto (FuelLabs#3235)
Browse files Browse the repository at this point in the history
This will allow us to deprecate & remove the `test-sig-gen-util`.
As a related follow-up, I'll add a similar new SDK test for the
`ec_recover_EvmAddress` functionality which is also currently using the
`test-sig-gen-util`.
I'll also delete the old `test-sig-gen-util` in the second PR.

Closes FuelLabs#3179

Co-authored-by: Mohammad Fawaz <[email protected]>
  • Loading branch information
nfurfaro and mohammadfawaz authored Nov 2, 2022
1 parent 11496e6 commit 37a3687
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 99 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 2 additions & 0 deletions test/src/sdk-harness/test_projects/ec_recover/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[[package]]
name = 'core'
source = 'path+from-root-219CAA2B0E1853AB'
source = 'path+from-root-773F5D8CD6B5DB48'

[[package]]
name = 'ec_recover_test'
name = 'ec_recover'
source = 'root'
dependencies = ['std']

[[package]]
name = 'std'
source = 'path+from-root-219CAA2B0E1853AB'
source = 'path+from-root-773F5D8CD6B5DB48'
dependencies = ['core']
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "ec_recover_test"
name = "ec_recover"

[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
std = { path = "../../../../../sway-lib-std" }
109 changes: 109 additions & 0 deletions test/src/sdk-harness/test_projects/ec_recover/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use fuel_types::Bytes64;
use fuels::{
prelude::*,
signers::fuel_crypto::{Message, PublicKey, SecretKey, Signature},
tx::Bytes32,
};
use rand::{rngs::StdRng, Rng, SeedableRng};

abigen!(
EcRecoverContract,
"test_projects/ec_recover/out/debug/ec_recover-abi.json"
);

async fn setup_env() -> Result<
(
EcRecoverContract,
SecretKey,
PublicKey,
WalletUnlocked,
Message,
Bytes64,
),
Error,
> {
let mut rng = StdRng::seed_from_u64(1000);
let msg_bytes: Bytes32 = rng.gen();
let private_key = SecretKey::random(&mut rng);
let public_key = PublicKey::from(&private_key);
let msg = unsafe { Message::from_bytes_unchecked(*msg_bytes) };
let sig = Signature::sign(&private_key, &msg);
let sig_bytes: Bytes64 = Bytes64::try_from(sig).unwrap();
let mut wallet = WalletUnlocked::new_from_private_key(private_key, None);

let num_assets = 1;
let coins_per_asset = 10;
let amount_per_coin = 15;
let (coins, _asset_ids) = setup_multiple_assets_coins(
wallet.address(),
num_assets,
coins_per_asset,
amount_per_coin,
);
let (provider, _socket_addr) = setup_test_provider(coins.clone(), vec![], None).await;
wallet.set_provider(provider);

let contract_id = Contract::deploy(
"test_projects/ec_recover/out/debug/ec_recover.bin",
&wallet,
TxParameters::default(),
StorageConfiguration::default(),
)
.await?;

let contract_instance = EcRecoverContract::new(contract_id, wallet.clone());

Ok((
contract_instance,
private_key,
public_key,
wallet,
msg,
sig_bytes,
))
}

#[tokio::test]
async fn can_recover_public_key() {
let (contract, _secret, public_key, _wallet, msg, sig_bytes) = setup_env().await.unwrap();
let sig_r = &sig_bytes[..32];
let sig_v_s = &sig_bytes[32..];
let response = contract
.methods()
.recover_pub_key(
Bits256(sig_r.try_into().unwrap()),
Bits256(sig_v_s.try_into().unwrap()),
Bits256(msg.into()),
)
.call()
.await
.unwrap();

let first = response.value.0;
let second = response.value.1;
let arrays: [[u8; 32]; 2] = [first.0, second.0];
let joined: Vec<u8> = arrays.into_iter().flat_map(|s| s.into_iter()).collect();
let joined_array: [u8; 64] = joined.try_into().unwrap();
let pubkey = Bytes64::new(joined_array);

assert_eq!(pubkey, Bytes64::new(*public_key));
}

#[tokio::test]
async fn can_recover_address() {
let (contract, _secret, _public_key, wallet, msg, sig_bytes) = setup_env().await.unwrap();
let sig_r = &sig_bytes[..32];
let sig_v_s = &sig_bytes[32..];
let response = contract
.methods()
.recover_address(
Bits256(sig_r.try_into().unwrap()),
Bits256(sig_v_s.try_into().unwrap()),
Bits256(*msg),
)
.call()
.await
.unwrap();

assert_eq!(Bech32Address::from(response.value), *wallet.address());
}
19 changes: 19 additions & 0 deletions test/src/sdk-harness/test_projects/ec_recover/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
contract;

use std::{b512::B512, ecr::{ec_recover, ec_recover_address}};

abi EcRecover {
fn recover_address(sig_r: b256, sig_v_s: b256, hash: b256) -> Address;
fn recover_pub_key(sig_r: b256, sig_v_s: b256, hash: b256) -> (b256, b256);
}

impl EcRecover for Contract {
fn recover_address(sig_r: b256, sig_v_s: b256, hash: b256) -> Address {
let sig = B512::from(sig_r, sig_v_s);
ec_recover_address(sig, hash).unwrap()
}
fn recover_pub_key(sig_r: b256, sig_v_s: b256, hash: b256) -> (b256, b256) {
let sig = B512::from(sig_r, sig_v_s);
ec_recover(sig, hash).unwrap().into()
}
}
1 change: 1 addition & 0 deletions test/src/sdk-harness/test_projects/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod block;
mod call_frames;
mod context;
mod contract_bytecode;
mod ec_recover;
mod ec_recover_and_match_predicate;
mod evm;
mod exponentiation;
Expand Down

0 comments on commit 37a3687

Please sign in to comment.