Skip to content

Commit

Permalink
Testcase: Add a trait: ChainConfig, used for other on-chain signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
joii2020 committed Jan 11, 2024
1 parent 5e8c467 commit 56e5b6e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 103 deletions.
157 changes: 67 additions & 90 deletions tests/omni_lock_rust/tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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")
Expand All @@ -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);
Expand All @@ -1275,16 +1274,16 @@ 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)
}
}

#[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);
Expand All @@ -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)
}
}
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -1389,11 +1393,7 @@ pub struct TestConfig {
pub running_script: Script,
pub leading_witness_count: usize,

// Bitcoin
pub eos: Option<EOSConfig>,
pub tron: Option<TronConfig>,
pub bitcoin: Option<BitcoinConfig>,
pub dogecoin: Option<DogecoinConfig>,
pub chain_config: Option<Box<dyn ChainConfig>>,
}

#[derive(Copy, Clone, PartialEq)]
Expand Down Expand Up @@ -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][..])
};
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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<dyn ChainConfig>) {
let pkhash = config.get_pubkey_hash(&self.pubkey);
self.chain_config = Some(config);
self.id.blake160 = Bytes::from(pkhash.to_vec());
}

Expand Down
21 changes: 11 additions & 10 deletions tests/omni_lock_rust/tests/test_omni_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions tests/omni_lock_rust/tests/test_sudt_supply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 56e5b6e

Please sign in to comment.