Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Caller addresses function #6514

Merged
merged 18 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 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,60 @@ 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> {
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
let inputs = input_count().as_u64();
let mut addresses = Vec::new();
let mut iter = 0;

while iter < inputs {
let type_of_input = input_type(iter);
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
match type_of_input {
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
Some(Input::Coin) | Some(Input::Message) => {
// If input type is Coin or Message, get the owner address.
let owner_of_input = match type_of_input {
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
Some(Input::Coin) => input_coin_owner(iter),
Some(Input::Message) => input_message_sender(iter),
_ => None, // Shouldn't reach this case due to outer match.
};

// If we successfully retrieved an owner address, add it to the vector.
if let Some(address) = owner_of_input {
addresses.push(address);
}
},
_ => {
// Input type is neither Coin nor Message, continue looping.
}
}

iter += 1;
}

addresses // Return the collected addresses.
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
}

/// 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
Loading