@@ -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 ,
@@ -61,7 +61,7 @@ use log::{debug, error, info, trace};
61
61
use crate :: descriptor:: ExtractPolicy ;
62
62
use crate :: keys:: ExtScriptContext ;
63
63
use crate :: wallet:: signer:: { SignerId , SignersContainer } ;
64
- use crate :: wallet:: utils:: { self , After , Older , SecpCtx } ;
64
+ use crate :: wallet:: utils:: { After , Older , SecpCtx } ;
65
65
66
66
use super :: checksum:: get_checksum;
67
67
use super :: error:: Error ;
@@ -128,13 +128,13 @@ pub enum SatisfiableItem {
128
128
} ,
129
129
/// Absolute timeclock timestamp
130
130
AbsoluteTimelock {
131
- /// The timestamp value
132
- value : u32 ,
131
+ /// The timelock value
132
+ value : LockTime ,
133
133
} ,
134
134
/// Relative timelock locktime
135
135
RelativeTimelock {
136
- /// The locktime value
137
- value : u32 ,
136
+ /// The timelock value
137
+ value : Sequence ,
138
138
} ,
139
139
/// Multi-signature public keys with threshold count
140
140
Multisig {
@@ -442,32 +442,29 @@ pub struct Policy {
442
442
443
443
/// An extra condition that must be satisfied but that is out of control of the user
444
444
/// TODO: use `bitcoin::LockTime` and `bitcoin::Sequence`
445
- #[ derive( Hash , Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Ord , Default , Serialize ) ]
445
+ #[ derive( Hash , Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Default , Serialize ) ]
446
446
pub struct Condition {
447
447
/// Optional CheckSequenceVerify condition
448
448
#[ serde( skip_serializing_if = "Option::is_none" ) ]
449
- pub csv : Option < u32 > ,
449
+ pub csv : Option < Sequence > ,
450
450
/// Optional timelock condition
451
451
#[ serde( skip_serializing_if = "Option::is_none" ) ]
452
- pub timelock : Option < u32 > ,
452
+ pub timelock : Option < LockTime > ,
453
453
}
454
454
455
455
impl Condition {
456
- fn merge_nlocktime ( a : u32 , b : u32 ) -> Result < u32 , PolicyError > {
457
- if ( a < utils :: BLOCKS_TIMELOCK_THRESHOLD ) != ( b < utils :: BLOCKS_TIMELOCK_THRESHOLD ) {
456
+ fn merge_nlocktime ( a : LockTime , b : LockTime ) -> Result < LockTime , PolicyError > {
457
+ if !a . is_same_unit ( b ) {
458
458
Err ( PolicyError :: MixedTimelockUnits )
459
+ } else if a > b {
460
+ Ok ( a)
459
461
} else {
460
- Ok ( max ( a , b ) )
462
+ Ok ( b )
461
463
}
462
464
}
463
465
464
- fn merge_nsequence ( a : u32 , b : u32 ) -> Result < u32 , PolicyError > {
465
- let mask = utils:: SEQUENCE_LOCKTIME_TYPE_FLAG | utils:: SEQUENCE_LOCKTIME_MASK ;
466
-
467
- let a = a & mask;
468
- let b = b & mask;
469
-
470
- if ( a < utils:: SEQUENCE_LOCKTIME_TYPE_FLAG ) != ( b < utils:: SEQUENCE_LOCKTIME_TYPE_FLAG ) {
466
+ fn merge_nsequence ( a : Sequence , b : Sequence ) -> Result < Sequence , PolicyError > {
467
+ if a. is_time_locked ( ) != b. is_time_locked ( ) {
471
468
Err ( PolicyError :: MixedTimelockUnits )
472
469
} else {
473
470
Ok ( max ( a, b) )
@@ -899,12 +896,12 @@ impl<Ctx: ScriptContext + 'static> ExtractPolicy for Miniscript<DescriptorPublic
899
896
}
900
897
Terminal :: After ( value) => {
901
898
let mut policy: Policy = SatisfiableItem :: AbsoluteTimelock {
902
- value : value. to_u32 ( ) ,
899
+ value : value. into ( ) ,
903
900
}
904
901
. into ( ) ;
905
902
policy. contribution = Satisfaction :: Complete {
906
903
condition : Condition {
907
- timelock : Some ( value. to_u32 ( ) ) ,
904
+ timelock : Some ( value. into ( ) ) ,
908
905
csv : None ,
909
906
} ,
910
907
} ;
@@ -928,14 +925,11 @@ impl<Ctx: ScriptContext + 'static> ExtractPolicy for Miniscript<DescriptorPublic
928
925
Some ( policy)
929
926
}
930
927
Terminal :: Older ( value) => {
931
- let mut policy: Policy = SatisfiableItem :: RelativeTimelock {
932
- value : value. to_consensus_u32 ( ) ,
933
- }
934
- . into ( ) ;
928
+ let mut policy: Policy = SatisfiableItem :: RelativeTimelock { value : * value } . into ( ) ;
935
929
policy. contribution = Satisfaction :: Complete {
936
930
condition : Condition {
937
931
timelock : None ,
938
- csv : Some ( value. to_consensus_u32 ( ) ) ,
932
+ csv : Some ( * value) ,
939
933
} ,
940
934
} ;
941
935
if let BuildSatisfaction :: PsbtTimelocks {
@@ -1440,8 +1434,8 @@ mod test {
1440
1434
&& m == & 2
1441
1435
&& items. len( ) == 3
1442
1436
&& conditions. get( & vec![ 0 , 1 ] ) . unwrap( ) . iter( ) . next( ) . unwrap( ) . csv. is_none( )
1443
- && conditions. get( & vec![ 0 , 2 ] ) . unwrap( ) . iter( ) . next( ) . unwrap( ) . csv == Some ( sequence)
1444
- && conditions. get( & vec![ 1 , 2 ] ) . unwrap( ) . iter( ) . next( ) . unwrap( ) . csv == Some ( sequence)
1437
+ && conditions. get( & vec![ 0 , 2 ] ) . unwrap( ) . iter( ) . next( ) . unwrap( ) . csv == Some ( Sequence ( sequence) )
1438
+ && conditions. get( & vec![ 1 , 2 ] ) . unwrap( ) . iter( ) . next( ) . unwrap( ) . csv == Some ( Sequence ( sequence) )
1445
1439
)
1446
1440
) ;
1447
1441
}
0 commit comments