@@ -45,7 +45,7 @@ use serde::{Serialize, Serializer};
45
45
46
46
use bitcoin:: hashes:: { hash160, ripemd160, sha256} ;
47
47
use bitcoin:: util:: bip32:: Fingerprint ;
48
- use bitcoin:: { PublicKey , XOnlyPublicKey } ;
48
+ use bitcoin:: { LockTime , PublicKey , Sequence , XOnlyPublicKey } ;
49
49
50
50
use miniscript:: descriptor:: {
51
51
DescriptorPublicKey , ShInner , SinglePub , SinglePubKey , SortedMultiVec , WshInner ,
@@ -59,7 +59,7 @@ use log::{debug, error, info, trace};
59
59
use crate :: descriptor:: ExtractPolicy ;
60
60
use crate :: keys:: ExtScriptContext ;
61
61
use crate :: wallet:: signer:: { SignerId , SignersContainer } ;
62
- use crate :: wallet:: utils:: { self , After , Older , SecpCtx } ;
62
+ use crate :: wallet:: utils:: { After , Older , SecpCtx } ;
63
63
64
64
use super :: checksum:: get_checksum;
65
65
use super :: error:: Error ;
@@ -126,13 +126,13 @@ pub enum SatisfiableItem {
126
126
} ,
127
127
/// Absolute timeclock timestamp
128
128
AbsoluteTimelock {
129
- /// The timestamp value
130
- value : u32 ,
129
+ /// The timelock value
130
+ value : LockTime ,
131
131
} ,
132
132
/// Relative timelock locktime
133
133
RelativeTimelock {
134
- /// The locktime value
135
- value : u32 ,
134
+ /// The timelock value
135
+ value : Sequence ,
136
136
} ,
137
137
/// Multi-signature public keys with threshold count
138
138
Multisig {
@@ -440,32 +440,29 @@ pub struct Policy {
440
440
441
441
/// An extra condition that must be satisfied but that is out of control of the user
442
442
/// TODO: use `bitcoin::LockTime` and `bitcoin::Sequence`
443
- #[ derive( Hash , Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Ord , Default , Serialize ) ]
443
+ #[ derive( Hash , Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Default , Serialize ) ]
444
444
pub struct Condition {
445
445
/// Optional CheckSequenceVerify condition
446
446
#[ serde( skip_serializing_if = "Option::is_none" ) ]
447
- pub csv : Option < u32 > ,
447
+ pub csv : Option < Sequence > ,
448
448
/// Optional timelock condition
449
449
#[ serde( skip_serializing_if = "Option::is_none" ) ]
450
- pub timelock : Option < u32 > ,
450
+ pub timelock : Option < LockTime > ,
451
451
}
452
452
453
453
impl Condition {
454
- fn merge_nlocktime ( a : u32 , b : u32 ) -> Result < u32 , PolicyError > {
455
- if ( a < utils :: BLOCKS_TIMELOCK_THRESHOLD ) != ( b < utils :: BLOCKS_TIMELOCK_THRESHOLD ) {
454
+ fn merge_nlocktime ( a : LockTime , b : LockTime ) -> Result < LockTime , PolicyError > {
455
+ if !a . is_same_unit ( b ) {
456
456
Err ( PolicyError :: MixedTimelockUnits )
457
+ } else if a > b {
458
+ Ok ( a)
457
459
} else {
458
- Ok ( max ( a , b ) )
460
+ Ok ( b )
459
461
}
460
462
}
461
463
462
- fn merge_nsequence ( a : u32 , b : u32 ) -> Result < u32 , PolicyError > {
463
- let mask = utils:: SEQUENCE_LOCKTIME_TYPE_FLAG | utils:: SEQUENCE_LOCKTIME_MASK ;
464
-
465
- let a = a & mask;
466
- let b = b & mask;
467
-
468
- if ( a < utils:: SEQUENCE_LOCKTIME_TYPE_FLAG ) != ( b < utils:: SEQUENCE_LOCKTIME_TYPE_FLAG ) {
464
+ fn merge_nsequence ( a : Sequence , b : Sequence ) -> Result < Sequence , PolicyError > {
465
+ if a. is_time_locked ( ) != b. is_time_locked ( ) {
469
466
Err ( PolicyError :: MixedTimelockUnits )
470
467
} else {
471
468
Ok ( max ( a, b) )
@@ -894,12 +891,12 @@ impl<Ctx: ScriptContext + 'static> ExtractPolicy for Miniscript<DescriptorPublic
894
891
}
895
892
Terminal :: After ( value) => {
896
893
let mut policy: Policy = SatisfiableItem :: AbsoluteTimelock {
897
- value : value. to_u32 ( ) ,
894
+ value : value. into ( ) ,
898
895
}
899
896
. into ( ) ;
900
897
policy. contribution = Satisfaction :: Complete {
901
898
condition : Condition {
902
- timelock : Some ( value. to_u32 ( ) ) ,
899
+ timelock : Some ( value. into ( ) ) ,
903
900
csv : None ,
904
901
} ,
905
902
} ;
@@ -923,14 +920,11 @@ impl<Ctx: ScriptContext + 'static> ExtractPolicy for Miniscript<DescriptorPublic
923
920
Some ( policy)
924
921
}
925
922
Terminal :: Older ( value) => {
926
- let mut policy: Policy = SatisfiableItem :: RelativeTimelock {
927
- value : value. to_consensus_u32 ( ) ,
928
- }
929
- . into ( ) ;
923
+ let mut policy: Policy = SatisfiableItem :: RelativeTimelock { value : * value } . into ( ) ;
930
924
policy. contribution = Satisfaction :: Complete {
931
925
condition : Condition {
932
926
timelock : None ,
933
- csv : Some ( value. to_consensus_u32 ( ) ) ,
927
+ csv : Some ( * value) ,
934
928
} ,
935
929
} ;
936
930
if let BuildSatisfaction :: PsbtTimelocks {
@@ -1432,8 +1426,8 @@ mod test {
1432
1426
&& m == & 2
1433
1427
&& items. len( ) == 3
1434
1428
&& conditions. get( & vec![ 0 , 1 ] ) . unwrap( ) . iter( ) . next( ) . unwrap( ) . csv. is_none( )
1435
- && conditions. get( & vec![ 0 , 2 ] ) . unwrap( ) . iter( ) . next( ) . unwrap( ) . csv == Some ( sequence)
1436
- && conditions. get( & vec![ 1 , 2 ] ) . unwrap( ) . iter( ) . next( ) . unwrap( ) . csv == Some ( sequence)
1429
+ && conditions. get( & vec![ 0 , 2 ] ) . unwrap( ) . iter( ) . next( ) . unwrap( ) . csv == Some ( Sequence ( sequence) )
1430
+ && conditions. get( & vec![ 1 , 2 ] ) . unwrap( ) . iter( ) . next( ) . unwrap( ) . csv == Some ( Sequence ( sequence) )
1437
1431
)
1438
1432
) ;
1439
1433
}
0 commit comments