Skip to content

Commit 7987a55

Browse files
committed
fix(descriptor): reject bare descriptors in check_wallet_descriptor
Bare descriptors (e.g. pk()) have no standard address form. Passing one to the wallet would not be caught at creation time and would cause panics later when address derivation is attempted. Add an UnsupportedDescriptorType variant to DescriptorError and check for DescriptorType::Bare in check_wallet_descriptor, returning the new error before the descriptor reaches wallet internals. Closes #54
1 parent 33fb97b commit 7987a55

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

src/descriptor/error.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ pub enum Error {
4444
Hex(bitcoin::hex::HexToBytesError),
4545
/// The provided wallet descriptors are identical
4646
ExternalAndInternalAreTheSame,
47+
/// Descriptor type is not supported by the wallet (e.g. bare scripts have no address form)
48+
UnsupportedDescriptorType,
4749
}
4850

4951
impl From<crate::keys::KeyError> for Error {
@@ -84,6 +86,12 @@ impl fmt::Display for Error {
8486
Self::ExternalAndInternalAreTheSame => {
8587
write!(f, "External and internal descriptors are the same")
8688
}
89+
Self::UnsupportedDescriptorType => {
90+
write!(
91+
f,
92+
"Descriptor type is not supported by the wallet; bare scripts have no address form"
93+
)
94+
}
8795
}
8896
}
8997
}

src/descriptor/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ pub(crate) fn check_wallet_descriptor(
321321
));
322322
}
323323

324+
// Reject bare descriptors: they have no standard address form and would cause panics
325+
// inside the wallet when address derivation is attempted.
326+
if descriptor.desc_type() == DescriptorType::Bare {
327+
return Err(DescriptorError::UnsupportedDescriptorType);
328+
}
329+
324330
// Run miniscript's sanity check, which will look for duplicated keys and other potential
325331
// issues.
326332
descriptor.sanity_check()?;
@@ -912,6 +918,16 @@ mod test {
912918
let result = check_wallet_descriptor(&descriptor);
913919

914920
assert!(result.is_err());
921+
922+
// Bare descriptors (e.g. pk()) have no standard address form and must be rejected.
923+
let descriptor = Descriptor::<DescriptorPublicKey>::from_str(
924+
"pk(tpubD6NzVbkrYhZ4XHndKkuB8FifXm8r5FQHwrN6oZuWCz13qb93rtgKvD4PQsqC4HP4yhV3tA2fqr2RbY5mNXfM7RxXUoeABoDtsFUq2zJq6YK/0/*)",
925+
)
926+
.expect("must parse");
927+
assert_matches!(
928+
check_wallet_descriptor(&descriptor),
929+
Err(DescriptorError::UnsupportedDescriptorType)
930+
);
915931
}
916932

917933
#[test]

0 commit comments

Comments
 (0)