Skip to content

Commit

Permalink
Add invoker
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch committed Sep 24, 2022
1 parent ac38205 commit 8019d6a
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 21 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion soroban-auth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Env, Vec<RawVal>>) {
match sig {
Signature::Contract => {
Signature::Invoker => {
env.get_invoking_contract();
}
Signature::Ed25519(e) => verify_ed25519_signature(env, &e, name, args.into_val(env)),
Expand Down
8 changes: 4 additions & 4 deletions soroban-auth/src/public_types.rs
Original file line number Diff line number Diff line change
@@ -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`].
Expand Down Expand Up @@ -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),
}
Expand All @@ -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()),
}
Expand All @@ -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>),
}
Expand Down
5 changes: 5 additions & 0 deletions soroban-sdk/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,11 @@ impl<const N: usize> TryIntoVal<Env, BytesN<N>> for ScVal {
}

impl<const N: usize> BytesN<N> {
#[inline(always)]
pub(crate) unsafe fn unchecked_new(obj: EnvObj) -> Self {
Self(Bytes::unchecked_new(obj))
}

pub fn env(&self) -> &Env {
self.0.env()
}
Expand Down
13 changes: 13 additions & 0 deletions soroban-sdk/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub use internal::Val;
pub type EnvVal = internal::EnvVal<Env, RawVal>;
pub type EnvObj = internal::EnvVal<Env, Object>;

use crate::invoker::Invoker;
use crate::{
contract_data::ContractData, deploy::Deployer, events::Events, ledger::Ledger, logging::Logger,
Bytes, BytesN, Vec,
Expand Down Expand Up @@ -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.
///
Expand Down
10 changes: 10 additions & 0 deletions soroban-sdk/src/invoker.rs
Original file line number Diff line number Diff line change
@@ -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>),
}
2 changes: 2 additions & 0 deletions soroban-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ macro_rules! panic_error {

mod env;

mod invoker;
pub mod xdr;

pub use env::ConversionError;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk/tests/contractfile_with_sha256.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk/tests/contractimport_with_sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 8019d6a

Please sign in to comment.