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

Increase Safety when introspecting transaction inputs #6405

Merged
merged 17 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
44 changes: 20 additions & 24 deletions sway-lib-std/src/auth.sw
Original file line number Diff line number Diff line change
Expand Up @@ -140,42 +140,42 @@ pub fn msg_sender() -> Result<Identity, AuthError> {
/// }
/// ```
pub fn caller_address() -> Result<Address, AuthError> {
let inputs = input_count();
let inputs = input_count().as_u64();
let mut candidate = None;
let mut i = 0u16;
let mut iter = 0;

// Note: `inputs_count` is guaranteed to be at least 1 for any valid tx.
while i < inputs {
let type_of_input = input_type(i.as_u64());
while iter < inputs {
let type_of_input = input_type(iter);
match type_of_input {
Input::Coin => (),
Input::Message => (),
Some(Input::Coin) => (),
Some(Input::Message) => (),
_ => {
// type != InputCoin or InputMessage, continue looping.
i += 1u16;
iter += 1;
continue;
}
}

// type == InputCoin or InputMessage.
let owner_of_input = match type_of_input {
Input::Coin => {
input_coin_owner(i.as_u64())
Some(Input::Coin) => {
input_coin_owner(iter)
},
Input::Message => {
Some(input_message_sender(i.as_u64()))
Some(Input::Message) => {
input_message_sender(iter)
},
_ => {
// type != InputCoin or InputMessage, continue looping.
i += 1u16;
iter += 1;
continue;
}
};

if candidate.is_none() {
// This is the first input seen of the correct type.
candidate = owner_of_input;
i += 1u16;
iter += 1;
continue;
}

Expand All @@ -184,7 +184,7 @@ pub fn caller_address() -> Result<Address, AuthError> {
// at this point, so we can unwrap safely.
if owner_of_input.unwrap() == candidate.unwrap() {
// Owners are a match, continue looping.
i += 1u16;
iter += 1;
continue;
}

Expand All @@ -203,23 +203,19 @@ pub fn caller_address() -> Result<Address, AuthError> {
///
/// # Returns
///
/// * [Address] - The address of this predicate.
///
/// # Reverts
///
/// * When called outside of a predicate program.
/// * [Option<Address>] - The address of this predicate.
///
/// # Examples
///
/// ```sway
/// use std::auth::predicate_address;
///
/// fn main() {
/// let this_predicate = predicate_address();
/// let this_predicate = predicate_address().unwrap();
/// log(this_predicate);
/// }
/// ```
pub fn predicate_address() -> Address {
pub fn predicate_address() -> Option<Address> {
// Get index of current predicate.
// i3 = GM_GET_VERIFYING_PREDICATE
let predicate_index = asm(r1) {
Expand All @@ -230,10 +226,10 @@ pub fn predicate_address() -> Address {
let type_of_input = input_type(predicate_index);

match type_of_input {
Input::Coin => input_coin_owner(predicate_index).unwrap(),
Input::Message => input_message_recipient(predicate_index),
Some(Input::Coin) => input_coin_owner(predicate_index),
Some(Input::Message) => input_message_recipient(predicate_index),
_ => {
revert(0)
None
}
}
}
Loading
Loading