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

dev functions for deployment #2

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
target
dev_signer
dev_account
dev_account
sepolia
sepolia_account
1 change: 1 addition & 0 deletions .snfoundry_cache/.prev_tests_failed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rosettacontracts_integrationtest::factory_tests::deploy_check_initials
25 changes: 21 additions & 4 deletions src/accounts/base.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub type EthPublicKey = starknet::secp256k1::Secp256k1Point;
use starknet::EthAddress;
#[starknet::interface]
pub trait IRosettaAccount<TState> {
fn __execute__(self: @TState, calls: Array<felt252>) -> Array<Span<felt252>>;
Expand All @@ -10,19 +11,21 @@ pub trait IRosettaAccount<TState> {
self: @TState, class_hash: felt252, contract_address_salt: felt252, public_key: EthPublicKey
) -> felt252;
fn get_public_key(self: @TState) -> EthPublicKey;
fn get_ethereum_address(self: @TState) -> EthAddress;
fn set_public_key(ref self: TState, new_public_key: EthPublicKey, signature: Span<felt252>);
// Camel case
fn isValidSignature(self: @TState, hash: felt252, signature: Array<felt252>) -> felt252;
fn getPublicKey(self: @TState) -> EthPublicKey;
fn getEthereumAddress(self: @TState) -> EthAddress;
fn setPublicKey(ref self: TState, newPublicKey: EthPublicKey, signature: Span<felt252>);
}

