ckb_sdk/transaction/handler/
mod.rs1use std::any::Any;
2
3use crate::{
4 core::TransactionBuilder, tx_builder::TxBuilderError, unlock::MultisigConfig, NetworkInfo,
5 ScriptGroup,
6};
7
8use self::{
9 sighash::Secp256k1Blake160SighashAllScriptContext, sudt::SudtContext, typeid::TypeIdContext,
10};
11
12pub mod multisig;
13pub mod sighash;
14pub mod sudt;
15pub mod typeid;
16
17#[async_trait::async_trait]
18pub trait ScriptHandler: Send + Sync {
19 fn build_transaction(
23 &self,
24 tx_builder: &mut TransactionBuilder,
25 script_group: &mut ScriptGroup,
26 context: &dyn HandlerContext,
27 ) -> Result<bool, TxBuilderError>;
28 #[cfg(not(target_arch = "wasm32"))]
29 fn init(&mut self, network: &NetworkInfo) -> Result<(), TxBuilderError>;
30 async fn init_async(&mut self, network: &NetworkInfo) -> Result<(), TxBuilderError>;
31}
32
33pub trait Type2Any: 'static {
34 fn as_any(&self) -> &dyn Any;
35}
36
37impl<T: 'static> Type2Any for T {
38 fn as_any(&self) -> &dyn Any {
39 self
40 }
41}
42
43pub trait HandlerContext: Type2Any + Send + Sync {}
44
45pub struct HandlerContexts {
46 pub contexts: Vec<Box<dyn HandlerContext>>,
47}
48
49impl Default for HandlerContexts {
50 fn default() -> Self {
51 Self {
52 contexts: vec![
53 Box::new(Secp256k1Blake160SighashAllScriptContext),
54 Box::new(SudtContext),
55 Box::new(TypeIdContext),
56 ],
57 }
58 }
59}
60
61impl HandlerContexts {
62 pub fn new_sighash() -> Self {
63 Self {
64 contexts: vec![Box::new(Secp256k1Blake160SighashAllScriptContext)],
65 }
66 }
67
68 pub fn new_multisig(config: MultisigConfig) -> Self {
69 Self {
70 contexts: vec![Box::new(
71 multisig::Secp256k1Blake160MultisigAllScriptContext::new(config),
72 )],
73 }
74 }
75
76 pub fn add_context(&mut self, context: Box<dyn HandlerContext>) {
77 self.contexts.push(context);
78 }
79}