Skip to content

Commit

Permalink
Call check_same_env on function parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
masonforest committed Jun 28, 2023
1 parent 7499739 commit 05e9e68
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions soroban-sdk/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
//! Crypto contains functions for cryptographic functions.
use crate::{
env::internal, unwrap::UnwrapInfallible, Bytes, BytesN, Env, IntoVal,
TryIntoVal, Val, Vec,
env::internal, unwrap::UnwrapInfallible, Bytes, BytesN, Env, EnvBase, IntoVal, TryIntoVal, Val,
Vec,
};

/// Crypto provides access to cryptographic functions.
pub struct Crypto {
env: Env,
}


impl Crypto {
pub(crate) fn new(env: &Env) -> Crypto {
Crypto { env: env.clone() }
Expand All @@ -22,13 +21,15 @@ impl Crypto {
/// Returns the SHA-256 hash of the data.
pub fn sha256(&self, data: &Bytes) -> BytesN<32> {
let env = self.env();
env.check_same_env(data.env());
let bin = internal::Env::compute_hash_sha256(env, data.into()).unwrap_infallible();
unsafe { BytesN::unchecked_new(env.clone(), bin) }
}

// Reseeds the pseudorandom number generator (PRNG) with the provided `seed` value.
pub fn prng_reseed(&self, seed: &Bytes) {
let env = self.env();
env.check_same_env(seed.env());
internal::Env::prng_reseed(env, seed.into()).unwrap_infallible();
}

Expand All @@ -46,7 +47,10 @@ impl Crypto {
V: IntoVal<Env, Vec<Val>>,
{
let env = self.env();
internal::Env::prng_vec_shuffle(env, v.into_val(env).to_object())
let v_val = v.into_val(env);
env.check_same_env(v_val.env());

internal::Env::prng_vec_shuffle(env, v_val.to_object())
.unwrap_infallible()
.try_into_val(env)
.unwrap_infallible()
Expand All @@ -61,6 +65,9 @@ impl Crypto {
/// If the signature verification fails.
pub fn ed25519_verify(&self, public_key: &BytesN<32>, message: &Bytes, signature: &BytesN<64>) {
let env = self.env();
env.check_same_env(public_key.env());
env.check_same_env(message.env());
env.check_same_env(signature.env());
let _ = internal::Env::verify_sig_ed25519(
env,
public_key.to_object(),
Expand Down

0 comments on commit 05e9e68

Please sign in to comment.