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

refact omnilock transaction builder #74

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
52 changes: 52 additions & 0 deletions examples/dao_deposit_example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use ckb_sdk::{
transaction::{
builder::{CkbTransactionBuilder, SimpleTransactionBuilder},
handler::{dao, HandlerContexts},
input::InputIterator,
signer::{SignContexts, TransactionSigner},
TransactionBuilderConfiguration,
},
Address, CkbRpcClient, NetworkInfo,
};
use ckb_types::h256;
use std::{error::Error as StdErr, str::FromStr};

fn main() -> Result<(), Box<dyn StdErr>> {
let network_info = NetworkInfo::testnet();
let sender = Address::from_str("ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq2qf8keemy2p5uu0g0gn8cd4ju23s5269qk8rg4r")?;
let receiver=Address::from_str("ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqv5dsed9par23x4g58seaw58j3ym5ml2hs8ztche")?;

let configuration = TransactionBuilderConfiguration::new_with_network(network_info.clone())?;

let iterator = InputIterator::new(vec![(&sender).into()], configuration.network_info());
let mut builder = SimpleTransactionBuilder::new(configuration, iterator);

let context = dao::DepositContext::new((&receiver).into(), 510_0000_0000u64);
let mut contexts = HandlerContexts::default();
contexts.add_context(Box::new(context) as Box<_>);

builder.set_change_lock((&sender).into());
let mut tx_with_groups = builder.build(&mut contexts)?;

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 private_keys = vec![h256!(
"0x6c9ed03816e3111e49384b8d180174ad08e29feb1393ea1b51cef1c505d4e36a"
)];
TransactionSigner::new(&network_info).sign_transaction(
&mut tx_with_groups,
&SignContexts::new_sighash_h256(private_keys)?,
)?;

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 tx_hash = CkbRpcClient::new(network_info.url.as_str())
.send_transaction(json_tx.inner, None)
.expect("send transaction");
// example tx: 8b7ab7770c821fa8dc70738d5d6ef43da46706541c258b9a02edf66948039798
println!(">>> tx {} sent! <<<", tx_hash);

Ok(())
}
61 changes: 61 additions & 0 deletions examples/dao_withdraw_phrase1_example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use ckb_sdk::{
transaction::{
builder::{CkbTransactionBuilder, SimpleTransactionBuilder},
handler::{dao, HandlerContexts},
input::InputIterator,
signer::{SignContexts, TransactionSigner},
TransactionBuilderConfiguration,
},
Address, CkbRpcClient, NetworkInfo,
};
use ckb_types::h256;
use std::{error::Error as StdErr, str::FromStr};

fn main() -> Result<(), Box<dyn StdErr>> {
let network_info = NetworkInfo::testnet();
let sender = Address::from_str("ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq2qf8keemy2p5uu0g0gn8cd4ju23s5269qk8rg4r")?;

let configuration = TransactionBuilderConfiguration::new_with_network(network_info.clone())?;

let iterator = InputIterator::new(vec![(&sender).into()], configuration.network_info());
let mut builder = SimpleTransactionBuilder::new(configuration, iterator);

let mut context = dao::WithdrawPhrase1Context::new(network_info.url.clone());
let input_outpoint = serde_json::from_str::<ckb_jsonrpc_types::OutPoint>(
r#"
{
"tx_hash": "0x2aba579894cdf5f6c4afd3ada52792c4405fe6ba64d05226fb63fa5c1ec6f666",
"index": "0x0"
}
"#,
)
.unwrap();
context.add_input_outpoint(input_outpoint.into(), None);
let mut contexts = HandlerContexts::default();
contexts.add_context(Box::new(context) as Box<_>);

builder.set_change_lock((&sender).into());
let mut tx_with_groups = builder.build(&mut contexts)?;

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 private_keys = vec![h256!(
"0x6c9ed03816e3111e49384b8d180174ad08e29feb1393ea1b51cef1c505d4e36a"
)];
TransactionSigner::new(&network_info).sign_transaction(
&mut tx_with_groups,
&SignContexts::new_sighash_h256(private_keys)?,
)?;

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 tx_hash = CkbRpcClient::new(network_info.url.as_str())
.send_transaction(json_tx.inner, None)
.expect("send transaction");
// example tx: b615b9cbb566af18dd2d860836b89e07a86dfcc7af510595dcb404f1b19e6d7e
println!(">>> tx {} sent! <<<", tx_hash);

Ok(())
}
61 changes: 61 additions & 0 deletions examples/dao_withdraw_phrase2_example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use ckb_sdk::{
transaction::{
builder::{CkbTransactionBuilder, SimpleTransactionBuilder},
handler::{dao, HandlerContexts},
input::InputIterator,
signer::{SignContexts, TransactionSigner},
TransactionBuilderConfiguration,
},
Address, CkbRpcClient, NetworkInfo,
};
use ckb_types::h256;
use std::{error::Error as StdErr, str::FromStr};

