diff --git a/tests/omni_lock_rust/tests/misc.rs b/tests/omni_lock_rust/tests/misc.rs index bb9ccfc..9edf699 100644 --- a/tests/omni_lock_rust/tests/misc.rs +++ b/tests/omni_lock_rust/tests/misc.rs @@ -645,20 +645,13 @@ pub fn sign_tx_by_input_group( }); blake2b.finalize(&mut message); - let message = if config.id.flags == IDENTITY_FLAGS_ETHEREUM { - convert_keccak256_hash(&message) - } else if config.id.flags == IDENTITY_FLAGS_EOS { - assert!(config.eos.is_some()); - config.eos.as_ref().unwrap().convert_message(&message) - } else if config.id.flags == IDENTITY_FLAGS_TRON { - assert!(config.tron.is_some()); - config.tron.as_ref().unwrap().convert_message(&message) - } else if config.id.flags == IDENTITY_FLAGS_BITCOIN { - assert!(config.bitcoin.is_some()); - config.bitcoin.as_ref().unwrap().convert_message(&message) - } else if config.id.flags == IDENTITY_FLAGS_DOGECOIN { - assert!(config.dogecoin.is_some()); - config.dogecoin.as_ref().unwrap().convert_message(&message) + let message = if use_chain_confg(config.id.flags) { + assert!(config.chain_config.is_some()); + config + .chain_config + .as_ref() + .unwrap() + .convert_message(&message) } else { CkbH256::from(message) }; @@ -702,37 +695,9 @@ pub fn sign_tx_by_input_group( &identity, None, ) - } else if config.id.flags == IDENTITY_FLAGS_EOS { - let sig_bytes = config - .eos - .as_ref() - .unwrap() - .sign(&config.private_key, message); - gen_witness_lock( - sig_bytes, - config.use_rc, - config.use_rc_identity, - &proof_vec, - &identity, - None, - ) - } else if config.id.flags == IDENTITY_FLAGS_BITCOIN { + } else if use_chain_confg(config.id.flags) { let sig_bytes = config - .bitcoin - .as_ref() - .unwrap() - .sign(&config.private_key, message); - gen_witness_lock( - sig_bytes, - config.use_rc, - config.use_rc_identity, - &proof_vec, - &identity, - None, - ) - } else if config.id.flags == IDENTITY_FLAGS_DOGECOIN { - let sig_bytes = config - .dogecoin + .chain_config .as_ref() .unwrap() .sign(&config.private_key, message); @@ -1165,6 +1130,40 @@ impl Default for MultisigTestConfig { } } +pub trait ChainConfig { + fn get_pubkey_hash(&self, pubkey: &Pubkey) -> [u8; 20]; + fn convert_message(&self, message: &[u8; 32]) -> CkbH256; + fn sign(&self, privkey: &Privkey, message: CkbH256) -> Bytes; +} + +pub fn use_chain_confg(flags: u8) -> bool { + flags == IDENTITY_FLAGS_ETHEREUM + || flags == IDENTITY_FLAGS_EOS + || flags == IDENTITY_FLAGS_TRON + || flags == IDENTITY_FLAGS_BITCOIN + || flags == IDENTITY_FLAGS_DOGECOIN +} + +#[derive(Default)] +pub struct EthereumConfig { + pub pubkey_err: bool, +} + +impl ChainConfig for EthereumConfig { + 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 { + convert_keccak256_hash(message) + } + + 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, @@ -1179,8 +1178,8 @@ impl Default for BitcoinConfig { } } -impl BitcoinConfig { - pub fn get_pubkey_hash(&self, pubkey: &Pubkey) -> [u8; 20] { +impl ChainConfig for BitcoinConfig { + fn get_pubkey_hash(&self, pubkey: &Pubkey) -> [u8; 20] { if self.pubkey_err { let mut r = [0u8; 20]; thread_rng().fill_bytes(&mut r); @@ -1237,7 +1236,7 @@ impl BitcoinConfig { CkbH256::from(msg) } - pub fn sign(&self, privkey: &Privkey, message: CkbH256) -> Bytes { + fn sign(&self, privkey: &Privkey, message: CkbH256) -> Bytes { let sign = privkey .sign_recoverable(&message) .expect("sign secp256k1") @@ -1256,12 +1255,12 @@ impl BitcoinConfig { #[derive(Default)] pub struct DogecoinConfig(pub BitcoinConfig); -impl DogecoinConfig { - pub fn get_pubkey_hash(&self, pubkey: &Pubkey) -> [u8; 20] { +impl ChainConfig for DogecoinConfig { + fn get_pubkey_hash(&self, pubkey: &Pubkey) -> [u8; 20] { self.0.get_pubkey_hash(pubkey) } - pub fn convert_message(&self, message: &[u8; 32]) -> CkbH256 { + fn convert_message(&self, message: &[u8; 32]) -> CkbH256 { let message_magic = b"\x19Dogecoin Signed Message:\n\x40"; let msg_hex = hex::encode(message); assert_eq!(msg_hex.len(), 64); @@ -1275,7 +1274,7 @@ impl DogecoinConfig { CkbH256::from(msg) } - pub fn sign(&self, privkey: &Privkey, message: CkbH256) -> Bytes { + fn sign(&self, privkey: &Privkey, message: CkbH256) -> Bytes { self.0.sign(privkey, message) } } @@ -1283,8 +1282,8 @@ impl DogecoinConfig { #[derive(Default)] pub struct EOSConfig(pub BitcoinConfig); -impl EOSConfig { - pub fn get_pubkey_hash(&self, pubkey: &Pubkey) -> [u8; 20] { +impl ChainConfig for EOSConfig { + fn get_pubkey_hash(&self, pubkey: &Pubkey) -> [u8; 20] { if self.0.pubkey_err { let mut r = [0u8; 20]; thread_rng().fill_bytes(&mut r); @@ -1307,11 +1306,11 @@ impl EOSConfig { ckb_hash::blake2b_256(buf)[..20].try_into().unwrap() } - pub fn convert_message(&self, message: &[u8; 32]) -> CkbH256 { + fn convert_message(&self, message: &[u8; 32]) -> CkbH256 { CkbH256::from_slice(message).unwrap() } - pub fn sign(&self, privkey: &Privkey, message: CkbH256) -> Bytes { + fn sign(&self, privkey: &Privkey, message: CkbH256) -> Bytes { self.0.sign(privkey, message) } } @@ -1321,8 +1320,8 @@ pub struct TronConfig { pub pubkey_err: bool, } -impl TronConfig { - pub fn get_pubkey_hash(&self, pubkey: &Pubkey) -> [u8; 20] { +impl ChainConfig for TronConfig { + fn get_pubkey_hash(&self, pubkey: &Pubkey) -> [u8; 20] { if self.pubkey_err { let mut r = [0u8; 20]; thread_rng().fill_bytes(&mut r); @@ -1338,7 +1337,7 @@ impl TronConfig { r[12..].try_into().unwrap() } - pub fn convert_message(&self, message: &[u8; 32]) -> CkbH256 { + fn convert_message(&self, message: &[u8; 32]) -> CkbH256 { let eth_prefix: &[u8; 24] = b"\x19TRON Signed Message:\n32"; let mut hasher = Keccak256::new(); hasher.update(eth_prefix); @@ -1347,6 +1346,11 @@ impl TronConfig { let rr = CkbH256::from_slice(r.as_slice()).expect("convert_keccak256_hash"); rr } + + fn sign(&self, privkey: &Privkey, message: CkbH256) -> Bytes { + let sig = privkey.sign_recoverable(&message).expect("sign"); + Bytes::from(sig.serialize()) + } } pub struct TestConfig { @@ -1389,11 +1393,7 @@ pub struct TestConfig { pub running_script: Script, pub leading_witness_count: usize, - // Bitcoin - pub eos: Option, - pub tron: Option, - pub bitcoin: Option, - pub dogecoin: Option, + pub chain_config: Option>, } #[derive(Copy, Clone, PartialEq)] @@ -1436,8 +1436,6 @@ impl TestConfig { let blake160 = if flags == IDENTITY_FLAGS_PUBKEY_HASH { pubkey_hash - } else if flags == IDENTITY_FLAGS_ETHEREUM { - keccak160(&pubkey.as_ref()[..]) } else { Bytes::from(&[0; 20][..]) }; @@ -1493,10 +1491,7 @@ impl TestConfig { running_script: Default::default(), leading_witness_count: 0, - eos: None, - tron: None, - bitcoin: None, - dogecoin: None, + chain_config: None, } } @@ -1538,27 +1533,9 @@ impl TestConfig { self.use_rc_identity = used; } - pub fn set_eos(&mut self, eos: EOSConfig) { - let pkhash = eos.get_pubkey_hash(&self.pubkey); - self.eos = Some(eos); - self.id.blake160 = Bytes::from(pkhash.to_vec()); - } - - pub fn set_tron(&mut self, tron: TronConfig) { - let pkhash = tron.get_pubkey_hash(&self.pubkey); - self.tron = Some(tron); - self.id.blake160 = Bytes::from(pkhash.to_vec()); - } - - pub fn set_bitcoin(&mut self, btc: BitcoinConfig) { - let pkhash = btc.get_pubkey_hash(&self.pubkey); - self.bitcoin = Some(btc); - self.id.blake160 = Bytes::from(pkhash.to_vec()); - } - - pub fn set_dogecoin(&mut self, dogecoin: DogecoinConfig) { - let pkhash = dogecoin.get_pubkey_hash(&self.pubkey); - self.dogecoin = Some(dogecoin); + pub fn set_chain_config(&mut self, config: Box) { + let pkhash = config.get_pubkey_hash(&self.pubkey); + self.chain_config = Some(config); self.id.blake160 = Bytes::from(pkhash.to_vec()); } diff --git a/tests/omni_lock_rust/tests/test_omni_lock.rs b/tests/omni_lock_rust/tests/test_omni_lock.rs index 70fe680..292b2ef 100644 --- a/tests/omni_lock_rust/tests/test_omni_lock.rs +++ b/tests/omni_lock_rust/tests/test_omni_lock.rs @@ -488,6 +488,7 @@ fn test_eth_unlock() { let mut data_loader = DummyDataLoader::new(); let mut config = TestConfig::new(IDENTITY_FLAGS_ETHEREUM, false); + config.set_chain_config(Box::new(EthereumConfig::default())); let tx = gen_tx(&mut data_loader, &mut config); let tx = sign_tx(&mut data_loader, tx, &mut config); @@ -507,10 +508,10 @@ fn test_btc_success(vtype: u8) { let mut data_loader = DummyDataLoader::new(); let mut config = TestConfig::new(IDENTITY_FLAGS_BITCOIN, false); - config.set_bitcoin(BitcoinConfig { + config.set_chain_config(Box::new(BitcoinConfig { sign_vtype: vtype, pubkey_err: false, - }); + })); let tx = gen_tx(&mut data_loader, &mut config); let tx = sign_tx(&mut data_loader, tx, &mut config); @@ -530,10 +531,10 @@ fn test_btc_err_pubkey(vtype: u8) { let mut data_loader = DummyDataLoader::new(); let mut config = TestConfig::new(IDENTITY_FLAGS_BITCOIN, false); - config.set_bitcoin(BitcoinConfig { + config.set_chain_config(Box::new(BitcoinConfig { sign_vtype: vtype, pubkey_err: true, - }); + })); let tx = gen_tx(&mut data_loader, &mut config); let tx = sign_tx(&mut data_loader, tx, &mut config); @@ -568,7 +569,7 @@ fn test_dogecoin_unlock() { let mut data_loader = DummyDataLoader::new(); let mut config = TestConfig::new(IDENTITY_FLAGS_DOGECOIN, false); - config.set_dogecoin(DogecoinConfig::default()); + config.set_chain_config(Box::new(DogecoinConfig::default())); let tx = gen_tx(&mut data_loader, &mut config); let tx = sign_tx(&mut data_loader, tx, &mut config); @@ -591,7 +592,7 @@ fn test_dogecoin_err_pubkey() { let mut config = TestConfig::new(IDENTITY_FLAGS_DOGECOIN, false); let mut dogecoin = DogecoinConfig::default(); dogecoin.0.pubkey_err = true; - config.set_dogecoin(dogecoin); + config.set_chain_config(Box::new(dogecoin)); let tx = gen_tx(&mut data_loader, &mut config); let tx = sign_tx(&mut data_loader, tx, &mut config); @@ -613,7 +614,7 @@ fn test_eos_success(vtype: u8) { let mut config = TestConfig::new(IDENTITY_FLAGS_EOS, false); let mut eos = EOSConfig::default(); eos.0.sign_vtype = vtype; - config.set_eos(EOSConfig::default()); + config.set_chain_config(Box::new(EOSConfig::default())); let tx: ckb_types::core::TransactionView = gen_tx(&mut data_loader, &mut config); let tx = sign_tx(&mut data_loader, tx, &mut config); @@ -636,7 +637,7 @@ fn test_eos_err_pubkey(vtype: u8) { let mut eos = EOSConfig::default(); eos.0.sign_vtype = vtype; eos.0.pubkey_err = true; - config.set_eos(eos); + config.set_chain_config(Box::new(eos)); let tx: ckb_types::core::TransactionView = gen_tx(&mut data_loader, &mut config); let tx = sign_tx(&mut data_loader, tx, &mut config); @@ -669,7 +670,7 @@ fn test_tron_unlock() { let mut data_loader = DummyDataLoader::new(); let mut config = TestConfig::new(IDENTITY_FLAGS_TRON, false); - config.set_tron(TronConfig::default()); + config.set_chain_config(Box::new(TronConfig::default())); let tx = gen_tx(&mut data_loader, &mut config); let tx = sign_tx(&mut data_loader, tx, &mut config); @@ -692,7 +693,7 @@ fn test_tron_err_pubkey() { let mut config = TestConfig::new(IDENTITY_FLAGS_TRON, false); let mut tron = TronConfig::default(); tron.pubkey_err = true; - config.set_tron(tron); + config.set_chain_config(Box::new(tron)); let tx = gen_tx(&mut data_loader, &mut config); let tx = sign_tx(&mut data_loader, tx, &mut config); diff --git a/tests/omni_lock_rust/tests/test_sudt_supply.rs b/tests/omni_lock_rust/tests/test_sudt_supply.rs index 94c4e28..7473287 100644 --- a/tests/omni_lock_rust/tests/test_sudt_supply.rs +++ b/tests/omni_lock_rust/tests/test_sudt_supply.rs @@ -11,9 +11,9 @@ use rand::{thread_rng, Rng}; use misc::{ assert_script_error, build_always_success_script, build_resolved_tx, debug_printer, gen_tx, - sign_tx, DummyDataLoader, TestConfig, ALWAYS_SUCCESS, CKB_INVALID_DATA, ERROR_BURN, - ERROR_EXCEED_SUPPLY, ERROR_NO_INFO_CELL, ERROR_SUPPLY_AMOUNT, IDENTITY_FLAGS_ETHEREUM, - MAX_CYCLES, SIMPLE_UDT, + sign_tx, DummyDataLoader, EthereumConfig, TestConfig, ALWAYS_SUCCESS, CKB_INVALID_DATA, + ERROR_BURN, ERROR_EXCEED_SUPPLY, ERROR_NO_INFO_CELL, ERROR_SUPPLY_AMOUNT, + IDENTITY_FLAGS_ETHEREUM, MAX_CYCLES, SIMPLE_UDT, }; mod misc; @@ -176,6 +176,7 @@ where { let mut data_loader = DummyDataLoader::new(); let mut config = TestConfig::new(IDENTITY_FLAGS_ETHEREUM, false); + config.set_chain_config(Box::new(EthereumConfig::default())); let tx = gen_tx_fn(&mut data_loader, &mut config); let tx = sign_tx(&mut data_loader, tx, &mut config);