Skip to content

Commit

Permalink
Fix account tests after merge
Browse files Browse the repository at this point in the history
  • Loading branch information
immrsd committed Aug 13, 2024
1 parent 5d41e21 commit 5ef9f96
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 71 deletions.
99 changes: 44 additions & 55 deletions packages/account/src/tests/test_dual_eth_account.cairo
Original file line number Diff line number Diff line change
@@ -1,46 +1,40 @@
use openzeppelin_account::dual_eth_account::{DualCaseEthAccountABI, DualCaseEthAccount};
use openzeppelin_account::dual_eth_account::DualCaseEthAccount;
use openzeppelin_account::interface::{EthAccountABIDispatcherTrait, EthAccountABIDispatcher};
use openzeppelin_account::utils::secp256k1::{DebugSecp256k1Point, Secp256k1PointPartialEq};
use openzeppelin_introspection::interface::ISRC5_ID;

use openzeppelin_test_common::eth_account::get_accept_ownership_signature;
use openzeppelin_testing as utils;
use openzeppelin_testing::constants::secp256k1::KEY_PAIR;
use openzeppelin_testing::constants::{ETH_PUBKEY, NEW_ETH_PUBKEY, TRANSACTION_HASH};
use openzeppelin_testing::signing::Secp256k1SerializedSigning;
use openzeppelin_testing::constants::TRANSACTION_HASH;
use openzeppelin_testing::constants::secp256k1::{KEY_PAIR, KEY_PAIR_2};
use openzeppelin_testing::signing::{Secp256k1KeyPair, Secp256k1SerializedSigning};
use openzeppelin_utils::serde::SerializedAppend;
use snforge_std::start_cheat_caller_address;

//
// Setup
//

