Skip to content

Commit

Permalink
Add eth displaying testcase (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
joii2020 authored Jan 11, 2024
1 parent 9bca6d5 commit 12027c3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tests/omni_lock_rust/tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,7 @@ pub const IDENTITY_FLAGS_TRON: u8 = 3;
pub const IDENTITY_FLAGS_BITCOIN: u8 = 4;
pub const IDENTITY_FLAGS_DOGECOIN: u8 = 5;
pub const IDENTITY_FLAGS_MULTISIG: u8 = 6;
pub const IDENTITY_FLAGS_ETHEREUM_DISPLAYING: u8 = 18;

pub const IDENTITY_FLAGS_OWNER_LOCK: u8 = 0xFC;
pub const IDENTITY_FLAGS_EXEC: u8 = 0xFD;
Expand Down Expand Up @@ -1162,6 +1163,7 @@ pub fn use_chain_confg(flags: u8) -> bool {
|| flags == IDENTITY_FLAGS_TRON
|| flags == IDENTITY_FLAGS_BITCOIN
|| flags == IDENTITY_FLAGS_DOGECOIN
|| flags == IDENTITY_FLAGS_ETHEREUM_DISPLAYING
}

#[derive(Default)]
Expand All @@ -1184,6 +1186,37 @@ impl ChainConfig for EthereumConfig {
}
}

#[derive(Default)]
pub struct EthereumDisplayConfig {
pub pubkey_err: bool,
}

impl ChainConfig for EthereumDisplayConfig {
fn get_pubkey_hash(&self, pubkey: &Pubkey) -> [u8; 20] {
keccak160(&pubkey.as_ref()[..]).to_vec().try_into().unwrap()
}

fn convert_message(&self, message: &[u8; 32]) -> CkbH256 {
let eth_prefix = b"\x19Ethereum Signed Message:\n";

let mut hasher = Keccak256::new();
hasher.update(eth_prefix);
hasher.update(Bytes::from(format!(
"{}",
COMMON_PREFIX.len() + message.len() * 2
)));
hasher.update(Bytes::from(COMMON_PREFIX));
hasher.update(hex::encode(message));
let r = hasher.finalize();
CkbH256::from_slice(r.as_slice()).expect("convert_keccak256_hash")
}

fn sign(&self, privkey: &Privkey, message: CkbH256) -> Bytes {
let sig = privkey.sign_recoverable(&message).expect("sign");
Bytes::from(sig.serialize())
}
}

pub struct BitcoinConfig {
pub sign_vtype: u8,
pub pubkey_err: bool,
Expand Down
21 changes: 21 additions & 0 deletions tests/omni_lock_rust/tests/test_omni_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,3 +709,24 @@ fn test_tron_err_pubkey() {
assert!(verify_result.is_err());
assert_script_error(verify_result.unwrap_err(), ERROR_PUBKEY_BLAKE160_HASH);
}

#[test]
fn test_eth_displaying_unlock() {
let mut data_loader = DummyDataLoader::new();

let mut config = TestConfig::new(IDENTITY_FLAGS_ETHEREUM_DISPLAYING, false);
config.set_chain_config(Box::new(EthereumDisplayConfig::default()));

let tx = gen_tx(&mut data_loader, &mut config);
let tx = sign_tx(&mut data_loader, tx, &mut config);
let resolved_tx = build_resolved_tx(&data_loader, &tx);

let consensus = misc::gen_consensus();
let tx_env = misc::gen_tx_env();
let mut verifier =
TransactionScriptsVerifier::new(&resolved_tx, &consensus, &data_loader, &tx_env);

verifier.set_debug_printer(debug_printer);
let verify_result = verifier.verify(MAX_CYCLES);
verify_result.expect("pass verification");
}

0 comments on commit 12027c3

Please sign in to comment.