Skip to content

Commit

Permalink
chore: owner lock test
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Apr 26, 2024
1 parent 6c91cd2 commit 7852b40
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 24 deletions.
100 changes: 87 additions & 13 deletions src/tests/transaction/omnilock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ckb_types::{
core::DepType,
packed::{CellOutput, OutPoint},
prelude::*,
H256,
H160, H256,
};

use crate::{
Expand Down Expand Up @@ -289,17 +289,6 @@ fn test_omnilock_solana() {
omnilock_test(cfg, &sign_context_2);
}

#[ignore]
#[test]
fn test_omnilock_owner() {
let account0_key = secp256k1::SecretKey::from_slice(ACCOUNT0_KEY.as_bytes()).unwrap();
let pubkey = secp256k1::PublicKey::from_secret_key(&SECP256K1, &account0_key);
let cfg = OmniLockConfig::new_ownerlock(blake160(&Pubkey::from(pubkey).serialize()));
// cfg.enable_cobuild(true);
let sign_context = SignContexts::new_omnilock(vec![account0_key], cfg.clone());
omnilock_test(cfg, &sign_context)
}

fn omnilock_test(cfg: OmniLockConfig, sign_context: &SignContexts) {
let network_info = NetworkInfo::testnet();

Expand Down Expand Up @@ -345,7 +334,7 @@ fn omnilock_test(cfg: OmniLockConfig, sign_context: &SignContexts) {
crate::ScriptId::new_data1(H256::from(blake2b_256(OMNILOCK_BIN))),
crate::transaction::signer::omnilock::OmnilockSigner {},
)
.sign_transaction(&mut tx_with_groups, &sign_context)
.sign_transaction(&mut tx_with_groups, sign_context)
.unwrap();

// let json_tx = ckb_jsonrpc_types::TransactionView::from(tx_with_groups.get_tx_view().clone());
Expand All @@ -369,3 +358,88 @@ fn omnilock_test(cfg: OmniLockConfig, sign_context: &SignContexts) {

ctx.verify(tx, FEE_RATE).unwrap();
}

#[test]
fn test_omnilock_owner_lock() {
test_omnilock_owner_lock_tranfer(false);
test_omnilock_owner_lock_tranfer(true)
}

fn test_omnilock_owner_lock_tranfer(cobuild: bool) {
let network_info = NetworkInfo::testnet();
let receiver = build_sighash_script(ACCOUNT2_ARG);
let sender1 = build_sighash_script(ACCOUNT0_ARG);
let account0_key = secp256k1::SecretKey::from_slice(ACCOUNT0_KEY.as_bytes()).unwrap();
let hash = H160::from_slice(&sender1.calc_script_hash().as_slice()[0..20]).unwrap();
let mut cfg = OmniLockConfig::new_ownerlock(hash);
cfg.enable_cobuild(cobuild);
let sender0 = build_omnilock_script(&cfg);
let mut sign_context = SignContexts::new_omnilock(vec![account0_key.clone()], cfg.clone());
let hashall_unlock =
crate::transaction::signer::sighash::Secp256k1Blake160SighashAllSignerContext::new(vec![
account0_key.clone(),
]);
sign_context.add_context(Box::new(hashall_unlock));

let (ctx, mut outpoints) = init_context(
vec![(OMNILOCK_BIN, true)],
vec![
(sender0.clone(), Some(150 * ONE_CKB)),
(sender1.clone(), Some(61 * ONE_CKB)),
],
);

let configuration = test_omnilock_config(outpoints.pop().unwrap());
let iterator = InputIterator::new_with_cell_collector(
vec![sender0.clone(), sender1.clone()],
Box::new(ctx.to_live_cells_context()) as Box<_>,
);
let mut builder = SimpleTransactionBuilder::new(configuration, iterator);
let output = CellOutput::new_builder()
.capacity((110 * ONE_CKB).pack())
.lock(receiver.clone())
.build();
builder.add_output_and_data(output.clone(), ckb_types::packed::Bytes::default());
builder.set_change_lock(sender0.clone());

let context = OmnilockScriptContext::new(cfg.clone(), network_info.url.clone());
let mut contexts = HandlerContexts::default();
contexts.add_context(Box::new(context) as Box<_>);

let mut tx_with_groups = builder.build(&contexts).expect("build failed");

// let json_tx = ckb_jsonrpc_types::TransactionView::from(tx_with_groups.get_tx_view().clone());
// println!("tx: {}", serde_json::to_string_pretty(&json_tx).unwrap());

TransactionSigner::new(&network_info)
// use unitest lock to verify
.insert_unlocker(
crate::ScriptId::new_data1(H256::from(blake2b_256(OMNILOCK_BIN))),
crate::transaction::signer::omnilock::OmnilockSigner {},
)
.sign_transaction(&mut tx_with_groups, &sign_context)
.unwrap();

let tx = tx_with_groups.get_tx_view().clone();
// let json_tx = ckb_jsonrpc_types::TransactionView::from(tx_with_groups.get_tx_view().clone());
// println!("tx: {}", serde_json::to_string_pretty(&json_tx).unwrap());

let script_groups = tx_with_groups.script_groups.clone();
assert_eq!(script_groups.len(), 2);
assert_eq!(tx.header_deps().len(), 0);
assert_eq!(tx.cell_deps().len(), 2);
assert_eq!(tx.inputs().len(), 2);
let mut senders = vec![sender0.clone(), sender1.clone()];
for out_point in tx.input_pts_iter() {
let sender = ctx.get_input(&out_point).unwrap().0.lock();
assert!(senders.contains(&sender));
senders.retain(|x| x != &sender);
}
assert_eq!(tx.outputs().len(), 2);
assert_eq!(tx.output(0).unwrap(), output);
assert_eq!(tx.output(1).unwrap().lock(), sender0);
let change_capacity: u64 = tx.output(1).unwrap().capacity().unpack();
let fee = (150 + 61 - 110) * ONE_CKB - change_capacity;
assert_eq!(tx.data().as_reader().serialized_size_in_block() as u64, fee);
ctx.verify(tx, FEE_RATE).unwrap();
}
13 changes: 8 additions & 5 deletions src/tests/tx_builder/omni_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ fn test_omnilock_simple_hash_rc_input(mut cfg: OmniLockConfig) {
cfg.set_admin_config(admin_config);

let sender = build_omnilock_script(&cfg);
for (lock, capacity_opt) in vec![(sender.clone(), Some(100 * ONE_CKB))] {
for (lock, capacity_opt) in [(sender.clone(), Some(100 * ONE_CKB))] {
ctx.add_simple_live_cell(random_out_point(), lock, capacity_opt);
}

Expand Down Expand Up @@ -399,7 +399,7 @@ fn test_omnilock_simple_hash_rc(mut cfg: OmniLockConfig, unlock_mode: OmniUnlock
OmniUnlockMode::Normal => (None, 0),
};
let sender = build_omnilock_script(&cfg);
for (lock, capacity_opt) in vec![(sender.clone(), Some(300 * ONE_CKB))] {
for (lock, capacity_opt) in [(sender.clone(), Some(300 * ONE_CKB))] {
ctx.add_simple_live_cell(random_out_point(), lock, capacity_opt);
}

Expand Down Expand Up @@ -509,7 +509,7 @@ fn test_omnilock_simple_hash_rc2(mut cfg: OmniLockConfig) {
cfg.set_admin_config(admin_config);

let sender = build_omnilock_script(&cfg);
for (lock, capacity_opt) in vec![(sender.clone(), Some(300 * ONE_CKB))] {
for (lock, capacity_opt) in [(sender.clone(), Some(300 * ONE_CKB))] {
ctx.add_simple_live_cell(random_out_point(), lock, capacity_opt);
}

Expand Down Expand Up @@ -678,7 +678,7 @@ fn test_omnilock_transfer_from_multisig_wl_commnon(unlock_mode: OmniUnlockMode)
false,
));
let sender = build_omnilock_script(&cfg);
for (lock, capacity_opt) in vec![
for (lock, capacity_opt) in [
(sender.clone(), Some(100 * ONE_CKB)),
(sender.clone(), Some(200 * ONE_CKB)),
(sender.clone(), Some(300 * ONE_CKB)),
Expand Down Expand Up @@ -810,6 +810,9 @@ fn test_omnilock_transfer_from_ownerlock() {
.build_balanced(&mut cell_collector, &ctx, &ctx, &ctx, &balancer, &unlockers)
.unwrap();

let json_tx = ckb_jsonrpc_types::TransactionView::from(tx.clone());
println!("tx: {}", serde_json::to_string_pretty(&json_tx).unwrap());

let (new_tx, new_locked_groups) = unlock_tx(tx.clone(), &ctx, &unlockers).unwrap();
assert!(new_locked_groups.is_empty());
tx = new_tx;
Expand Down Expand Up @@ -866,7 +869,7 @@ fn test_omnilock_transfer_from_ownerlock_wl_admin() {
false,
));
let sender0 = build_omnilock_script(&cfg);
for (lock, capacity_opt) in vec![(sender0.clone(), Some(50 * ONE_CKB))] {
for (lock, capacity_opt) in [(sender0.clone(), Some(50 * ONE_CKB))] {
ctx.add_simple_live_cell(random_out_point(), lock, capacity_opt);
}

Expand Down
1 change: 1 addition & 0 deletions src/tests/tx_builder/omni_lock_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub fn build_always_success_script() -> Script {
// * build RCE cell, is_type = true. Only the Script.code_hash is kept for further use.
// when in this case, to make "args" passed in unique
// when in_input_cell is on, the cell is not in deps but in input.
#[allow(clippy::useless_vec)]
fn build_script(
ctx: &mut Context,
is_type: bool,
Expand Down
8 changes: 8 additions & 0 deletions src/traits/default_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,14 @@ impl SecpCkbRawKeySigner {
let hash160 = eos_auth(&pubkey.into(), vtype);
self.keys.insert(hash160.into(), key);
}

pub fn new_with_owner_lock(keys: Vec<secp256k1::SecretKey>, hash: H160) -> Self {
let mut signer = SecpCkbRawKeySigner::default();
for key in keys {
signer.keys.insert(hash.clone(), key);
}
signer
}
}

impl Signer for SecpCkbRawKeySigner {
Expand Down
1 change: 0 additions & 1 deletion src/transaction/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ fn inner_build<

// setup change output and data
change_builder.init(&mut tx);

// collect inputs
for (input_index, input) in input_iter.enumerate() {
let input = input?;
Expand Down
4 changes: 4 additions & 0 deletions src/transaction/signer/omnilock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ impl OmnilockSignerContext {
self.cfg.use_rsa,
self.cfg.use_iso9796_2,
)),
IdentityFlag::OwnerLock => Box::new(SecpCkbRawKeySigner::new_with_owner_lock(
self.keys.clone(),
self.cfg.id().auth_content().clone(),
)),
_ => Box::new(SecpCkbRawKeySigner::new_with_secret_keys(self.keys.clone())),
};
let omnilock_signer = OmniLockScriptSigner::new(signer, self.cfg.clone(), self.unlock_mode);
Expand Down
1 change: 1 addition & 0 deletions src/types/script_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl ScriptId {
}

/// Generate a dummy TypeId script with a placeholder args
#[allow(clippy::useless_vec)]
pub fn dummy_type_id_script(&self) -> Script {
Script::new_builder()
.code_hash(self.code_hash.pack())
Expand Down
6 changes: 1 addition & 5 deletions src/unlock/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ impl ScriptSigner for OmniLockScriptSigner {
generate_message(&tx_new, script_group, zero_lock)?
};
let signature = match id.flag() {
IdentityFlag::PubkeyHash => {
IdentityFlag::PubkeyHash | IdentityFlag::OwnerLock => {
self.signer
.sign(id.auth_content().as_ref(), message.as_ref(), true, tx)?
}
Expand Down Expand Up @@ -924,10 +924,6 @@ impl ScriptSigner for OmniLockScriptSigner {
self.signer
.sign(id.auth_content().as_ref(), msg.as_slice(), true, tx)?
}
IdentityFlag::OwnerLock => {
self.signer
.sign(id.auth_content().as_ref(), message.as_ref(), true, tx)?
}
IdentityFlag::Dl => {
let (sig, _pubkey) = if self.config.use_rsa {
(
Expand Down

0 comments on commit 7852b40

Please sign in to comment.