Skip to content

Commit

Permalink
Merge branch 'master' into kayagokalp/proxy-with-chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
sdankel authored Aug 16, 2024
2 parents 3688741 + 3700e3f commit d2562a5
Show file tree
Hide file tree
Showing 11 changed files with 426 additions and 272 deletions.
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
}
}
}
29 changes: 17 additions & 12 deletions sway-lib-std/src/ecr.sw
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ library;

use ::address::Address;
use ::b512::B512;
use ::bytes::Bytes;
use ::registers::error;
use ::hash::*;
use ::result::Result::{self, *};
Expand All @@ -11,6 +12,8 @@ use ::result::Result::{self, *};
pub enum EcRecoverError {
/// The error variant used when the recover fails.
UnrecoverablePublicKey: (),
/// The length of the message is zero.
ZeroLengthMessage: (),
}

/// Recover the public key derived from the private key used to sign a message.
Expand Down Expand Up @@ -129,8 +132,8 @@ pub fn ec_recover_r1(signature: B512, msg_hash: b256) -> Result<B512, EcRecoverE
/// # Arguments
///
/// * `public_key`: [b256] - The public key that signed the message.
/// * `signature`: [B512] - The signature generated by signing a message hash.
/// * `msg_hash`: [b256] - The hashed signed data.
/// * `signature`: [B512] - The signature generated by signing a message.
/// * `msg`: [Bytes] - The signed data.
///
/// # Returns
///
Expand All @@ -139,7 +142,7 @@ pub fn ec_recover_r1(signature: B512, msg_hash: b256) -> Result<B512, EcRecoverE
/// # Examples
///
/// ```sway
/// use std::{ecr::ed_verify, b512::B512};
/// use std::{ecr::ed_verify, b512::B512, bytes::Bytes};
///
/// fn foo() {
/// let pub_key = 0x314fa58689bbe1da2430517de2d772b384a1c1d2e9cb87e73c6afcf246045b10;
Expand All @@ -150,22 +153,24 @@ pub fn ec_recover_r1(signature: B512, msg_hash: b256) -> Result<B512, EcRecoverE
/// let lo = 0xf5084560039486d3462dd65a40c80a74709b2f06d450ffc5dc00345c6b2cdd00;
/// let signature: B512 = B512::from((hi, lo));
/// // A verified public key with signature
/// let verified = ed_verify(pub_key, signature, msg_hash).unwrap();
/// let verified = ed_verify(pub_key, signature, Bytes::from(msg_hash)).unwrap();
/// assert(verified);
/// }
/// ```
pub fn ed_verify(
public_key: b256,
signature: B512,
msg_hash: b256,
) -> Result<bool, EcRecoverError> {
pub fn ed_verify(public_key: b256, signature: B512, msg: Bytes) -> Result<bool, EcRecoverError> {
let len = msg.len();

if len == 0 {
return Err(EcRecoverError::ZeroLengthMessage);
};

let was_error = asm(
buffer: public_key,
sig: __addr_of(signature),
hash: msg_hash,
len: 32,
msg: msg.ptr(),
len: len,
) {
ed19 buffer sig hash len;
ed19 buffer sig msg len;
err
};
// check the $err register to see if the `ed19` opcode succeeded
Expand Down
Loading

0 comments on commit d2562a5

Please sign in to comment.