Skip to content

Commit

Permalink
Provide host function for authorizing deep contract calls from curren…
Browse files Browse the repository at this point in the history
…t contract (#840)

* Functionally no-op auth refactoring.

- Factor out invocation tracking methods
- Use host objects instead of XDR

This is to prepare for reusing the code for invoker auth trees, as well as to improve metering.

* Sub-contract auth support and corresponding tests.
  • Loading branch information
dmkozh committed Jun 14, 2023
1 parent c2a5f1c commit 3dfc216
Show file tree
Hide file tree
Showing 34 changed files with 942 additions and 387 deletions.
37 changes: 21 additions & 16 deletions soroban-env-common/env.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,41 +69,34 @@
},
{
"export": "3",
"name": "get_current_contract_id",
"args": [],
"return": "BytesObject",
"docs": "Gets the 32-byte identifer of the current contract. Traps if the running contract was not invoked by a contract."
},
{
"export": "4",
"name": "get_ledger_version",
"args": [],
"return": "U32Val",
"docs": "Return the protocol version of the current ledger as a u32."
},
{
"export": "5",
"export": "4",
"name": "get_ledger_sequence",
"args": [],
"return": "U32Val",
"docs": "Return the sequence number of the current ledger as a u32."
},
{
"export": "6",
"export": "5",
"name": "get_ledger_timestamp",
"args": [],
"return": "U64Val",
"docs": "Return the timestamp number of the current ledger as a u64."
},
{
"export": "7",
"export": "6",
"name": "get_current_call_stack",
"args": [],
"return": "VecObject",
"docs": "Returns the full call stack from the first contract call to the current one as a vector of vectors, where the inside vector contains the contract id as Hash, and a function as a Symbol."
},
{
"export": "8",
"export": "7",
"name": "fail_with_error",
"args": [
{
Expand All @@ -115,14 +108,14 @@
"docs": "Causes the currently executing contract to fail immediately with a provided error code, which must be of error-type `ScErrorType::Contract`. Does not actually return."
},
{
"export": "9",
"export": "8",
"name": "get_ledger_network_id",
"args": [],
"return": "BytesObject",
"docs": "Return the network id (sha256 hash of network passphrase) of the current ledger as `Bytes`. The value is always 32 bytes in length."
},
{
"export": "a",
"export": "9",
"name": "get_current_contract_address",
"args": [],
"return": "AddressObject",
Expand Down Expand Up @@ -1575,7 +1568,7 @@
"type": "VecObject"
}
],
"return": "RawVal",
"return": "Void",
"docs": "Checks if the address has authorized the invocation of the current contract function with the provided arguments. Traps if the invocation hasn't been authorized."
},
{
Expand All @@ -1587,7 +1580,7 @@
"type": "AddressObject"
}
],
"return": "RawVal",
"return": "Void",
"docs": "Checks if the address has authorized the invocation of the current contract function with all the arguments of the invocation. Traps if the invocation hasn't been authorized."
},
{
Expand Down Expand Up @@ -1637,7 +1630,19 @@
],
"return": "RawVal",
"docs": "Returns the 32-byte contract identifier corresponding to the provided Address object. If the Address doesn't belong to an account, returns RawVal corresponding to the unit type (`()`)."
}
},
{
"export": "5",
"name": "authorize_as_curr_contract",
"args": [
{
"name": "auth_entires",
"type": "VecObject"
}
],
"return": "Void",
"docs": "Authorizes sub-contract calls for the next contract call on behalf of the current contract. Every entry in the argument vector corresponds to `InvokerContractAuthEntry` contract type that authorizes a tree of `require_auth` calls on behalf of the current contract. The entries must not contain any authorizations for the direct contract call, i.e. if current contract needs to call contract function F1 that calls function F2 both of which require auth, only F2 should be present in `auth_entries`."
}
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion soroban-env-common/src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub const ENV_META_V0_SECTION_NAME: &str = "contractenvmetav0";

soroban_env_macros::generate_env_meta_consts!(
ledger_protocol_version: 20,
pre_release_version: 42,
pre_release_version: 43,
);

pub fn get_ledger_protocol_version(interface_version: u64) -> u32 {
Expand Down
10 changes: 9 additions & 1 deletion soroban-env-common/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,18 @@ impl TryFrom<&SymbolSmall> for ScVal {

#[cfg(feature = "std")]
impl<E: Env> TryFromVal<E, Symbol> for ScVal {
type Error = ConversionError;
fn try_from_val(e: &E, s: &Symbol) -> Result<Self, ConversionError> {
Ok(ScVal::Symbol(ScSymbol::try_from_val(e, s)?))
}
}

#[cfg(feature = "std")]
impl<E: Env> TryFromVal<E, Symbol> for ScSymbol {
type Error = ConversionError;
fn try_from_val(e: &E, s: &Symbol) -> Result<Self, ConversionError> {
let sstr = SymbolStr::try_from_val(e, s)?;
Ok(ScVal::Symbol(ScSymbol(sstr.0.as_slice().try_into()?)))
Ok(ScSymbol(sstr.0.as_slice()[0..sstr.len()].try_into()?))
}
}

Expand Down

0 comments on commit 3dfc216

Please sign in to comment.