Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
feat: recover eth address cairo1 helper (#780)
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat authored Apr 19, 2024
1 parent 1dc5269 commit 291e45a
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions crates/contracts/src/cairo1_helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,16 @@ pub trait IHelpers<T> {
self: @T, words: Array<u64>, last_input_word: u64, last_input_num_bytes: usize
) -> u256;

fn verify_eth_signature(
self: @T, msg_hash: u256, signature: Signature, eth_address: EthAddress
);
/// Recovers the Ethereum address from a message hash and a signature.
///
/// # Arguments
///
/// * `msg_hash` - The hash of the message.
/// * `signature` - The signature to recover the address from.
///
/// # Returns
/// The Ethereum address recovered from the signature.
fn recover_eth_address(self: @T, msg_hash: u256, signature: Signature) -> EthAddress;
}


Expand All @@ -69,7 +76,9 @@ mod embeddable_impls {
use evm::precompiles::modexp::ModExp;
use evm::precompiles::sha256::Sha256;
use starknet::EthAddress;
use starknet::eth_signature::{Signature, verify_eth_signature};
use starknet::eth_signature::{Signature, public_key_point_to_eth_address};
use starknet::secp256_trait::{recover_public_key};
use starknet::secp256k1::Secp256k1Point;
use utils::helpers::U256Trait;


Expand Down Expand Up @@ -108,10 +117,12 @@ mod embeddable_impls {
cairo_keccak(ref words, last_input_word, last_input_num_bytes).reverse_endianness()
}

fn verify_eth_signature(
self: @TContractState, msg_hash: u256, signature: Signature, eth_address: EthAddress
) {
verify_eth_signature(msg_hash, signature, eth_address);
fn recover_eth_address(
self: @TContractState, msg_hash: u256, signature: Signature
) -> EthAddress {
let public_key_point = recover_public_key::<Secp256k1Point>(:msg_hash, :signature)
.unwrap();
public_key_point_to_eth_address(:public_key_point)
}
}
}
Expand Down

0 comments on commit 291e45a

Please sign in to comment.