fn setup_snake() -> (DualCaseEthAccount, EthAccountABIDispatcher) {
fn setup_snake(key_pair: Secp256k1KeyPair) -> (DualCaseEthAccount, EthAccountABIDispatcher) {
let mut calldata = array![];
calldata.append_serde(ETH_PUBKEY());
calldata.append_serde(key_pair.public_key);

let target = utils::declare_and_deploy("SnakeEthAccountMock", calldata);
(
DualCaseEthAccount { contract_address: target },
EthAccountABIDispatcher { contract_address: target }
)
let contract_address = utils::declare_and_deploy("SnakeEthAccountMock", calldata);
(DualCaseEthAccount { contract_address }, EthAccountABIDispatcher { contract_address })
}

fn setup_camel() -> (DualCaseEthAccount, EthAccountABIDispatcher) {
fn setup_camel(key_pair: Secp256k1KeyPair) -> (DualCaseEthAccount, EthAccountABIDispatcher) {
let mut calldata = array![];
calldata.append_serde(ETH_PUBKEY());
calldata.append_serde(key_pair.public_key);

let target = utils::declare_and_deploy("CamelEthAccountMock", calldata);
(
DualCaseEthAccount { contract_address: target },
EthAccountABIDispatcher { contract_address: target }
)
let contract_address = utils::declare_and_deploy("CamelEthAccountMock", calldata);
(DualCaseEthAccount { contract_address }, EthAccountABIDispatcher { contract_address })
}

fn setup_non_account() -> DualCaseEthAccount {
let calldata = array![];
let target = utils::declare_and_deploy("NonImplementingMock", calldata);
DualCaseEthAccount { contract_address: target }
let contract_address = utils::declare_and_deploy("NonImplementingMock", calldata);
DualCaseEthAccount { contract_address }
}

fn setup_account_panic() -> (DualCaseEthAccount, DualCaseEthAccount) {
Expand All @@ -58,37 +52,40 @@ fn setup_account_panic() -> (DualCaseEthAccount, DualCaseEthAccount) {

#[test]
fn test_dual_set_public_key() {
let (snake_dispatcher, target) = setup_snake();
let key_pair = KEY_PAIR();
let (snake_dispatcher, target) = setup_snake(key_pair);
let contract_address = snake_dispatcher.contract_address;

let new_key_pair = KEY_PAIR_2();
start_cheat_caller_address(contract_address, contract_address);
let signature = get_accept_ownership_signature(
contract_address, key_pair.public_key, new_key_pair
);
snake_dispatcher.set_public_key(new_key_pair.public_key, signature);

let key_pair = KEY_PAIR();
let signature = get_accept_ownership_signature(contract_address, ETH_PUBKEY(), key_pair);

snake_dispatcher.set_public_key(key_pair.public_key, signature);
assert_eq!(target.get_public_key(), key_pair.public_key);
assert_eq!(target.get_public_key(), new_key_pair.public_key);
}

#[test]
#[ignore] // REASON: should_panic attribute not fit for complex panic messages.
#[should_panic(expected: ('ENTRYPOINT_NOT_FOUND',))]
fn test_dual_no_set_public_key() {
let dispatcher = setup_non_account();
dispatcher.set_public_key(NEW_ETH_PUBKEY(), array![].span());
dispatcher.set_public_key(KEY_PAIR().public_key, array![].span());
}

#[test]
#[should_panic(expected: ("Some error",))]
fn test_dual_set_public_key_exists_and_panics() {
let (dispatcher, _) = setup_account_panic();
dispatcher.set_public_key(NEW_ETH_PUBKEY(), array![].span());
dispatcher.set_public_key(KEY_PAIR().public_key, array![].span());
}

#[test]
fn test_dual_get_public_key() {
let (snake_dispatcher, _) = setup_snake();
assert_eq!(snake_dispatcher.get_public_key(), ETH_PUBKEY());
let key_pair = KEY_PAIR();
let (snake_dispatcher, _) = setup_snake(key_pair);
assert_eq!(snake_dispatcher.get_public_key(), key_pair.public_key);
}

#[test]
Expand All @@ -108,16 +105,11 @@ fn test_dual_get_public_key_exists_and_panics() {

#[test]
fn test_dual_is_valid_signature() {
let (snake_dispatcher, target) = setup_snake();
let contract_address = snake_dispatcher.contract_address;

start_cheat_caller_address(contract_address, contract_address);
let key_pair = KEY_PAIR();
let signature = get_accept_ownership_signature(contract_address, ETH_PUBKEY(), key_pair);
let (snake_dispatcher, target) = setup_snake(key_pair);
let contract_address = snake_dispatcher.contract_address;

target.set_public_key(key_pair.public_key, signature);
let serialized_signature = key_pair.serialized_sign(TRANSACTION_HASH.into());

let is_valid = snake_dispatcher.is_valid_signature(TRANSACTION_HASH, serialized_signature);
assert_eq!(is_valid, starknet::VALIDATED);
}
Expand Down Expand Up @@ -145,7 +137,7 @@ fn test_dual_is_valid_signature_exists_and_panics() {

#[test]
fn test_dual_supports_interface() {
let (snake_dispatcher, _) = setup_snake();
let (snake_dispatcher, _) = setup_snake(KEY_PAIR());
assert!(snake_dispatcher.supports_interface(ISRC5_ID), "Should implement ISRC5");
}

Expand All @@ -171,31 +163,34 @@ fn test_dual_supports_interface_exists_and_panics() {
#[test]
#[ignore] // REASON: foundry entrypoint_not_found error message inconsistent with mainnet.
fn test_dual_setPublicKey() {
let (camel_dispatcher, target) = setup_camel();
let key_pair = KEY_PAIR();
let (camel_dispatcher, target) = setup_camel(key_pair);
let contract_address = camel_dispatcher.contract_address;

start_cheat_caller_address(contract_address, contract_address);
let new_key_pair = KEY_PAIR_2();
let signature = get_accept_ownership_signature(
contract_address, key_pair.public_key, new_key_pair
);

let key_pair = KEY_PAIR();
let signature = get_accept_ownership_signature(contract_address, ETH_PUBKEY(), key_pair);

camel_dispatcher.set_public_key(key_pair.public_key, signature);
assert_eq!(target.getPublicKey(), key_pair.public_key);
camel_dispatcher.set_public_key(new_key_pair.public_key, signature);
assert_eq!(target.getPublicKey(), new_key_pair.public_key);
}

#[test]
#[ignore] // REASON: foundry entrypoint_not_found error message inconsistent with mainnet.
#[should_panic(expected: ("Some error",))]
fn test_dual_setPublicKey_exists_and_panics() {
let (_, dispatcher) = setup_account_panic();
dispatcher.set_public_key(NEW_ETH_PUBKEY(), array![].span());
dispatcher.set_public_key(KEY_PAIR_2().public_key, array![].span());
}

#[test]
#[ignore] // REASON: foundry entrypoint_not_found error message inconsistent with mainnet.
fn test_dual_getPublicKey() {
let (camel_dispatcher, _) = setup_camel();
assert_eq!(camel_dispatcher.get_public_key(), ETH_PUBKEY());
let key_pair = KEY_PAIR();
let (camel_dispatcher, _) = setup_camel(key_pair);
assert_eq!(camel_dispatcher.get_public_key(), key_pair.public_key);
}

#[test]
Expand All @@ -209,17 +204,11 @@ fn test_dual_getPublicKey_exists_and_panics() {
#[test]
#[ignore] // REASON: foundry entrypoint_not_found error message inconsistent with mainnet.
fn test_dual_isValidSignature() {
let (camel_dispatcher, target) = setup_camel();
let contract_address = camel_dispatcher.contract_address;

start_cheat_caller_address(contract_address, contract_address);

let key_pair = KEY_PAIR();
let signature = get_accept_ownership_signature(contract_address, ETH_PUBKEY(), key_pair);
let (camel_dispatcher, target) = setup_camel(key_pair);
let contract_address = camel_dispatcher.contract_address;

target.setPublicKey(key_pair.public_key, signature);
let serialized_signature = key_pair.serialized_sign(TRANSACTION_HASH.into());

let is_valid = camel_dispatcher.is_valid_signature(TRANSACTION_HASH, serialized_signature);
assert_eq!(is_valid, starknet::VALIDATED);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/account/src/tests/test_eth_account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ use openzeppelin_test_common::eth_account::{
SIGNED_TX_DATA, SignedTransactionData, get_accept_ownership_signature
};
use openzeppelin_testing as utils;
use openzeppelin_testing::constants::secp256k1::KEY_PAIR;
use openzeppelin_testing::constants::secp256k1::{KEY_PAIR, KEY_PAIR_2};
use openzeppelin_testing::constants::{
ETH_PUBKEY, NEW_ETH_PUBKEY, SALT, ZERO, OTHER, RECIPIENT, CALLER, QUERY_VERSION,
MIN_TRANSACTION_VERSION
SALT, ZERO, OTHER, RECIPIENT, CALLER, QUERY_VERSION, MIN_TRANSACTION_VERSION
};
use openzeppelin_testing::signing::Secp256k1KeyPair;
use openzeppelin_token::erc20::interface::IERC20DispatcherTrait;
use openzeppelin_utils::selectors;
use openzeppelin_utils::serde::SerializedAppend;
Expand Down
8 changes: 3 additions & 5 deletions packages/presets/src/tests/test_eth_account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ use openzeppelin_test_common::eth_account::{
};
use openzeppelin_test_common::upgrades::UpgradeableSpyHelpers;
use openzeppelin_testing as utils;
use openzeppelin_testing::signing::Secp256k1KeyPair;
use openzeppelin_testing::constants::secp256k1::{KEY_PAIR, KEY_PAIR_2};
use openzeppelin_testing::constants::{
CLASS_HASH_ZERO, ETH_PUBKEY, NEW_ETH_PUBKEY, SALT, ZERO, RECIPIENT, QUERY_VERSION,
MIN_TRANSACTION_VERSION, CALLER, OTHER
};
use openzeppelin_testing::constants::{CLASS_HASH_ZERO, ZERO, RECIPIENT, CALLER, OTHER};
use openzeppelin_testing::constants::{SALT, QUERY_VERSION, MIN_TRANSACTION_VERSION};
use openzeppelin_testing::signing::Secp256k1KeyPair;
use openzeppelin_token::erc20::interface::IERC20DispatcherTrait;
use openzeppelin_utils::selectors;
use openzeppelin_utils::serde::SerializedAppend;
Expand Down
8 changes: 0 additions & 8 deletions packages/testing/src/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ pub fn BASE_URI_2() -> ByteArray {
"https://api.example.com/v2/"
}

pub fn ETH_PUBKEY() -> EthPublicKey {
Secp256Trait::secp256_ec_get_point_from_x_syscall(3, false).unwrap_syscall().unwrap()
}

pub fn NEW_ETH_PUBKEY() -> EthPublicKey {
Secp256Trait::secp256_ec_get_point_from_x_syscall(4, false).unwrap_syscall().unwrap()
}

pub fn ADMIN() -> ContractAddress {
contract_address_const::<'ADMIN'>()
}
Expand Down

0 comments on commit 5ef9f96

Please sign in to comment.