#[starknet::contract(account)]
mod RosettaAccount {
pub mod RosettaAccount {
use super::EthPublicKey;
use core::num::traits::Zero;
use starknet::{
EthAddress, get_execution_info, get_contract_address, get_caller_address, get_tx_info
EthAddress, get_contract_address, get_caller_address, get_tx_info
};
use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};
use rosettacontracts::accounts::utils::{is_valid_eth_signature, Secp256k1PointStorePacking};
Expand All @@ -45,17 +48,23 @@ mod RosettaAccount {
fn constructor(ref self: ContractState, eth_account: EthAddress) {
self.ethereum_address.write(eth_account);
}

// TODO: Raw transaction tx.signature da, __execute__ parametresindede bit locationlar mı olacak??
#[abi(embed_v0)]
impl AccountImpl of super::IRosettaAccount<ContractState> {
// Instead of Array<Call> we use Array<felt252> since we pass different values to the
// parameter
// It is EOA execution so multiple calls are not possible
// calls params can include raw signed tx or can include the abi parsing bit locations for calldata
fn __execute__(self: @ContractState, calls: Array<felt252>) -> Array<Span<felt252>> {
let sender = get_caller_address();
assert(sender.is_zero(), Errors::INVALID_CALLER);
// TODO: Check tx version

// TODO: Exec calls
// TODO: Exec call

// 1) Deserialize tx data from signature to get calldata and target contract.
// 2) Match the entrypoint and call contract with calldata parsed according this function bit size param
array![array!['todo'].span()]
}

fn __validate__(self: @ContractState, calls: Array<felt252>) -> felt252 {
Expand Down Expand Up @@ -96,6 +105,10 @@ mod RosettaAccount {
self.ethereum_public_key.read()
}

fn get_ethereum_address(self: @ContractState) -> EthAddress {
self.ethereum_address.read()
}

// We dont need that function
fn set_public_key(
ref self: ContractState, new_public_key: EthPublicKey, signature: Span<felt252>
Expand All @@ -111,6 +124,10 @@ mod RosettaAccount {
self.get_public_key()
}

fn getEthereumAddress(self: @ContractState) -> EthAddress {
self.get_ethereum_address()
}

// We dont need that function
fn setPublicKey(
ref self: ContractState, newPublicKey: EthPublicKey, signature: Span<felt252>
Expand Down
32 changes: 26 additions & 6 deletions src/factory.cairo
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
use starknet::{ContractAddress, ClassHash, EthAddress};

#[starknet::interface]
trait IFactory<TContractState> {
pub trait IFactory<TContractState> {
fn lens(self: @TContractState) -> ContractAddress;
fn current_account_class(self: @TContractState) -> ClassHash;
fn precalculate_starknet_address(self: @TContractState, address: EthAddress) -> ContractAddress;
fn deploy_account(self: @TContractState, address: EthAddress) -> ContractAddress;
fn upgrade_contract(self: @TContractState, new_class: ClassHash);
fn change_account_class(ref self: TContractState, new_class: ClassHash);
}

#[starknet::contract]
mod Factory {
pub mod Factory {
use core::option::OptionTrait;
use starknet::{ContractAddress, ClassHash, EthAddress};
use starknet::syscalls::{deploy_syscall};
use starknet::{ContractAddress, ClassHash, EthAddress, get_caller_address};
use starknet::syscalls::{deploy_syscall, replace_class_syscall};
use core::traits::{Into, TryInto};
use openzeppelin::utils::deployments::{calculate_contract_address_from_deploy_syscall};
use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};

#[storage]
struct Storage {
account_class: ClassHash,
lens: ContractAddress
lens: ContractAddress,
dev: ContractAddress
}

#[constructor]
fn constructor(ref self: ContractState, lens: ContractAddress, account_class: ClassHash) {
fn constructor(ref self: ContractState, lens: ContractAddress, account_class: ClassHash, dev: ContractAddress) {
self.account_class.write(account_class);
self.lens.write(lens);
self.dev.write(dev);
}

#[abi(embed_v0)]
Expand Down Expand Up @@ -60,6 +65,7 @@ mod Factory {
)
}

// TODO: this funcation can be removed
/// Deploys new rosettanet account. Fails if account already deployed
/// # Params
/// `address` - Ethereum Address that will be used to deploy starknet account.
Expand All @@ -77,5 +83,19 @@ mod Factory {
// Todo: register lens if needed ?? Or we can use precalculate
account
}

// REMOVE THIS FUNCTION AFTER DEVELOPMENT
fn upgrade_contract(self: @ContractState, new_class: ClassHash) {
assert(get_caller_address() == self.dev.read(), 'only dev');

replace_class_syscall(new_class).unwrap();
}

// REMOVE THIS FUNCTION AFTER DEVELOPMENT
fn change_account_class(ref self: ContractState, new_class: ClassHash) {
assert(get_caller_address() == self.dev.read(), 'only dev');

self.account_class.write(new_class);
}
}
}
8 changes: 4 additions & 4 deletions src/lens/lens_dev.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ mod LensDev {
use super::ILensDev;
use starknet::{ContractAddress, EthAddress};
use core::poseidon::PoseidonTrait;
use core::hash::{HashStateTrait, HashStateExTrait};
use core::hash::{HashStateTrait};
use core::traits::{Into, TryInto};
use core::num::traits::{Zero};

use starknet::storage::{Map};

#[storage]
struct Storage {
eth_address_to_sn_address: LegacyMap::<EthAddress, ContractAddress>,
sn_address_to_eth_address: LegacyMap::<ContractAddress, EthAddress>,
eth_address_to_sn_address: Map<EthAddress, ContractAddress>,
sn_address_to_eth_address: Map<ContractAddress, EthAddress>,
}

#[constructor]
Expand Down
29 changes: 29 additions & 0 deletions tests/factory_tests.cairo
Original file line number Diff line number Diff line change
@@ -1 +1,30 @@
use openzeppelin_utils::serde::SerializedAppend;
use rosettacontracts::accounts::base::{RosettaAccount, IRosettaAccountDispatcher, IRosettaAccountDispatcherTrait};
use rosettacontracts::factory::{Factory, IFactoryDispatcher, IFactoryDispatcherTrait};
use snforge_std::{declare, get_class_hash, ContractClassTrait, DeclareResultTrait};
use starknet::{EthAddress};

#[test]
fn deploy_check_initials() {
let account_contract_class = declare("RosettaAccount").unwrap().contract_class();

let factory_contract_class = declare("Factory").unwrap().contract_class();

let mut factory_calldata = array![];

factory_calldata.append_serde('123123');
factory_calldata.append_serde('0');

let (factory_contract_address, _) = factory_contract_class.deploy(@factory_calldata).unwrap();

let factory_dispatcher = IFactoryDispatcher { contract_address: factory_contract_address };

let ethereum_address: EthAddress = 0x11655f4Ee2A5B66F9DCbe758e9FcdCd3eBF95eE5.try_into().unwrap();

let precalculated_address = factory_dispatcher.precalculate_starknet_address(ethereum_address);

let (account_contract_address, _) = account_contract_class.deploy(@array![ethereum_address.into()]).unwrap();

assert(precalculated_address == account_contract_address, 'precalculated address wrong');
// TODO: burayı fixle neden hata dönüyor ve debug print ekle
}
5 changes: 5 additions & 0 deletions tests/test_data.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Eth Account used in tests
0x11655f4Ee2A5B66F9DCbe758e9FcdCd3eBF95eE5
### Test transaction
[ ] Transfer or approve of standard erc20 in starknet
[ ] Account contruction etc.
Loading