forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test ec_recover using SDK + fuel-crypto (FuelLabs#3235)
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
1 parent
11496e6
commit 37a3687
Showing
9 changed files
with
136 additions
and
99 deletions.
There are no files selected for viewing
22 changes: 0 additions & 22 deletions
22
test/src/e2e_vm_tests/test_programs/should_pass/stdlib/ec_recover_test/json_abi_oracle.json
This file was deleted.
Oops, something went wrong.
69 changes: 0 additions & 69 deletions
69
test/src/e2e_vm_tests/test_programs/should_pass/stdlib/ec_recover_test/src/main.sw
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
test/src/e2e_vm_tests/test_programs/should_pass/stdlib/ec_recover_test/test.toml
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
out | ||
target |
6 changes: 3 additions & 3 deletions
6
...uld_pass/stdlib/ec_recover_test/Forc.lock → ...arness/test_projects/ec_recover/Forc.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters