diff --git a/soroban-sdk/src/auth.rs b/soroban-sdk/src/auth.rs index 7cac8349..4d2091d1 100644 --- a/soroban-sdk/src/auth.rs +++ b/soroban-sdk/src/auth.rs @@ -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. /// @@ -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, ) -> Result<(), Self::Error>; diff --git a/soroban-sdk/src/crypto.rs b/soroban-sdk/src/crypto.rs index ee8b3dce..f94e5582 100644 --- a/soroban-sdk/src/crypto.rs +++ b/soroban-sdk/src/crypto.rs @@ -1,32 +1,45 @@ //! Crypto contains functions for cryptographic functions. -use crate::{env::internal, unwrap::UnwrapInfallible, Bytes, BytesN, Env, IntoVal, Val}; -pub struct Hash(BytesN); +use crate::{env::internal, unwrap::UnwrapInfallible, Bytes, BytesN, Env, IntoVal, Val, ConversionError, TryFromVal}; -impl IntoVal for Hash { +/// 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 for Digest { fn into_val(&self, e: &Env) -> Val { self.0.into_val(e) } } -impl IntoVal> for Hash { - fn into_val(&self, _e: &Env) -> BytesN { +impl IntoVal> for Digest { + fn into_val(&self, _e: &Env) -> BytesN<32> { self.0.clone() } } -impl Into> for Hash { - fn into(self) -> BytesN { +impl Into> for Digest { + fn into(self) -> BytesN<32> { self.0 } } -impl Into<[u8; N]> for Hash { - fn into(self) -> [u8; N] { +impl Into<[u8; 32]> for Digest { + fn into(self) -> [u8; 32] { self.0.into() } } +impl TryFromVal for Digest { + type Error = ConversionError; + + fn try_from_val(env: &Env, v: &Val) -> Result { + Ok(Digest(BytesN::<32>::try_from_val(env, v)?)) + } +} + /// Crypto provides access to cryptographic functions. pub struct Crypto { env: Env, @@ -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. @@ -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> { @@ -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();