-
Notifications
You must be signed in to change notification settings - Fork 152
Add PsbtInputExt::update_with_descriptor #339
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f3198dc
Add find_derivation_index_for_spk
LLFourn cb0dd48
Add PsbtInputExt::update_with_descriptor
LLFourn e85b99c
Add segwit_version to DescriptorType
LLFourn 3374e11
fixup! Add segwit_version to DescriptorType
LLFourn f40dc83
fixup! Add find_derivation_index_for_spk
LLFourn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,13 +23,15 @@ | |
//! these with BIP32 paths, pay-to-contract instructions, etc. | ||
//! | ||
|
||
use std::ops::Range; | ||
use std::{collections::HashMap, sync::Arc}; | ||
use std::{ | ||
fmt, | ||
str::{self, FromStr}, | ||
}; | ||
|
||
use bitcoin::blockdata::witness::Witness; | ||
use bitcoin::util::address::WitnessVersion; | ||
use bitcoin::{self, secp256k1, Script}; | ||
|
||
use self::checksum::verify_checksum; | ||
|
@@ -259,6 +261,22 @@ pub enum DescriptorType { | |
Tr, | ||
} | ||
|
||
impl DescriptorType { | ||
/// Returns the segwit version implied by the descriptor type. | ||
/// | ||
/// This will return `Some(WitnessVersion::V0)` whether it is "native" segwitv0 or "wrapped" p2sh segwit. | ||
pub fn segwit_version(&self) -> Option<WitnessVersion> { | ||
use self::DescriptorType::*; | ||
match self { | ||
Tr => Some(WitnessVersion::V1), | ||
Wpkh | ShWpkh | Wsh | ShWsh | ShWshSortedMulti | WshSortedMulti => { | ||
Some(WitnessVersion::V0) | ||
} | ||
Bare | Sh | Pkh | ShSortedMulti => None, | ||
} | ||
} | ||
} | ||
|
||
impl<Pk: MiniscriptKey> Descriptor<Pk> { | ||
// Keys | ||
|
||
|
@@ -749,6 +767,31 @@ impl Descriptor<DescriptorPublicKey> { | |
|
||
descriptor.to_string() | ||
} | ||
|
||
/// Utility method for deriving the descriptor at each index in a range to find one matching | ||
/// `script_pubkey`. | ||
/// | ||
/// If it finds a match then it returns the index it was derived at and the concrete | ||
/// descriptor at that index. If the descriptor is non-derivable then it will simply check the | ||
/// script pubkey against the descriptor and return it if it matches (in this case the index | ||
/// returned will be meaningless). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool, TIL we derive at an index not from an index. Cheers. |
||
pub fn find_derivation_index_for_spk<C: secp256k1::Verification>( | ||
&self, | ||
secp: &secp256k1::Secp256k1<C>, | ||
script_pubkey: &Script, | ||
range: Range<u32>, | ||
) -> Result<Option<(u32, Descriptor<bitcoin::PublicKey>)>, ConversionError> { | ||
let range = if self.is_deriveable() { range } else { 0..1 }; | ||
|
||
for i in range { | ||
let concrete = self.derived_descriptor(&secp, i)?; | ||
if &concrete.script_pubkey() == script_pubkey { | ||
return Ok(Some((i, concrete))); | ||
} | ||
} | ||
|
||
Ok(None) | ||
} | ||
} | ||
|
||
impl<Pk> expression::FromTree for Descriptor<Pk> | ||
|
@@ -1736,4 +1779,31 @@ pk(03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8))"; | |
Descriptor::<DescriptorPublicKey>::from_str(&format!("wsh(pk({}))", x_only_key)) | ||
.unwrap_err(); | ||
} | ||
|
||
#[test] | ||
fn test_find_derivation_index_for_spk() { | ||
let secp = secp256k1::Secp256k1::verification_only(); | ||
let descriptor = Descriptor::from_str("tr([73c5da0a/86'/0'/0']xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0/*)").unwrap(); | ||
let script_at_0_1 = Script::from_str( | ||
"5120a82f29944d65b86ae6b5e5cc75e294ead6c59391a1edc5e016e3498c67fc7bbb", | ||
) | ||
.unwrap(); | ||
let expected_concrete = Descriptor::from_str( | ||
"tr(0283dfe85a3151d2517290da461fe2815591ef69f2b18a2ce63f01697a8b313145)", | ||
) | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
descriptor.find_derivation_index_for_spk(&secp, &script_at_0_1, 0..1), | ||
Ok(None) | ||
); | ||
assert_eq!( | ||
descriptor.find_derivation_index_for_spk(&secp, &script_at_0_1, 0..2), | ||
Ok(Some((1, expected_concrete.clone()))) | ||
); | ||
assert_eq!( | ||
descriptor.find_derivation_index_for_spk(&secp, &script_at_0_1, 0..10), | ||
Ok(Some((1, expected_concrete))) | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would also add a
is_taproot()
method, even though it will be unused here in miniscript. I'm sure somebody will find that useful!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
segwit_version(&self) -> Option<u8>
would be the most general?