From 8019d6a119ace1f28ff7bbefbfd3ec2912856a5b Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Fri, 23 Sep 2022 20:26:06 -0700 Subject: [PATCH 01/14] Add invoker --- Cargo.lock | 12 ++++++------ Cargo.toml | 12 ++++++------ soroban-auth/src/lib.rs | 2 +- soroban-auth/src/public_types.rs | 8 ++++---- soroban-sdk/src/bytes.rs | 5 +++++ soroban-sdk/src/env.rs | 13 +++++++++++++ soroban-sdk/src/invoker.rs | 10 ++++++++++ soroban-sdk/src/lib.rs | 2 ++ soroban-sdk/tests/contractfile_with_sha256.rs | 2 +- soroban-sdk/tests/contractimport_with_sha256.rs | 2 +- .../contractfile_with_sha256_verify_fail.stderr | 2 +- .../contractimport_with_sha256_verify_fail.stderr | 2 +- 12 files changed, 51 insertions(+), 21 deletions(-) create mode 100644 soroban-sdk/src/invoker.rs diff --git a/Cargo.lock b/Cargo.lock index 72ece0b71..6a09b8809 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -784,7 +784,7 @@ dependencies = [ [[package]] name = "soroban-env-common" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=d43b297#d43b29760d95a7fa5994e14756dbbfe6d79f8323" +source = "git+https://github.com/stellar/rs-soroban-env?rev=7452ef04#7452ef04ef204549d529fefdf88d84b2e44f5e98" dependencies = [ "soroban-env-macros", "soroban-wasmi", @@ -795,7 +795,7 @@ dependencies = [ [[package]] name = "soroban-env-guest" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=d43b297#d43b29760d95a7fa5994e14756dbbfe6d79f8323" +source = "git+https://github.com/stellar/rs-soroban-env?rev=7452ef04#7452ef04ef204549d529fefdf88d84b2e44f5e98" dependencies = [ "soroban-env-common", "static_assertions", @@ -804,7 +804,7 @@ dependencies = [ [[package]] name = "soroban-env-host" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=d43b297#d43b29760d95a7fa5994e14756dbbfe6d79f8323" +source = "git+https://github.com/stellar/rs-soroban-env?rev=7452ef04#7452ef04ef204549d529fefdf88d84b2e44f5e98" dependencies = [ "backtrace", "dyn-fmt", @@ -827,7 +827,7 @@ dependencies = [ [[package]] name = "soroban-env-macros" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=d43b297#d43b29760d95a7fa5994e14756dbbfe6d79f8323" +source = "git+https://github.com/stellar/rs-soroban-env?rev=7452ef04#7452ef04ef204549d529fefdf88d84b2e44f5e98" dependencies = [ "itertools", "proc-macro2", @@ -839,7 +839,7 @@ dependencies = [ [[package]] name = "soroban-native-sdk-macros" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=d43b297#d43b29760d95a7fa5994e14756dbbfe6d79f8323" +source = "git+https://github.com/stellar/rs-soroban-env?rev=7452ef04#7452ef04ef204549d529fefdf88d84b2e44f5e98" dependencies = [ "itertools", "proc-macro2", @@ -936,7 +936,7 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "stellar-xdr" version = "0.0.2" -source = "git+https://github.com/stellar/rs-stellar-xdr?rev=c026149#c026149f0e8a1a7c7564aac6e6dc30e4d0102c90" +source = "git+https://github.com/stellar/rs-stellar-xdr?rev=9aae807#9aae807d14f2fb5427da8f5a22195a12c1e03fd1" dependencies = [ "base64", "serde", diff --git a/Cargo.toml b/Cargo.toml index 7c5a7ab2f..c4a5a2709 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,12 +29,12 @@ soroban-sdk = { path = "soroban-sdk" } soroban-auth = { path = "soroban-auth" } soroban-spec = { path = "soroban-spec" } soroban-sdk-macros = { path = "soroban-sdk-macros" } -soroban-env-common = { git = "https://github.com/stellar/rs-soroban-env", rev = "d43b297" } -soroban-env-guest = { git = "https://github.com/stellar/rs-soroban-env", rev = "d43b297" } -soroban-env-host = { git = "https://github.com/stellar/rs-soroban-env", rev = "d43b297" } -soroban-env-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "d43b297" } -soroban-native-sdk-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "d43b297" } -stellar-xdr = { git = "https://github.com/stellar/rs-stellar-xdr", rev = "c026149" } +soroban-env-common = { git = "https://github.com/stellar/rs-soroban-env", rev = "7452ef04" } +soroban-env-guest = { git = "https://github.com/stellar/rs-soroban-env", rev = "7452ef04" } +soroban-env-host = { git = "https://github.com/stellar/rs-soroban-env", rev = "7452ef04" } +soroban-env-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "7452ef04" } +soroban-native-sdk-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "7452ef04" } +stellar-xdr = { git = "https://github.com/stellar/rs-stellar-xdr", rev = "9aae807" } wasmi = { package = "soroban-wasmi", git = "https://github.com/stellar/wasmi", rev = "a61b6df" } # soroban-env-common = { path = "../rs-soroban-env/soroban-env-common" } diff --git a/soroban-auth/src/lib.rs b/soroban-auth/src/lib.rs index a4e48e3e2..90554db3b 100644 --- a/soroban-auth/src/lib.rs +++ b/soroban-auth/src/lib.rs @@ -109,7 +109,7 @@ fn verify_account_signatures(env: &Env, auth: &AccountSignatures, name: Symbol, /// invocations to be replayable if it is important they are not.** pub fn verify(env: &Env, sig: &Signature, name: Symbol, args: impl IntoVal>) { match sig { - Signature::Contract => { + Signature::Invoker => { env.get_invoking_contract(); } Signature::Ed25519(e) => verify_ed25519_signature(env, &e, name, args.into_val(env)), diff --git a/soroban-auth/src/public_types.rs b/soroban-auth/src/public_types.rs index 65cc2d7ef..3aeacdd2e 100644 --- a/soroban-auth/src/public_types.rs +++ b/soroban-auth/src/public_types.rs @@ -1,4 +1,4 @@ -use soroban_sdk::{contracttype, Bytes, BytesN, Env, RawVal, Symbol, Vec}; +use soroban_sdk::{contracttype, Bytes, BytesN, Env, Invoker, RawVal, Symbol, Vec}; /// An Ed25519 signature contains a single signature for the /// [`SignaturePayload`]. @@ -26,7 +26,7 @@ pub struct AccountSignatures { #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] #[contracttype(lib = "soroban_auth")] pub enum Signature { - Contract, + Invoker, Ed25519(Ed25519Signature), Account(AccountSignatures), } @@ -35,7 +35,7 @@ impl Signature { /// Returns the identifier that this signatures claims to authenticate. pub fn identifier(&self, env: &Env) -> Identifier { match self { - Signature::Contract => Identifier::Contract(env.get_invoking_contract()), + Signature::Invoker => Identifier::Invoker(env.invoker()), Signature::Ed25519(e) => Identifier::Ed25519(e.public_key.clone()), Signature::Account(a) => Identifier::Account(a.account_id.clone()), } @@ -53,7 +53,7 @@ impl Signature { #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] #[contracttype(lib = "soroban_auth")] pub enum Identifier { - Contract(BytesN<32>), + Invoker(Invoker), Ed25519(BytesN<32>), Account(BytesN<32>), } diff --git a/soroban-sdk/src/bytes.rs b/soroban-sdk/src/bytes.rs index 4139d3a78..ef267e8ce 100644 --- a/soroban-sdk/src/bytes.rs +++ b/soroban-sdk/src/bytes.rs @@ -917,6 +917,11 @@ impl TryIntoVal> for ScVal { } impl BytesN { + #[inline(always)] + pub(crate) unsafe fn unchecked_new(obj: EnvObj) -> Self { + Self(Bytes::unchecked_new(obj)) + } + pub fn env(&self) -> &Env { self.0.env() } diff --git a/soroban-sdk/src/env.rs b/soroban-sdk/src/env.rs index 7aed6512f..0bb7ac0e3 100644 --- a/soroban-sdk/src/env.rs +++ b/soroban-sdk/src/env.rs @@ -49,6 +49,7 @@ pub use internal::Val; pub type EnvVal = internal::EnvVal; pub type EnvObj = internal::EnvVal; +use crate::invoker::Invoker; use crate::{ contract_data::ContractData, deploy::Deployer, events::Events, ledger::Ledger, logging::Logger, Bytes, BytesN, Vec, @@ -177,6 +178,18 @@ impl Env { .unwrap() } + pub fn invoker(&self) -> Invoker { + match internal::Env::get_invoker_type(self) { + 0 => Invoker::Account(unsafe { + BytesN::unchecked_new(internal::Env::get_invoking_account(self).in_env(self)) + }), + 1 => Invoker::Contract(unsafe { + BytesN::unchecked_new(internal::Env::get_invoking_contract(self).in_env(self)) + }), + _ => panic!("unrecognized invoker type"), + } + } + /// Get the 32-byte hash identifier of the contract that invoked this /// contract. /// diff --git a/soroban-sdk/src/invoker.rs b/soroban-sdk/src/invoker.rs new file mode 100644 index 000000000..39ac2043c --- /dev/null +++ b/soroban-sdk/src/invoker.rs @@ -0,0 +1,10 @@ +use crate::BytesN; + +/// TODO: Make Invoker storable and convertible to RawVal. + +/// Invoker is the invoker of a contract. +#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] +pub enum Invoker { + Account(BytesN<32>), + Contract(BytesN<32>), +} diff --git a/soroban-sdk/src/lib.rs b/soroban-sdk/src/lib.rs index a97de6adf..9f22ad6c4 100644 --- a/soroban-sdk/src/lib.rs +++ b/soroban-sdk/src/lib.rs @@ -136,6 +136,7 @@ macro_rules! panic_error { mod env; +mod invoker; pub mod xdr; pub use env::ConversionError; @@ -182,6 +183,7 @@ mod vec; pub use account::Account; pub use bigint::{BigInt, Sign}; pub use bytes::{Bytes, BytesN}; +pub use invoker::Invoker; pub use map::Map; pub use set::Set; pub use vec::Vec; diff --git a/soroban-sdk/tests/contractfile_with_sha256.rs b/soroban-sdk/tests/contractfile_with_sha256.rs index 71948852e..2d7442418 100644 --- a/soroban-sdk/tests/contractfile_with_sha256.rs +++ b/soroban-sdk/tests/contractfile_with_sha256.rs @@ -1,6 +1,6 @@ pub const WASM: &[u8] = soroban_sdk::contractfile!( file = "../target/wasm32-unknown-unknown/release/example_add_i32.wasm", - sha256 = "a99f9c800317a653dd6fe106e6c9789f0033b69b6bf9ff4c7fcdd63530557cb3", + sha256 = "7d4839b87fc1d005e4ef213b1e5c53e6ffddf577b0ca69818420c7a8865c385b", ); #[test] diff --git a/soroban-sdk/tests/contractimport_with_sha256.rs b/soroban-sdk/tests/contractimport_with_sha256.rs index 9c040f589..49ee7e44a 100644 --- a/soroban-sdk/tests/contractimport_with_sha256.rs +++ b/soroban-sdk/tests/contractimport_with_sha256.rs @@ -7,7 +7,7 @@ const ADD_CONTRACT_ID: [u8; 32] = [0; 32]; mod addcontract { soroban_sdk::contractimport!( file = "../target/wasm32-unknown-unknown/release/example_add_i32.wasm", - sha256 = "a99f9c800317a653dd6fe106e6c9789f0033b69b6bf9ff4c7fcdd63530557cb3", + sha256 = "7d4839b87fc1d005e4ef213b1e5c53e6ffddf577b0ca69818420c7a8865c385b", ); } diff --git a/soroban-sdk/tests/trybuild/contractfile_with_sha256_verify_fail.stderr b/soroban-sdk/tests/trybuild/contractfile_with_sha256_verify_fail.stderr index c0f22ad67..ed6c907f2 100644 --- a/soroban-sdk/tests/trybuild/contractfile_with_sha256_verify_fail.stderr +++ b/soroban-sdk/tests/trybuild/contractfile_with_sha256_verify_fail.stderr @@ -1,4 +1,4 @@ -error: sha256 does not match, expected: a99f9c800317a653dd6fe106e6c9789f0033b69b6bf9ff4c7fcdd63530557cb3 +error: sha256 does not match, expected: 7d4839b87fc1d005e4ef213b1e5c53e6ffddf577b0ca69818420c7a8865c385b --> tests/trybuild/contractfile_with_sha256_verify_fail.rs:3:5 | 3 | sha256 = "0000000000000000000000000000000000000000000000000000000000000000", diff --git a/soroban-sdk/tests/trybuild/contractimport_with_sha256_verify_fail.stderr b/soroban-sdk/tests/trybuild/contractimport_with_sha256_verify_fail.stderr index c298c5767..6c0322a35 100644 --- a/soroban-sdk/tests/trybuild/contractimport_with_sha256_verify_fail.stderr +++ b/soroban-sdk/tests/trybuild/contractimport_with_sha256_verify_fail.stderr @@ -1,4 +1,4 @@ -error: sha256 does not match, expected: a99f9c800317a653dd6fe106e6c9789f0033b69b6bf9ff4c7fcdd63530557cb3 +error: sha256 does not match, expected: 7d4839b87fc1d005e4ef213b1e5c53e6ffddf577b0ca69818420c7a8865c385b --> tests/trybuild/contractimport_with_sha256_verify_fail.rs:3:5 | 3 | sha256 = "0000000000000000000000000000000000000000000000000000000000000000", From 261854bb1e765b13c7ae674d8d47b8db3c3b7b27 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Fri, 23 Sep 2022 21:55:50 -0700 Subject: [PATCH 02/14] invoker --- soroban-sdk/src/invoker.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/soroban-sdk/src/invoker.rs b/soroban-sdk/src/invoker.rs index 39ac2043c..b7e47fa2b 100644 --- a/soroban-sdk/src/invoker.rs +++ b/soroban-sdk/src/invoker.rs @@ -1,8 +1,9 @@ -use crate::BytesN; +use crate::{contracttype, BytesN}; /// TODO: Make Invoker storable and convertible to RawVal. /// Invoker is the invoker of a contract. +#[contracttype(crate_path = "crate", export = false)] #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] pub enum Invoker { Account(BytesN<32>), From 25d9c8888d3f646cfe1e5fca99c1192306507c37 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Fri, 23 Sep 2022 21:56:01 -0700 Subject: [PATCH 03/14] auth --- soroban-auth/Cargo.toml | 1 + soroban-auth/tests/test.rs | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/soroban-auth/Cargo.toml b/soroban-auth/Cargo.toml index 5e63ea451..570f76795 100644 --- a/soroban-auth/Cargo.toml +++ b/soroban-auth/Cargo.toml @@ -21,5 +21,6 @@ ed25519-dalek = { version = "1.0.1", optional = true } rand = { version = "0.7.3", optional = true } [dev_dependencies] +soroban-sdk = { version = "0.0.4", features = ["testutils"] } ed25519-dalek = { version = "1.0.1" } rand = { version = "0.7.3" } diff --git a/soroban-auth/tests/test.rs b/soroban-auth/tests/test.rs index ba83ab217..ff02adbfe 100644 --- a/soroban-auth/tests/test.rs +++ b/soroban-auth/tests/test.rs @@ -18,12 +18,12 @@ fn read_nonce(e: &Env, id: &Identifier) -> BigInt { } fn verify_and_consume_nonce(e: &Env, id: &Identifier, expected_nonce: &BigInt) { - // replay protection is not required for Contract authorization because + // replay protection is not required for invoker authorization because // there's no cryptographic signature involved. All that's checked is the - // invoking contract, so this contract just expects 0. - if matches!(id, Identifier::Contract(_)) { + // invoker, so this contract just expects 0. + if matches!(id, Identifier::Invoker(_)) { if BigInt::zero(&e) != expected_nonce { - panic!("nonce should be zero for Contract") + panic!("nonce should be zero for invoker") } return; } @@ -60,7 +60,7 @@ pub struct OuterTestContract; impl OuterTestContract { pub fn authorize(e: Env, contract_id: BytesN<32>) { let client = TestContractClient::new(&e, contract_id); - client.verify_sig(&Signature::Contract, &BigInt::zero(&e)); + client.verify_sig(&Signature::Invoker, &BigInt::zero(&e)); } } From eb3fec4cd274fb2f6ce1ff67d10fc9a5c7c39375 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Fri, 23 Sep 2022 22:02:45 -0700 Subject: [PATCH 04/14] Make crate path configurable on contracttype, contracterror --- soroban-sdk-macros/src/derive_enum.rs | 155 ++++++++--------- soroban-sdk-macros/src/derive_enum_int.rs | 59 +++---- .../src/derive_error_enum_int.rs | 58 +++---- soroban-sdk-macros/src/derive_struct.rs | 157 +++++++++--------- soroban-sdk-macros/src/lib.rs | 18 +- 5 files changed, 229 insertions(+), 218 deletions(-) diff --git a/soroban-sdk-macros/src/derive_enum.rs b/soroban-sdk-macros/src/derive_enum.rs index 2132da2dc..df39d40ad 100644 --- a/soroban-sdk-macros/src/derive_enum.rs +++ b/soroban-sdk-macros/src/derive_enum.rs @@ -2,7 +2,7 @@ use itertools::MultiUnzip; use proc_macro2::TokenStream as TokenStream2; use quote::{format_ident, quote}; use soroban_env_common::Symbol; -use syn::{spanned::Spanned, DataEnum, Error, Ident}; +use syn::{spanned::Spanned, DataEnum, Error, Ident, Path}; use stellar_xdr::{ ScSpecEntry, ScSpecTypeDef, ScSpecUdtUnionCaseV0, ScSpecUdtUnionV0, VecM, WriteXdr, @@ -11,6 +11,7 @@ use stellar_xdr::{ use crate::map_type::map_type; pub fn derive_type_enum( + path: &Path, enum_ident: &Ident, data: &DataEnum, spec: bool, @@ -43,7 +44,7 @@ pub fn derive_type_enum( let discriminant_const_sym_ident = format_ident!("DISCRIMINANT_SYM_{}", name.to_uppercase()); let discriminant_const_u64_ident = format_ident!("DISCRIMINANT_U64_{}", name.to_uppercase()); let discriminant_const_sym = quote! { - const #discriminant_const_sym_ident: soroban_sdk::Symbol = soroban_sdk::symbol!(#name); + const #discriminant_const_sym_ident: #path::Symbol = #path::Symbol::from_str(#name); }; let discriminant_const_u64 = quote! { const #discriminant_const_u64_ident: u64 = #discriminant_const_sym_ident.to_raw().get_payload(); @@ -66,22 +67,22 @@ pub fn derive_type_enum( let try_from = quote! { #discriminant_const_u64_ident => { if iter.len() > 1 { - return Err(soroban_sdk::ConversionError); + return Err(#path::ConversionError); } - Self::#ident(iter.next().ok_or(soroban_sdk::ConversionError)??.try_into_val(env)?) + Self::#ident(iter.next().ok_or(#path::ConversionError)??.try_into_val(env)?) } }; let into = quote! { #enum_ident::#ident(ref value) => (#discriminant_const_sym_ident, value).into_val(env) }; let try_from_xdr = quote! { #name => { if iter.len() > 1 { - return Err(soroban_sdk::xdr::Error::Invalid); + return Err(#path::xdr::Error::Invalid); } - let rv: soroban_sdk::RawVal = iter.next().ok_or(soroban_sdk::xdr::Error::Invalid)?.try_into_val(env).map_err(|_| soroban_sdk::xdr::Error::Invalid)?; - Self::#ident(rv.try_into_val(env).map_err(|_| soroban_sdk::xdr::Error::Invalid)?) + let rv: #path::RawVal = iter.next().ok_or(#path::xdr::Error::Invalid)?.try_into_val(env).map_err(|_| #path::xdr::Error::Invalid)?; + Self::#ident(rv.try_into_val(env).map_err(|_| #path::xdr::Error::Invalid)?) } }; - let into_xdr = quote! { #enum_ident::#ident(value) => (#name, value).try_into().map_err(|_| soroban_sdk::xdr::Error::Invalid)? }; + let into_xdr = quote! { #enum_ident::#ident(value) => (#name, value).try_into().map_err(|_| #path::xdr::Error::Invalid)? }; (spec_case, discriminant_const, try_from, into, try_from_xdr, into_xdr) } else { let spec_case = ScSpecUdtUnionCaseV0 { @@ -91,7 +92,7 @@ pub fn derive_type_enum( let try_from = quote! { #discriminant_const_u64_ident => { if iter.len() > 0 { - return Err(soroban_sdk::ConversionError); + return Err(#path::ConversionError); } Self::#ident } @@ -100,12 +101,12 @@ pub fn derive_type_enum( let try_from_xdr = quote! { #name => { if iter.len() > 0 { - return Err(soroban_sdk::xdr::Error::Invalid); + return Err(#path::xdr::Error::Invalid); } Self::#ident } }; - let into_xdr = quote! { #enum_ident::#ident => (#name,).try_into().map_err(|_| soroban_sdk::xdr::Error::Invalid)? }; + let into_xdr = quote! { #enum_ident::#ident => (#name,).try_into().map_err(|_| #path::xdr::Error::Invalid)? }; (spec_case, discriminant_const, try_from, into, try_from_xdr, into_xdr) } }) @@ -146,33 +147,33 @@ pub fn derive_type_enum( quote! { #spec_gen - impl soroban_sdk::TryFromVal for #enum_ident { - type Error = soroban_sdk::ConversionError; + impl #path::TryFromVal<#path::Env, #path::RawVal> for #enum_ident { + type Error = #path::ConversionError; #[inline(always)] - fn try_from_val(env: &soroban_sdk::Env, val: soroban_sdk::RawVal) -> Result { - use soroban_sdk::TryIntoVal; + fn try_from_val(env: &#path::Env, val: #path::RawVal) -> Result { + use #path::TryIntoVal; #(#discriminant_consts)* - let vec: soroban_sdk::Vec = val.try_into_val(env)?; + let vec: #path::Vec<#path::RawVal> = val.try_into_val(env)?; let mut iter = vec.iter(); - let discriminant = iter.next().ok_or(soroban_sdk::ConversionError)??; + let discriminant = iter.next().ok_or(#path::ConversionError)??; Ok(match discriminant.get_payload() { #(#try_froms,)* - _ => Err(soroban_sdk::ConversionError{})?, + _ => Err(#path::ConversionError{})?, }) } } - impl soroban_sdk::TryIntoVal for soroban_sdk::RawVal { - type Error = soroban_sdk::ConversionError; + impl #path::TryIntoVal<#path::Env, #enum_ident> for #path::RawVal { + type Error = #path::ConversionError; #[inline(always)] - fn try_into_val(self, env: &soroban_sdk::Env) -> Result<#enum_ident, Self::Error> { - <_ as soroban_sdk::TryFromVal<_, _>>::try_from_val(env, self) + fn try_into_val(self, env: &#path::Env) -> Result<#enum_ident, Self::Error> { + <_ as #path::TryFromVal<_, _>>::try_from_val(env, self) } } - impl soroban_sdk::IntoVal for #enum_ident { + impl #path::IntoVal<#path::Env, #path::RawVal> for #enum_ident { #[inline(always)] - fn into_val(self, env: &soroban_sdk::Env) -> soroban_sdk::RawVal { + fn into_val(self, env: &#path::Env) -> #path::RawVal { #(#discriminant_consts)* match &self { #(#intos,)* @@ -180,9 +181,9 @@ pub fn derive_type_enum( } } - impl soroban_sdk::IntoVal for &#enum_ident { + impl #path::IntoVal<#path::Env, #path::RawVal> for &#enum_ident { #[inline(always)] - fn into_val(self, env: &soroban_sdk::Env) -> soroban_sdk::RawVal { + fn into_val(self, env: &#path::Env) -> #path::RawVal { #(#discriminant_consts)* match self { #(#intos,)* @@ -191,83 +192,83 @@ pub fn derive_type_enum( } #[cfg(any(test, feature = "testutils"))] - impl soroban_sdk::TryFromVal for #enum_ident { - type Error = soroban_sdk::xdr::Error; + impl #path::TryFromVal<#path::Env, #path::xdr::ScVec> for #enum_ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_from_val(env: &soroban_sdk::Env, val: soroban_sdk::xdr::ScVec) -> Result { - use soroban_sdk::xdr::Validate; - use soroban_sdk::TryIntoVal; + fn try_from_val(env: &#path::Env, val: #path::xdr::ScVec) -> Result { + use #path::xdr::Validate; + use #path::TryIntoVal; let vec = val; let mut iter = vec.iter(); - let discriminant: soroban_sdk::xdr::ScSymbol = iter.next().ok_or(soroban_sdk::xdr::Error::Invalid)?.clone().try_into().map_err(|_| soroban_sdk::xdr::Error::Invalid)?; + let discriminant: #path::xdr::ScSymbol = iter.next().ok_or(#path::xdr::Error::Invalid)?.clone().try_into().map_err(|_| #path::xdr::Error::Invalid)?; let discriminant_name: &str = &discriminant.to_string()?; Ok(match discriminant_name { #(#try_from_xdrs,)* - _ => Err(soroban_sdk::xdr::Error::Invalid)?, + _ => Err(#path::xdr::Error::Invalid)?, }) } } #[cfg(any(test, feature = "testutils"))] - impl soroban_sdk::TryIntoVal for soroban_sdk::xdr::ScVec { - type Error = soroban_sdk::xdr::Error; + impl #path::TryIntoVal<#path::Env, #enum_ident> for #path::xdr::ScVec { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into_val(self, env: &soroban_sdk::Env) -> Result<#enum_ident, Self::Error> { - <_ as soroban_sdk::TryFromVal<_, _>>::try_from_val(env, self) + fn try_into_val(self, env: &#path::Env) -> Result<#enum_ident, Self::Error> { + <_ as #path::TryFromVal<_, _>>::try_from_val(env, self) } } #[cfg(any(test, feature = "testutils"))] - impl soroban_sdk::TryFromVal for #enum_ident { - type Error = soroban_sdk::xdr::Error; + impl #path::TryFromVal<#path::Env, #path::xdr::ScObject> for #enum_ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_from_val(env: &soroban_sdk::Env, val: soroban_sdk::xdr::ScObject) -> Result { - if let soroban_sdk::xdr::ScObject::Vec(vec) = val { - <_ as soroban_sdk::TryFromVal<_, _>>::try_from_val(env, vec) + fn try_from_val(env: &#path::Env, val: #path::xdr::ScObject) -> Result { + if let #path::xdr::ScObject::Vec(vec) = val { + <_ as #path::TryFromVal<_, _>>::try_from_val(env, vec) } else { - Err(soroban_sdk::xdr::Error::Invalid) + Err(#path::xdr::Error::Invalid) } } } #[cfg(any(test, feature = "testutils"))] - impl soroban_sdk::TryIntoVal for soroban_sdk::xdr::ScObject { - type Error = soroban_sdk::xdr::Error; + impl #path::TryIntoVal<#path::Env, #enum_ident> for #path::xdr::ScObject { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into_val(self, env: &soroban_sdk::Env) -> Result<#enum_ident, Self::Error> { - <_ as soroban_sdk::TryFromVal<_, _>>::try_from_val(env, self) + fn try_into_val(self, env: &#path::Env) -> Result<#enum_ident, Self::Error> { + <_ as #path::TryFromVal<_, _>>::try_from_val(env, self) } } #[cfg(any(test, feature = "testutils"))] - impl soroban_sdk::TryFromVal for #enum_ident { - type Error = soroban_sdk::xdr::Error; + impl #path::TryFromVal<#path::Env, #path::xdr::ScVal> for #enum_ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_from_val(env: &soroban_sdk::Env, val: soroban_sdk::xdr::ScVal) -> Result { - if let soroban_sdk::xdr::ScVal::Object(Some(obj)) = val { - <_ as soroban_sdk::TryFromVal<_, _>>::try_from_val(env, obj) + fn try_from_val(env: &#path::Env, val: #path::xdr::ScVal) -> Result { + if let #path::xdr::ScVal::Object(Some(obj)) = val { + <_ as #path::TryFromVal<_, _>>::try_from_val(env, obj) } else { - Err(soroban_sdk::xdr::Error::Invalid) + Err(#path::xdr::Error::Invalid) } } } #[cfg(any(test, feature = "testutils"))] - impl soroban_sdk::TryIntoVal for soroban_sdk::xdr::ScVal { - type Error = soroban_sdk::xdr::Error; + impl #path::TryIntoVal<#path::Env, #enum_ident> for #path::xdr::ScVal { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into_val(self, env: &soroban_sdk::Env) -> Result<#enum_ident, Self::Error> { - <_ as soroban_sdk::TryFromVal<_, _>>::try_from_val(env, self) + fn try_into_val(self, env: &#path::Env) -> Result<#enum_ident, Self::Error> { + <_ as #path::TryFromVal<_, _>>::try_from_val(env, self) } } #[cfg(any(test, feature = "testutils"))] - impl TryInto for &#enum_ident { - type Error = soroban_sdk::xdr::Error; + impl TryInto<#path::xdr::ScVec> for &#enum_ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into(self) -> Result { + fn try_into(self) -> Result<#path::xdr::ScVec, Self::Error> { extern crate alloc; Ok(match self { #(#into_xdrs,)* @@ -276,46 +277,46 @@ pub fn derive_type_enum( } #[cfg(any(test, feature = "testutils"))] - impl TryInto for #enum_ident { - type Error = soroban_sdk::xdr::Error; + impl TryInto<#path::xdr::ScVec> for #enum_ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into(self) -> Result { + fn try_into(self) -> Result<#path::xdr::ScVec, Self::Error> { (&self).try_into() } } #[cfg(any(test, feature = "testutils"))] - impl TryInto for &#enum_ident { - type Error = soroban_sdk::xdr::Error; + impl TryInto<#path::xdr::ScObject> for &#enum_ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into(self) -> Result { - Ok(soroban_sdk::xdr::ScObject::Vec(self.try_into()?)) + fn try_into(self) -> Result<#path::xdr::ScObject, Self::Error> { + Ok(#path::xdr::ScObject::Vec(self.try_into()?)) } } #[cfg(any(test, feature = "testutils"))] - impl TryInto for #enum_ident { - type Error = soroban_sdk::xdr::Error; + impl TryInto<#path::xdr::ScObject> for #enum_ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into(self) -> Result { + fn try_into(self) -> Result<#path::xdr::ScObject, Self::Error> { (&self).try_into() } } #[cfg(any(test, feature = "testutils"))] - impl TryInto for &#enum_ident { - type Error = soroban_sdk::xdr::Error; + impl TryInto<#path::xdr::ScVal> for &#enum_ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into(self) -> Result { - Ok(soroban_sdk::xdr::ScVal::Object(Some(self.try_into()?))) + fn try_into(self) -> Result<#path::xdr::ScVal, Self::Error> { + Ok(#path::xdr::ScVal::Object(Some(self.try_into()?))) } } #[cfg(any(test, feature = "testutils"))] - impl TryInto for #enum_ident { - type Error = soroban_sdk::xdr::Error; + impl TryInto<#path::xdr::ScVal> for #enum_ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into(self) -> Result { + fn try_into(self) -> Result<#path::xdr::ScVal, Self::Error> { (&self).try_into() } } diff --git a/soroban-sdk-macros/src/derive_enum_int.rs b/soroban-sdk-macros/src/derive_enum_int.rs index ae9ce3061..9c9d1ae51 100644 --- a/soroban-sdk-macros/src/derive_enum_int.rs +++ b/soroban-sdk-macros/src/derive_enum_int.rs @@ -2,13 +2,14 @@ use itertools::MultiUnzip; use proc_macro2::TokenStream as TokenStream2; use quote::{format_ident, quote}; use stellar_xdr::ScSpecUdtEnumV0; -use syn::{spanned::Spanned, DataEnum, Error, ExprLit, Ident, Lit}; +use syn::{spanned::Spanned, DataEnum, Error, ExprLit, Ident, Lit, Path}; use stellar_xdr::{ScSpecEntry, ScSpecUdtEnumCaseV0, VecM, WriteXdr}; // TODO: Add conversions to/from ScVal types. pub fn derive_type_enum_int( + path: &Path, enum_ident: &Ident, data: &DataEnum, spec: bool, @@ -87,39 +88,39 @@ pub fn derive_type_enum_int( quote! { #spec_gen - impl soroban_sdk::TryFromVal for #enum_ident { - type Error = soroban_sdk::ConversionError; + impl #path::TryFromVal<#path::Env, #path::RawVal> for #enum_ident { + type Error = #path::ConversionError; #[inline(always)] - fn try_from_val(env: &soroban_sdk::Env, val: soroban_sdk::RawVal) -> Result { - use soroban_sdk::TryIntoVal; + fn try_from_val(env: &#path::Env, val: #path::RawVal) -> Result { + use #path::TryIntoVal; let discriminant: u32 = val.try_into_val(env)?; Ok(match discriminant { #(#try_froms,)* - _ => Err(soroban_sdk::ConversionError{})?, + _ => Err(#path::ConversionError{})?, }) } } - impl soroban_sdk::TryIntoVal for soroban_sdk::RawVal { - type Error = soroban_sdk::ConversionError; + impl #path::TryIntoVal<#path::Env, #enum_ident> for #path::RawVal { + type Error = #path::ConversionError; #[inline(always)] - fn try_into_val(self, env: &soroban_sdk::Env) -> Result<#enum_ident, Self::Error> { - <_ as soroban_sdk::TryFromVal<_, _>>::try_from_val(env, self) + fn try_into_val(self, env: &#path::Env) -> Result<#enum_ident, Self::Error> { + <_ as #path::TryFromVal<_, _>>::try_from_val(env, self) } } - impl soroban_sdk::IntoVal for #enum_ident { + impl #path::IntoVal<#path::Env, #path::RawVal> for #enum_ident { #[inline(always)] - fn into_val(self, env: &soroban_sdk::Env) -> soroban_sdk::RawVal { + fn into_val(self, env: &#path::Env) -> #path::RawVal { match &self { #(#intos,)* } } } - impl soroban_sdk::IntoVal for &#enum_ident { + impl #path::IntoVal<#path::Env, #path::RawVal> for &#enum_ident { #[inline(always)] - fn into_val(self, env: &soroban_sdk::Env) -> soroban_sdk::RawVal { + fn into_val(self, env: &#path::Env) -> #path::RawVal { match self { #(#intos,)* } @@ -127,41 +128,41 @@ pub fn derive_type_enum_int( } #[cfg(any(test, feature = "testutils"))] - impl soroban_sdk::TryFromVal for #enum_ident { - type Error = soroban_sdk::xdr::Error; + impl #path::TryFromVal<#path::Env, #path::xdr::ScVal> for #enum_ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_from_val(env: &soroban_sdk::Env, val: soroban_sdk::xdr::ScVal) -> Result { - let discriminant: u32 = val.try_into().map_err(|_| soroban_sdk::xdr::Error::Invalid)?; + fn try_from_val(env: &#path::Env, val: #path::xdr::ScVal) -> Result { + let discriminant: u32 = val.try_into().map_err(|_| #path::xdr::Error::Invalid)?; Ok(match discriminant { #(#try_froms,)* - _ => Err(soroban_sdk::xdr::Error::Invalid)?, + _ => Err(#path::xdr::Error::Invalid)?, }) } } #[cfg(any(test, feature = "testutils"))] - impl soroban_sdk::TryIntoVal for soroban_sdk::xdr::ScVal { - type Error = soroban_sdk::xdr::Error; + impl #path::TryIntoVal<#path::Env, #enum_ident> for #path::xdr::ScVal { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into_val(self, env: &soroban_sdk::Env) -> Result<#enum_ident, Self::Error> { - <_ as soroban_sdk::TryFromVal<_, _>>::try_from_val(env, self) + fn try_into_val(self, env: &#path::Env) -> Result<#enum_ident, Self::Error> { + <_ as #path::TryFromVal<_, _>>::try_from_val(env, self) } } #[cfg(any(test, feature = "testutils"))] - impl TryInto for &#enum_ident { - type Error = soroban_sdk::xdr::Error; + impl TryInto<#path::xdr::ScVal> for &#enum_ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into(self) -> Result { + fn try_into(self) -> Result<#path::xdr::ScVal, Self::Error> { Ok((*self as u32).into()) } } #[cfg(any(test, feature = "testutils"))] - impl TryInto for #enum_ident { - type Error = soroban_sdk::xdr::Error; + impl TryInto<#path::xdr::ScVal> for #enum_ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into(self) -> Result { + fn try_into(self) -> Result<#path::xdr::ScVal, Self::Error> { Ok((self as u32).into()) } } diff --git a/soroban-sdk-macros/src/derive_error_enum_int.rs b/soroban-sdk-macros/src/derive_error_enum_int.rs index 49623c71a..18e157aae 100644 --- a/soroban-sdk-macros/src/derive_error_enum_int.rs +++ b/soroban-sdk-macros/src/derive_error_enum_int.rs @@ -2,9 +2,10 @@ use itertools::MultiUnzip; use proc_macro2::TokenStream as TokenStream2; use quote::{format_ident, quote}; use stellar_xdr::{ScSpecEntry, ScSpecUdtErrorEnumCaseV0, ScSpecUdtErrorEnumV0, VecM, WriteXdr}; -use syn::{spanned::Spanned, DataEnum, Error, ExprLit, Ident, Lit}; +use syn::{spanned::Spanned, DataEnum, Error, ExprLit, Ident, Lit, Path}; pub fn derive_type_error_enum_int( + path: &Path, enum_ident: &Ident, data: &DataEnum, spec: bool, @@ -43,7 +44,8 @@ pub fn derive_type_error_enum_int( value: discriminant, }; let try_from = quote! { #discriminant => Self::#ident }; - let into = quote! { #enum_ident::#ident => soroban_sdk::Status::from_contract_error(#discriminant) }; + let into = + quote! { #enum_ident::#ident => #path::Status::from_contract_error(#discriminant) }; (spec_case, try_from, into) }) .multiunzip(); @@ -83,11 +85,11 @@ pub fn derive_type_error_enum_int( quote! { #spec_gen - impl TryFrom for #enum_ident { - type Error = soroban_sdk::Status; + impl TryFrom<#path::Status> for #enum_ident { + type Error = #path::Status; #[inline(always)] - fn try_from(status: soroban_sdk::Status) -> Result { - if status.is_type(soroban_sdk::xdr::ScStatusType::ContractError) { + fn try_from(status: #path::Status) -> Result { + if status.is_type(#path::xdr::ScStatusType::ContractError) { let discriminant = status.get_code(); Ok(match discriminant { #(#try_froms,)* @@ -99,59 +101,59 @@ pub fn derive_type_error_enum_int( } } - impl TryFrom<&soroban_sdk::Status> for #enum_ident { - type Error = soroban_sdk::Status; + impl TryFrom<&#path::Status> for #enum_ident { + type Error = #path::Status; #[inline(always)] - fn try_from(status: &soroban_sdk::Status) -> Result { - <_ as TryFrom>::try_from(*status) + fn try_from(status: &#path::Status) -> Result { + <_ as TryFrom<#path::Status>>::try_from(*status) } } - impl From<#enum_ident> for soroban_sdk::Status { + impl From<#enum_ident> for #path::Status { #[inline(always)] - fn from(val: #enum_ident) -> soroban_sdk::Status { + fn from(val: #enum_ident) -> #path::Status { match val { #(#intos,)* } } } - impl From<&#enum_ident> for soroban_sdk::Status { + impl From<&#enum_ident> for #path::Status { #[inline(always)] - fn from(val: &#enum_ident) -> soroban_sdk::Status { + fn from(val: &#enum_ident) -> #path::Status { <_ as From<#enum_ident>>::from(*val) } } - impl soroban_sdk::TryFromVal for #enum_ident { - type Error = soroban_sdk::ConversionError; + impl #path::TryFromVal<#path::Env, #path::RawVal> for #enum_ident { + type Error = #path::ConversionError; #[inline(always)] - fn try_from_val(env: &soroban_sdk::Env, val: soroban_sdk::RawVal) -> Result { - use soroban_sdk::TryIntoVal; - let status: soroban_sdk::Status = val.try_into_val(env)?; - status.try_into().map_err(|_| soroban_sdk::ConversionError) + fn try_from_val(env: &#path::Env, val: #path::RawVal) -> Result { + use #path::TryIntoVal; + let status: #path::Status = val.try_into_val(env)?; + status.try_into().map_err(|_| #path::ConversionError) } } - impl soroban_sdk::TryIntoVal for soroban_sdk::RawVal { - type Error = soroban_sdk::ConversionError; + impl #path::TryIntoVal<#path::Env, #enum_ident> for #path::RawVal { + type Error = #path::ConversionError; #[inline(always)] - fn try_into_val(self, env: &soroban_sdk::Env) -> Result<#enum_ident, Self::Error> { - <_ as soroban_sdk::TryFromVal<_, _>>::try_from_val(env, self) + fn try_into_val(self, env: &#path::Env) -> Result<#enum_ident, Self::Error> { + <_ as #path::TryFromVal<_, _>>::try_from_val(env, self) } } - impl soroban_sdk::IntoVal for #enum_ident { + impl #path::IntoVal<#path::Env, #path::RawVal> for #enum_ident { #[inline(always)] - fn into_val(self, env: &soroban_sdk::Env) -> soroban_sdk::RawVal { + fn into_val(self, env: &#path::Env) -> #path::RawVal { let status: Status = self.into(); status.into_val(env) } } - impl soroban_sdk::IntoVal for &#enum_ident { + impl #path::IntoVal<#path::Env, #path::RawVal> for &#enum_ident { #[inline(always)] - fn into_val(self, env: &soroban_sdk::Env) -> soroban_sdk::RawVal { + fn into_val(self, env: &#path::Env) -> #path::RawVal { let status: Status = self.into(); status.into_val(env) } diff --git a/soroban-sdk-macros/src/derive_struct.rs b/soroban-sdk-macros/src/derive_struct.rs index 266b814b3..8862126d9 100644 --- a/soroban-sdk-macros/src/derive_struct.rs +++ b/soroban-sdk-macros/src/derive_struct.rs @@ -2,7 +2,7 @@ use itertools::MultiUnzip; use proc_macro2::{Literal, TokenStream as TokenStream2}; use quote::{format_ident, quote}; use soroban_env_common::Symbol; -use syn::{spanned::Spanned, DataStruct, Error, Ident}; +use syn::{spanned::Spanned, DataStruct, Error, Ident, Path}; use stellar_xdr::{ ScSpecEntry, ScSpecTypeDef, ScSpecUdtStructFieldV0, ScSpecUdtStructV0, VecM, WriteXdr, @@ -15,6 +15,7 @@ use crate::map_type::map_type; // types with private fields. pub fn derive_type_struct( + path: &Path, ident: &Ident, data: &DataStruct, spec: bool, @@ -58,27 +59,27 @@ pub fn derive_type_struct( } }, }; - let map_key = quote! { ::soroban_sdk::symbol!(#name) }; + let map_key = quote! { #path::symbol!(#name) }; let try_from = quote! { #ident: if let Some(Ok(val)) = map.get(#map_key) { val.try_into_val(env)? } else { - Err(soroban_sdk::ConversionError)? + Err(#path::ConversionError)? } }; let into = quote! { map.set(#map_key, (&self.#ident).into_val(env)) }; let try_from_xdr = quote! { #ident: { - let key = &#name.try_into().map_err(|_| soroban_sdk::xdr::Error::Invalid)?; - let idx = map.binary_search_by_key(key, |entry| entry.key.clone()).map_err(|_| soroban_sdk::xdr::Error::Invalid)?; - let rv: soroban_sdk::RawVal = (&map[idx].val.clone()).try_into_val(env).map_err(|_| soroban_sdk::xdr::Error::Invalid)?; - rv.try_into_val(env).map_err(|_| soroban_sdk::xdr::Error::Invalid)? + let key = &#name.try_into().map_err(|_| #path::xdr::Error::Invalid)?; + let idx = map.binary_search_by_key(key, |entry| entry.key.clone()).map_err(|_| #path::xdr::Error::Invalid)?; + let rv: #path::RawVal = (&map[idx].val.clone()).try_into_val(env).map_err(|_| #path::xdr::Error::Invalid)?; + rv.try_into_val(env).map_err(|_| #path::xdr::Error::Invalid)? } }; let into_xdr = quote! { - soroban_sdk::xdr::ScMapEntry { - key: #name.try_into().map_err(|_| soroban_sdk::xdr::Error::Invalid)?, - val: (&self.#ident).try_into().map_err(|_| soroban_sdk::xdr::Error::Invalid)?, + #path::xdr::ScMapEntry { + key: #name.try_into().map_err(|_| #path::xdr::Error::Invalid)?, + val: (&self.#ident).try_into().map_err(|_| #path::xdr::Error::Invalid)?, } }; (spec_field, try_from, into, try_from_xdr, into_xdr) @@ -120,14 +121,14 @@ pub fn derive_type_struct( quote! { #spec_gen - impl soroban_sdk::TryFromVal for #ident { - type Error = soroban_sdk::ConversionError; + impl #path::TryFromVal<#path::Env, #path::RawVal> for #ident { + type Error = #path::ConversionError; #[inline(always)] - fn try_from_val(env: &soroban_sdk::Env, val: soroban_sdk::RawVal) -> Result { - use soroban_sdk::TryIntoVal; - let map: soroban_sdk::Map = val.try_into_val(env)?; + fn try_from_val(env: &#path::Env, val: #path::RawVal) -> Result { + use #path::TryIntoVal; + let map: #path::Map<#path::Symbol, #path::RawVal> = val.try_into_val(env)?; if map.len() != #field_count_u32 { - return Err(soroban_sdk::ConversionError); + return Err(#path::ConversionError); } Ok(Self{ #(#try_froms,)* @@ -135,42 +136,42 @@ pub fn derive_type_struct( } } - impl soroban_sdk::TryIntoVal for soroban_sdk::RawVal { - type Error = soroban_sdk::ConversionError; + impl #path::TryIntoVal<#path::Env, #ident> for #path::RawVal { + type Error = #path::ConversionError; #[inline(always)] - fn try_into_val(self, env: &soroban_sdk::Env) -> Result<#ident, Self::Error> { - <_ as soroban_sdk::TryFromVal<_, _>>::try_from_val(env, self) + fn try_into_val(self, env: &#path::Env) -> Result<#ident, Self::Error> { + <_ as #path::TryFromVal<_, _>>::try_from_val(env, self) } } - impl soroban_sdk::IntoVal for #ident { + impl #path::IntoVal<#path::Env, #path::RawVal> for #ident { #[inline(always)] - fn into_val(self, env: &soroban_sdk::Env) -> soroban_sdk::RawVal { - let mut map = soroban_sdk::Map::::new(env); + fn into_val(self, env: &#path::Env) -> #path::RawVal { + let mut map = #path::Map::<#path::Symbol, #path::RawVal>::new(env); #(#intos;)* map.into() } } - impl soroban_sdk::IntoVal for &#ident { + impl #path::IntoVal<#path::Env, #path::RawVal> for &#ident { #[inline(always)] - fn into_val(self, env: &soroban_sdk::Env) -> soroban_sdk::RawVal { - let mut map = soroban_sdk::Map::::new(env); + fn into_val(self, env: &#path::Env) -> #path::RawVal { + let mut map = #path::Map::<#path::Symbol, #path::RawVal>::new(env); #(#intos;)* map.into() } } #[cfg(any(test, feature = "testutils"))] - impl soroban_sdk::TryFromVal for #ident { - type Error = soroban_sdk::xdr::Error; + impl #path::TryFromVal<#path::Env, #path::xdr::ScMap> for #ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_from_val(env: &soroban_sdk::Env, val: soroban_sdk::xdr::ScMap) -> Result { - use soroban_sdk::xdr::Validate; - use soroban_sdk::TryIntoVal; + fn try_from_val(env: &#path::Env, val: #path::xdr::ScMap) -> Result { + use #path::xdr::Validate; + use #path::TryIntoVal; let map = val; if map.len() != #field_count_usize { - return Err(soroban_sdk::xdr::Error::Invalid); + return Err(#path::xdr::Error::Invalid); } map.validate()?; Ok(Self{ @@ -180,112 +181,112 @@ pub fn derive_type_struct( } #[cfg(any(test, feature = "testutils"))] - impl soroban_sdk::TryIntoVal for soroban_sdk::xdr::ScMap { - type Error = soroban_sdk::xdr::Error; + impl #path::TryIntoVal<#path::Env, #ident> for #path::xdr::ScMap { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into_val(self, env: &soroban_sdk::Env) -> Result<#ident, Self::Error> { - <_ as soroban_sdk::TryFromVal<_, _>>::try_from_val(env, self) + fn try_into_val(self, env: &#path::Env) -> Result<#ident, Self::Error> { + <_ as #path::TryFromVal<_, _>>::try_from_val(env, self) } } #[cfg(any(test, feature = "testutils"))] - impl soroban_sdk::TryFromVal for #ident { - type Error = soroban_sdk::xdr::Error; + impl #path::TryFromVal<#path::Env, #path::xdr::ScObject> for #ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_from_val(env: &soroban_sdk::Env, val: soroban_sdk::xdr::ScObject) -> Result { - if let soroban_sdk::xdr::ScObject::Map(map) = val { - <_ as soroban_sdk::TryFromVal<_, _>>::try_from_val(env, map) + fn try_from_val(env: &#path::Env, val: #path::xdr::ScObject) -> Result { + if let #path::xdr::ScObject::Map(map) = val { + <_ as #path::TryFromVal<_, _>>::try_from_val(env, map) } else { - Err(soroban_sdk::xdr::Error::Invalid) + Err(#path::xdr::Error::Invalid) } } } #[cfg(any(test, feature = "testutils"))] - impl soroban_sdk::TryIntoVal for soroban_sdk::xdr::ScObject { - type Error = soroban_sdk::xdr::Error; + impl #path::TryIntoVal<#path::Env, #ident> for #path::xdr::ScObject { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into_val(self, env: &soroban_sdk::Env) -> Result<#ident, Self::Error> { - <_ as soroban_sdk::TryFromVal<_, _>>::try_from_val(env, self) + fn try_into_val(self, env: &#path::Env) -> Result<#ident, Self::Error> { + <_ as #path::TryFromVal<_, _>>::try_from_val(env, self) } } #[cfg(any(test, feature = "testutils"))] - impl soroban_sdk::TryFromVal for #ident { - type Error = soroban_sdk::xdr::Error; + impl #path::TryFromVal<#path::Env, #path::xdr::ScVal> for #ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_from_val(env: &soroban_sdk::Env, val: soroban_sdk::xdr::ScVal) -> Result { - if let soroban_sdk::xdr::ScVal::Object(Some(obj)) = val { - <_ as soroban_sdk::TryFromVal<_, _>>::try_from_val(env, obj) + fn try_from_val(env: &#path::Env, val: #path::xdr::ScVal) -> Result { + if let #path::xdr::ScVal::Object(Some(obj)) = val { + <_ as #path::TryFromVal<_, _>>::try_from_val(env, obj) } else { - Err(soroban_sdk::xdr::Error::Invalid) + Err(#path::xdr::Error::Invalid) } } } #[cfg(any(test, feature = "testutils"))] - impl soroban_sdk::TryIntoVal for soroban_sdk::xdr::ScVal { - type Error = soroban_sdk::xdr::Error; + impl #path::TryIntoVal<#path::Env, #ident> for #path::xdr::ScVal { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into_val(self, env: &soroban_sdk::Env) -> Result<#ident, Self::Error> { - <_ as soroban_sdk::TryFromVal<_, _>>::try_from_val(env, self) + fn try_into_val(self, env: &#path::Env) -> Result<#ident, Self::Error> { + <_ as #path::TryFromVal<_, _>>::try_from_val(env, self) } } #[cfg(any(test, feature = "testutils"))] - impl TryInto for &#ident { - type Error = soroban_sdk::xdr::Error; + impl TryInto<#path::xdr::ScMap> for &#ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into(self) -> Result { + fn try_into(self) -> Result<#path::xdr::ScMap, Self::Error> { extern crate alloc; - use soroban_sdk::TryFromVal; - soroban_sdk::xdr::ScMap::sorted_from(alloc::vec![ + use #path::TryFromVal; + #path::xdr::ScMap::sorted_from(alloc::vec![ #(#into_xdrs,)* ]) } } #[cfg(any(test, feature = "testutils"))] - impl TryInto for #ident { - type Error = soroban_sdk::xdr::Error; + impl TryInto<#path::xdr::ScMap> for #ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into(self) -> Result { + fn try_into(self) -> Result<#path::xdr::ScMap, Self::Error> { (&self).try_into() } } #[cfg(any(test, feature = "testutils"))] - impl TryInto for &#ident { - type Error = soroban_sdk::xdr::Error; + impl TryInto<#path::xdr::ScObject> for &#ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into(self) -> Result { - Ok(soroban_sdk::xdr::ScObject::Map(self.try_into()?)) + fn try_into(self) -> Result<#path::xdr::ScObject, Self::Error> { + Ok(#path::xdr::ScObject::Map(self.try_into()?)) } } #[cfg(any(test, feature = "testutils"))] - impl TryInto for #ident { - type Error = soroban_sdk::xdr::Error; + impl TryInto<#path::xdr::ScObject> for #ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into(self) -> Result { + fn try_into(self) -> Result<#path::xdr::ScObject, Self::Error> { (&self).try_into() } } #[cfg(any(test, feature = "testutils"))] - impl TryInto for &#ident { - type Error = soroban_sdk::xdr::Error; + impl TryInto<#path::xdr::ScVal> for &#ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into(self) -> Result { - Ok(soroban_sdk::xdr::ScVal::Object(Some(self.try_into()?))) + fn try_into(self) -> Result<#path::xdr::ScVal, Self::Error> { + Ok(#path::xdr::ScVal::Object(Some(self.try_into()?))) } } #[cfg(any(test, feature = "testutils"))] - impl TryInto for #ident { - type Error = soroban_sdk::xdr::Error; + impl TryInto<#path::xdr::ScVal> for #ident { + type Error = #path::xdr::Error; #[inline(always)] - fn try_into(self) -> Result { + fn try_into(self) -> Result<#path::xdr::ScVal, Self::Error> { (&self).try_into() } } diff --git a/soroban-sdk-macros/src/lib.rs b/soroban-sdk-macros/src/lib.rs index cd5a007e9..2ed5ecbcf 100644 --- a/soroban-sdk-macros/src/lib.rs +++ b/soroban-sdk-macros/src/lib.rs @@ -24,8 +24,8 @@ use quote::quote; use sha2::{Digest, Sha256}; use std::fs; use syn::{ - parse_macro_input, spanned::Spanned, AttributeArgs, DeriveInput, Error, ItemImpl, LitStr, Type, - Visibility, + parse_macro_input, parse_str, spanned::Spanned, AttributeArgs, DeriveInput, Error, ItemImpl, + LitStr, Path, Type, Visibility, }; use self::derive_client::ClientItem; @@ -34,6 +34,10 @@ use soroban_spec::gen::rust::{generate_from_wasm, GenerateFromFileError}; use soroban_env_common::Symbol; +fn default_crate_path() -> Path { + parse_str("::soroban_sdk").unwrap() +} + #[proc_macro] pub fn symbol(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as LitStr); @@ -111,6 +115,8 @@ pub fn contractimpl(_metadata: TokenStream, input: TokenStream) -> TokenStream { #[derive(Debug, FromMeta)] struct ContractTypeArgs { + #[darling(default = "default_crate_path")] + crate_path: Path, lib: Option, export: Option, } @@ -149,7 +155,7 @@ pub fn contracttype(metadata: TokenStream, input: TokenStream) -> TokenStream { matches!(input.vis, Visibility::Public(_)) }; let derived = match &input.data { - syn::Data::Struct(s) => derive_type_struct(ident, s, gen_spec, &args.lib), + syn::Data::Struct(s) => derive_type_struct(&args.crate_path, ident, s, gen_spec, &args.lib), syn::Data::Enum(e) => { let count_of_variants = e.variants.len(); let count_of_int_variants = e @@ -158,9 +164,9 @@ pub fn contracttype(metadata: TokenStream, input: TokenStream) -> TokenStream { .filter(|v| v.discriminant.is_some()) .count(); if count_of_int_variants == 0 { - derive_type_enum(ident, e, gen_spec, &args.lib) + derive_type_enum(&args.crate_path, ident, e, gen_spec, &args.lib) } else if count_of_int_variants == count_of_variants { - derive_type_enum_int(ident, e, gen_spec, &args.lib) + derive_type_enum_int(&args.crate_path, ident, e, gen_spec, &args.lib) } else { Error::new(input.span(), "enums are supported as contract types only when all variants have an explicit integer literal, or when all variants are unit or single field") .to_compile_error() @@ -207,7 +213,7 @@ pub fn contracterror(metadata: TokenStream, input: TokenStream) -> TokenStream { let derived = match &input.data { syn::Data::Enum(e) => { if e.variants.iter().all(|v| v.discriminant.is_some()) { - derive_type_error_enum_int(ident, e, gen_spec, &args.lib) + derive_type_error_enum_int(&args.crate_path, ident, e, gen_spec, &args.lib) } else { Error::new(input.span(), "enums are supported as contract errors only when all variants have an explicit integer literal") .to_compile_error() From ad6325a451b85c8a7e2c17de06ee139881775ac9 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Fri, 23 Sep 2022 22:07:57 -0700 Subject: [PATCH 05/14] remove todo --- soroban-sdk/src/invoker.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/soroban-sdk/src/invoker.rs b/soroban-sdk/src/invoker.rs index b7e47fa2b..a29ce25d8 100644 --- a/soroban-sdk/src/invoker.rs +++ b/soroban-sdk/src/invoker.rs @@ -1,7 +1,5 @@ use crate::{contracttype, BytesN}; -/// TODO: Make Invoker storable and convertible to RawVal. - /// Invoker is the invoker of a contract. #[contracttype(crate_path = "crate", export = false)] #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] From 1d4de80a91cc826607d97f41bbb43c31e6e47c7a Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Sun, 25 Sep 2022 20:15:40 -0700 Subject: [PATCH 06/14] wip --- soroban-sdk-macros/src/map_type.rs | 1 + soroban-sdk/src/invoker.rs | 2 +- soroban-spec/src/gen/json/types.rs | 2 ++ soroban-spec/src/gen/rust/types.rs | 1 + 4 files changed, 5 insertions(+), 1 deletion(-) diff --git a/soroban-sdk-macros/src/map_type.rs b/soroban-sdk-macros/src/map_type.rs index 20e1054b5..79e8746c8 100644 --- a/soroban-sdk-macros/src/map_type.rs +++ b/soroban-sdk-macros/src/map_type.rs @@ -30,6 +30,7 @@ pub fn map_type(t: &Type) -> Result { "Status" => Ok(ScSpecTypeDef::Status), "Bytes" => Ok(ScSpecTypeDef::Bytes), "BigInt" => Ok(ScSpecTypeDef::BigInt), + "Invoker" => Ok(ScSpecTypeDef::Invoker), s => Ok(ScSpecTypeDef::Udt(ScSpecTypeUdt { name: s.try_into().map_err(|e| { Error::new( diff --git a/soroban-sdk/src/invoker.rs b/soroban-sdk/src/invoker.rs index a29ce25d8..7d2e68229 100644 --- a/soroban-sdk/src/invoker.rs +++ b/soroban-sdk/src/invoker.rs @@ -1,7 +1,7 @@ use crate::{contracttype, BytesN}; /// Invoker is the invoker of a contract. -#[contracttype(crate_path = "crate", export = false)] +#[contracttype(crate_path = "crate", lib = "soroban_sdk", export = false)] #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] pub enum Invoker { Account(BytesN<32>), diff --git a/soroban-spec/src/gen/json/types.rs b/soroban-spec/src/gen/json/types.rs index a78b682ed..ca7493300 100644 --- a/soroban-spec/src/gen/json/types.rs +++ b/soroban-spec/src/gen/json/types.rs @@ -99,6 +99,7 @@ pub enum Type { Status, Bytes, BigInt, + Invoker, Map { key: Box, value: Box }, Option { value: Box }, Result { value: Box, error: Box }, @@ -174,6 +175,7 @@ impl From<&ScSpecTypeDef> for Type { ScSpecTypeDef::Status => Type::Status, ScSpecTypeDef::Bytes => Type::Bytes, ScSpecTypeDef::BigInt => Type::BigInt, + ScSpecTypeDef::Invoker => Type::Invoker, } } } diff --git a/soroban-spec/src/gen/rust/types.rs b/soroban-spec/src/gen/rust/types.rs index c90d6f5f8..7f0b03eae 100644 --- a/soroban-spec/src/gen/rust/types.rs +++ b/soroban-spec/src/gen/rust/types.rs @@ -130,6 +130,7 @@ pub fn generate_type_ident(spec: &ScSpecTypeDef) -> TokenStream { ScSpecTypeDef::Status => quote! { ::soroban_sdk::Status }, ScSpecTypeDef::Bytes => quote! { ::soroban_sdk::Bytes }, ScSpecTypeDef::BigInt => quote! { ::soroban_sdk::BigInt }, + ScSpecTypeDef::Invoker => quote! { ::soroban_sdk::Invoker }, ScSpecTypeDef::Option(o) => { let value_ident = generate_type_ident(&o.value_type); quote! { Option<#value_ident> } From a65dd58a3bc88edb3c84e22c8522347f49452e97 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Mon, 26 Sep 2022 10:24:23 -0700 Subject: [PATCH 07/14] use invoker type --- Cargo.lock | 12 ++++++------ Cargo.toml | 12 ++++++------ soroban-sdk/src/env.rs | 11 +++++++---- soroban-sdk/src/invoker.rs | 3 +++ 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6a09b8809..326730920 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -784,7 +784,7 @@ dependencies = [ [[package]] name = "soroban-env-common" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=7452ef04#7452ef04ef204549d529fefdf88d84b2e44f5e98" +source = "git+https://github.com/stellar/rs-soroban-env?rev=74940dbb#74940dbb0924875c7bf9a3ee48598a4b1e8d4962" dependencies = [ "soroban-env-macros", "soroban-wasmi", @@ -795,7 +795,7 @@ dependencies = [ [[package]] name = "soroban-env-guest" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=7452ef04#7452ef04ef204549d529fefdf88d84b2e44f5e98" +source = "git+https://github.com/stellar/rs-soroban-env?rev=74940dbb#74940dbb0924875c7bf9a3ee48598a4b1e8d4962" dependencies = [ "soroban-env-common", "static_assertions", @@ -804,7 +804,7 @@ dependencies = [ [[package]] name = "soroban-env-host" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=7452ef04#7452ef04ef204549d529fefdf88d84b2e44f5e98" +source = "git+https://github.com/stellar/rs-soroban-env?rev=74940dbb#74940dbb0924875c7bf9a3ee48598a4b1e8d4962" dependencies = [ "backtrace", "dyn-fmt", @@ -827,7 +827,7 @@ dependencies = [ [[package]] name = "soroban-env-macros" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=7452ef04#7452ef04ef204549d529fefdf88d84b2e44f5e98" +source = "git+https://github.com/stellar/rs-soroban-env?rev=74940dbb#74940dbb0924875c7bf9a3ee48598a4b1e8d4962" dependencies = [ "itertools", "proc-macro2", @@ -839,7 +839,7 @@ dependencies = [ [[package]] name = "soroban-native-sdk-macros" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=7452ef04#7452ef04ef204549d529fefdf88d84b2e44f5e98" +source = "git+https://github.com/stellar/rs-soroban-env?rev=74940dbb#74940dbb0924875c7bf9a3ee48598a4b1e8d4962" dependencies = [ "itertools", "proc-macro2", @@ -936,7 +936,7 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "stellar-xdr" version = "0.0.2" -source = "git+https://github.com/stellar/rs-stellar-xdr?rev=9aae807#9aae807d14f2fb5427da8f5a22195a12c1e03fd1" +source = "git+https://github.com/stellar/rs-stellar-xdr?rev=17b62ab1#17b62ab16ca27e70aa17664d6bd711286f84b838" dependencies = [ "base64", "serde", diff --git a/Cargo.toml b/Cargo.toml index c4a5a2709..8ba23c6df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,12 +29,12 @@ soroban-sdk = { path = "soroban-sdk" } soroban-auth = { path = "soroban-auth" } soroban-spec = { path = "soroban-spec" } soroban-sdk-macros = { path = "soroban-sdk-macros" } -soroban-env-common = { git = "https://github.com/stellar/rs-soroban-env", rev = "7452ef04" } -soroban-env-guest = { git = "https://github.com/stellar/rs-soroban-env", rev = "7452ef04" } -soroban-env-host = { git = "https://github.com/stellar/rs-soroban-env", rev = "7452ef04" } -soroban-env-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "7452ef04" } -soroban-native-sdk-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "7452ef04" } -stellar-xdr = { git = "https://github.com/stellar/rs-stellar-xdr", rev = "9aae807" } +soroban-env-common = { git = "https://github.com/stellar/rs-soroban-env", rev = "74940dbb" } +soroban-env-guest = { git = "https://github.com/stellar/rs-soroban-env", rev = "74940dbb" } +soroban-env-host = { git = "https://github.com/stellar/rs-soroban-env", rev = "74940dbb" } +soroban-env-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "74940dbb" } +soroban-native-sdk-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "74940dbb" } +stellar-xdr = { git = "https://github.com/stellar/rs-stellar-xdr", rev = "17b62ab1" } wasmi = { package = "soroban-wasmi", git = "https://github.com/stellar/wasmi", rev = "a61b6df" } # soroban-env-common = { path = "../rs-soroban-env/soroban-env-common" } diff --git a/soroban-sdk/src/env.rs b/soroban-sdk/src/env.rs index 0bb7ac0e3..1f021e777 100644 --- a/soroban-sdk/src/env.rs +++ b/soroban-sdk/src/env.rs @@ -37,6 +37,7 @@ pub use internal::ConversionError; pub use internal::EnvBase; pub use internal::FromVal; pub use internal::IntoVal; +use internal::InvokerType; pub use internal::Object; pub use internal::RawVal; pub use internal::RawValConvertible; @@ -179,14 +180,16 @@ impl Env { } pub fn invoker(&self) -> Invoker { - match internal::Env::get_invoker_type(self) { - 0 => Invoker::Account(unsafe { + let invoker_type: InvokerType = internal::Env::get_invoker_type(self) + .try_into() + .expect("unrecogniaed invoker type"); + match invoker_type { + InvokerType::Account => Invoker::Account(unsafe { BytesN::unchecked_new(internal::Env::get_invoking_account(self).in_env(self)) }), - 1 => Invoker::Contract(unsafe { + InvokerType::Contract => Invoker::Contract(unsafe { BytesN::unchecked_new(internal::Env::get_invoking_contract(self).in_env(self)) }), - _ => panic!("unrecognized invoker type"), } } diff --git a/soroban-sdk/src/invoker.rs b/soroban-sdk/src/invoker.rs index 7d2e68229..8c243e366 100644 --- a/soroban-sdk/src/invoker.rs +++ b/soroban-sdk/src/invoker.rs @@ -1,6 +1,9 @@ use crate::{contracttype, BytesN}; /// Invoker is the invoker of a contract. +// The Invoker type is a contracttype and transmitted to the host like an enum, +// however it is not exported into contract specs because it has its own spec +// definition type dedicated to it. #[contracttype(crate_path = "crate", lib = "soroban_sdk", export = false)] #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] pub enum Invoker { From 0f3fc6db210fbac583238509f73b8f005836550f Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Mon, 26 Sep 2022 11:20:31 -0700 Subject: [PATCH 08/14] add test --- Cargo.lock | 4 ---- Cargo.toml | 16 ++++++++-------- soroban-sdk/src/env.rs | 4 ++++ tests/invoke_contract/src/lib.rs | 6 ++++-- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 326730920..13e44c813 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -784,7 +784,6 @@ dependencies = [ [[package]] name = "soroban-env-common" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=74940dbb#74940dbb0924875c7bf9a3ee48598a4b1e8d4962" dependencies = [ "soroban-env-macros", "soroban-wasmi", @@ -795,7 +794,6 @@ dependencies = [ [[package]] name = "soroban-env-guest" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=74940dbb#74940dbb0924875c7bf9a3ee48598a4b1e8d4962" dependencies = [ "soroban-env-common", "static_assertions", @@ -804,7 +802,6 @@ dependencies = [ [[package]] name = "soroban-env-host" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=74940dbb#74940dbb0924875c7bf9a3ee48598a4b1e8d4962" dependencies = [ "backtrace", "dyn-fmt", @@ -827,7 +824,6 @@ dependencies = [ [[package]] name = "soroban-env-macros" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=74940dbb#74940dbb0924875c7bf9a3ee48598a4b1e8d4962" dependencies = [ "itertools", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index 8ba23c6df..0009df84d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,18 +29,18 @@ soroban-sdk = { path = "soroban-sdk" } soroban-auth = { path = "soroban-auth" } soroban-spec = { path = "soroban-spec" } soroban-sdk-macros = { path = "soroban-sdk-macros" } -soroban-env-common = { git = "https://github.com/stellar/rs-soroban-env", rev = "74940dbb" } -soroban-env-guest = { git = "https://github.com/stellar/rs-soroban-env", rev = "74940dbb" } -soroban-env-host = { git = "https://github.com/stellar/rs-soroban-env", rev = "74940dbb" } -soroban-env-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "74940dbb" } +# soroban-env-common = { git = "https://github.com/stellar/rs-soroban-env", rev = "74940dbb" } +# soroban-env-guest = { git = "https://github.com/stellar/rs-soroban-env", rev = "74940dbb" } +# soroban-env-host = { git = "https://github.com/stellar/rs-soroban-env", rev = "74940dbb" } +# soroban-env-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "74940dbb" } soroban-native-sdk-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "74940dbb" } stellar-xdr = { git = "https://github.com/stellar/rs-stellar-xdr", rev = "17b62ab1" } wasmi = { package = "soroban-wasmi", git = "https://github.com/stellar/wasmi", rev = "a61b6df" } -# soroban-env-common = { path = "../rs-soroban-env/soroban-env-common" } -# soroban-env-guest = { path = "../rs-soroban-env/soroban-env-guest" } -# soroban-env-host = { path = "../rs-soroban-env/soroban-env-host" } -# soroban-env-macros = { path = "../rs-soroban-env/soroban-env-macros" } +soroban-env-common = { path = "../rs-soroban-env/soroban-env-common" } +soroban-env-guest = { path = "../rs-soroban-env/soroban-env-guest" } +soroban-env-host = { path = "../rs-soroban-env/soroban-env-host" } +soroban-env-macros = { path = "../rs-soroban-env/soroban-env-macros" } [profile.dev] overflow-checks = true diff --git a/soroban-sdk/src/env.rs b/soroban-sdk/src/env.rs index 1f021e777..5308875e9 100644 --- a/soroban-sdk/src/env.rs +++ b/soroban-sdk/src/env.rs @@ -310,6 +310,10 @@ impl Env { internal::budget::Budget::default(), ); + env_impl.set_source_account(xdr::AccountId(xdr::PublicKey::PublicKeyTypeEd25519( + xdr::Uint256([0; 32]), + ))); + let l = internal::LedgerInfo { protocol_version: 0, sequence_number: 0, diff --git a/tests/invoke_contract/src/lib.rs b/tests/invoke_contract/src/lib.rs index 99aa39f90..29ef5f7ac 100644 --- a/tests/invoke_contract/src/lib.rs +++ b/tests/invoke_contract/src/lib.rs @@ -1,5 +1,5 @@ #![no_std] -use soroban_sdk::{contractimpl, symbol, vec, BytesN, Env, IntoVal}; +use soroban_sdk::{contractimpl, symbol, vec, BytesN, Env, IntoVal, Invoker}; pub struct Contract; @@ -7,6 +7,7 @@ pub struct Contract; impl Contract { // TODO: Prevent arg overlap with generated args. pub fn add_with(env: Env, x: i32, y: i32, contract_id: BytesN<32>) -> i32 { + assert!(matches!(env.invoker(), Invoker::Account(_))); env.invoke_contract( &contract_id, &symbol!("add"), @@ -19,7 +20,8 @@ pub struct AddContract; #[contractimpl] impl AddContract { - pub fn add(a: i32, b: i32) -> i32 { + pub fn add(env: Env, a: i32, b: i32) -> i32 { + assert!(matches!(env.invoker(), Invoker::Contract(_))); a + b } } From 68b8dc7b04cb669d119a5be08c9ce0af5375a99e Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Mon, 26 Sep 2022 12:17:12 -0700 Subject: [PATCH 09/14] upd --- Cargo.lock | 8 ++++++-- Cargo.toml | 17 ++++++----------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 13e44c813..069338b99 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -784,6 +784,7 @@ dependencies = [ [[package]] name = "soroban-env-common" version = "0.0.5" +source = "git+https://github.com/stellar/rs-soroban-env?rev=4a7f11bd#4a7f11bd4f9d3459b9c19f8600090dbf150f8188" dependencies = [ "soroban-env-macros", "soroban-wasmi", @@ -794,6 +795,7 @@ dependencies = [ [[package]] name = "soroban-env-guest" version = "0.0.5" +source = "git+https://github.com/stellar/rs-soroban-env?rev=4a7f11bd#4a7f11bd4f9d3459b9c19f8600090dbf150f8188" dependencies = [ "soroban-env-common", "static_assertions", @@ -802,6 +804,7 @@ dependencies = [ [[package]] name = "soroban-env-host" version = "0.0.5" +source = "git+https://github.com/stellar/rs-soroban-env?rev=4a7f11bd#4a7f11bd4f9d3459b9c19f8600090dbf150f8188" dependencies = [ "backtrace", "dyn-fmt", @@ -824,6 +827,7 @@ dependencies = [ [[package]] name = "soroban-env-macros" version = "0.0.5" +source = "git+https://github.com/stellar/rs-soroban-env?rev=4a7f11bd#4a7f11bd4f9d3459b9c19f8600090dbf150f8188" dependencies = [ "itertools", "proc-macro2", @@ -835,7 +839,7 @@ dependencies = [ [[package]] name = "soroban-native-sdk-macros" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=74940dbb#74940dbb0924875c7bf9a3ee48598a4b1e8d4962" +source = "git+https://github.com/stellar/rs-soroban-env?rev=4a7f11bd#4a7f11bd4f9d3459b9c19f8600090dbf150f8188" dependencies = [ "itertools", "proc-macro2", @@ -932,7 +936,7 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "stellar-xdr" version = "0.0.2" -source = "git+https://github.com/stellar/rs-stellar-xdr?rev=17b62ab1#17b62ab16ca27e70aa17664d6bd711286f84b838" +source = "git+https://github.com/stellar/rs-stellar-xdr?rev=3c495a5a#3c495a5a7dfba1cdbe6dc170c533effe0cb2e2c3" dependencies = [ "base64", "serde", diff --git a/Cargo.toml b/Cargo.toml index 0009df84d..e6b3d6932 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,19 +29,14 @@ soroban-sdk = { path = "soroban-sdk" } soroban-auth = { path = "soroban-auth" } soroban-spec = { path = "soroban-spec" } soroban-sdk-macros = { path = "soroban-sdk-macros" } -# soroban-env-common = { git = "https://github.com/stellar/rs-soroban-env", rev = "74940dbb" } -# soroban-env-guest = { git = "https://github.com/stellar/rs-soroban-env", rev = "74940dbb" } -# soroban-env-host = { git = "https://github.com/stellar/rs-soroban-env", rev = "74940dbb" } -# soroban-env-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "74940dbb" } -soroban-native-sdk-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "74940dbb" } -stellar-xdr = { git = "https://github.com/stellar/rs-stellar-xdr", rev = "17b62ab1" } +soroban-env-common = { git = "https://github.com/stellar/rs-soroban-env", rev = "4a7f11bd" } +soroban-env-guest = { git = "https://github.com/stellar/rs-soroban-env", rev = "4a7f11bd" } +soroban-env-host = { git = "https://github.com/stellar/rs-soroban-env", rev = "4a7f11bd" } +soroban-env-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "4a7f11bd" } +soroban-native-sdk-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "4a7f11bd" } +stellar-xdr = { git = "https://github.com/stellar/rs-stellar-xdr", rev = "3c495a5a" } wasmi = { package = "soroban-wasmi", git = "https://github.com/stellar/wasmi", rev = "a61b6df" } -soroban-env-common = { path = "../rs-soroban-env/soroban-env-common" } -soroban-env-guest = { path = "../rs-soroban-env/soroban-env-guest" } -soroban-env-host = { path = "../rs-soroban-env/soroban-env-host" } -soroban-env-macros = { path = "../rs-soroban-env/soroban-env-macros" } - [profile.dev] overflow-checks = true panic = "abort" From 6434ef8669908b839d48baacdbdd6ef666779677 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Mon, 26 Sep 2022 12:34:44 -0700 Subject: [PATCH 10/14] plumb account idg --- soroban-sdk/src/account.rs | 160 ++++++++++++++++++ soroban-sdk/src/env.rs | 4 +- soroban-sdk/src/invoker.rs | 4 +- soroban-sdk/src/lib.rs | 2 +- soroban-sdk/tests/contractfile_with_sha256.rs | 2 +- .../tests/contractimport_with_sha256.rs | 2 +- ...ontractfile_with_sha256_verify_fail.stderr | 2 +- ...tractimport_with_sha256_verify_fail.stderr | 2 +- 8 files changed, 170 insertions(+), 8 deletions(-) diff --git a/soroban-sdk/src/account.rs b/soroban-sdk/src/account.rs index ae52affb1..ffd8ad9b8 100644 --- a/soroban-sdk/src/account.rs +++ b/soroban-sdk/src/account.rs @@ -7,9 +7,169 @@ use core::{ use crate::{ env::internal::{Env as _, RawVal, RawValConvertible}, env::EnvObj, + xdr::ScObjectType, Bytes, BytesN, ConversionError, Env, EnvVal, IntoVal, Object, TryFromVal, TryIntoVal, }; +/// Account ID is a Stellar account ID. +/// +/// The ID is opaque and does not expose the identifier to the contract, but the +/// value is unique and can be used as a key in maps, or compared with other +/// account identifiers. +#[derive(Clone)] +pub struct AccountId(EnvObj); + +impl Debug for AccountId { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "AccountId(..)")?; + Ok(()) + } +} + +impl Eq for AccountId {} + +impl PartialEq for AccountId { + fn eq(&self, other: &Self) -> bool { + self.partial_cmp(other) == Some(Ordering::Equal) + } +} + +impl PartialOrd for AccountId { + fn partial_cmp(&self, other: &Self) -> Option { + Some(Ord::cmp(self, other)) + } +} + +impl Ord for AccountId { + fn cmp(&self, other: &Self) -> core::cmp::Ordering { + self.0.cmp(&other.0) + } +} + +impl TryFromVal for AccountId { + type Error = ConversionError; + + fn try_from_val(env: &Env, val: Object) -> Result { + if val.is_obj_type(ScObjectType::AccountId) { + Ok(AccountId(val.in_env(env))) + } else { + Err(ConversionError {}) + } + } +} + +impl TryIntoVal for Object { + type Error = >::Error; + + fn try_into_val(self, env: &Env) -> Result { + <_ as TryFromVal<_, Object>>::try_from_val(env, self) + } +} + +impl TryFromVal for AccountId { + type Error = >::Error; + + fn try_from_val(env: &Env, val: RawVal) -> Result { + <_ as TryFromVal<_, Object>>::try_from_val(env, val.try_into()?) + } +} + +impl TryIntoVal for RawVal { + type Error = >::Error; + + fn try_into_val(self, env: &Env) -> Result { + <_ as TryFromVal<_, RawVal>>::try_from_val(env, self) + } +} + +impl IntoVal for AccountId { + fn into_val(self, _env: &Env) -> Object { + self.to_object() + } +} + +impl IntoVal for &AccountId { + fn into_val(self, _env: &Env) -> Object { + self.to_object() + } +} + +impl IntoVal for AccountId { + fn into_val(self, _env: &Env) -> RawVal { + self.to_raw() + } +} + +impl IntoVal for &AccountId { + fn into_val(self, _env: &Env) -> RawVal { + self.to_raw() + } +} + +#[cfg(not(target_family = "wasm"))] +use super::xdr::ScVal; + +#[cfg(not(target_family = "wasm"))] +impl TryFrom<&AccountId> for ScVal { + type Error = ConversionError; + fn try_from(v: &AccountId) -> Result { + ScVal::try_from_val(&v.0.env, v.0.val.to_raw()) + } +} + +#[cfg(not(target_family = "wasm"))] +impl TryFrom for ScVal { + type Error = ConversionError; + fn try_from(v: AccountId) -> Result { + (&v).try_into() + } +} + +#[cfg(not(target_family = "wasm"))] +impl TryFromVal for AccountId { + type Error = ConversionError; + fn try_from_val(env: &Env, val: ScVal) -> Result { + <_ as TryFromVal<_, Object>>::try_from_val( + env, + val.try_into_val(env).map_err(|_| ConversionError)?, + ) + } +} + +#[cfg(not(target_family = "wasm"))] +impl TryIntoVal for ScVal { + type Error = ConversionError; + fn try_into_val(self, env: &Env) -> Result { + AccountId::try_from_val(env, self) + } +} + +impl AccountId { + pub(crate) unsafe fn unchecked_new(obj: EnvObj) -> Self { + Self(obj) + } + + pub fn env(&self) -> &Env { + self.0.env() + } + + pub fn as_raw(&self) -> &RawVal { + self.0.as_raw() + } + + pub fn as_object(&self) -> &Object { + self.0.as_object() + } + + pub fn to_raw(&self) -> RawVal { + self.0.to_raw() + } + + pub fn to_object(&self) -> Object { + self.0.to_object() + } +} + /// Account references a Stellar account and provides access to information /// about the account, such as its thresholds and signers. #[derive(Clone)] diff --git a/soroban-sdk/src/env.rs b/soroban-sdk/src/env.rs index 5308875e9..808f284be 100644 --- a/soroban-sdk/src/env.rs +++ b/soroban-sdk/src/env.rs @@ -51,6 +51,7 @@ pub type EnvVal = internal::EnvVal; pub type EnvObj = internal::EnvVal; use crate::invoker::Invoker; +use crate::AccountId; use crate::{ contract_data::ContractData, deploy::Deployer, events::Events, ledger::Ledger, logging::Logger, Bytes, BytesN, Vec, @@ -179,13 +180,14 @@ impl Env { .unwrap() } + /// Get the invoker of the current executing contract. pub fn invoker(&self) -> Invoker { let invoker_type: InvokerType = internal::Env::get_invoker_type(self) .try_into() .expect("unrecogniaed invoker type"); match invoker_type { InvokerType::Account => Invoker::Account(unsafe { - BytesN::unchecked_new(internal::Env::get_invoking_account(self).in_env(self)) + AccountId::unchecked_new(internal::Env::get_invoking_account(self).in_env(self)) }), InvokerType::Contract => Invoker::Contract(unsafe { BytesN::unchecked_new(internal::Env::get_invoking_contract(self).in_env(self)) diff --git a/soroban-sdk/src/invoker.rs b/soroban-sdk/src/invoker.rs index 8c243e366..91e253969 100644 --- a/soroban-sdk/src/invoker.rs +++ b/soroban-sdk/src/invoker.rs @@ -1,4 +1,4 @@ -use crate::{contracttype, BytesN}; +use crate::{contracttype, AccountId, BytesN}; /// Invoker is the invoker of a contract. // The Invoker type is a contracttype and transmitted to the host like an enum, @@ -7,6 +7,6 @@ use crate::{contracttype, BytesN}; #[contracttype(crate_path = "crate", lib = "soroban_sdk", export = false)] #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] pub enum Invoker { - Account(BytesN<32>), + Account(AccountId), Contract(BytesN<32>), } diff --git a/soroban-sdk/src/lib.rs b/soroban-sdk/src/lib.rs index 9f22ad6c4..d41e2d08f 100644 --- a/soroban-sdk/src/lib.rs +++ b/soroban-sdk/src/lib.rs @@ -180,7 +180,7 @@ pub mod logging; mod map; mod set; mod vec; -pub use account::Account; +pub use account::{Account, AccountId}; pub use bigint::{BigInt, Sign}; pub use bytes::{Bytes, BytesN}; pub use invoker::Invoker; diff --git a/soroban-sdk/tests/contractfile_with_sha256.rs b/soroban-sdk/tests/contractfile_with_sha256.rs index 2d7442418..e97dcd25e 100644 --- a/soroban-sdk/tests/contractfile_with_sha256.rs +++ b/soroban-sdk/tests/contractfile_with_sha256.rs @@ -1,6 +1,6 @@ pub const WASM: &[u8] = soroban_sdk::contractfile!( file = "../target/wasm32-unknown-unknown/release/example_add_i32.wasm", - sha256 = "7d4839b87fc1d005e4ef213b1e5c53e6ffddf577b0ca69818420c7a8865c385b", + sha256 = "dd5db60c98c2dc971c9754e306e36122a1d3982d4e0928b36fb6491a95f58420", ); #[test] diff --git a/soroban-sdk/tests/contractimport_with_sha256.rs b/soroban-sdk/tests/contractimport_with_sha256.rs index 49ee7e44a..956c04b2a 100644 --- a/soroban-sdk/tests/contractimport_with_sha256.rs +++ b/soroban-sdk/tests/contractimport_with_sha256.rs @@ -7,7 +7,7 @@ const ADD_CONTRACT_ID: [u8; 32] = [0; 32]; mod addcontract { soroban_sdk::contractimport!( file = "../target/wasm32-unknown-unknown/release/example_add_i32.wasm", - sha256 = "7d4839b87fc1d005e4ef213b1e5c53e6ffddf577b0ca69818420c7a8865c385b", + sha256 = "dd5db60c98c2dc971c9754e306e36122a1d3982d4e0928b36fb6491a95f58420", ); } diff --git a/soroban-sdk/tests/trybuild/contractfile_with_sha256_verify_fail.stderr b/soroban-sdk/tests/trybuild/contractfile_with_sha256_verify_fail.stderr index ed6c907f2..be9cbcdb7 100644 --- a/soroban-sdk/tests/trybuild/contractfile_with_sha256_verify_fail.stderr +++ b/soroban-sdk/tests/trybuild/contractfile_with_sha256_verify_fail.stderr @@ -1,4 +1,4 @@ -error: sha256 does not match, expected: 7d4839b87fc1d005e4ef213b1e5c53e6ffddf577b0ca69818420c7a8865c385b +error: sha256 does not match, expected: dd5db60c98c2dc971c9754e306e36122a1d3982d4e0928b36fb6491a95f58420 --> tests/trybuild/contractfile_with_sha256_verify_fail.rs:3:5 | 3 | sha256 = "0000000000000000000000000000000000000000000000000000000000000000", diff --git a/soroban-sdk/tests/trybuild/contractimport_with_sha256_verify_fail.stderr b/soroban-sdk/tests/trybuild/contractimport_with_sha256_verify_fail.stderr index 6c0322a35..83e917326 100644 --- a/soroban-sdk/tests/trybuild/contractimport_with_sha256_verify_fail.stderr +++ b/soroban-sdk/tests/trybuild/contractimport_with_sha256_verify_fail.stderr @@ -1,4 +1,4 @@ -error: sha256 does not match, expected: 7d4839b87fc1d005e4ef213b1e5c53e6ffddf577b0ca69818420c7a8865c385b +error: sha256 does not match, expected: dd5db60c98c2dc971c9754e306e36122a1d3982d4e0928b36fb6491a95f58420 --> tests/trybuild/contractimport_with_sha256_verify_fail.rs:3:5 | 3 | sha256 = "0000000000000000000000000000000000000000000000000000000000000000", From 1c58f68f23828f53aa9fba38e8149ba4ff0ff974 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Mon, 26 Sep 2022 12:38:02 -0700 Subject: [PATCH 11/14] typo --- soroban-sdk/src/env.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/soroban-sdk/src/env.rs b/soroban-sdk/src/env.rs index 808f284be..6d8277851 100644 --- a/soroban-sdk/src/env.rs +++ b/soroban-sdk/src/env.rs @@ -184,7 +184,7 @@ impl Env { pub fn invoker(&self) -> Invoker { let invoker_type: InvokerType = internal::Env::get_invoker_type(self) .try_into() - .expect("unrecogniaed invoker type"); + .expect("unrecognized invoker type"); match invoker_type { InvokerType::Account => Invoker::Account(unsafe { AccountId::unchecked_new(internal::Env::get_invoking_account(self).in_env(self)) From 51636e818f99665055563432eebbc2bcae1667a0 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Mon, 26 Sep 2022 12:39:45 -0700 Subject: [PATCH 12/14] add testutils --- soroban-sdk/src/env.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/soroban-sdk/src/env.rs b/soroban-sdk/src/env.rs index 6d8277851..2a9091b34 100644 --- a/soroban-sdk/src/env.rs +++ b/soroban-sdk/src/env.rs @@ -327,6 +327,12 @@ impl Env { Env { env_impl } } + /// Sets the source account in the [Env], which will be accessible via + /// [Env::invoker] when the current executing contract is directly invoked. + pub fn set_source_account(&self, account_id: xdr::AccountId) { + self.env_impl.set_source_account(account_id) + } + /// Sets ledger information in the [Env], which will be accessible via /// [Env::ledger]. pub fn set_ledger(&self, li: internal::LedgerInfo) { From b56f26bd6b9f1b9d6fe232ab2539566d6c7c4330 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Mon, 26 Sep 2022 14:02:33 -0700 Subject: [PATCH 13/14] upd env --- Cargo.lock | 10 +++++----- Cargo.toml | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 069338b99..331ba700c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -784,7 +784,7 @@ dependencies = [ [[package]] name = "soroban-env-common" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=4a7f11bd#4a7f11bd4f9d3459b9c19f8600090dbf150f8188" +source = "git+https://github.com/stellar/rs-soroban-env?rev=43422654#43422654bfb203041bce73ccdafeb9dd30de5c1e" dependencies = [ "soroban-env-macros", "soroban-wasmi", @@ -795,7 +795,7 @@ dependencies = [ [[package]] name = "soroban-env-guest" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=4a7f11bd#4a7f11bd4f9d3459b9c19f8600090dbf150f8188" +source = "git+https://github.com/stellar/rs-soroban-env?rev=43422654#43422654bfb203041bce73ccdafeb9dd30de5c1e" dependencies = [ "soroban-env-common", "static_assertions", @@ -804,7 +804,7 @@ dependencies = [ [[package]] name = "soroban-env-host" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=4a7f11bd#4a7f11bd4f9d3459b9c19f8600090dbf150f8188" +source = "git+https://github.com/stellar/rs-soroban-env?rev=43422654#43422654bfb203041bce73ccdafeb9dd30de5c1e" dependencies = [ "backtrace", "dyn-fmt", @@ -827,7 +827,7 @@ dependencies = [ [[package]] name = "soroban-env-macros" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=4a7f11bd#4a7f11bd4f9d3459b9c19f8600090dbf150f8188" +source = "git+https://github.com/stellar/rs-soroban-env?rev=43422654#43422654bfb203041bce73ccdafeb9dd30de5c1e" dependencies = [ "itertools", "proc-macro2", @@ -839,7 +839,7 @@ dependencies = [ [[package]] name = "soroban-native-sdk-macros" version = "0.0.5" -source = "git+https://github.com/stellar/rs-soroban-env?rev=4a7f11bd#4a7f11bd4f9d3459b9c19f8600090dbf150f8188" +source = "git+https://github.com/stellar/rs-soroban-env?rev=43422654#43422654bfb203041bce73ccdafeb9dd30de5c1e" dependencies = [ "itertools", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index e6b3d6932..1c09921fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,11 +29,11 @@ soroban-sdk = { path = "soroban-sdk" } soroban-auth = { path = "soroban-auth" } soroban-spec = { path = "soroban-spec" } soroban-sdk-macros = { path = "soroban-sdk-macros" } -soroban-env-common = { git = "https://github.com/stellar/rs-soroban-env", rev = "4a7f11bd" } -soroban-env-guest = { git = "https://github.com/stellar/rs-soroban-env", rev = "4a7f11bd" } -soroban-env-host = { git = "https://github.com/stellar/rs-soroban-env", rev = "4a7f11bd" } -soroban-env-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "4a7f11bd" } -soroban-native-sdk-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "4a7f11bd" } +soroban-env-common = { git = "https://github.com/stellar/rs-soroban-env", rev = "43422654" } +soroban-env-guest = { git = "https://github.com/stellar/rs-soroban-env", rev = "43422654" } +soroban-env-host = { git = "https://github.com/stellar/rs-soroban-env", rev = "43422654" } +soroban-env-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "43422654" } +soroban-native-sdk-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "43422654" } stellar-xdr = { git = "https://github.com/stellar/rs-stellar-xdr", rev = "3c495a5a" } wasmi = { package = "soroban-wasmi", git = "https://github.com/stellar/wasmi", rev = "a61b6df" } From 0a972559c5e833b3df39f91ca8a8eae3f451fe56 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Mon, 26 Sep 2022 14:09:21 -0700 Subject: [PATCH 14/14] deprecate old fn for getting invoking contract --- soroban-auth/src/lib.rs | 8 +++----- soroban-sdk/src/env.rs | 8 ++------ 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/soroban-auth/src/lib.rs b/soroban-auth/src/lib.rs index 90554db3b..83fc65bf2 100644 --- a/soroban-auth/src/lib.rs +++ b/soroban-auth/src/lib.rs @@ -91,8 +91,8 @@ fn verify_account_signatures(env: &Env, auth: &AccountSignatures, name: Symbol, /// /// - Contract Signature /// -/// An invoking contract can sign the message by simply making the invocation. -/// No actual signature of [`SignaturePayload`] is required. +/// An invoking account or contract can sign the message by simply making the +/// invocation. No actual signature of [`SignaturePayload`] is required. /// /// - Ed25519 Signature /// @@ -109,9 +109,7 @@ fn verify_account_signatures(env: &Env, auth: &AccountSignatures, name: Symbol, /// invocations to be replayable if it is important they are not.** pub fn verify(env: &Env, sig: &Signature, name: Symbol, args: impl IntoVal>) { match sig { - Signature::Invoker => { - env.get_invoking_contract(); - } + Signature::Invoker => {} Signature::Ed25519(e) => verify_ed25519_signature(env, &e, name, args.into_val(env)), Signature::Account(a) => verify_account_signatures(env, &a, name, args.into_val(env)), } diff --git a/soroban-sdk/src/env.rs b/soroban-sdk/src/env.rs index 2a9091b34..86cd9f941 100644 --- a/soroban-sdk/src/env.rs +++ b/soroban-sdk/src/env.rs @@ -195,12 +195,8 @@ impl Env { } } - /// Get the 32-byte hash identifier of the contract that invoked this - /// contract. - /// - /// # Panics - /// - /// Will panic the contract was not invoked by another contract. + #[doc(hidden)] + #[deprecated(note = "use Env::invoker")] pub fn get_invoking_contract(&self) -> BytesN<32> { let rv = internal::Env::get_invoking_contract(self).to_raw(); let bin = Bytes::try_from_val(self, rv).unwrap();