Skip to content

Commit 8cab6f0

Browse files
committed
Use Sequence and LockTime
1 parent 3130301 commit 8cab6f0

File tree

10 files changed

+104
-154
lines changed

10 files changed

+104
-154
lines changed

src/blockchain/any.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl_from!(boxed rpc::RpcBlockchain, AnyBlockchain, Rpc, #[cfg(feature = "rpc")]
194194
/// );
195195
/// # }
196196
/// ```
197-
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq)]
197+
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)]
198198
#[serde(tag = "type", rename_all = "snake_case")]
199199
pub enum AnyBlockchainConfig {
200200
#[cfg(feature = "electrum")]

src/blockchain/electrum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ impl<'a, 'b, D: Database> TxCache<'a, 'b, D> {
296296
}
297297

298298
/// Configuration for an [`ElectrumBlockchain`]
299-
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, PartialEq)]
299+
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, PartialEq, Eq)]
300300
pub struct ElectrumBlockchainConfig {
301301
/// URL of the Electrum server (such as ElectrumX, Esplora, BWT) may start with `ssl://` or `tcp://` and include a port
302302
///

src/descriptor/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ impl DescriptorMeta for ExtendedDescriptor {
461461
// that come before the wildcard, so we take them directly from `xpub` and then append
462462
// the final index
463463
if verify_key(
464-
&xpub,
464+
xpub,
465465
&xpub.derivation_path.extend(derive_path.clone()),
466466
expected,
467467
) {

src/descriptor/policy.rs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use serde::{Serialize, Serializer};
4545

4646
use bitcoin::hashes::{hash160, ripemd160, sha256};
4747
use bitcoin::util::bip32::Fingerprint;
48-
use bitcoin::{PublicKey, XOnlyPublicKey};
48+
use bitcoin::{LockTime, PublicKey, Sequence, XOnlyPublicKey};
4949

5050
use miniscript::descriptor::{
5151
DescriptorPublicKey, ShInner, SinglePub, SinglePubKey, SortedMultiVec, WshInner,
@@ -61,7 +61,7 @@ use log::{debug, error, info, trace};
6161
use crate::descriptor::ExtractPolicy;
6262
use crate::keys::ExtScriptContext;
6363
use crate::wallet::signer::{SignerId, SignersContainer};
64-
use crate::wallet::utils::{self, After, Older, SecpCtx};
64+
use crate::wallet::utils::{After, Older, SecpCtx};
6565

6666
use super::checksum::get_checksum;
6767
use super::error::Error;
@@ -128,13 +128,13 @@ pub enum SatisfiableItem {
128128
},
129129
/// Absolute timeclock timestamp
130130
AbsoluteTimelock {
131-
/// The timestamp value
132-
value: u32,
131+
/// The timelock value
132+
value: LockTime,
133133
},
134134
/// Relative timelock locktime
135135
RelativeTimelock {
136-
/// The locktime value
137-
value: u32,
136+
/// The timelock value
137+
value: Sequence,
138138
},
139139
/// Multi-signature public keys with threshold count
140140
Multisig {
@@ -442,32 +442,29 @@ pub struct Policy {
442442

443443
/// An extra condition that must be satisfied but that is out of control of the user
444444
/// 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)]
446446
pub struct Condition {
447447
/// Optional CheckSequenceVerify condition
448448
#[serde(skip_serializing_if = "Option::is_none")]
449-
pub csv: Option<u32>,
449+
pub csv: Option<Sequence>,
450450
/// Optional timelock condition
451451
#[serde(skip_serializing_if = "Option::is_none")]
452-
pub timelock: Option<u32>,
452+
pub timelock: Option<LockTime>,
453453
}
454454

455455
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) {
458458
Err(PolicyError::MixedTimelockUnits)
459+
} else if a > b {
460+
Ok(a)
459461
} else {
460-
Ok(max(a, b))
462+
Ok(b)
461463
}
462464
}
463465

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() {
471468
Err(PolicyError::MixedTimelockUnits)
472469
} else {
473470
Ok(max(a, b))
@@ -899,12 +896,12 @@ impl<Ctx: ScriptContext + 'static> ExtractPolicy for Miniscript<DescriptorPublic
899896
}
900897
Terminal::After(value) => {
901898
let mut policy: Policy = SatisfiableItem::AbsoluteTimelock {
902-
value: value.to_u32(),
899+
value: value.into(),
903900
}
904901
.into();
905902
policy.contribution = Satisfaction::Complete {
906903
condition: Condition {
907-
timelock: Some(value.to_u32()),
904+
timelock: Some(value.into()),
908905
csv: None,
909906
},
910907
};
@@ -928,14 +925,11 @@ impl<Ctx: ScriptContext + 'static> ExtractPolicy for Miniscript<DescriptorPublic
928925
Some(policy)
929926
}
930927
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();
935929
policy.contribution = Satisfaction::Complete {
936930
condition: Condition {
937931
timelock: None,
938-
csv: Some(value.to_consensus_u32()),
932+
csv: Some(*value),
939933
},
940934
};
941935
if let BuildSatisfaction::PsbtTimelocks {
@@ -1440,8 +1434,8 @@ mod test {
14401434
&& m == &2
14411435
&& items.len() == 3
14421436
&& 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))
14451439
)
14461440
);
14471441
}

src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ pub enum Error {
152152
/// Errors returned by miniscript when updating inconsistent PSBTs
153153
#[derive(Debug, Clone)]
154154
pub enum MiniscriptPsbtError {
155-
ConversionError(miniscript::descriptor::ConversionError),
156-
UtxoUpdateError(miniscript::psbt::UtxoUpdateError),
157-
OutputUpdateError(miniscript::psbt::OutputUpdateError),
155+
Conversion(miniscript::descriptor::ConversionError),
156+
UtxoUpdate(miniscript::psbt::UtxoUpdateError),
157+
OutputUpdate(miniscript::psbt::OutputUpdateError),
158158
}
159159

160160
/// Represents the last failed [`crate::blockchain::WalletSync`] sync attempt in which we were short

src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ pub struct LocalUtxo {
166166
}
167167

168168
/// A [`Utxo`] with its `satisfaction_weight`.
169-
#[derive(Debug, Clone, PartialEq)]
169+
#[derive(Debug, Clone, PartialEq, Eq)]
170170
pub struct WeightedUtxo {
171171
/// The weight of the witness data and `scriptSig` expressed in [weight units]. This is used to
172172
/// properly maintain the feerate when adding this input to a transaction during coin selection.
@@ -177,7 +177,7 @@ pub struct WeightedUtxo {
177177
pub utxo: Utxo,
178178
}
179179

180-
#[derive(Debug, Clone, PartialEq)]
180+
#[derive(Debug, Clone, PartialEq, Eq)]
181181
/// An unspent transaction output (UTXO).
182182
pub enum Utxo {
183183
/// A UTXO owned by the local wallet.

0 commit comments

Comments
 (0)