Skip to content

Commit

Permalink
Remove generic size, fix to 32
Browse files Browse the repository at this point in the history
  • Loading branch information
jayz22 committed Apr 12, 2024
1 parent cd73396 commit 0d1783b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
4 changes: 2 additions & 2 deletions soroban-sdk/src/auth.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Auth contains types for building custom account contracts.

use crate::{contracttype, crypto::Hash, Address, BytesN, Env, Error, Symbol, Val, Vec};
use crate::{contracttype, crypto::Digest, Address, BytesN, Env, Error, Symbol, Val, Vec};

/// Context of a single authorized call performed by an address.
///
Expand Down Expand Up @@ -77,7 +77,7 @@ pub trait CustomAccountInterface {
/// Check that the signatures and auth contexts are valid.
fn __check_auth(
env: Env,
signature_payload: Hash<32>,
signature_payload: Digest,
signatures: Self::Signature,
auth_contexts: Vec<Context>,
) -> Result<(), Self::Error>;
Expand Down
43 changes: 28 additions & 15 deletions soroban-sdk/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
//! Crypto contains functions for cryptographic functions.
use crate::{env::internal, unwrap::UnwrapInfallible, Bytes, BytesN, Env, IntoVal, Val};

pub struct Hash<const N: usize>(BytesN<N>);
use crate::{env::internal, unwrap::UnwrapInfallible, Bytes, BytesN, Env, IntoVal, Val, ConversionError, TryFromVal};

impl<const N: usize> IntoVal<Env, Val> for Hash<N> {
/// A wrapper type for a 32-byte cryptographic hash.
///
/// This struct is designed to be used in contexts where a hash value generated
/// by a secure cryptographic function is required.
pub struct Digest(BytesN<32>);

impl IntoVal<Env, Val> for Digest {
fn into_val(&self, e: &Env) -> Val {
self.0.into_val(e)
}
}

impl<const N: usize> IntoVal<Env, BytesN<N>> for Hash<N> {
fn into_val(&self, _e: &Env) -> BytesN<N> {
impl IntoVal<Env, BytesN<32>> for Digest {
fn into_val(&self, _e: &Env) -> BytesN<32> {
self.0.clone()
}
}

impl<const N: usize> Into<BytesN<N>> for Hash<N> {
fn into(self) -> BytesN<N> {
impl Into<BytesN<32>> for Digest {
fn into(self) -> BytesN<32> {
self.0
}
}

impl<const N: usize> Into<[u8; N]> for Hash<N> {
fn into(self) -> [u8; N] {
impl Into<[u8; 32]> for Digest {
fn into(self) -> [u8; 32] {
self.0.into()
}
}

impl TryFromVal <Env, Val> for Digest {
type Error = ConversionError;

fn try_from_val(env: &Env, v: &Val) -> Result<Self, Self::Error> {
Ok(Digest(BytesN::<32>::try_from_val(env, v)?))
}
}

/// Crypto provides access to cryptographic functions.
pub struct Crypto {
env: Env,
Expand All @@ -42,17 +55,17 @@ impl Crypto {
}

/// Returns the SHA-256 hash of the data.
pub fn sha256(&self, data: &Bytes) -> Hash<32> {
pub fn sha256(&self, data: &Bytes) -> Digest {
let env = self.env();
let bin = internal::Env::compute_hash_sha256(env, data.into()).unwrap_infallible();
unsafe { Hash(BytesN::unchecked_new(env.clone(), bin)) }
unsafe { Digest(BytesN::unchecked_new(env.clone(), bin)) }
}

/// Returns the Keccak-256 hash of the data.
pub fn keccak256(&self, data: &Bytes) -> Hash<32> {
pub fn keccak256(&self, data: &Bytes) -> Digest {
let env = self.env();
let bin = internal::Env::compute_hash_keccak256(env, data.into()).unwrap_infallible();
unsafe { Hash(BytesN::unchecked_new(env.clone(), bin)) }
unsafe { Digest(BytesN::unchecked_new(env.clone(), bin)) }
}

/// Verifies an ed25519 signature.
Expand Down Expand Up @@ -80,7 +93,7 @@ impl Crypto {
/// for a given recovery_id byte.
pub fn secp256k1_recover(
&self,
message_digest: &Hash<32>,
message_digest: &Digest,
signature: &BytesN<64>,
recorvery_id: u32,
) -> BytesN<65> {
Expand All @@ -95,7 +108,7 @@ impl Crypto {
pub fn secp256r1_verify(
&self,
public_key: &BytesN<65>,
message_digest: &Hash<32>,
message_digest: &Digest,
signature: &BytesN<64>,
) {
let env = self.env();
Expand Down

0 comments on commit 0d1783b

Please sign in to comment.