Skip to content

Commit ca3d7c1

Browse files
committed
descriptors: introduce a newtype for the multipath descriptor
The multipath descriptor has very different properties than the receive and change ones. Use a newtype to assist us in differentiating those.
1 parent 1320ee3 commit ca3d7c1

File tree

11 files changed

+131
-113
lines changed

11 files changed

+131
-113
lines changed

src/bitcoin/d/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
///! Implementation of the Bitcoin interface using bitcoind.
22
///!
33
///! We use the RPC interface and a watchonly descriptor wallet.
4-
use crate::{bitcoin::BlockChainTip, config, descriptors::InheritanceDescriptor};
4+
use crate::{bitcoin::BlockChainTip, config, descriptors::MultipathDescriptor};
55

66
use std::{collections::HashSet, convert::TryInto, fs, io, str::FromStr, time::Duration};
77

@@ -352,7 +352,7 @@ impl BitcoinD {
352352
}
353353

354354
// Import the receive and change descriptors from the multipath descriptor to bitcoind.
355-
fn import_descriptor(&self, desc: &InheritanceDescriptor) -> Option<String> {
355+
fn import_descriptor(&self, desc: &MultipathDescriptor) -> Option<String> {
356356
let descriptors = [desc.receive_descriptor(), desc.change_descriptor()]
357357
.iter()
358358
.map(|desc| {
@@ -401,7 +401,7 @@ impl BitcoinD {
401401
/// Create the watchonly wallet on bitcoind, and import it the main descriptor.
402402
pub fn create_watchonly_wallet(
403403
&self,
404-
main_descriptor: &InheritanceDescriptor,
404+
main_descriptor: &MultipathDescriptor,
405405
) -> Result<(), BitcoindError> {
406406
// Remove any leftover. This can happen if we delete the watchonly wallet but don't restart
407407
// bitcoind.
@@ -441,7 +441,7 @@ impl BitcoinD {
441441
/// Perform various sanity checks on the bitcoind instance.
442442
pub fn sanity_check(
443443
&self,
444-
main_descriptor: &InheritanceDescriptor,
444+
main_descriptor: &MultipathDescriptor,
445445
config_network: bitcoin::Network,
446446
) -> Result<(), BitcoindError> {
447447
// Check the minimum supported bitcoind version

src/bitcoin/poller/looper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ pub fn looper(
219219
db: sync::Arc<sync::Mutex<dyn DatabaseInterface>>,
220220
shutdown: sync::Arc<atomic::AtomicBool>,
221221
poll_interval: time::Duration,
222-
desc: descriptors::InheritanceDescriptor,
222+
desc: descriptors::MultipathDescriptor,
223223
) {
224224
let mut last_poll = None;
225225
let mut synced = false;

src/bitcoin/poller/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl Poller {
2222
bit: sync::Arc<sync::Mutex<dyn BitcoinInterface>>,
2323
db: sync::Arc<sync::Mutex<dyn DatabaseInterface>>,
2424
poll_interval: time::Duration,
25-
desc: descriptors::InheritanceDescriptor,
25+
desc: descriptors::MultipathDescriptor,
2626
) -> Poller {
2727
let shutdown = sync::Arc::from(atomic::AtomicBool::from(false));
2828
let handle = thread::Builder::new()

src/commands/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ impl DaemonControl {
174174
fn derived_desc(&self, index: bip32::ChildNumber) -> descriptors::DerivedInheritanceDescriptor {
175175
self.config
176176
.main_descriptor
177-
.derive_receive(index, &self.secp)
177+
.receive_descriptor()
178+
.derive(index, &self.secp)
178179
}
179180
}
180181

@@ -487,7 +488,7 @@ impl DaemonControl {
487488

488489
#[derive(Debug, Clone, Serialize, Deserialize)]
489490
pub struct GetInfoDescriptors {
490-
pub main: descriptors::InheritanceDescriptor,
491+
pub main: descriptors::MultipathDescriptor,
491492
}
492493

493494
/// Information about the daemon

src/config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::descriptors::InheritanceDescriptor;
1+
use crate::descriptors::MultipathDescriptor;
22

33
use std::{net::SocketAddr, path::PathBuf, str::FromStr, time::Duration};
44

@@ -92,7 +92,7 @@ pub struct Config {
9292
deserialize_with = "deserialize_fromstr",
9393
serialize_with = "serialize_to_string"
9494
)]
95-
pub main_descriptor: InheritanceDescriptor,
95+
pub main_descriptor: MultipathDescriptor,
9696
/// Settings for the Bitcoin interface
9797
pub bitcoin_config: BitcoinConfig,
9898
/// Settings specific to bitcoind as the Bitcoin interface
@@ -113,7 +113,7 @@ pub enum ConfigError {
113113
DatadirNotFound,
114114
FileNotFound,
115115
ReadingFile(String),
116-
UnexpectedDescriptor(Box<InheritanceDescriptor>),
116+
UnexpectedDescriptor(Box<MultipathDescriptor>),
117117
Unexpected(String),
118118
}
119119

src/database/sqlite/mod.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818
},
1919
Coin,
2020
},
21-
descriptors::InheritanceDescriptor,
21+
descriptors::MultipathDescriptor,
2222
};
2323

2424
use std::{convert::TryInto, fmt, io, path};
@@ -36,7 +36,7 @@ pub enum SqliteDbError {
3636
FileNotFound(path::PathBuf),
3737
UnsupportedVersion(i64),
3838
InvalidNetwork(bitcoin::Network),
39-
DescriptorMismatch(Box<InheritanceDescriptor>),
39+
DescriptorMismatch(Box<MultipathDescriptor>),
4040
Rusqlite(rusqlite::Error),
4141
}
4242

@@ -80,7 +80,7 @@ impl From<rusqlite::Error> for SqliteDbError {
8080
#[derive(Debug, Clone)]
8181
pub struct FreshDbOptions {
8282
pub bitcoind_network: bitcoin::Network,
83-
pub main_descriptor: InheritanceDescriptor,
83+
pub main_descriptor: MultipathDescriptor,
8484
}
8585

8686
#[derive(Debug, Clone)]
@@ -119,7 +119,7 @@ impl SqliteDb {
119119
pub fn sanity_check(
120120
&self,
121121
bitcoind_network: bitcoin::Network,
122-
main_descriptor: &InheritanceDescriptor,
122+
main_descriptor: &MultipathDescriptor,
123123
) -> Result<(), SqliteDbError> {
124124
let mut conn = self.connection()?;
125125

@@ -240,7 +240,8 @@ impl SqliteConn {
240240
let next_la_index = next_index + LOOK_AHEAD_LIMIT - 1;
241241
let next_la_address = db_wallet
242242
.main_descriptor
243-
.derive_receive(next_la_index.into(), secp)
243+
.receive_descriptor()
244+
.derive(next_la_index.into(), secp)
244245
.address(network);
245246
db_tx
246247
.execute(
@@ -485,7 +486,7 @@ mod tests {
485486

486487
fn dummy_options() -> FreshDbOptions {
487488
let desc_str = "wsh(andor(pk(tpubDEN9WSToTyy9ZQfaYqSKfmVqmq1VVLNtYfj3Vkqh67et57eJ5sTKZQBkHqSwPUsoSskJeaYnPttHe2VrkCsKA27kUaN9SDc5zhqeLzKa1rr/<0;1>/*),older(10000),pk(tpubD8LYfn6njiA2inCoxwM7EuN3cuLVcaHAwLYeups13dpevd3nHLRdK9NdQksWXrhLQVxcUZRpnp5CkJ1FhE61WRAsHxDNAkvGkoQkAeWDYjV/<0;1>/*)))#5f6qd0d9";
488-
let main_descriptor = InheritanceDescriptor::from_str(desc_str).unwrap();
489+
let main_descriptor = MultipathDescriptor::from_str(desc_str).unwrap();
489490
FreshDbOptions {
490491
bitcoind_network: bitcoin::Network::Bitcoin,
491492
main_descriptor,
@@ -534,7 +535,7 @@ mod tests {
534535
.contains("Database was created for network");
535536
fs::remove_file(&db_path).unwrap();
536537
let other_desc_str = "wsh(andor(pk(tpubDExU4YLJkyQ9RRbVScQq2brFxWWha7WmAUByPWyaWYwmcTv3Shx8aHp6mVwuE5n4TeM4z5DTWGf2YhNPmXtfvyr8cUDVvA3txdrFnFgNdF7/<0;1>/*),older(10000),pk(tpubD8LYfn6njiA2inCoxwM7EuN3cuLVcaHAwLYeups13dpevd3nHLRdK9NdQksWXrhLQVxcUZRpnp5CkJ1FhE61WRAsHxDNAkvGkoQkAeWDYjV/<0;1>/*)))";
537-
let other_desc = InheritanceDescriptor::from_str(other_desc_str).unwrap();
538+
let other_desc = MultipathDescriptor::from_str(other_desc_str).unwrap();
538539
let db = SqliteDb::new(db_path.clone(), Some(options.clone()), &secp).unwrap();
539540
db.sanity_check(bitcoin::Network::Bitcoin, &other_desc)
540541
.unwrap_err()
@@ -714,23 +715,26 @@ mod tests {
714715
// There is the index for the first index
715716
let addr = options
716717
.main_descriptor
717-
.derive_receive(0.into(), &secp)
718+
.receive_descriptor()
719+
.derive(0.into(), &secp)
718720
.address(options.bitcoind_network);
719721
let db_addr = conn.db_address(&addr).unwrap();
720722
assert_eq!(db_addr.derivation_index, 0.into());
721723

722724
// There is the index for the 199th index (look-ahead limit)
723725
let addr = options
724726
.main_descriptor
725-
.derive_receive(199.into(), &secp)
727+
.receive_descriptor()
728+
.derive(199.into(), &secp)
726729
.address(options.bitcoind_network);
727730
let db_addr = conn.db_address(&addr).unwrap();
728731
assert_eq!(db_addr.derivation_index, 199.into());
729732

730733
// And not for the 200th one.
731734
let addr = options
732735
.main_descriptor
733-
.derive_receive(200.into(), &secp)
736+
.receive_descriptor()
737+
.derive(200.into(), &secp)
734738
.address(options.bitcoind_network);
735739
assert!(conn.db_address(&addr).is_none());
736740

src/database/sqlite/schema.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::descriptors::InheritanceDescriptor;
1+
use crate::descriptors::MultipathDescriptor;
22

33
use std::{convert::TryFrom, str::FromStr};
44

@@ -103,7 +103,7 @@ impl TryFrom<&rusqlite::Row<'_>> for DbTip {
103103
pub struct DbWallet {
104104
pub id: i64,
105105
pub timestamp: u32,
106-
pub main_descriptor: InheritanceDescriptor,
106+
pub main_descriptor: MultipathDescriptor,
107107
pub deposit_derivation_index: bip32::ChildNumber,
108108
}
109109

@@ -115,7 +115,7 @@ impl TryFrom<&rusqlite::Row<'_>> for DbWallet {
115115
let timestamp = row.get(1)?;
116116

117117
let desc_str: String = row.get(2)?;
118-
let main_descriptor = InheritanceDescriptor::from_str(&desc_str)
118+
let main_descriptor = MultipathDescriptor::from_str(&desc_str)
119119
.expect("Insane database: can't parse deposit descriptor");
120120

121121
let der_idx: u32 = row.get(3)?;

src/database/sqlite/utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ pub fn create_fresh_db(
9898
// TODO: have this as a helper in descriptors.rs
9999
let address = options
100100
.main_descriptor
101-
.derive_receive(index.into(), secp)
101+
.receive_descriptor()
102+
.derive(index.into(), secp)
102103
.address(options.bitcoind_network);
103104
query += &format!(
104105
"INSERT INTO addresses (address, derivation_index) VALUES (\"{}\", {});\n",

0 commit comments

Comments
 (0)