Skip to content

Commit

Permalink
Rename and expose inputs_owner function (FuelLabs#5138)
Browse files Browse the repository at this point in the history
## Description
Renamed the `inputs_owner` function to `caller_address` to be consistent
with the `caller_contract_id` function, and makes the function public,
changing its return type from a Result of an Identity to a Result of an
Address

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Igor Rončević <[email protected]>
Co-authored-by: K1-R1 <[email protected]>
  • Loading branch information
3 people authored Sep 22, 2023
1 parent eb63ae4 commit 15aa402
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions sway-lib-std/src/auth.sw
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use ::inputs::{Input, input_count, input_owner, input_type};
pub enum AuthError {
/// The caller is external, but the inputs to the transaction are not all owned by the same address.
InputsNotAllOwnedBySameAddress: (),
/// The caller is internal, but the `caller_address` function was called.
CallerIsInternal: (),
}

/// Returns `true` if the caller is external (i.e. a `script`).
Expand Down Expand Up @@ -79,7 +81,8 @@ pub fn caller_contract_id() -> ContractId {
///
/// # Additional Information
///
/// Returns a Err if the caller is external and the inputs to the transaction are not all owned by the same address.
/// Returns an `AuthError::InputsNotAllOwnedBySameAddress` if the caller is external and the inputs to the transaction are not all owned by the same address.
/// Should not return an `AuthError::CallerIsInternal` under any circumstances.
///
/// # Returns
///
Expand All @@ -93,12 +96,16 @@ pub fn caller_contract_id() -> ContractId {
/// Ok(Identity::Address(address)) => log(address),
/// Ok(Identity::ContractId(contract_id)) => log(contract_id),
/// Err(AuthError::InputsNotAllOwnedBySameAddress) => log("Inputs not all owned by same address."),
/// Err(AuthError::CallerIsInternal) => log("Hell froze over."),
/// }
/// }
/// ```
pub fn msg_sender() -> Result<Identity, AuthError> {
if caller_is_external() {
inputs_owner()
match caller_address() {
Err(err) => Err(err),
Ok(owner) => Ok(Identity::Address(owner)),
}
} else {
// Get caller's `ContractId`.
Ok(Identity::ContractId(caller_contract_id()))
Expand All @@ -108,13 +115,9 @@ pub fn msg_sender() -> Result<Identity, AuthError> {
/// Get the owner of the inputs (of type `Input::Coin` or `Input::Message`) to a
/// `TransactionScript` if they all share the same owner.
///
/// # Additional Information
///
/// Will never return a Ok(Identity::ContractId).
///
/// # Returns
///
/// * [Result<Identity, AuthError>] - `Ok(Identity)` if the owner can be determined, `Err(AuthError)` otherwise.
/// * [Result<Address, AuthError>] - `Ok(Address)` if the owner can be determined, `Err(AuthError)` otherwise.
///
/// # Examples
///
Expand All @@ -123,13 +126,12 @@ pub fn msg_sender() -> Result<Identity, AuthError> {
///
/// fn foo() {
/// match inputs_owner() {
/// Ok(Identity::Address(address)) => log(address),
/// Ok(Identity::ContractId(_)) => log("Hell froze over."),
/// Ok(address) => log(address),
/// Err(AuthError::InputsNotAllOwnedBySameAddress) => log("Inputs not all owned by same address."),
/// }
/// }
/// ```
fn inputs_owner() -> Result<Identity, AuthError> {
pub fn caller_address() -> Result<Address, AuthError> {
let inputs = input_count();
let mut candidate = None;
let mut i = 0u8;
Expand Down Expand Up @@ -169,6 +171,9 @@ fn inputs_owner() -> Result<Identity, AuthError> {
return Err(AuthError::InputsNotAllOwnedBySameAddress);
}

// `candidate` must be `Some` at this point, so can unwrap safely.
Ok(Identity::Address(candidate.unwrap()))
// `candidate` must be `Some` if the caller is an address, otherwise it's a contract.
match candidate {
Some(address) => Ok(address),
None => Err(AuthError::CallerIsInternal),
}
}

0 comments on commit 15aa402

Please sign in to comment.