@@ -10,8 +10,8 @@ use ckb_types::{
1010use crate :: {
1111 constants:: ONE_CKB ,
1212 tests:: {
13- build_omnilock_script, build_sighash_script, init_context, ACCOUNT0_KEY , ACCOUNT2_ARG ,
14- FEE_RATE , OMNILOCK_BIN ,
13+ build_omnilock_script, build_sighash_script, init_context, ACCOUNT0_ARG , ACCOUNT0_KEY ,
14+ ACCOUNT1_ARG , ACCOUNT1_KEY , ACCOUNT2_ARG , FEE_RATE , OMNILOCK_BIN ,
1515 } ,
1616 transaction:: {
1717 builder:: { CkbTransactionBuilder , SimpleTransactionBuilder } ,
@@ -26,8 +26,8 @@ use crate::{
2626 signer:: { SignContexts , TransactionSigner } ,
2727 TransactionBuilderConfiguration ,
2828 } ,
29- unlock:: OmniLockConfig ,
30- util:: keccak160,
29+ unlock:: { MultisigConfig , OmniLockConfig } ,
30+ util:: { blake160 , keccak160} ,
3131 NetworkInfo ,
3232} ;
3333
@@ -65,11 +65,17 @@ fn test_omnilock_config(omnilock_outpoint: OutPoint) -> TransactionBuilderConfig
6565}
6666
6767#[ test]
68- fn test_transfer_from_omnilock_ethereum ( ) {
68+ fn test_omnilock_ethereum ( ) {
69+ omnilock_ethereum ( false ) ;
70+ omnilock_ethereum ( true )
71+ }
72+
73+ fn omnilock_ethereum ( cobuild : bool ) {
6974 let network_info = NetworkInfo :: testnet ( ) ;
7075 let account0_key = secp256k1:: SecretKey :: from_slice ( ACCOUNT0_KEY . as_bytes ( ) ) . unwrap ( ) ;
7176 let pubkey = secp256k1:: PublicKey :: from_secret_key ( & SECP256K1 , & account0_key) ;
72- let cfg = OmniLockConfig :: new_ethereum ( keccak160 ( Pubkey :: from ( pubkey) . as_ref ( ) ) ) ;
77+ let mut cfg = OmniLockConfig :: new_ethereum ( keccak160 ( Pubkey :: from ( pubkey) . as_ref ( ) ) ) ;
78+ cfg. enable_cobuild ( cobuild) ;
7379
7480 let sender = build_omnilock_script ( & cfg) ;
7581 let receiver = build_sighash_script ( ACCOUNT2_ARG ) ;
@@ -104,8 +110,8 @@ fn test_transfer_from_omnilock_ethereum() {
104110
105111 let mut tx_with_groups = builder. build ( & contexts) . expect ( "build failed" ) ;
106112
107- let json_tx = ckb_jsonrpc_types:: TransactionView :: from ( tx_with_groups. get_tx_view ( ) . clone ( ) ) ;
108- println ! ( "tx: {}" , serde_json:: to_string_pretty( & json_tx) . unwrap( ) ) ;
113+ // let json_tx = ckb_jsonrpc_types::TransactionView::from(tx_with_groups.get_tx_view().clone());
114+ // println!("tx: {}", serde_json::to_string_pretty(&json_tx).unwrap());
109115
110116 TransactionSigner :: new ( & network_info)
111117 // use unitest lock to verify
@@ -119,8 +125,171 @@ fn test_transfer_from_omnilock_ethereum() {
119125 )
120126 . unwrap ( ) ;
121127
122- let json_tx = ckb_jsonrpc_types:: TransactionView :: from ( tx_with_groups. get_tx_view ( ) . clone ( ) ) ;
123- println ! ( "tx: {}" , serde_json:: to_string_pretty( & json_tx) . unwrap( ) ) ;
128+ // let json_tx = ckb_jsonrpc_types::TransactionView::from(tx_with_groups.get_tx_view().clone());
129+ // println!("tx: {}", serde_json::to_string_pretty(&json_tx).unwrap());
130+
131+ let tx = tx_with_groups. get_tx_view ( ) . clone ( ) ;
132+ let script_groups = tx_with_groups. script_groups . clone ( ) ;
133+ assert_eq ! ( script_groups. len( ) , 1 ) ;
134+ assert_eq ! ( tx. header_deps( ) . len( ) , 0 ) ;
135+ assert_eq ! ( tx. cell_deps( ) . len( ) , 2 ) ;
136+ assert_eq ! ( tx. inputs( ) . len( ) , 2 ) ;
137+ for out_point in tx. input_pts_iter ( ) {
138+ assert_eq ! ( ctx. get_input( & out_point) . unwrap( ) . 0 . lock( ) , sender) ;
139+ }
140+ assert_eq ! ( tx. outputs( ) . len( ) , 2 ) ;
141+ assert_eq ! ( tx. output( 0 ) . unwrap( ) , output) ;
142+ assert_eq ! ( tx. output( 1 ) . unwrap( ) . lock( ) , sender) ;
143+ let change_capacity: u64 = tx. output ( 1 ) . unwrap ( ) . capacity ( ) . unpack ( ) ;
144+ let fee = ( 100 + 200 - 120 ) * ONE_CKB - change_capacity;
145+ assert_eq ! ( tx. data( ) . as_reader( ) . serialized_size_in_block( ) as u64 , fee) ;
146+
147+ ctx. verify ( tx, FEE_RATE ) . unwrap ( ) ;
148+ }
149+
150+ #[ test]
151+ fn test_omnilock_pubkeyhash ( ) {
152+ omnilock_pubkeyhash ( false ) ;
153+ omnilock_pubkeyhash ( true )
154+ }
155+
156+ fn omnilock_pubkeyhash ( cobuild : bool ) {
157+ let network_info = NetworkInfo :: testnet ( ) ;
158+ let account0_key = secp256k1:: SecretKey :: from_slice ( ACCOUNT0_KEY . as_bytes ( ) ) . unwrap ( ) ;
159+ let pubkey = secp256k1:: PublicKey :: from_secret_key ( & SECP256K1 , & account0_key) ;
160+ let mut cfg = OmniLockConfig :: new_pubkey_hash ( blake160 ( & pubkey. serialize ( ) ) ) ;
161+ cfg. enable_cobuild ( cobuild) ;
162+
163+ let sender = build_omnilock_script ( & cfg) ;
164+ let receiver = build_sighash_script ( ACCOUNT2_ARG ) ;
165+ let ( ctx, mut outpoints) = init_context (
166+ vec ! [ ( OMNILOCK_BIN , true ) ] ,
167+ vec ! [
168+ ( sender. clone( ) , Some ( 100 * ONE_CKB ) ) ,
169+ ( sender. clone( ) , Some ( 200 * ONE_CKB ) ) ,
170+ ( sender. clone( ) , Some ( 300 * ONE_CKB ) ) ,
171+ ] ,
172+ ) ;
173+
174+ let configuration = test_omnilock_config ( outpoints. pop ( ) . unwrap ( ) ) ;
175+
176+ let iterator = InputIterator :: new_with_cell_collector (
177+ vec ! [ sender. clone( ) ] ,
178+ Box :: new ( ctx. to_live_cells_context ( ) ) as Box < _ > ,
179+ ) ;
180+ let mut builder = SimpleTransactionBuilder :: new ( configuration, iterator) ;
181+
182+ let output = CellOutput :: new_builder ( )
183+ . capacity ( ( 120 * ONE_CKB ) . pack ( ) )
184+ . lock ( receiver)
185+ . build ( ) ;
186+ builder. add_output_and_data ( output. clone ( ) , ckb_types:: packed:: Bytes :: default ( ) ) ;
187+ builder. set_change_lock ( sender. clone ( ) ) ;
188+
189+ let context = OmnilockScriptContext :: new ( cfg. clone ( ) , network_info. url . clone ( ) ) ;
190+ let mut contexts = HandlerContexts :: default ( ) ;
191+ contexts. add_context ( Box :: new ( context) as Box < _ > ) ;
192+
193+ let mut tx_with_groups = builder. build ( & contexts) . expect ( "build failed" ) ;
194+
195+ // let json_tx = ckb_jsonrpc_types::TransactionView::from(tx_with_groups.get_tx_view().clone());
196+
197+ TransactionSigner :: new ( & network_info)
198+ // use unitest lock to verify
199+ . insert_unlocker (
200+ crate :: ScriptId :: new_data1 ( H256 :: from ( blake2b_256 ( OMNILOCK_BIN ) ) ) ,
201+ crate :: transaction:: signer:: omnilock:: OmnilockSigner { } ,
202+ )
203+ . sign_transaction (
204+ & mut tx_with_groups,
205+ & SignContexts :: new_omnilock ( vec ! [ account0_key] , cfg) ,
206+ )
207+ . unwrap ( ) ;
208+
209+ let tx = tx_with_groups. get_tx_view ( ) . clone ( ) ;
210+ let script_groups = tx_with_groups. script_groups . clone ( ) ;
211+ assert_eq ! ( script_groups. len( ) , 1 ) ;
212+ assert_eq ! ( tx. header_deps( ) . len( ) , 0 ) ;
213+ assert_eq ! ( tx. cell_deps( ) . len( ) , 2 ) ;
214+ assert_eq ! ( tx. inputs( ) . len( ) , 2 ) ;
215+ for out_point in tx. input_pts_iter ( ) {
216+ assert_eq ! ( ctx. get_input( & out_point) . unwrap( ) . 0 . lock( ) , sender) ;
217+ }
218+ assert_eq ! ( tx. outputs( ) . len( ) , 2 ) ;
219+ assert_eq ! ( tx. output( 0 ) . unwrap( ) , output) ;
220+ assert_eq ! ( tx. output( 1 ) . unwrap( ) . lock( ) , sender) ;
221+ let change_capacity: u64 = tx. output ( 1 ) . unwrap ( ) . capacity ( ) . unpack ( ) ;
222+ let fee = ( 100 + 200 - 120 ) * ONE_CKB - change_capacity;
223+ assert_eq ! ( tx. data( ) . as_reader( ) . serialized_size_in_block( ) as u64 , fee) ;
224+
225+ ctx. verify ( tx, FEE_RATE ) . unwrap ( ) ;
226+ }
227+
228+ #[ test]
229+ fn test_omnilock_multisign ( ) {
230+ omnilock_multisign ( false ) ;
231+ omnilock_multisign ( true )
232+ }
233+
234+ fn omnilock_multisign ( cobuild : bool ) {
235+ let network_info = NetworkInfo :: testnet ( ) ;
236+ let account0_key = secp256k1:: SecretKey :: from_slice ( ACCOUNT0_KEY . as_bytes ( ) ) . unwrap ( ) ;
237+ let account1_key = secp256k1:: SecretKey :: from_slice ( ACCOUNT1_KEY . as_bytes ( ) ) . unwrap ( ) ;
238+ let lock_args = vec ! [
239+ ACCOUNT0_ARG . clone( ) ,
240+ ACCOUNT1_ARG . clone( ) ,
241+ ACCOUNT2_ARG . clone( ) ,
242+ ] ;
243+ let multi_cfg = MultisigConfig :: new_with ( lock_args, 0 , 2 ) . unwrap ( ) ;
244+ let mut cfg = OmniLockConfig :: new_multisig ( multi_cfg) ;
245+ cfg. enable_cobuild ( cobuild) ;
246+
247+ let sender = build_omnilock_script ( & cfg) ;
248+ let receiver = build_sighash_script ( ACCOUNT2_ARG ) ;
249+
250+ let ( ctx, mut outpoints) = init_context (
251+ vec ! [ ( OMNILOCK_BIN , true ) ] ,
252+ vec ! [
253+ ( sender. clone( ) , Some ( 100 * ONE_CKB ) ) ,
254+ ( sender. clone( ) , Some ( 200 * ONE_CKB ) ) ,
255+ ( sender. clone( ) , Some ( 300 * ONE_CKB ) ) ,
256+ ] ,
257+ ) ;
258+
259+ let configuration = test_omnilock_config ( outpoints. pop ( ) . unwrap ( ) ) ;
260+
261+ let iterator = InputIterator :: new_with_cell_collector (
262+ vec ! [ sender. clone( ) ] ,
263+ Box :: new ( ctx. to_live_cells_context ( ) ) as Box < _ > ,
264+ ) ;
265+ let mut builder = SimpleTransactionBuilder :: new ( configuration, iterator) ;
266+
267+ let output = CellOutput :: new_builder ( )
268+ . capacity ( ( 120 * ONE_CKB ) . pack ( ) )
269+ . lock ( receiver)
270+ . build ( ) ;
271+ builder. add_output_and_data ( output. clone ( ) , ckb_types:: packed:: Bytes :: default ( ) ) ;
272+ builder. set_change_lock ( sender. clone ( ) ) ;
273+
274+ let context = OmnilockScriptContext :: new ( cfg. clone ( ) , network_info. url . clone ( ) ) ;
275+ let mut contexts = HandlerContexts :: default ( ) ;
276+ contexts. add_context ( Box :: new ( context) as Box < _ > ) ;
277+
278+ let mut tx_with_groups = builder. build ( & contexts) . expect ( "build failed" ) ;
279+
280+ // let json_tx = ckb_jsonrpc_types::TransactionView::from(tx_with_groups.get_tx_view().clone());
281+
282+ TransactionSigner :: new ( & network_info)
283+ // use unitest lock to verify
284+ . insert_unlocker (
285+ crate :: ScriptId :: new_data1 ( H256 :: from ( blake2b_256 ( OMNILOCK_BIN ) ) ) ,
286+ crate :: transaction:: signer:: omnilock:: OmnilockSigner { } ,
287+ )
288+ . sign_transaction (
289+ & mut tx_with_groups,
290+ & SignContexts :: new_omnilock ( vec ! [ account0_key, account1_key] , cfg) ,
291+ )
292+ . unwrap ( ) ;
124293
125294 let tx = tx_with_groups. get_tx_view ( ) . clone ( ) ;
126295 let script_groups = tx_with_groups. script_groups . clone ( ) ;
0 commit comments