Skip to content

Commit

Permalink
Merge pull request #30 from ermvrs/main
Browse files Browse the repository at this point in the history
Calldata validation tests
  • Loading branch information
ermvrs authored Dec 11, 2024
2 parents 474f9df + 119b6bf commit 8b68784
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .snfoundry_cache/.prev_tests_failed
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rosettacontracts_integrationtest::accounts_tests::test_validation_with_access_list
rosettacontracts::accounts::encoding::tests::rlp_encode_access_list
1 change: 1 addition & 0 deletions src/accounts/base.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ pub mod RosettaAccount {
if(current_directive == 2_u8) {
let eth_address: EthAddress = (*calldata.at(index)).try_into().unwrap();
let sn_address = IRosettanetDispatcher{contract_address: self.registry.read()}.get_starknet_address(eth_address);
assert(sn_address != starknet::contract_address_const::<0>(), 'calldata address not registered');
updated_array.append(sn_address.into());
} else {
updated_array.append(*calldata.at(index));
Expand Down
32 changes: 32 additions & 0 deletions src/accounts/encoding.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ pub fn deserialize_bytes(value: felt252, len: usize) -> Span<u8> {
ba.into_bytes()
}

pub fn deserialize_u256_with_zeroes(value:u256) -> Span<u8> {
let mut ba: core::byte_array::ByteArray = Default::default();
let low_bytes = deserialize_bytes(value.low.into(), 16);
let high_bytes = deserialize_bytes(value.high.into(), 16);
ba.append(@ByteArrayExTrait::from_bytes(low_bytes));
ba.append(@ByteArrayExTrait::from_bytes(high_bytes));
ba.into_bytes()
}

pub fn deserialize_u256(value: u256) -> Span<u8> {
// Bu fonksiyonu tamamla
let mut ba: core::byte_array::ByteArray = Default::default();
Expand Down Expand Up @@ -124,6 +133,7 @@ pub fn deserialize_bytes_non_zeroes(value: felt252, len: usize) -> Span<u8> {
#[cfg(test)]
mod tests {
use crate::accounts::encoding::{Eip1559Transaction, rlp_encode_eip1559, deserialize_bytes_non_zeroes, bytes_from_felts, deserialize_u256, deserialize_u256_span};
use crate::utils::transaction::eip2930::{AccessListItem};
use core::num::traits::{Bounded};

#[test]
Expand All @@ -147,6 +157,28 @@ mod tests {
assert_eq!(*encoded.at(3), 0x0B);
assert_eq!(*encoded.at(4), 0x75);
}

#[test]
fn rlp_encode_access_list() {
let access_list_item = AccessListItem {
ethereum_address: 0x5703ff58bB0CA34F870a8bC18dDd541f29375978.try_into().unwrap(),
storage_keys: array![0_u256, 1_u256].span()
};
let tx = Eip1559Transaction {
chain_id: 11155111,
nonce: 87,
max_priority_fee_per_gas: 1638611,
max_fee_per_gas: 16357352599,
gas_limit: 21000,
to: 0xC7f5D5D3725f36CF36477B84010EB8DdE42D3636.try_into().unwrap(),
value: 0x0,
input: array![0xf4,0xac,0xc7,0xb5].span(),
access_list: array![access_list_item].span(),
};

let encoded = rlp_encode_eip1559(tx);
assert_eq!(encoded.len(), 142);
}

#[test]
fn rlp_encode_transaction_value() {
Expand Down
7 changes: 6 additions & 1 deletion src/accounts/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::accounts::encoding::{Eip1559Transaction, deserialize_bytes, deseriali
use crate::utils::constants::{POW_2_250};
use crate::utils::traits::SpanDefault;
use crate::utils::bytes::{U8SpanExTrait, ByteArrayExTrait};
use crate::utils::transaction::eip2930::{AccessListItem};
use starknet::eth_signature::{verify_eth_signature};

pub const CHAIN_ID: u64 = 11155111; // TODO: Correct it
Expand All @@ -24,6 +25,7 @@ pub struct RosettanetCall {
pub gas_limit: u64,
pub value: u256, // To be used future
pub calldata: Span<felt252>, // Calldata len must be +1 directive len
pub access_list: Span<AccessListItem>,
pub directives: Span<u8>, // 0 -> do nothing, 1 -> u256, 2-> address
pub target_function: Span<felt252> // Function name and types to used to calculate eth func signature
}
Expand Down Expand Up @@ -62,7 +64,7 @@ pub fn parse_transaction(call: RosettanetCall) -> Eip1559Transaction {
gas_limit: call.gas_limit,
to: call.to,
value: call.value,
access_list: array![].span(), // Do we need these?
access_list: call.access_list,
input: calldata_bytes
};

Expand Down Expand Up @@ -209,6 +211,7 @@ mod tests {
gas_limit: 21000,
value: 1,
calldata: array![].span(),
access_list: array![].span(),
directives: array![].span(),
target_function: array![].span()
};
Expand All @@ -231,6 +234,7 @@ mod tests {
gas_limit: 21000,
value: 0,
calldata: calldata,
access_list: array![].span(),
directives: directives,
target_function: target_function
};
Expand All @@ -255,6 +259,7 @@ mod tests {
gas_limit: 21000,
value: 0,
calldata: calldata,
access_list: array![].span(),
directives: directives,
target_function: target_function
};
Expand Down
16 changes: 16 additions & 0 deletions src/rosettanet.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub trait IRosettanet<TState> {
fn register_contract(ref self: TState, address: ContractAddress); // Registers existing starknet contract to registry
fn deploy_account(ref self: TState, eth_address: EthAddress) -> ContractAddress; // Deploys starknet account and returns address
fn set_account_class(ref self: TState, class: ClassHash); // Sets account class, this function will be removed after stable account
fn register_matched_addresses(ref self: TState, sn_address: ContractAddress, eth_address: EthAddress); // Will be used during alpha
fn upgrade(ref self: TState, class: ClassHash); // Upgrades contract
// Read methods
fn get_starknet_address(self: @TState, eth_address: EthAddress) -> ContractAddress;
Expand Down Expand Up @@ -71,6 +72,9 @@ pub mod Rosettanet {
fn constructor(ref self: ContractState, developer: ContractAddress, strk: ContractAddress) {
self.dev.write(developer);
self.strk.write(strk);

let strk_eth_address = self.generate_eth_address(strk);
self.update_registry(strk, strk_eth_address);
}

#[abi(embed_v0)]
Expand Down Expand Up @@ -114,6 +118,18 @@ pub mod Rosettanet {
self.emit(AccountClassChanged {changer: get_caller_address(), new_class: class});
}

/// Updates registry without generating eth address
/// # Arguments
/// * `sn_address` - Starknet address
/// * `eth_address` - Ethereum address
fn register_matched_addresses(ref self: ContractState, sn_address: ContractAddress, eth_address: EthAddress) {
assert(get_caller_address() == self.dev.read(), 'only dev');

self.update_registry(sn_address, eth_address);

self.emit(AddressRegistered {sn_address: sn_address, eth_address: eth_address});
}

/// Updates this contracts class
/// # Arguments
/// * `class` - New class hash
Expand Down
6 changes: 3 additions & 3 deletions src/utils/transaction/eip2930.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::starknet::EthAddress;
use crate::utils::transaction::common::TxKind;
use alexandria_encoding::rlp::{RLPItem};
use crate::utils::traits::SpanDefault;
use crate::accounts::encoding::{deserialize_bytes};
use crate::accounts::encoding::{deserialize_bytes, deserialize_u256_with_zeroes};


#[derive(Copy, Drop, Serde, PartialEq, Debug)]
Expand All @@ -28,8 +28,8 @@ pub impl AccessListItemImpl of AccessListItemTrait {
let AccessListItem { ethereum_address, mut storage_keys } = *self;

let mut storage_keys_arr = array![];
for storage_key in storage_keys { // deserialize u256 kullan TODO
storage_keys_arr.append(RLPItem::String(deserialize_bytes((*storage_key).try_into().unwrap(), 32))); // TODO deserialize 32 bytes olmaz 16 low high al
for storage_key in storage_keys {
storage_keys_arr.append(RLPItem::String(deserialize_u256_with_zeroes(*storage_key)));
};

let addr = RLPItem::String(deserialize_bytes(ethereum_address.into(), 20));
Expand Down
Loading

0 comments on commit 8b68784

Please sign in to comment.