Skip to content

Commit

Permalink
test: add more test
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed May 10, 2024
1 parent c76e8be commit e552c1a
Show file tree
Hide file tree
Showing 15 changed files with 1,821 additions and 157 deletions.
1 change: 1 addition & 0 deletions examples/omnilock_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ fn main() -> Result<(), Box<dyn StdErr>> {
&SignContexts::new_omnilock(
[secp256k1::SecretKey::from_slice(private_key.as_bytes())?].to_vec(),
omni_cfg,
ckb_sdk::unlock::OmniUnlockMode::Normal,
),
)?;

Expand Down
2 changes: 2 additions & 0 deletions examples/omnilock_multisig_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ fn main() -> Result<(), Box<dyn StdErr>> {
&SignContexts::new_omnilock(
[secp256k1::SecretKey::from_slice(private_key.as_bytes())?].to_vec(),
omni_cfg.clone(),
ckb_sdk::unlock::OmniUnlockMode::Normal,
),
)?;
let private_key = h256!("0x4fd809631a6aa6e3bb378dd65eae5d71df895a82c91a615a1e8264741515c79c");
Expand All @@ -70,6 +71,7 @@ fn main() -> Result<(), Box<dyn StdErr>> {
&SignContexts::new_omnilock(
[secp256k1::SecretKey::from_slice(private_key.as_bytes())?].to_vec(),
omni_cfg,
ckb_sdk::unlock::OmniUnlockMode::Normal,
),
)?;

Expand Down
3 changes: 2 additions & 1 deletion src/core/advanced_builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ impl TransactionBuilder {
if witness_data.is_empty() {
WitnessArgs::default()
} else {
WitnessArgs::from_slice(witness_data.as_ref()).unwrap()
WitnessArgs::from_slice(witness_data.as_ref())
.expect("WitnessArgs expected but failed to decode ")
}
}

Expand Down
1,712 changes: 1,664 additions & 48 deletions src/tests/transaction/omnilock.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/tests/tx_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ mod cheque;
mod cycle;
mod dao;
mod omni_lock;
mod omni_lock_util;
pub mod omni_lock_util;
mod transfer;
mod udt;
25 changes: 11 additions & 14 deletions src/traits/default_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,14 +648,6 @@ 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 Expand Up @@ -702,18 +694,22 @@ impl Drop for SecpCkbRawKeySigner {
/// A signer use ed25519 raw key
#[derive(Clone)]
pub struct Ed25519Signer {
key: ed25519_dalek::SigningKey,
keys: HashMap<H160, ed25519_dalek::SigningKey>,
}

impl Ed25519Signer {
pub fn new(key: ed25519_dalek::SigningKey) -> Self {
Self { key }
pub fn new(keys: Vec<ed25519_dalek::SigningKey>) -> Self {
let mut res = HashMap::with_capacity(keys.len());
for key in keys {
res.insert(blake160(key.verifying_key().as_bytes()), key);
}
Self { keys: res }
}
}

impl Signer for Ed25519Signer {
fn match_id(&self, id: &[u8]) -> bool {
id.len() == 20 && blake160(&self.key.verifying_key().as_bytes()[..]).as_bytes() == id
id.len() == 20 && self.keys.contains_key(&H160::from_slice(id).unwrap())
}

fn sign(
Expand All @@ -733,9 +729,10 @@ impl Signer for Ed25519Signer {
)));
}

let sig = self.key.clone().sign(message);
let key = self.keys.get(&H160::from_slice(id).unwrap()).unwrap();
let sig = key.clone().sign(message);

let verifying_key = self.key.verifying_key();
let verifying_key = key.verifying_key();
// 64 + 32
let mut sig_plus_pubkey = sig.to_vec();
sig_plus_pubkey.extend(verifying_key.as_bytes());
Expand Down
38 changes: 36 additions & 2 deletions src/transaction/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use super::{handler::HandlerContexts, input::TransactionInput};
use crate::{
core::TransactionBuilder,
traits::CellCollectorError,
traits::{CellCollectorError, LiveCell},
transaction::TransactionBuilderConfiguration,
tx_builder::{BalanceTxCapacityError, TxBuilderError},
ScriptGroup, TransactionWithScriptGroups,
Expand Down Expand Up @@ -143,6 +143,7 @@ fn inner_build<
input_iter: I,
configuration: &TransactionBuilderConfiguration,
contexts: &HandlerContexts,
rc_cells: Vec<LiveCell>,
) -> Result<TransactionWithScriptGroups, TxBuilderError> {
let mut lock_groups: HashMap<Byte32, ScriptGroup> = HashMap::default();
let mut type_groups: HashMap<Byte32, ScriptGroup> = HashMap::default();
Expand All @@ -159,10 +160,43 @@ fn inner_build<
}
}

