Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get ordered list of recovery paths #557

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,10 @@ impl DaemonControl {
// Query the coins that we can spend through the specified recovery path (if no recovery
// path specified, use the first available one) from the database.
let current_height = self.bitcoin.chain_tip().height;
let timelock =
timelock.unwrap_or_else(|| self.config.main_descriptor.first_timelock_value());
let timelock = timelock.unwrap_or_else(|| {
let timelocks = self.config.main_descriptor.timelock_values();
timelocks[0] // Access the first timelock value
});
let height_delta: i32 = timelock.try_into().expect("Must fit, it's a u16");
let sweepable_coins = db_conn
.coins(CoinType::Unspent)
Expand Down
24 changes: 12 additions & 12 deletions src/descriptors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,15 @@ impl LianaDescriptor {
.expect("We never create a Liana descriptor with an invalid Liana policy.")
}

/// Get the value (in blocks) of the smallest relative timelock of the recovery paths.
pub fn first_timelock_value(&self) -> u16 {
*self
.policy()
.recovery_paths
.iter()
.next()
.expect("There is always at least one recovery path")
.0
/// Get an ordered list of timelocks of the recovery paths.
pub fn timelock_values(&self) -> Vec<u16> {
let timelocks: Vec<u16> = self.policy().recovery_paths.keys().copied().collect();
// Assert that the resulting vector is not empty
assert!(
!timelocks.is_empty(),
"The timelock_values vector should never be empty"
);
timelocks
}

/// Get the maximum size in WU of a satisfaction for this descriptor.
Expand Down Expand Up @@ -593,13 +593,13 @@ mod tests {
LianaDescriptor::from_str("wsh(or_i(pk([abcdef01]tpubDEN9WSToTyy9ZQfaYqSKfmVqmq1VVLNtYfj3Vkqh67et57eJ5sTKZQBkHqSwPUsoSskJeaYnPttHe2VrkCsKA27kUaN9SDc5zhqeLzKa1rr/<0;1>/*),pk([abcdef01]tpubD8LYfn6njiA2inCoxwM7EuN3cuLVcaHAwLYeups13dpevd3nHLRdK9NdQksWXrhLQVxcUZRpnp5CkJ1FhE61WRAsHxDNAkvGkoQkAeWDYjV/<0;1>/*)))").unwrap_err();

let desc = LianaDescriptor::from_str("wsh(andor(pk([abcdef01]tpubDEN9WSToTyy9ZQfaYqSKfmVqmq1VVLNtYfj3Vkqh67et57eJ5sTKZQBkHqSwPUsoSskJeaYnPttHe2VrkCsKA27kUaN9SDc5zhqeLzKa1rr/<0;1>/*),older(1),pk([abcdef01]tpubD8LYfn6njiA2inCoxwM7EuN3cuLVcaHAwLYeups13dpevd3nHLRdK9NdQksWXrhLQVxcUZRpnp5CkJ1FhE61WRAsHxDNAkvGkoQkAeWDYjV/<0;1>/*)))").unwrap();
assert_eq!(desc.first_timelock_value(), 1);
assert_eq!(desc.timelock_values(), vec![1]);

let desc = LianaDescriptor::from_str("wsh(andor(pk([abcdef01]tpubDEN9WSToTyy9ZQfaYqSKfmVqmq1VVLNtYfj3Vkqh67et57eJ5sTKZQBkHqSwPUsoSskJeaYnPttHe2VrkCsKA27kUaN9SDc5zhqeLzKa1rr/<0;1>/*),older(42000),pk([abcdef01]tpubD8LYfn6njiA2inCoxwM7EuN3cuLVcaHAwLYeups13dpevd3nHLRdK9NdQksWXrhLQVxcUZRpnp5CkJ1FhE61WRAsHxDNAkvGkoQkAeWDYjV/<0;1>/*)))").unwrap();
assert_eq!(desc.first_timelock_value(), 42000);
assert_eq!(desc.timelock_values(), vec![42000]);

let desc = LianaDescriptor::from_str("wsh(andor(pk([abcdef01]tpubDEN9WSToTyy9ZQfaYqSKfmVqmq1VVLNtYfj3Vkqh67et57eJ5sTKZQBkHqSwPUsoSskJeaYnPttHe2VrkCsKA27kUaN9SDc5zhqeLzKa1rr/<0;1>/*),older(65535),pk([abcdef01]tpubD8LYfn6njiA2inCoxwM7EuN3cuLVcaHAwLYeups13dpevd3nHLRdK9NdQksWXrhLQVxcUZRpnp5CkJ1FhE61WRAsHxDNAkvGkoQkAeWDYjV/<0;1>/*)))").unwrap();
assert_eq!(desc.first_timelock_value(), 0xffff);
assert_eq!(desc.timelock_values(), vec![0xffff]);
}

#[test]
Expand Down