fn main() -> Result<(), Box<dyn StdErr>> {
let network_info = NetworkInfo::testnet();
let sender = Address::from_str("ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq2qf8keemy2p5uu0g0gn8cd4ju23s5269qk8rg4r")?;

let configuration = TransactionBuilderConfiguration::new_with_network(network_info.clone())?;

let iterator = InputIterator::new(vec![(&sender).into()], configuration.network_info());
let mut builder = SimpleTransactionBuilder::new(configuration, iterator);

let input_outpoint = serde_json::from_str::<ckb_jsonrpc_types::OutPoint>(
r#"
{
"tx_hash": "0x770f930ed3bf35664cb6a112edce3287712f0613c74c1f1176e099ee51268489",
"index": "0x0"
}
"#,
)
.unwrap();
let context =
dao::WithdrawPhrase2Context::new(vec![input_outpoint.into()], network_info.url.clone());
let mut contexts = HandlerContexts::default();
contexts.add_context(Box::new(context) as Box<_>);

builder.set_change_lock((&sender).into());
let mut tx_with_groups = builder.build(&mut contexts)?;

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 private_keys = vec![h256!(
"0x6c9ed03816e3111e49384b8d180174ad08e29feb1393ea1b51cef1c505d4e36a"
)];
TransactionSigner::new(&network_info).sign_transaction(
&mut tx_with_groups,
&SignContexts::new_sighash_h256(private_keys)?,
)?;

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 tx_hash = CkbRpcClient::new(network_info.url.as_str())
.send_transaction(json_tx.inner, None)
.expect("send transaction");
// example tx: 0xaae93c573848a632f06f01c7c444c90aa490253f35b4212d147882266960a267
println!(">>> tx {} sent! <<<", tx_hash);

Ok(())
}
58 changes: 58 additions & 0 deletions examples/omnilock_example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use ckb_sdk::{
transaction::{
builder::{CkbTransactionBuilder, SimpleTransactionBuilder},
handler::{omnilock, HandlerContexts},
input::InputIterator,
signer::{SignContexts, TransactionSigner},
TransactionBuilderConfiguration,
},
unlock::OmniLockConfig,
Address, CkbRpcClient, NetworkInfo,
};
use ckb_types::{core::Capacity, h256};
use std::{error::Error as StdErr, str::FromStr};

