Repository navigation
Validate policy keys and hashes before compiling descriptors #347
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
Open
Olorunshogo
wants to merge
2
commits into
bitcoindevkit:master
Choose a base branch
from
Olorunshogo:fix/compile-key-validation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+201
−38
Open
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -22,7 +22,12 @@ use { | |
| key::{Parity, rand}, | ||
| secp256k1::{PublicKey, Scalar, Secp256k1, SecretKey}, | ||
| }, | ||
| miniscript::{Descriptor, descriptor::TapTree, policy::Concrete}, | ||
| miniscript::{ | ||
| Descriptor, ForEachKey, FromStrKey, Legacy, MiniscriptKey, ScriptContext, Segwitv0, | ||
| Tap, | ||
| descriptor::{DescriptorPublicKey, TapTree}, | ||
| policy::Concrete, | ||
| }, | ||
| }, | ||
| std::{str::FromStr, sync::Arc}, | ||
| }; | ||
|
|
@@ -80,6 +85,11 @@ pub struct CompileCommand { | |
| default_value_t = ScriptType::Wsh | ||
| )] | ||
| script_type: ScriptType, | ||
| /// Skip key and hash validation, accepting placeholders (e.g. pk(A)). Useful for | ||
| /// inspecting a policy's shape without real keys. Descriptors compiled with this | ||
| /// flag are not usable for spending. | ||
| #[arg(long = "placeholders")] | ||
| placeholders: bool, | ||
| } | ||
|
|
||
| /// Script types the compiled policy can be embedded in. | ||
|
|
@@ -92,48 +102,99 @@ pub enum ScriptType { | |
| Tr, | ||
| } | ||
|
|
||
| /// Rejects keys that parse fine on their own but aren't valid for `Ctx` (e.g. an | ||
| /// x-only key under `Segwitv0`), which miniscript's compiler otherwise turns into | ||
| /// a panic instead of an error. A no-op for `Pk = String`, since `String` never | ||
| /// reports itself as uncompressed or x-only. | ||
|
Olorunshogo marked this conversation as resolved.
|
||
| #[cfg(feature = "compiler")] | ||
| fn check_policy_keys<Pk: MiniscriptKey, Ctx: ScriptContext>( | ||
|
Collaborator
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. Please kindly move this fn to |
||
| policy: &Concrete<Pk>, | ||
| ) -> Result<(), Error> { | ||
| let mut key_error = None; | ||
| policy.for_each_key(|pk| match Ctx::check_pk(pk) { | ||
| Ok(()) => true, | ||
| Err(e) => { | ||
| key_error = Some(e); | ||
| false | ||
| } | ||
| }); | ||
|
|
||
| match key_error { | ||
| Some(e) => Err(Error::Generic(format!("Invalid policy: {e}"))), | ||
| None => Ok(()), | ||
| } | ||
| } | ||
|
|
||
| /// Parses `policy_str` as `Pk` and compiles it for `script_type`. Returns the | ||
| /// descriptor string and, for `tr`, the random tweak `r` used to derive the | ||
| /// unspendable internal key so the caller can verify it. | ||
|
Olorunshogo marked this conversation as resolved.
|
||
| #[cfg(feature = "compiler")] | ||
| fn compile_policy<Pk: FromStrKey>( | ||
|
Collaborator
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. Same applies for this fn. Move to |
||
| policy_str: &str, | ||
| script_type: ScriptType, | ||
| ) -> Result<(String, Option<String>), Error> { | ||
| let policy: Concrete<Pk> = Concrete::from_str(policy_str) | ||
| .map_err(|e| Error::Generic(format!("Invalid policy: {e}")))?; | ||
|
|
||
| let mut r = None; | ||
|
|
||
| // Compile per branch, not once up front: the contexts have different script | ||
| // limits, and the narrowest one would reject policies valid for the requested type. | ||
| let descriptor = match script_type { | ||
| ScriptType::Sh => { | ||
| check_policy_keys::<Pk, Legacy>(&policy)?; | ||
| Descriptor::new_sh(policy.compile()?) | ||
| } | ||
| ScriptType::Wsh => { | ||
| check_policy_keys::<Pk, Segwitv0>(&policy)?; | ||
| Descriptor::new_wsh(policy.compile()?) | ||
| } | ||
| ScriptType::ShWsh => { | ||
| check_policy_keys::<Pk, Segwitv0>(&policy)?; | ||
| Descriptor::new_sh_wsh(policy.compile()?) | ||
| } | ||
| ScriptType::Tr => { | ||
| check_policy_keys::<Pk, Tap>(&policy)?; | ||
| // Use a randomized unspendable internal key (H + rG) instead of a fixed NUMS | ||
| // point. This improves privacy by preventing observers from determining whether | ||
| // key-path spending is disabled. `r` is returned so the user can verify the key | ||
| // is derived from the NUMS point. See BIP-341. | ||
| let secp = Secp256k1::new(); | ||
| let r_secret = SecretKey::new(&mut rand::thread_rng()); | ||
| r = Some(r_secret.display_secret().to_string()); | ||
|
|
||
| let nums_key = XOnlyPublicKey::from_str(NUMS_UNSPENDABLE_KEY_HEX) | ||
| .map_err(|e| Error::Generic(format!("Invalid NUMS key: {e}")))?; | ||
| let nums_point = PublicKey::from_x_only_public_key(nums_key, Parity::Even); | ||
|
|
||
| let internal_key_point = nums_point | ||
| .add_exp_tweak(&secp, &Scalar::from(r_secret)) | ||
| .map_err(|e| Error::Generic(format!("Failed to tweak NUMS key: {e}")))?; | ||
| let (xonly_internal_key, _) = internal_key_point.x_only_public_key(); | ||
|
|
||
| let tree = TapTree::Leaf(Arc::new(policy.compile()?)); | ||
| let internal_key = Pk::from_str(&xonly_internal_key.to_string()) | ||
| .map_err(|e| Error::Generic(format!("Failed to parse internal key: {e}")))?; | ||
| Descriptor::new_tr(internal_key, Some(tree)) | ||
| } | ||
| }?; | ||
|
|
||
| Ok((descriptor.to_string(), r)) | ||
| } | ||
|
|
||
| #[cfg(feature = "compiler")] | ||
| impl AppCommand<AppContext<Init>> for CompileCommand { | ||
| type Output = DescriptorResult; | ||
|
|
||
| fn execute(&self, _ctx: &mut AppContext<Init>) -> Result<Self::Output, Error> { | ||
| let policy: Concrete<String> = Concrete::from_str(&self.policy) | ||
| .map_err(|e| Error::Generic(format!("Invalid policy: {e}")))?; | ||
|
|
||
| let mut r = None; | ||
|
|
||
| // Compile per branch, not once up front: the contexts have different script | ||
| // limits, and the narrowest one would reject policies valid for the requested type. | ||
| let descriptor = match self.script_type { | ||
| ScriptType::Sh => Descriptor::new_sh(policy.compile()?), | ||
| ScriptType::Wsh => Descriptor::new_wsh(policy.compile()?), | ||
| ScriptType::ShWsh => Descriptor::new_sh_wsh(policy.compile()?), | ||
| ScriptType::Tr => { | ||
| // Use a randomized unspendable internal key (H + rG) instead of a fixed NUMS | ||
| // point. This improves privacy by preventing observers from determining whether | ||
| // key-path spending is disabled. `r` is returned so the user can verify the key | ||
| // is derived from the NUMS point. See BIP-341. | ||
| let secp = Secp256k1::new(); | ||
| let r_secret = SecretKey::new(&mut rand::thread_rng()); | ||
| r = Some(r_secret.display_secret().to_string()); | ||
|
|
||
| let nums_key = XOnlyPublicKey::from_str(NUMS_UNSPENDABLE_KEY_HEX) | ||
| .map_err(|e| Error::Generic(format!("Invalid NUMS key: {e}")))?; | ||
| let nums_point = PublicKey::from_x_only_public_key(nums_key, Parity::Even); | ||
|
|
||
| let internal_key_point = | ||
| nums_point | ||
| .add_exp_tweak(&secp, &Scalar::from(r_secret)) | ||
| .map_err(|e| Error::Generic(format!("Failed to tweak NUMS key: {e}")))?; | ||
| let (xonly_internal_key, _) = internal_key_point.x_only_public_key(); | ||
|
|
||
| let tree = TapTree::Leaf(Arc::new(policy.compile()?)); | ||
| Descriptor::new_tr(xonly_internal_key.to_string(), Some(tree)) | ||
| } | ||
| }?; | ||
| let (descriptor, r) = if self.placeholders { | ||
| compile_policy::<String>(&self.policy, self.script_type)? | ||
| } else { | ||
| compile_policy::<DescriptorPublicKey>(&self.policy, self.script_type)? | ||
| }; | ||
|
|
||
| Ok(DescriptorResult { | ||
| descriptor: Some(descriptor.to_string()), | ||
| descriptor: Some(descriptor), | ||
| mnemonic: None, | ||
| multipath_descriptor: None, | ||
| public_descriptors: None, | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.