@@ -11,6 +11,7 @@ use ckb_sdk::{
1111 Address , CkbRpcClient , NetworkInfo ,
1212} ;
1313use ckb_types:: { core:: Capacity , h160, h256} ;
14+ use ckb_types:: packed:: Transaction as PackedTransaction ;
1415use std:: { env, error:: Error as StdErr , str:: FromStr } ;
1516
1617#[ cfg( test) ]
@@ -168,11 +169,11 @@ mod tests {
168169 Ok ( ( ) )
169170 }
170171
171- // 2.1 简单转账测试
172+ // 2.1 简单转账测试 - 与测试网交互
172173 #[ test]
173174 fn test_simple_transfer ( ) -> Result < ( ) , Box < dyn StdErr > > {
174175 let network_info = NetworkInfo :: testnet ( ) ;
175- let configuration = TransactionBuilderConfiguration :: new_with_network ( network_info. clone ( ) ) ?;
176+ let mut configuration = TransactionBuilderConfiguration :: new_with_network ( network_info. clone ( ) ) ?;
176177
177178 let multisig_config = MultisigConfig :: new_with (
178179 MultisigScript :: V2 ,
@@ -185,10 +186,13 @@ mod tests {
185186 ) ?;
186187 let sender = multisig_config. to_address ( network_info. network_type , MultisigScript :: V2 , None ) ;
187188 let receiver = Address :: from_str ( "ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq2qf8keemy2p5uu0g0gn8cd4ju23s5269qk8rg4r" ) ?;
189+
190+ println ! ( "多签发送地址: {}" , sender) ;
191+ println ! ( "接收地址: {}" , receiver) ;
188192
189193 let iterator = InputIterator :: new_with_address ( & [ sender] , & network_info) ;
190194 let mut builder = SimpleTransactionBuilder :: new ( configuration, iterator) ;
191- builder. add_output ( & receiver, Capacity :: shannons ( 6100000000u64 ) ) ;
195+ builder. add_output ( & receiver, Capacity :: shannons ( 6100001000u64 ) ) ; // 增加容量确保足够
192196
193197 let mut tx_with_groups =
194198 builder. build ( & HandlerContexts :: new_multisig ( multisig_config. clone ( ) ) ) ?;
@@ -208,10 +212,30 @@ mod tests {
208212 & SignContexts :: new_multisig_h256 ( & private_key2, multisig_config) ?,
209213 ) ?;
210214
211- // 验证交易构建是否成功
212- assert ! ( tx_with_groups. get_tx_view( ) . inputs( ) . len( ) > 0 ) ;
213- assert ! ( tx_with_groups. get_tx_view( ) . outputs( ) . len( ) > 0 ) ;
215+ // 获取签名后的交易
216+ let tx = tx_with_groups. get_tx_view ( ) . data ( ) ;
217+ let packed_tx: PackedTransaction = tx. into ( ) ;
218+
219+ // 打印交易详情
220+ println ! ( "交易哈希: {}" , tx_with_groups. get_tx_view( ) . hash( ) ) ;
221+ println ! ( "交易输入数量: {}" , tx_with_groups. get_tx_view( ) . inputs( ) . len( ) ) ;
222+ println ! ( "交易输出数量: {}" , tx_with_groups. get_tx_view( ) . outputs( ) . len( ) ) ;
223+
224+ // 连接到测试网节点,发送交易(用tx_pool_accept_test方式,不上链)
225+ let ckb_client = CkbRpcClient :: new ( & network_info. url ) ;
214226
227+ // let result = ckb_client.send_transaction(packed_tx.into(), Some(OutputsValidator::Passthrough));
228+
229+ // match result {
230+ // Ok(tx_hash) => {
231+ // println!("交易发送成功,交易哈希: {}", tx_hash);
232+ // assert!(true);
233+ // },
234+ // Err(err) => {
235+ // println!("交易发送失败: {}", err);
236+ // assert!(false, "交易发送失败: {}", err);
237+ // }
238+ // }
215239 Ok ( ( ) )
216240 }
217241
@@ -410,6 +434,165 @@ mod tests {
410434}
411435
412436
437+ #[ cfg( test) ]
438+ mod tx_pool_accept_tests {
439+ use super :: * ;
440+ use ckb_jsonrpc_types:: OutputsValidator ;
441+ use ckb_types:: packed:: Transaction as PackedTransaction ;
442+
443+ // 测试旧的多签(Legacy)能够构造交易并通过 test_tx_pool_accept 成功
444+ #[ test]
445+ fn test_legacy_multisig_tx_pool_accept ( ) -> Result < ( ) , Box < dyn StdErr > > {
446+ let network_info = NetworkInfo :: testnet ( ) ;
447+ let configuration = TransactionBuilderConfiguration :: new_with_network ( network_info. clone ( ) ) ?;
448+
449+ // 创建Legacy多签配置
450+ let multisig_config = MultisigConfig :: new_with (
451+ MultisigScript :: Legacy ,
452+ vec ! [
453+ h160!( "0x7336b0ba900684cb3cb00f0d46d4f64c0994a562" ) ,
454+ h160!( "0x5724c1e3925a5206944d753a6f3edaedf977d77f" ) ,
455+ ] ,
456+ 0 ,
457+ 2 ,
458+ ) ?;
459+ let sender = multisig_config. to_address ( network_info. network_type , MultisigScript :: Legacy , None ) ;
460+ let receiver = Address :: from_str ( "ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq2qf8keemy2p5uu0g0gn8cd4ju23s5269qk8rg4r" ) ?;
461+
462+ let iterator = InputIterator :: new_with_address ( & [ sender] , & network_info) ;
463+ let mut builder = SimpleTransactionBuilder :: new ( configuration, iterator) ;
464+ builder. add_output ( & receiver, Capacity :: shannons ( 6100000000u64 ) ) ;
465+
466+ let mut tx_with_groups =
467+ builder. build ( & HandlerContexts :: new_multisig ( multisig_config. clone ( ) ) ) ?;
468+
469+ // 签名交易
470+ let private_key1 = h256 ! ( "0x4fd809631a6aa6e3bb378dd65eae5d71df895a82c91a615a1e8264741515c79c" ) ;
471+ let signer1 = TransactionSigner :: new ( & network_info) ;
472+ signer1. sign_transaction (
473+ & mut tx_with_groups,
474+ & SignContexts :: new_multisig_h256 ( & private_key1, multisig_config. clone ( ) ) ?,
475+ ) ?;
476+
477+ let private_key2 = h256 ! ( "0x7438f7b35c355e3d2fb9305167a31a72d22ddeafb80a21cc99ff6329d92e8087" ) ;
478+ let signer2 = TransactionSigner :: new ( & network_info) ;
479+ signer2. sign_transaction (
480+ & mut tx_with_groups,
481+ & SignContexts :: new_multisig_h256 ( & private_key2, multisig_config) ?,
482+ ) ?;
483+
484+ // 获取签名后的交易
485+ let tx = tx_with_groups. get_tx_view ( ) . data ( ) ;
486+ let packed_tx: PackedTransaction = tx. into ( ) ;
487+
488+ // 连接到测试网节点
489+ let ckb_client = CkbRpcClient :: new ( & network_info. url ) ;
490+
491+ // 使用test_tx_pool_accept验证交易
492+ let result = ckb_client. test_tx_pool_accept ( packed_tx. into ( ) , Some ( OutputsValidator :: Passthrough ) ) ;
493+
494+ // 打印结果
495+ match result {
496+ Ok ( entry) => {
497+ println ! ( "Legacy多签交易验证成功: {:?}" , entry) ;
498+ assert ! ( true ) ;
499+ } ,
500+ Err ( err) => {
501+ println ! ( "Legacy多签交易验证失败: {}" , err) ;
502+ assert ! ( false , "Legacy多签交易验证失败: {}" , err) ;
503+ }
504+ }
505+
506+ Ok ( ( ) )
507+ }
508+
509+ // 测试新的多签(V2)能够构造交易并通过 test_tx_pool_accept 成功
510+ #[ test]
511+ fn test_v2_multisig_tx_pool_accept ( ) -> Result < ( ) , Box < dyn StdErr > > {
512+ use ckb_sdk:: transaction:: handler:: multisig:: Secp256k1Blake160MultisigAllScriptHandler ;
513+
514+ let network_info = NetworkInfo :: testnet ( ) ;
515+ let mut configuration = TransactionBuilderConfiguration :: new_with_network ( network_info. clone ( ) ) ?;
516+ // 创建V2多签配置
517+ let multisig_config = MultisigConfig :: new_with (
518+ MultisigScript :: V2 ,
519+ vec ! [
520+ h160!( "0x7336b0ba900684cb3cb00f0d46d4f64c0994a562" ) ,
521+ h160!( "0x5724c1e3925a5206944d753a6f3edaedf977d77f" ) ,
522+ ] ,
523+ 0 ,
524+ 2 ,
525+ ) ?;
526+ let sender = multisig_config. to_address ( network_info. network_type , MultisigScript :: V2 , None ) ;
527+ let receiver = Address :: from_str ( "ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq2qf8keemy2p5uu0g0gn8cd4ju23s5269qk8rg4r" ) ?;
528+
529+ println ! ( "Multisig sender address: {}" , sender) ;
530+ println ! ( "Multisig receiver address: {}" , receiver) ;
531+
532+ // 创建多签脚本处理器,它会自动添加正确的cell_dep
533+ let multisig_handler = Secp256k1Blake160MultisigAllScriptHandler :: new (
534+ & network_info,
535+ MultisigScript :: V2 ,
536+ ) ?;
537+
538+ // 打印V2多签脚本ID和cell_deps
539+ let v2_script_id = MultisigScript :: V2 . script_id ( ) ;
540+ println ! ( "V2 script ID: code_hash={}, hash_type={:?}" , v2_script_id. code_hash, v2_script_id. hash_type) ;
541+
542+ let iterator = InputIterator :: new_with_address ( & [ sender] , & network_info) ;
543+ let mut builder = SimpleTransactionBuilder :: new ( configuration, iterator) ;
544+
545+ // 增加输出金额,确保有足够的容量
546+ builder. add_output ( & receiver, Capacity :: shannons ( 6100001000u64 ) ) ;
547+
548+ // 使用HandlerContexts::new_multisig构建交易,它会自动添加正确的cell_dep
549+ let mut tx_with_groups =
550+ builder. build ( & HandlerContexts :: new_multisig ( multisig_config. clone ( ) ) ) ?;
551+
552+ // 签名交易
553+ let private_key1 = h256 ! ( "0x4fd809631a6aa6e3bb378dd65eae5d71df895a82c91a615a1e8264741515c79c" ) ;
554+ let signer1 = TransactionSigner :: new ( & network_info) ;
555+ signer1. sign_transaction (
556+ & mut tx_with_groups,
557+ & SignContexts :: new_multisig_h256 ( & private_key1, multisig_config. clone ( ) ) ?,
558+ ) ?;
559+
560+ let private_key2 = h256 ! ( "0x7438f7b35c355e3d2fb9305167a31a72d22ddeafb80a21cc99ff6329d92e8087" ) ;
561+ let signer2 = TransactionSigner :: new ( & network_info) ;
562+ signer2. sign_transaction (
563+ & mut tx_with_groups,
564+ & SignContexts :: new_multisig_h256 ( & private_key2, multisig_config) ?,
565+ ) ?;
566+
567+ // 获取签名后的交易
568+ let tx = tx_with_groups. get_tx_view ( ) . data ( ) ;
569+ let packed_tx: PackedTransaction = tx. into ( ) ;
570+
571+ // 打印交易的详细信息
572+ println ! ( "Transaction cell deps: {:?}" , tx_with_groups. get_tx_view( ) . cell_deps( ) ) ;
573+
574+ // 连接到测试网节点
575+ let ckb_client = CkbRpcClient :: new ( & network_info. url ) ;
576+
577+ // 使用test_tx_pool_accept验证交易
578+ let result = ckb_client. test_tx_pool_accept ( packed_tx. into ( ) , Some ( OutputsValidator :: Passthrough ) ) ;
579+
580+ // 打印结果
581+ match result {
582+ Ok ( entry) => {
583+ println ! ( "V2多签交易验证成功: {:?}" , entry) ;
584+ assert ! ( true ) ;
585+ } ,
586+ Err ( err) => {
587+ println ! ( "V2多签交易验证失败: {}" , err) ;
588+ assert ! ( false , "V2多签交易验证失败: {}" , err) ;
589+ }
590+ }
591+
592+ Ok ( ( ) )
593+ }
594+ }
595+
413596#[ cfg( test) ]
414597mod omni_multisig_tests {
415598 use super :: * ;
0 commit comments