Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add testcase: test_cobuild_otx_prefix #35

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions tests/omni_lock_rust/tests/test_otx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,78 @@ fn test_cobuild_otx_simple() {
verifier.verify(&tx, &dl).unwrap();
}

#[test]
fn test_cobuild_otx_prefix() {
let mut dl = Resource::default();
let mut px = Pickaxer::default();
let tx_builder = ckb_types::core::TransactionBuilder::default();

// Create otx prefix. Add a sighash all only cell for pay fees.
let prikey = "000000000000000000000000000000000000000000000000000000000000000f";
let prikey = ckb_crypto::secp::Privkey::from_str(prikey).unwrap();
let pubkey = prikey.pubkey().unwrap();
let pubkey_hash = hash_keccak160(&pubkey.as_ref()[..]);
let args = [vec![IDENTITY_FLAGS_ETHEREUM], pubkey_hash, vec![0x00]].concat();
let cell_meta_always_success = px.insert_cell_data(&mut dl, BINARY_ALWAYS_SUCCESS);
let cell_meta_secp256k1_data = px.insert_cell_data(&mut dl, BINARY_SECP256K1_DATA);
let cell_meta_omni_lock = px.insert_cell_data(&mut dl, BINARY_OMNI_LOCK);
let cell_meta_i = px.insert_cell_fund(&mut dl, px.create_script(&cell_meta_omni_lock, &args), None, &[]);
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_always_success));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_secp256k1_data));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_omni_lock));
let tx_builder = tx_builder.input(px.create_cell_input(&cell_meta_i));
let tx_builder = tx_builder.output(px.create_cell_output(px.create_script(&cell_meta_always_success, &[]), None));
let tx_builder = tx_builder.output_data(Vec::new().pack());
let tx_builder = tx_builder.witness(vec![0x00; 102].pack());

// Append otx
let os = schemas::basic::OtxStart::new_builder()
.start_cell_deps(3u32.pack())
.start_header_deps(0u32.pack())
.start_input_cell(1u32.pack())
.start_output_cell(1u32.pack())
.build();
let wl = schemas::top_level::WitnessLayout::new_builder().set(os).build();
let mut tx_builder = tx_builder.witness(wl.as_bytes().pack());
let otxs = vec![generate_otx_a0(&mut dl, &mut px), generate_otx_b0(&mut dl, &mut px)];
for otx in otxs {
for e in otx.cell_deps_iter() {
tx_builder = tx_builder.cell_dep(e);
}
for e in otx.inputs().into_iter() {
tx_builder = tx_builder.input(e);
}
for e in otx.outputs().into_iter() {
tx_builder = tx_builder.output(e);
}
for e in otx.outputs_data().into_iter() {
tx_builder = tx_builder.output_data(e);
}
for e in otx.witnesses().into_iter() {
tx_builder = tx_builder.witness(e);
}
}

// Create sign for prefix
let sign = cobuild_create_signing_message_hash_sighash_all_only(tx_builder.clone().build(), &dl);
println_hex("smh", &sign);
let sign = sign_ethereum(prikey, &sign);
let sign = omnilock_create_witness_lock(&sign);
let seal = [vec![0x00], sign].concat();
println_hex("seal", seal.as_slice());
let so = schemas::basic::SighashAllOnly::new_builder().seal(seal.pack()).build();
let wl = schemas::top_level::WitnessLayout::new_builder().set(so).build();
assert_eq!(wl.as_bytes().pack().len(), 102);
let mut wb = tx_builder.clone().build().witnesses().as_builder();
wb.replace(0, wl.as_bytes().pack());
let tx_builder = tx_builder.set_witnesses(wb.build().into_iter().collect());

let tx = tx_builder.build();
let tx = ckb_types::core::cell::resolve_transaction(tx, &mut HashSet::new(), &dl, &dl).unwrap();
let verifier = Verifier::default();
verifier.verify(&tx, &dl).unwrap();
}

#[test]
fn test_cobuild_otx_prefix_and_suffix() {
let mut dl = Resource::default();
Expand Down