Skip to content

Commit 4116dfa

Browse files
committed
Use Sequence and LockTime
1 parent a246755 commit 4116dfa

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
@@ -512,7 +512,7 @@ impl DescriptorMeta for ExtendedDescriptor {
512512
// that come before the wildcard, so we take them directly from `xpub` and then append
513513
// the final index
514514
if verify_key(
515-
&xpub,
515+
xpub,
516516
&xpub.derivation_path.extend(derive_path.clone()),
517517
expected,
518518
) {

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,
@@ -59,7 +59,7 @@ use log::{debug, error, info, trace};
5959
use crate::descriptor::ExtractPolicy;
6060
use crate::keys::ExtScriptContext;
6161
use crate::wallet::signer::{SignerId, SignersContainer};
62-
use crate::wallet::utils::{self, After, Older, SecpCtx};
62+
use crate::wallet::utils::{After, Older, SecpCtx};
6363

6464
use super::checksum::get_checksum;
6565
use super::error::Error;
@@ -126,13 +126,13 @@ pub enum SatisfiableItem {
126126
},
127127
/// Absolute timeclock timestamp
128128
AbsoluteTimelock {
129-
/// The timestamp value
130-
value: u32,
129+
/// The timelock value
130+
value: LockTime,
131131
},
132132
/// Relative timelock locktime
133133
RelativeTimelock {
134-
/// The locktime value
135-
value: u32,
134+
/// The timelock value
135+
value: Sequence,
136136
},
137137
/// Multi-signature public keys with threshold count
138138
Multisig {
@@ -440,32 +440,29 @@ pub struct Policy {
440440

441441
/// An extra condition that must be satisfied but that is out of control of the user
442442
/// 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)]
444444
pub struct Condition {
445445
/// Optional CheckSequenceVerify condition
446446
#[serde(skip_serializing_if = "Option::is_none")]
447-
pub csv: Option<u32>,
447+
pub csv: Option<Sequence>,
448448
/// Optional timelock condition
449449
#[serde(skip_serializing_if = "Option::is_none")]
450-
pub timelock: Option<u32>,
450+
pub timelock: Option<LockTime>,
451451
}
452452

453453
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) {
456456
Err(PolicyError::MixedTimelockUnits)
457+
} else if a > b {
458+
Ok(a)
457459
} else {
458-
Ok(max(a, b))
460+
Ok(b)
459461
}
460462
}
461463

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() {
469466
Err(PolicyError::MixedTimelockUnits)
470467
} else {
471468
Ok(max(a, b))
@@ -894,12 +891,12 @@ impl<Ctx: ScriptContext + 'static> ExtractPolicy for Miniscript<DescriptorPublic
894891
}
895892
Terminal::After(value) => {
896893
let mut policy: Policy = SatisfiableItem::AbsoluteTimelock {
897-
value: value.to_u32(),
894+
value: value.into(),
898895
}
899896
.into();
900897
policy.contribution = Satisfaction::Complete {
901898
condition: Condition {
902-
timelock: Some(value.to_u32()),
899+
timelock: Some(value.into()),
903900
csv: None,
904901
},
905902
};
@@ -923,14 +920,11 @@ impl<Ctx: ScriptContext + 'static> ExtractPolicy for Miniscript<DescriptorPublic
923920
Some(policy)
924921
}
925922
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();
930924
policy.contribution = Satisfaction::Complete {
931925
condition: Condition {
932926
timelock: None,
933-
csv: Some(value.to_consensus_u32()),
927+
csv: Some(*value),
934928
},
935929
};
936930
if let BuildSatisfaction::PsbtTimelocks {
@@ -1432,8 +1426,8 @@ mod test {
14321426
&& m == &2
14331427
&& items.len() == 3
14341428
&& 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))
14371431
)
14381432
);
14391433
}

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)