fn main() -> Result<(), Box<dyn StdErr>> {
let network_info = NetworkInfo::testnet();
let sender = Address::from_str("ckt1qrejnmlar3r452tcg57gvq8patctcgy8acync0hxfnyka35ywafvkqgqgpy7m88v3gxnn3apazvlpkkt32xz3tg5qq3kzjf3")?;
let receiver = Address::from_str("ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqv5dsed9par23x4g58seaw58j3ym5ml2hs8ztche")?;

let configuration = TransactionBuilderConfiguration::new_with_network(network_info.clone())?;

let iterator = InputIterator::new_with_address(&[sender.clone()], configuration.network_info());
let mut builder = SimpleTransactionBuilder::new(configuration, iterator);

builder.add_output_from_addr(&receiver, Capacity::bytes(128)?);
builder.set_change_addr(&sender);

let omni_cfg = OmniLockConfig::from_addr(&sender).unwrap();
let context = omnilock::OmnilockScriptContext::new(omni_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(&mut contexts)?;

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 private_key = h256!("0x6c9ed03816e3111e49384b8d180174ad08e29feb1393ea1b51cef1c505d4e36a");
TransactionSigner::new(&network_info).sign_transaction(
&mut tx_with_groups,
&SignContexts::new_omnilock(
secp256k1::SecretKey::from_slice(private_key.as_bytes())?,
omni_cfg,
),
)?;

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 tx_hash = CkbRpcClient::new(network_info.url.as_str())
.send_transaction(json_tx.inner, None)
.expect("send transaction");
// example tx: 0xc0c9954a3299b540e63351146a301438372abf93682d96c7cce691c334dd5757
println!(">>> tx {} sent! <<<", tx_hash);

Ok(())
}
77 changes: 77 additions & 0 deletions examples/omnilock_multisig_example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use ckb_sdk::{
transaction::{
builder::{CkbTransactionBuilder, SimpleTransactionBuilder},
handler::{omnilock, HandlerContexts},
input::InputIterator,
signer::{SignContexts, TransactionSigner},
TransactionBuilderConfiguration,
},
unlock::{MultisigConfig, OmniLockConfig},
Address, CkbRpcClient, NetworkInfo,
};
use ckb_types::{core::Capacity, h160, h256};
use std::{error::Error as StdErr, str::FromStr};

fn main() -> Result<(), Box<dyn StdErr>> {
let network_info = NetworkInfo::testnet();
let sender = Address::from_str("ckt1qrejnmlar3r452tcg57gvq8patctcgy8acync0hxfnyka35ywafvkqgxhjvp3k9pf88upngryvuxc346q7fq5qmlqqlrhr0p")?;
let receiver = Address::from_str("ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqv5dsed9par23x4g58seaw58j3ym5ml2hs8ztche")?;

let configuration = TransactionBuilderConfiguration::new_with_network(network_info.clone())?;

let iterator = InputIterator::new_with_address(&[sender.clone()], configuration.network_info());
let mut builder = SimpleTransactionBuilder::new(configuration, iterator);

builder.add_output_from_addr(&receiver, Capacity::bytes(128)?);
builder.set_change_addr(&sender);

let mut omni_cfg = OmniLockConfig::from_addr(&sender).unwrap();
let multisig_config = MultisigConfig::new_with(
vec![
h160!("0x7336b0ba900684cb3cb00f0d46d4f64c0994a562"),
h160!("0x5724c1e3925a5206944d753a6f3edaedf977d77f"),
],
0,
2,
)
.unwrap();
omni_cfg.set_multisig_config(Some(multisig_config));
let context = omnilock::OmnilockScriptContext::new(omni_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(&mut contexts)?;

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 signer = TransactionSigner::new(&network_info);
let private_key = h256!("0x7438f7b35c355e3d2fb9305167a31a72d22ddeafb80a21cc99ff6329d92e8087");
signer.sign_transaction(
&mut tx_with_groups,
&SignContexts::new_omnilock(
secp256k1::SecretKey::from_slice(private_key.as_bytes())?,
omni_cfg.clone(),
),
)?;
let private_key = h256!("0x4fd809631a6aa6e3bb378dd65eae5d71df895a82c91a615a1e8264741515c79c");
signer.sign_transaction(
&mut tx_with_groups,
&SignContexts::new_omnilock(
secp256k1::SecretKey::from_slice(private_key.as_bytes())?,
omni_cfg,
),
)?;

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 tx_hash = CkbRpcClient::new(network_info.url.as_str())
.send_transaction(json_tx.inner, None)
.expect("send transaction");
// example tx: 3c5062f75f8c9dc799a3286ebef070cd3aa1b51575244c912076b90cb915a374
println!(">>> tx {} sent! <<<", tx_hash);

Ok(())
}
2 changes: 1 addition & 1 deletion examples/send_ckb_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn main() -> Result<(), Box<dyn StdErr>> {
let addr = Address::from_str(sender)?;
builder.add_output_from_addr(&receiver, Capacity::shannons(510_0000_0000u64));
builder.set_change_addr(&addr);
let mut tx_with_groups = builder.build(&Default::default())?;
let mut tx_with_groups = builder.build(&mut Default::default())?;

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());
Expand Down
2 changes: 1 addition & 1 deletion examples/send_ckb_multisig_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn main() -> Result<(), Box<dyn StdErr>> {
builder.add_output_from_addr(&addr, Capacity::shannons(501_0000_0000u64));
builder.set_change_addr(&sender_addr);
let mut tx_with_groups =
builder.build(&HandlerContexts::new_multisig(multisig_config.clone()))?;
builder.build(&mut HandlerContexts::new_multisig(multisig_config.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());
Expand Down
57 changes: 57 additions & 0 deletions examples/sudt_issue_example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use ckb_sdk::{
transaction::{
builder::{CkbTransactionBuilder, SimpleTransactionBuilder},
handler::{udt, HandlerContexts},
input::InputIterator,
signer::{SignContexts, TransactionSigner},
TransactionBuilderConfiguration,
},
Address, CkbRpcClient, NetworkInfo,
};
use ckb_types::h256;
use std::{error::Error as StdErr, str::FromStr};

fn main() -> Result<(), Box<dyn StdErr>> {
let network_info = NetworkInfo::testnet();
let owner = Address::from_str("ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqdamwzrffgc54ef48493nfd2sd0h4cjnxg4850up")?;
let receiver=Address::from_str("ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqv5dsed9par23x4g58seaw58j3ym5ml2hs8ztche")?;

let configuration = TransactionBuilderConfiguration::new_with_network(network_info.clone())?;

let iterator = InputIterator::new(vec![(&owner).into()], configuration.network_info());
let mut builder = SimpleTransactionBuilder::new(configuration, iterator);

let context = udt::UdtIssueContext::new_default(
(&owner).into(),
(&receiver).into(),
10u128,
&network_info,
);
let mut contexts = HandlerContexts::default();
contexts.add_context(Box::new(context) as Box<_>);

builder.set_change_lock((&owner).into());
let mut tx_with_groups = builder.build(&mut contexts)?;

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 private_keys = vec![h256!(
"0x0c982052ffd4af5f3bbf232301dcddf468009161fc48ba1426e3ce0929fb59f8"
)];
TransactionSigner::new(&network_info).sign_transaction(
&mut tx_with_groups,
&SignContexts::new_sighash_h256(private_keys)?,
)?;

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 tx_hash = CkbRpcClient::new(network_info.url.as_str())
.send_transaction(json_tx.inner, None)
.expect("send transaction");
// example tx: e992819c59233c05eb83552203695840b5e76a2c3faf606adee4a1d1df4837d9
println!(">>> tx {} sent! <<<", tx_hash);

Ok(())
}
Loading