Skip to content

Commit

Permalink
Clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
masonforest committed Sep 18, 2023
1 parent f150698 commit a567261
Showing 1 changed file with 6 additions and 32 deletions.
38 changes: 6 additions & 32 deletions soroban-sdk/src/tests/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,21 @@ use crate::{self as soroban_sdk};
use crate::{bytes, bytesn, env::internal::U32Val, Bytes, BytesN, Env, IntoVal, Val};
use soroban_sdk::{contract, contractimpl};

#[contract]
pub struct TestCryptoContract;

#[contractimpl]
impl TestCryptoContract {
pub fn keccak256(env: Env, bytes: Bytes) -> BytesN<32> {
env.crypto().keccak256(&bytes)
}

pub fn recover_key_ecdsa_secp256k1(
env: Env,
message: Bytes,
signature: BytesN<64>,
recovery_id: U32Val,
) -> Bytes {
env.crypto()
.recover_key_ecdsa_secp256k1(&message, &signature, recovery_id)
}
}

#[test]
fn test_keccak256() {
let env = Env::default();
let contract_id = env.register_contract(None, TestCryptoContract);
let client = TestCryptoContractClient::new(&env, &contract_id);

let bytes = b"test vector for soroban".into_val(&env);

assert_eq!(
client.keccak256(&bytes),
bytesn!(
&env,
0x352fe2eaddf44eb02eb3eab1f8d6ff4ba426df4f1734b1e3f210d621ee8853d9
)
let expect = bytesn!(
&env,
0x352fe2eaddf44eb02eb3eab1f8d6ff4ba426df4f1734b1e3f210d621ee8853d9
);
assert_eq!(env.crypto().keccak256(&bytes), expect);
}

#[test]
fn test_recover_key_ecdsa_secp256k1() {
let env = Env::default();
let contract_id = env.register_contract(None, TestCryptoContract);
let client = TestCryptoContractClient::new(&env, &contract_id);

// From ethereum: https://github.com/ethereum/go-ethereum/blob/master/crypto/secp256k1/secp256_test.go

Expand All @@ -63,7 +36,8 @@ fn test_recover_key_ecdsa_secp256k1() {
);
let recovery_id = Val::from_u32(1);
assert_eq!(
client.recover_key_ecdsa_secp256k1(&message_digest, &signature, &recovery_id),
env.crypto()
.recover_key_ecdsa_secp256k1(&message_digest, &signature, recovery_id),
public_key
);
}

0 comments on commit a567261

Please sign in to comment.