11use core:: { result:: Result :: Ok , str:: FromStr } ;
2- use std:: cmp:: Reverse ;
2+ use std:: { cmp:: Reverse , sync :: Arc } ;
33
44use anyhow:: anyhow;
55use bitcoin:: {
@@ -33,6 +33,42 @@ use super::context::WriterContext;
3333
3434const BITCOIN_DUST_LIMIT : u64 = 546 ;
3535
36+ /// Config for creating envelope transactions.
37+ #[ derive( Debug , Clone ) ]
38+ pub struct EnvelopeConfig {
39+ pub params : Arc < Params > ,
40+ /// Address to send change and reveal output to
41+ pub sequencer_address : Address ,
42+ /// Amount to send to reveal address.
43+ ///
44+ /// NOTE: must be higher than the dust limit.
45+ //
46+ // TODO: Make this and all other bitcoin related values to Amount
47+ pub reveal_amount : u64 ,
48+ /// Bitcoin network
49+ pub network : Network ,
50+ /// Bitcoin fee rate, sats/vByte
51+ pub fee_rate : u64 ,
52+ }
53+
54+ impl EnvelopeConfig {
55+ pub fn new (
56+ params : Arc < Params > ,
57+ sequencer_address : Address ,
58+ network : Network ,
59+ fee_rate : u64 ,
60+ reveal_amount : u64 ,
61+ ) -> Self {
62+ Self {
63+ params,
64+ sequencer_address,
65+ reveal_amount,
66+ fee_rate,
67+ network,
68+ }
69+ }
70+ }
71+
3672// TODO: these might need to be in rollup params
3773#[ derive( Debug , Error ) ]
3874pub enum EnvelopeError {
@@ -64,24 +100,28 @@ pub(crate) async fn build_envelope_txs<R: Reader + Signer + Wallet>(
64100 FeePolicy :: Smart => ctx. client . estimate_smart_fee ( 1 ) . await ? * 2 ,
65101 FeePolicy :: Fixed ( val) => val,
66102 } ;
67- create_envelope_transactions ( ctx, payloads, utxos, fee_rate, network)
103+ let env_config = EnvelopeConfig :: new (
104+ ctx. params . clone ( ) ,
105+ ctx. sequencer_address . clone ( ) ,
106+ network,
107+ fee_rate,
108+ BITCOIN_DUST_LIMIT ,
109+ ) ;
110+ create_envelope_transactions ( & env_config, payloads, utxos)
68111 . map_err ( |e| anyhow:: anyhow!( e. to_string( ) ) )
69112}
70113
71- #[ allow( clippy:: too_many_arguments) ]
72- pub fn create_envelope_transactions < R : Reader + Signer + Wallet > (
73- ctx : & WriterContext < R > ,
114+ pub fn create_envelope_transactions (
115+ env_config : & EnvelopeConfig ,
74116 payloads : & [ L1Payload ] ,
75117 utxos : Vec < ListUnspent > ,
76- fee_rate : u64 ,
77- network : Network ,
78118) -> Result < ( Transaction , Transaction ) , EnvelopeError > {
79119 // Create commit key
80120 let key_pair = generate_key_pair ( ) ?;
81121 let public_key = XOnlyPublicKey :: from_keypair ( & key_pair) . 0 ;
82122
83123 // Start creating envelope content
84- let reveal_script = build_reveal_script ( ctx . params . as_ref ( ) , & public_key, payloads) ?;
124+ let reveal_script = build_reveal_script ( env_config . params . as_ref ( ) , & public_key, payloads) ?;
85125 // Create spend info for tapscript
86126 let taproot_spend_info = TaprootBuilder :: new ( )
87127 . add_leaf ( 0 , reveal_script. clone ( ) ) ?
@@ -93,14 +133,14 @@ pub fn create_envelope_transactions<R: Reader + Signer + Wallet>(
93133 SECP256K1 ,
94134 public_key,
95135 taproot_spend_info. merkle_root ( ) ,
96- network,
136+ env_config . network ,
97137 ) ;
98138
99139 // Calculate commit value
100140 let commit_value = calculate_commit_output_value (
101- & ctx . sequencer_address ,
102- ctx . config . reveal_amount ,
103- fee_rate,
141+ & env_config . sequencer_address ,
142+ env_config . reveal_amount ,
143+ env_config . fee_rate ,
104144 & reveal_script,
105145 & taproot_spend_info,
106146 ) ;
@@ -109,19 +149,19 @@ pub fn create_envelope_transactions<R: Reader + Signer + Wallet>(
109149 let ( unsigned_commit_tx, _) = build_commit_transaction (
110150 utxos,
111151 reveal_address. clone ( ) ,
112- ctx . sequencer_address . clone ( ) ,
152+ env_config . sequencer_address . clone ( ) ,
113153 commit_value,
114- fee_rate,
154+ env_config . fee_rate ,
115155 ) ?;
116156
117157 let output_to_reveal = unsigned_commit_tx. output [ 0 ] . clone ( ) ;
118158
119159 // Build reveal tx
120160 let mut reveal_tx = build_reveal_transaction (
121161 unsigned_commit_tx. clone ( ) ,
122- ctx . sequencer_address . clone ( ) ,
123- ctx . config . reveal_amount ,
124- fee_rate,
162+ env_config . sequencer_address . clone ( ) ,
163+ env_config . reveal_amount ,
164+ env_config . fee_rate ,
125165 & reveal_script,
126166 & taproot_spend_info
127167 . control_block ( & ( reveal_script. clone ( ) , LeafVersion :: TapScript ) )
@@ -138,7 +178,12 @@ pub fn create_envelope_transactions<R: Reader + Signer + Wallet>(
138178 ) ?;
139179
140180 // Check if envelope is locked to the correct address
141- assert_correct_address ( & key_pair, & taproot_spend_info, & reveal_address, network) ;
181+ assert_correct_address (
182+ & key_pair,
183+ & taproot_spend_info,
184+ & reveal_address,
185+ env_config. network ,
186+ ) ;
142187
143188 Ok ( ( unsigned_commit_tx, reveal_tx) )
144189}
@@ -672,14 +717,16 @@ mod tests {
672717 let ( ctx, _, _, utxos) = get_mock_data ( ) ;
673718
674719 let payload = L1Payload :: new_da ( vec ! [ 0u8 ; 100 ] ) ;
675- let ( commit, reveal) = super :: create_envelope_transactions (
676- & ctx,
677- & [ payload] ,
678- utxos. to_vec ( ) ,
679- 10 ,
680- bitcoin:: Network :: Bitcoin ,
681- )
682- . unwrap ( ) ;
720+
721+ let env_config = EnvelopeConfig :: new (
722+ ctx. params . clone ( ) ,
723+ ctx. sequencer_address . clone ( ) ,
724+ Network :: Regtest ,
725+ 1000 ,
726+ 546 ,
727+ ) ;
728+ let ( commit, reveal) =
729+ super :: create_envelope_transactions ( & env_config, & [ payload] , utxos. to_vec ( ) ) . unwrap ( ) ;
683730
684731 // check outputs
685732 assert_eq ! ( commit. output. len( ) , 2 , "commit tx should have 2 outputs" ) ;
0 commit comments