let rc_len = rc_cells.len();

for (input_index, input) in rc_cells.into_iter().enumerate() {
let input = TransactionInput::new(input, 0);
tx.input(input.cell_input());
tx.witness(packed::Bytes::default());

inputs.insert(
input.live_cell.out_point.clone(),
(
input.live_cell.output.clone(),
input.live_cell.output_data.clone(),
),
);

let previous_output = input.previous_output();
let lock_script = previous_output.lock();
lock_groups
.entry(lock_script.calc_script_hash())
.or_insert_with(|| ScriptGroup::from_lock_script(&lock_script))
.input_indices
.push(input_index);

if let Some(type_script) = previous_output.type_().to_opt() {
type_groups
.entry(type_script.calc_script_hash())
.or_insert_with(|| ScriptGroup::from_type_script(&type_script))
.input_indices
.push(input_index);
}
}

// setup change output and data
change_builder.init(&mut tx);
// collect inputs
for (input_index, input) in input_iter.enumerate() {
for (mut input_index, input) in input_iter.enumerate() {
input_index += rc_len;
let input = input?;
tx.input(input.cell_input());
tx.witness(packed::Bytes::default());
Expand Down
17 changes: 16 additions & 1 deletion src/transaction/builder/simple.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
core::TransactionBuilder,
traits::LiveCell,
transaction::{
handler::HandlerContexts, input::InputIterator, TransactionBuilderConfiguration,
},
Expand All @@ -24,6 +25,7 @@ pub struct SimpleTransactionBuilder {
input_iter: InputIterator,
/// The inner transaction builder
tx: TransactionBuilder,
rc_cells: Vec<LiveCell>,
}

impl SimpleTransactionBuilder {
Expand All @@ -37,9 +39,14 @@ impl SimpleTransactionBuilder {
configuration,
input_iter,
tx: TransactionBuilder::default(),
rc_cells: Vec::new(),
}
}

pub fn set_rc_cells(&mut self, rc_cells: Vec<LiveCell>) {
self.rc_cells = rc_cells
}

/// Update the change lock script.
pub fn set_change_lock(&mut self, lock_script: Script) {
self.change_lock = lock_script;
Expand Down Expand Up @@ -71,6 +78,7 @@ impl CkbTransactionBuilder for SimpleTransactionBuilder {
configuration,
input_iter,
tx,
rc_cells,
} = self;

let change_builder = DefaultChangeBuilder {
Expand All @@ -79,6 +87,13 @@ impl CkbTransactionBuilder for SimpleTransactionBuilder {
inputs: Vec::new(),
};

inner_build(tx, change_builder, input_iter, &configuration, contexts)
inner_build(
tx,
change_builder,
input_iter,
&configuration,
contexts,
rc_cells,
)
}
}
18 changes: 16 additions & 2 deletions src/transaction/builder/sudt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,14 @@ impl CkbTransactionBuilder for SudtTransactionBuilder {
};

if owner_mode {
inner_build(tx, change_builder, input_iter, &configuration, contexts)
inner_build(
tx,
change_builder,
input_iter,
&configuration,
contexts,
Default::default(),
)
} else {
let sudt_type_script =
build_sudt_type_script(configuration.network_info(), &sudt_owner_lock_script);
Expand All @@ -154,7 +161,14 @@ impl CkbTransactionBuilder for SudtTransactionBuilder {
.to_le_bytes()
.pack();
tx.set_output_data(tx.outputs_data.len() - 1, change_output_data);
return inner_build(tx, change_builder, input_iter, &configuration, contexts);
return inner_build(
tx,
change_builder,
input_iter,
&configuration,
contexts,
Default::default(),
);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/transaction/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl HandlerContexts {
}
}

#[allow(unused_macros)]
macro_rules! cell_dep {
($hash: literal, $idx: expr, $dep_type: expr) => {{
let out_point = ckb_types::packed::OutPoint::new_builder()
Expand All @@ -90,4 +91,5 @@ macro_rules! cell_dep {
}};
}

#[allow(unused_imports)]
pub(crate) use cell_dep;
48 changes: 10 additions & 38 deletions src/transaction/handler/omnilock.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use ckb_types::{
core::DepType,
h256,
packed::{CellDep, OutPoint, Script},
prelude::{Builder, Entity, Pack},
};

use super::{cell_dep, HandlerContext, ScriptHandler};
use super::{HandlerContext, ScriptHandler};
use crate::{
core::TransactionBuilder,
tx_builder::TxBuilderError,
Expand All @@ -20,8 +19,6 @@ use lazy_static::lazy_static;

pub struct OmnilockScriptHandler {
cell_deps: Vec<CellDep>,
sighash_dep: CellDep,
multisig_dep: CellDep,
lock_script_id: ScriptId,
}

Expand All @@ -41,6 +38,11 @@ impl OmnilockScriptContext {
rpc_url,
}
}

pub fn unlock_mode(mut self, unlock_mode: OmniUnlockMode) -> Self {
self.unlock_mode = unlock_mode;
self
}
}

impl HandlerContext for OmnilockScriptContext {}
Expand All @@ -53,8 +55,6 @@ impl OmnilockScriptHandler {
pub fn new_with_network(network: &NetworkInfo) -> Result<Self, TxBuilderError> {
let mut ret = Self {
cell_deps: vec![],
sighash_dep: Default::default(),
multisig_dep: Default::default(),
lock_script_id: ScriptId::default(),
};
ret.init(network)?;
Expand All @@ -65,6 +65,10 @@ impl OmnilockScriptHandler {
self.cell_deps = cell_deps;
}

pub fn insert_cell_dep(&mut self, cell_dep: CellDep) {
self.cell_deps.push(cell_dep)
}

pub fn set_lock_script_id(&mut self, lock_script_id: ScriptId) {
self.lock_script_id = lock_script_id;
}
Expand Down Expand Up @@ -128,40 +132,8 @@ impl ScriptHandler for OmnilockScriptHandler {

fn init(&mut self, network: &NetworkInfo) -> Result<(), TxBuilderError> {
if network.network_type == NetworkType::Mainnet {
self.sighash_dep = cell_dep!(
"0x71a7ba8fc96349fea0ed3a5c47992e3b4084b031a42264a018e0072e8172e46c",
0u32,
DepType::DepGroup
);
self.multisig_dep = cell_dep!(
"0x71a7ba8fc96349fea0ed3a5c47992e3b4084b031a42264a018e0072e8172e46c",
1u32,
DepType::DepGroup
);
self.cell_deps.push(self.sighash_dep.clone());
self.cell_deps.push(cell_dep!(
"0xc76edf469816aa22f416503c38d0b533d2a018e253e379f134c3985b3472c842",
0u32,
DepType::Code
));
self.lock_script_id = MAINNET_OMNILOCK_SCRIPT_ID.clone();
} else if network.network_type == NetworkType::Testnet {
self.sighash_dep = cell_dep!(
"0xf8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37",
0u32,
DepType::DepGroup
);
self.multisig_dep = cell_dep!(
"0xf8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37",
1u32,
DepType::DepGroup
);
self.cell_deps.push(self.sighash_dep.clone());
self.cell_deps.push(cell_dep!(
"0x3d4296df1bd2cc2bd3f483f61ab7ebeac462a2f336f2b944168fe6ba5d81c014",
0u32,
DepType::Code
));
self.lock_script_id = get_testnet_omnilock_script_id().clone();
} else {
return Err(TxBuilderError::UnsupportedNetworkType(network.network_type));
Expand Down
Loading

0 comments on commit e552c1a

Please sign in to comment.