Skip to content

Commit

Permalink
Add Caller addresses function (#6514)
Browse files Browse the repository at this point in the history
## Description
Adds a new `caller_addresses` function to return a list of all owners of
input coins and messages to a transaction

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] 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).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Sophie Dankel <47993817+sdankel@users.noreply.github.com>
  • Loading branch information
SwayStar123 and sdankel authored Sep 12, 2024
1 parent fa3dafa commit 0fa1b9a
Show file tree
Hide file tree
Showing 4 changed files with 490 additions and 3 deletions.
56 changes: 55 additions & 1 deletion sway-lib-std/src/auth.sw
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ use ::contract_id::ContractId;
use ::identity::Identity;
use ::option::Option::{self, *};
use ::result::Result::{self, *};
use ::inputs::{Input, input_coin_owner, input_count, input_message_recipient, input_type,};
use ::inputs::{
Input,
input_coin_owner,
input_count,
input_message_recipient,
input_message_sender,
input_type,
};
use ::revert::revert;
use ::vec::Vec;

/// The error type used when an `Identity` cannot be determined.
pub enum AuthError {
Expand Down Expand Up @@ -192,6 +200,52 @@ pub fn caller_address() -> Result<Address, AuthError> {
}
}

/// Get the owners of the inputs (of type `Input::Coin` or `Input::Message`) to a
/// `TransactionScript`.
///
/// # Additional Information
///
/// The list is not deduplicated, so there may be repeated addresses in the returned vector.
///
/// # Returns
///
/// * [Vec<Address>] - The addresses of the owners of the inputs.
///
/// # Examples
///
/// ```sway
/// use std::auth::caller_addresses;
///
/// fn foo(some_address: Address) {
/// let addresses = caller_addresses();
///
/// assert(addresses.get(0).unwrap() == some_address);
/// }
/// ```
pub fn caller_addresses() -> Vec<Address> {
let inputs = input_count().as_u64();
let mut addresses = Vec::new();
let mut iter = 0;

while iter < inputs {
// Call the corresponding function based on the input type.
let input_owner = match input_type(iter) {
Some(Input::Coin) => input_coin_owner(iter),
Some(Input::Message) => input_message_sender(iter),
_ => None, // If not Coin or Message, loop continues.
};

// If we successfully retrieved an owner address, add it to the vector.
if let Some(address) = input_owner {
addresses.push(address);
}

iter += 1;
}

addresses
}

/// Get the current predicate's address when called in an internal context.
///
/// # Returns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ abi AuthTesting {
fn is_caller_external() -> bool;
fn returns_msg_sender(expected_id: ContractId) -> bool;
fn returns_msg_sender_address(expected_id: Address) -> bool;
fn returns_caller_addresses() -> Vec<Address>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ impl AuthTesting for Contract {
}
ret
}

fn returns_caller_addresses() -> Vec<Address> {
caller_addresses()
}
}
Loading

0 comments on commit 0fa1b9a

Please sign in to comment.