11use bdk_coin_select:: {
22 ChangePolicy , DrainWeights , InsufficientFunds , Replace , Target , TargetFee , TargetOutputs ,
33} ;
4- use bitcoin:: { Amount , FeeRate , Transaction , TxOut , Weight } ;
4+ use bitcoin:: { Amount , FeeRate , Transaction , Weight } ;
55use miniscript:: bitcoin;
66
7- use crate :: { cs_feerate, DefiniteDescriptor , InputCandidates , InputGroup , Output , Selection } ;
7+ use crate :: { cs_feerate, InputCandidates , InputGroup , Output , ScriptSource , Selection } ;
88use alloc:: vec:: Vec ;
99use core:: fmt;
1010
@@ -15,7 +15,7 @@ pub struct Selector<'c> {
1515 target_outputs : Vec < Output > ,
1616 target : Target ,
1717 change_policy : bdk_coin_select:: ChangePolicy ,
18- change_descriptor : DefiniteDescriptor ,
18+ change_script : ScriptSource ,
1919 inner : bdk_coin_select:: CoinSelector < ' c > ,
2020}
2121
@@ -42,11 +42,14 @@ pub struct SelectorParams {
4242 /// To derive change output.
4343 ///
4444 /// Will error if this is unsatisfiable descriptor.
45- pub change_descriptor : DefiniteDescriptor ,
45+ pub change_script : ScriptSource ,
4646
4747 /// The policy to determine whether we create a change output.
4848 pub change_policy : ChangePolicyType ,
4949
50+ /// Weight of the change output plus the future weight to spend the change
51+ pub change_weight : DrainWeights ,
52+
5053 /// Params for replacing tx(s).
5154 pub replace : Option < RbfParams > ,
5255}
@@ -140,14 +143,16 @@ impl SelectorParams {
140143 pub fn new (
141144 target_feerate : bitcoin:: FeeRate ,
142145 target_outputs : Vec < Output > ,
143- change_descriptor : DefiniteDescriptor ,
146+ change_script : ScriptSource ,
144147 change_policy : ChangePolicyType ,
148+ change_weight : DrainWeights ,
145149 ) -> Self {
146150 Self {
147- change_descriptor,
148- change_policy,
149151 target_feerate,
150152 target_outputs,
153+ change_script,
154+ change_policy,
155+ change_weight,
151156 replace : None ,
152157 }
153158 }
@@ -171,36 +176,14 @@ impl SelectorParams {
171176 }
172177 }
173178
174- /// To change output weights.
175- ///
176- /// # Error
177- ///
178- /// Fails if `change_descriptor` cannot be satisfied.
179- pub fn to_cs_change_weights ( & self ) -> Result < bdk_coin_select:: DrainWeights , miniscript:: Error > {
180- Ok ( DrainWeights {
181- output_weight : ( TxOut {
182- script_pubkey : self . change_descriptor . script_pubkey ( ) ,
183- value : Amount :: ZERO ,
184- } )
185- . weight ( )
186- . to_wu ( ) ,
187- spend_weight : self . change_descriptor . max_weight_to_satisfy ( ) ?. to_wu ( ) ,
188- n_outputs : 1 ,
189- } )
190- }
191-
192179 /// To change policy.
193180 ///
194181 /// # Error
195182 ///
196183 /// Fails if `change_descriptor` cannot be satisfied.
197184 pub fn to_cs_change_policy ( & self ) -> Result < bdk_coin_select:: ChangePolicy , miniscript:: Error > {
198- let change_weights = self . to_cs_change_weights ( ) ?;
199- let dust_value = self
200- . change_descriptor
201- . script_pubkey ( )
202- . minimal_non_dust ( )
203- . to_sat ( ) ;
185+ let change_weights = self . change_weight ;
186+ let dust_value = self . change_script . script ( ) . minimal_non_dust ( ) . to_sat ( ) ;
204187 Ok ( match self . change_policy {
205188 ChangePolicyType :: NoDust => ChangePolicy :: min_value ( change_weights, dust_value) ,
206189 ChangePolicyType :: NoDustAndLeastWaste { longterm_feerate } => {
@@ -268,7 +251,7 @@ impl<'c> Selector<'c> {
268251 . to_cs_change_policy ( )
269252 . map_err ( SelectorError :: Miniscript ) ?;
270253 let target_outputs = params. target_outputs ;
271- let change_descriptor = params. change_descriptor ;
254+ let change_script = params. change_script ;
272255 if target. value ( ) > candidates. groups ( ) . map ( |grp| grp. value ( ) . to_sat ( ) ) . sum ( ) {
273256 return Err ( SelectorError :: CannotMeetTarget ( CannotMeetTarget ) ) ;
274257 }
@@ -281,7 +264,7 @@ impl<'c> Selector<'c> {
281264 target,
282265 target_outputs,
283266 change_policy,
284- change_descriptor ,
267+ change_script ,
285268 inner,
286269 } )
287270 }
@@ -358,10 +341,10 @@ impl<'c> Selector<'c> {
358341 outputs : {
359342 let mut outputs = self . target_outputs . clone ( ) ;
360343 if maybe_change. is_some ( ) {
361- outputs. push ( Output :: with_descriptor (
362- self . change_descriptor . clone ( ) ,
344+ outputs. push ( Output :: from ( (
345+ self . change_script . clone ( ) ,
363346 Amount :: from_sat ( maybe_change. value ) ,
364- ) ) ;
347+ ) ) ) ;
365348 }
366349 outputs
367350 } ,
0 commit comments