Skip to content

Commit

Permalink
Merge branch 'main' into rust182prep
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch authored Sep 29, 2024
2 parents 74e2da3 + 01f27c1 commit 459a9ae
Show file tree
Hide file tree
Showing 13 changed files with 955 additions and 329 deletions.
524 changes: 245 additions & 279 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion soroban-sdk-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ stellar-xdr = { workspace = true, features = ["curr", "std"] }
syn = {version="2.0",features=["full"]}
quote = "1.0"
proc-macro2 = "1.0"
itertools = "0.10.0"
itertools = "0.10.5"
darling = "0.20.0"
sha2 = "0.10.7"

Expand Down
96 changes: 96 additions & 0 deletions soroban-sdk/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,102 @@ macro_rules! bytesn {
};
}

#[macro_export]
macro_rules! impl_bytesn_repr {
($elem: ident, $size: literal) => {
impl $elem {
pub fn from_bytes(bytes: BytesN<$size>) -> Self {
Self(bytes)
}

pub fn to_bytes(&self) -> BytesN<$size> {
self.0.clone()
}

pub fn as_bytes(&self) -> &BytesN<$size> {
&self.0
}

pub fn to_array(&self) -> [u8; $size] {
self.0.to_array()
}

pub fn from_array(&self, env: &Env, array: &[u8; $size]) -> Self {
Self(<BytesN<$size>>::from_array(env, array))
}

pub fn as_val(&self) -> &Val {
self.0.as_val()
}

pub fn to_val(&self) -> Val {
self.0.to_val()
}

pub fn as_object(&self) -> &BytesObject {
self.0.as_object()
}

pub fn to_object(&self) -> BytesObject {
self.0.to_object()
}
}

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

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

fn try_from_val(env: &Env, val: &Val) -> Result<Self, Self::Error> {
let bytes = <BytesN<$size>>::try_from_val(env, val)?;
Ok($elem(bytes))
}
}

impl IntoVal<Env, BytesN<$size>> for $elem {
fn into_val(&self, _e: &Env) -> BytesN<$size> {
self.0.clone()
}
}

impl From<$elem> for Bytes {
fn from(v: $elem) -> Self {
v.0.into()
}
}

impl From<$elem> for BytesN<$size> {
fn from(v: $elem) -> Self {
v.0
}
}

impl Into<[u8; $size]> for $elem {
fn into(self) -> [u8; $size] {
self.0.into()
}
}

impl Eq for $elem {}

impl PartialEq for $elem {
fn eq(&self, other: &Self) -> bool {
self.0.partial_cmp(other.as_bytes()) == Some(Ordering::Equal)
}
}

impl Debug for $elem {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}({:?})", stringify!($elem), self.to_array())
}
}
};
}

/// Bytes is a contiguous growable array type containing `u8`s.
///
/// The array is stored in the Host and available to the Guest through the
Expand Down
32 changes: 32 additions & 0 deletions soroban-sdk/src/constructor_args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::{Env, IntoVal, Val, Vec};

pub trait ConstructorArgs: IntoVal<Env, Vec<Val>> {}

impl<T> ConstructorArgs for Vec<T> {}

macro_rules! impl_constructor_args_for_tuple {
( $($typ:ident $idx:tt)* ) => {
impl<$($typ),*> ConstructorArgs for ($($typ,)*)
where
$($typ: IntoVal<Env, Val>),*
{
}
};
}

// 0 topics
impl ConstructorArgs for () {}
// 1-13 topics
impl_constructor_args_for_tuple! { T0 0 }
impl_constructor_args_for_tuple! { T0 0 T1 1 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 10 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 10 T11 11 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 10 T11 11 T12 12 }
11 changes: 9 additions & 2 deletions soroban-sdk/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//! Crypto contains functions for cryptographic functions.

use crate::{
env::internal, env::internal::BytesObject, unwrap::UnwrapInfallible, Bytes, BytesN,
ConversionError, Env, IntoVal, TryFromVal, Val,
env::internal::{self, BytesObject},
unwrap::UnwrapInfallible,
Bytes, BytesN, ConversionError, Env, IntoVal, TryFromVal, Val,
};

pub mod bls12_381;
/// A BytesN<N> generated by a cryptographic hash function.
///
/// The `Hash<N>` type contains a `BytesN<N>` and can only be constructed in
Expand Down Expand Up @@ -174,6 +176,11 @@ impl Crypto {
let env = self.env();
CryptoHazmat::new(env).secp256r1_verify(public_key, &message_digest.0, signature)
}

/// Get a [Bls12_381] for accessing the bls12-381 functions.
pub fn bls12_381(&self) -> bls12_381::Bls12_381 {
bls12_381::Bls12_381::new(self.env())
}
}

/// # ⚠️ Hazardous Materials
Expand Down
Loading

0 comments on commit 459a9ae

Please sign in to comment.