Skip to content

Commit 7852b40

Browse files
committed
chore: owner lock test
1 parent 6c91cd2 commit 7852b40

File tree

8 files changed

+110
-24
lines changed

8 files changed

+110
-24
lines changed

src/tests/transaction/omnilock.rs

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ckb_types::{
66
core::DepType,
77
packed::{CellOutput, OutPoint},
88
prelude::*,
9-
H256,
9+
H160, H256,
1010
};
1111

1212
use crate::{
@@ -289,17 +289,6 @@ fn test_omnilock_solana() {
289289
omnilock_test(cfg, &sign_context_2);
290290
}
291291

292-
#[ignore]
293-
#[test]
294-
fn test_omnilock_owner() {
295-
let account0_key = secp256k1::SecretKey::from_slice(ACCOUNT0_KEY.as_bytes()).unwrap();
296-
let pubkey = secp256k1::PublicKey::from_secret_key(&SECP256K1, &account0_key);
297-
let cfg = OmniLockConfig::new_ownerlock(blake160(&Pubkey::from(pubkey).serialize()));
298-
// cfg.enable_cobuild(true);
299-
let sign_context = SignContexts::new_omnilock(vec![account0_key], cfg.clone());
300-
omnilock_test(cfg, &sign_context)
301-
}
302-
303292
fn omnilock_test(cfg: OmniLockConfig, sign_context: &SignContexts) {
304293
let network_info = NetworkInfo::testnet();
305294

@@ -345,7 +334,7 @@ fn omnilock_test(cfg: OmniLockConfig, sign_context: &SignContexts) {
345334
crate::ScriptId::new_data1(H256::from(blake2b_256(OMNILOCK_BIN))),
346335
crate::transaction::signer::omnilock::OmnilockSigner {},
347336
)
348-
.sign_transaction(&mut tx_with_groups, &sign_context)
337+
.sign_transaction(&mut tx_with_groups, sign_context)
349338
.unwrap();
350339

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

370359
ctx.verify(tx, FEE_RATE).unwrap();
371360
}
361+
362+
#[test]
363+
fn test_omnilock_owner_lock() {
364+
test_omnilock_owner_lock_tranfer(false);
365+
test_omnilock_owner_lock_tranfer(true)
366+
}
367+
368+
fn test_omnilock_owner_lock_tranfer(cobuild: bool) {
369+
let network_info = NetworkInfo::testnet();
370+
let receiver = build_sighash_script(ACCOUNT2_ARG);
371+
let sender1 = build_sighash_script(ACCOUNT0_ARG);
372+
let account0_key = secp256k1::SecretKey::from_slice(ACCOUNT0_KEY.as_bytes()).unwrap();
373+
let hash = H160::from_slice(&sender1.calc_script_hash().as_slice()[0..20]).unwrap();
374+
let mut cfg = OmniLockConfig::new_ownerlock(hash);
375+
cfg.enable_cobuild(cobuild);
376+
let sender0 = build_omnilock_script(&cfg);
377+
let mut sign_context = SignContexts::new_omnilock(vec![account0_key.clone()], cfg.clone());
378+
let hashall_unlock =
379+
crate::transaction::signer::sighash::Secp256k1Blake160SighashAllSignerContext::new(vec![
380+
account0_key.clone(),
381+
]);
382+
sign_context.add_context(Box::new(hashall_unlock));
383+
384+
let (ctx, mut outpoints) = init_context(
385+
vec![(OMNILOCK_BIN, true)],
386+
vec![
387+
(sender0.clone(), Some(150 * ONE_CKB)),
388+
(sender1.clone(), Some(61 * ONE_CKB)),
389+
],
390+
);
391+
392+
let configuration = test_omnilock_config(outpoints.pop().unwrap());
393+
let iterator = InputIterator::new_with_cell_collector(
394+
vec![sender0.clone(), sender1.clone()],
395+
Box::new(ctx.to_live_cells_context()) as Box<_>,
396+
);
397+
let mut builder = SimpleTransactionBuilder::new(configuration, iterator);
398+
let output = CellOutput::new_builder()
399+
.capacity((110 * ONE_CKB).pack())
400+
.lock(receiver.clone())
401+
.build();
402+
builder.add_output_and_data(output.clone(), ckb_types::packed::Bytes::default());
403+
builder.set_change_lock(sender0.clone());
404+
405+
let context = OmnilockScriptContext::new(cfg.clone(), network_info.url.clone());
406+
let mut contexts = HandlerContexts::default();
407+
contexts.add_context(Box::new(context) as Box<_>);
408+
409+
let mut tx_with_groups = builder.build(&contexts).expect("build failed");
410+
411+
// let json_tx = ckb_jsonrpc_types::TransactionView::from(tx_with_groups.get_tx_view().clone());
412+
// println!("tx: {}", serde_json::to_string_pretty(&json_tx).unwrap());
413+
414+
TransactionSigner::new(&network_info)
415+
// use unitest lock to verify
416+
.insert_unlocker(
417+
crate::ScriptId::new_data1(H256::from(blake2b_256(OMNILOCK_BIN))),
418+
crate::transaction::signer::omnilock::OmnilockSigner {},
419+
)
420+
.sign_transaction(&mut tx_with_groups, &sign_context)
421+
.unwrap();
422+
423+
let tx = tx_with_groups.get_tx_view().clone();
424+
// let json_tx = ckb_jsonrpc_types::TransactionView::from(tx_with_groups.get_tx_view().clone());
425+
// println!("tx: {}", serde_json::to_string_pretty(&json_tx).unwrap());
426+
427+
let script_groups = tx_with_groups.script_groups.clone();
428+
assert_eq!(script_groups.len(), 2);
429+
assert_eq!(tx.header_deps().len(), 0);
430+
assert_eq!(tx.cell_deps().len(), 2);
431+
assert_eq!(tx.inputs().len(), 2);
432+
let mut senders = vec![sender0.clone(), sender1.clone()];
433+
for out_point in tx.input_pts_iter() {
434+
let sender = ctx.get_input(&out_point).unwrap().0.lock();
435+
assert!(senders.contains(&sender));
436+
senders.retain(|x| x != &sender);
437+
}
438+
assert_eq!(tx.outputs().len(), 2);
439+
assert_eq!(tx.output(0).unwrap(), output);
440+
assert_eq!(tx.output(1).unwrap().lock(), sender0);
441+
let change_capacity: u64 = tx.output(1).unwrap().capacity().unpack();
442+
let fee = (150 + 61 - 110) * ONE_CKB - change_capacity;
443+
assert_eq!(tx.data().as_reader().serialized_size_in_block() as u64, fee);
444+
ctx.verify(tx, FEE_RATE).unwrap();
445+
}

src/tests/tx_builder/omni_lock.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ fn test_omnilock_simple_hash_rc_input(mut cfg: OmniLockConfig) {
207207
cfg.set_admin_config(admin_config);
208208

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

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

@@ -509,7 +509,7 @@ fn test_omnilock_simple_hash_rc2(mut cfg: OmniLockConfig) {
509509
cfg.set_admin_config(admin_config);
510510

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

@@ -678,7 +678,7 @@ fn test_omnilock_transfer_from_multisig_wl_commnon(unlock_mode: OmniUnlockMode)
678678
false,
679679
));
680680
let sender = build_omnilock_script(&cfg);
681-
for (lock, capacity_opt) in vec![
681+
for (lock, capacity_opt) in [
682682
(sender.clone(), Some(100 * ONE_CKB)),
683683
(sender.clone(), Some(200 * ONE_CKB)),
684684
(sender.clone(), Some(300 * ONE_CKB)),
@@ -810,6 +810,9 @@ fn test_omnilock_transfer_from_ownerlock() {
810810
.build_balanced(&mut cell_collector, &ctx, &ctx, &ctx, &balancer, &unlockers)
811811
.unwrap();
812812

813+
let json_tx = ckb_jsonrpc_types::TransactionView::from(tx.clone());
814+
println!("tx: {}", serde_json::to_string_pretty(&json_tx).unwrap());
815+
813816
let (new_tx, new_locked_groups) = unlock_tx(tx.clone(), &ctx, &unlockers).unwrap();
814817
assert!(new_locked_groups.is_empty());
815818
tx = new_tx;
@@ -866,7 +869,7 @@ fn test_omnilock_transfer_from_ownerlock_wl_admin() {
866869
false,
867870
));
868871
let sender0 = build_omnilock_script(&cfg);
869-
for (lock, capacity_opt) in vec![(sender0.clone(), Some(50 * ONE_CKB))] {
872+
for (lock, capacity_opt) in [(sender0.clone(), Some(50 * ONE_CKB))] {
870873
ctx.add_simple_live_cell(random_out_point(), lock, capacity_opt);
871874
}
872875

src/tests/tx_builder/omni_lock_util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub fn build_always_success_script() -> Script {
5151
// * build RCE cell, is_type = true. Only the Script.code_hash is kept for further use.
5252
// when in this case, to make "args" passed in unique
5353
// when in_input_cell is on, the cell is not in deps but in input.
54+
#[allow(clippy::useless_vec)]
5455
fn build_script(
5556
ctx: &mut Context,
5657
is_type: bool,

src/traits/default_impls.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,14 @@ impl SecpCkbRawKeySigner {
651651
let hash160 = eos_auth(&pubkey.into(), vtype);
652652
self.keys.insert(hash160.into(), key);
653653
}
654+
655+
pub fn new_with_owner_lock(keys: Vec<secp256k1::SecretKey>, hash: H160) -> Self {
656+
let mut signer = SecpCkbRawKeySigner::default();
657+
for key in keys {
658+
signer.keys.insert(hash.clone(), key);
659+
}
660+
signer
661+
}
654662
}
655663

656664
impl Signer for SecpCkbRawKeySigner {

src/transaction/builder/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ fn inner_build<
161161

162162
// setup change output and data
163163
change_builder.init(&mut tx);
164-
165164
// collect inputs
166165
for (input_index, input) in input_iter.enumerate() {
167166
let input = input?;

src/transaction/signer/omnilock.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ impl OmnilockSignerContext {
8282
self.cfg.use_rsa,
8383
self.cfg.use_iso9796_2,
8484
)),
85+
IdentityFlag::OwnerLock => Box::new(SecpCkbRawKeySigner::new_with_owner_lock(
86+
self.keys.clone(),
87+
self.cfg.id().auth_content().clone(),
88+
)),
8589
_ => Box::new(SecpCkbRawKeySigner::new_with_secret_keys(self.keys.clone())),
8690
};
8791
let omnilock_signer = OmniLockScriptSigner::new(signer, self.cfg.clone(), self.unlock_mode);

src/types/script_id.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ impl ScriptId {
3636
}
3737

3838
/// Generate a dummy TypeId script with a placeholder args
39+
#[allow(clippy::useless_vec)]
3940
pub fn dummy_type_id_script(&self) -> Script {
4041
Script::new_builder()
4142
.code_hash(self.code_hash.pack())

src/unlock/signer.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ impl ScriptSigner for OmniLockScriptSigner {
843843
generate_message(&tx_new, script_group, zero_lock)?
844844
};
845845
let signature = match id.flag() {
846-
IdentityFlag::PubkeyHash => {
846+
IdentityFlag::PubkeyHash | IdentityFlag::OwnerLock => {
847847
self.signer
848848
.sign(id.auth_content().as_ref(), message.as_ref(), true, tx)?
849849
}
@@ -924,10 +924,6 @@ impl ScriptSigner for OmniLockScriptSigner {
924924
self.signer
925925
.sign(id.auth_content().as_ref(), msg.as_slice(), true, tx)?
926926
}
927-
IdentityFlag::OwnerLock => {
928-
self.signer
929-
.sign(id.auth_content().as_ref(), message.as_ref(), true, tx)?
930-
}
931927
IdentityFlag::Dl => {
932928
let (sig, _pubkey) = if self.config.use_rsa {
933929
(

0 commit comments

Comments
 (0)