Skip to content

Commit

Permalink
Update to bitcoin 0.27.0-rc.1. Make miniscript optional
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Mar 13, 2022
1 parent 9004e69 commit ae60dc4
Show file tree
Hide file tree
Showing 28 changed files with 495 additions and 426 deletions.
370 changes: 154 additions & 216 deletions Cargo.lock

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "descriptor-wallet"
version = "0.6.0"
version = "0.6.0-beta.1"
license = "Apache-2.0"
authors = ["Dr. Maxim Orlovsky <[email protected]>"]
description = "Libraries and command line tool for building descriptor-based bitcoin wallets"
Expand All @@ -23,44 +23,44 @@ required-features = ["keygen", "bip39", "aes", "rpassword", "cli"]

[[bin]]
name = "btc-cold"
required-features = ["electrum", "cli"]
required-features = ["electrum", "cli", "miniscript"]

[dependencies]
amplify = "3.9.1"
descriptors = { version = "0.6.0", path = "./descriptors", optional = true }
bitcoin_scripts = { version = "0.6.0", path = "./scripts" }
bitcoin_hd = { version = "0.6.0", path = "./hd" }
bitcoin_onchain = { version = "0.6.0", path = "./onchain" }
psbt = { version = "0.6.0", path = "./psbt" }
slip132 = { version = "0.6.0", path = "./slip132", features = ["strict_encoding"] }
commit_verify = { version = "0.6.0", git = "https://github.com/LNP-BP/client_side_validation", branch = "develop" }
amplify = "3.12.0"
descriptors = { version = "0.6.0-beta.1", path = "./descriptors", optional = true, default-features = false }
bitcoin_scripts = { version = "0.6.0-beta.1", path = "./scripts" }
bitcoin_hd = { version = "0.6.0-beta.1", path = "./hd" }
bitcoin_onchain = { version = "0.6.0-beta.1", path = "./onchain" }
psbt = { version = "0.6.0-beta.1", path = "./psbt", default-features = false }
slip132 = { version = "0.6.0-beta.1", path = "./slip132", features = ["strict_encoding"] }
commit_verify = "0.6.0-alpha.1"
# TODO #5: Make strict encoding optional
strict_encoding = { version = "1.8.0", git = "https://github.com/LNP-BP/client_side_validation", branch = "develop", features = ["bitcoin", "miniscript", "derive"] }
lightning_encoding = { version = "0.6.0", git = "https://github.com/LNP-BP/lnp-core", branch = "v0.6" }
bitcoin = { version = "0.28", git = "https://github.com/LNP-BP/rust-bitcoin", branch = "taproot/all" }
miniscript = { version = "7", git = "https://github.com/LNP-BP/rust-miniscript", branch = "taproot", features = ["compiler"] }
electrum-client = { version = "0.9", git = "https://github.com/LNP-BP/rust-electrum-client", branch = "taproot", optional = true }
strict_encoding = { version = "1.8.0-rc.1", features = ["bitcoin", "derive"] }
bitcoin = "0.28.0-rc.1"
miniscript_crate = { package = "miniscript", version = "6.0.1", git = "https://github.com/rust-bitcoin/rust-miniscript", optional = true }
electrum-client = { version = "0.9.0-rc.1", optional = true }
serde_crate = { package = "serde", version = "1", features = ["derive"], optional = true }
serde_with = { version = "1.5", features = ["hex"], optional = true }
serde_yaml = { version = "0.8.21", optional = true }
chrono = "0.4"
clap = { version = "3.0.0-rc.4", optional = true, features = ["derive"] }
clap = { version = "3.0", optional = true, features = ["derive"] }
bip39 = { version = "1.0.1", optional = true }
aes = { version = "0.7.5", optional = true }
rpassword = { version = "5.0.1", optional = true }
colored = { version = "2", optional = true }

[dev-dependencies]
bitcoin = { version = "0.28", git = "https://github.com/LNP-BP/rust-bitcoin", branch = "taproot/all", features = ["rand"] }
bitcoin = { version = "0.28.0-rc.1", features = ["rand"] }

[features]
default = []
all = ["keygen", "serde", "electrum"]
miniscript = ["miniscript_crate", "bitcoin_hd/miniscript", "bitcoin_onchain/miniscript", "descriptors/miniscript", "psbt/miniscript", "bitcoin_scripts/miniscript"]
cli = ["serde", "colored", "clap", "serde_yaml", "bitcoin/base64"]
electrum = ["electrum-client", "bitcoin_onchain/electrum"]
keygen = ["bitcoin/rand", "commit_verify/rand", "amplify/rand", "descriptors/rand", "psbt/sign"]
serde = ["serde_crate", "serde_with", "amplify/serde",
"bitcoin/use-serde", "bitcoin_scripts/serde", "bitcoin_hd/serde", "miniscript/serde", "slip132/serde"]
"bitcoin/use-serde", "bitcoin_scripts/serde", "bitcoin_hd/serde", "slip132/serde"]

[workspace]
members = [".", "slip132", "libbitcoin", "descriptors", "scripts", "hd", "psbt", "onchain"]
Expand Down
14 changes: 7 additions & 7 deletions descriptors/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "descriptors"
version = "0.6.0"
version = "0.6.0-beta.1"
license = "Apache-2.0"
authors = ["Dr. Maxim Orlovsky <[email protected]>"]
description = "Bitcoin descriptors library (part of descriptor-wallet)"
Expand All @@ -13,12 +13,12 @@ edition = "2018"
exclude = []

[dependencies]
amplify = "3.9.1"
strict_encoding = { version = "1.8.0", git = "https://github.com/LNP-BP/client_side_validation", branch = "develop", features = ["bitcoin", "miniscript"] }
bitcoin = { version = "0.28", git = "https://github.com/LNP-BP/rust-bitcoin", branch = "taproot/all" }
miniscript = { version = "7", git = "https://github.com/LNP-BP/rust-miniscript", branch = "taproot", features = ["compiler"], optional = true }
bitcoin_hd = { version = "0.6.0", path = "../hd" }
bitcoin_scripts = { version = "0.6.0", path = "../scripts" }
amplify = "3.12.0"
strict_encoding = "1.8.0-rc.1"
bitcoin = "0.28.0-rc.1"
miniscript = { version = "6.0.1", git = "https://github.com/rust-bitcoin/rust-miniscript", features = ["compiler"], optional = true }
bitcoin_hd = { version = "0.6.0-beta.1", path = "../hd" }
bitcoin_scripts = { version = "0.6.0-beta.1", path = "../scripts" }

[features]
all = ["rand", "miniscript"]
Expand Down
13 changes: 8 additions & 5 deletions descriptors/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ use std::str::FromStr;

use amplify::Wrapper;
use bitcoin::hashes::Hash;
use bitcoin::schnorr::{self as bip340, TweakedPublicKey, UntweakedPublicKey};
use bitcoin::schnorr::{TweakedPublicKey, UntweakedPublicKey};
use bitcoin::secp256k1::{self, Secp256k1, Verification};
use bitcoin::util::address::WitnessVersion;
use bitcoin::util::taproot::TapBranchHash;
use bitcoin::{PubkeyHash, Script, ScriptHash, WPubkeyHash, WScriptHash};
use bitcoin::{PubkeyHash, Script, ScriptHash, WPubkeyHash, WScriptHash, XOnlyPublicKey};
use bitcoin_scripts::convert::{LockScriptError, ToPubkeyScript};
use bitcoin_scripts::{ConvertInfo, PubkeyScript, RedeemScript, WitnessScript};
#[cfg(feature = "miniscript")]
Expand Down Expand Up @@ -584,8 +584,9 @@ impl FromStr for ScriptPubkeyDescr {
))
} else if s.starts_with("tr(") {
let inner = s.trim_start_matches("tr(");
let pk = XOnlyPublicKey::from_str(inner).map_err(|_| Error::CantParseDescriptor)?;
Ok(ScriptPubkeyDescr::Tr(
inner.parse().map_err(|_| Error::CantParseDescriptor)?,
TweakedPublicKey::dangerous_assume_tweaked(pk),
))
} else {
Err(Error::CantParseDescriptor)
Expand Down Expand Up @@ -641,7 +642,7 @@ impl TryFrom<PubkeyScript> for ScriptPubkeyDescr {
Ok(ScriptPubkeyDescr::Wsh(WScriptHash::from_inner(hash_inner)))
}
(spk, _) if spk.is_v1_p2tr() => Ok(ScriptPubkeyDescr::Tr(
TweakedPublicKey::dangerous_assume_tweaked(bip340::PublicKey::from_slice(
TweakedPublicKey::dangerous_assume_tweaked(XOnlyPublicKey::from_slice(
&bytes[2..],
)?),
)),
Expand Down Expand Up @@ -829,7 +830,9 @@ pub enum ParseError {
}

#[cfg(not(feature = "miniscript"))]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Display)]
#[derive(
Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Display, Error
)]
#[display(Debug)]
pub enum CompilerError {}

Expand Down
14 changes: 7 additions & 7 deletions hd/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bitcoin_hd"
version = "0.6.0"
version = "0.6.0-beta.1"
license = "Apache-2.0"
authors = ["Dr. Maxim Orlovsky <[email protected]>"]
description = "Bitcoin hierarchical deterministic wallet library"
Expand All @@ -13,15 +13,15 @@ edition = "2018"
exclude = []

[dependencies]
amplify = "3.9.1"
strict_encoding = { version = "1.8.0", git = "https://github.com/LNP-BP/client_side_validation", branch = "develop" }
bitcoin = { version = "0.28", git = "https://github.com/LNP-BP/rust-bitcoin", branch = "taproot/all" }
miniscript = { version = "7", git = "https://github.com/LNP-BP/rust-miniscript", branch = "taproot" }
slip132 = { version = "0.6.0", path = "../slip132" }
amplify = "3.12.0"
strict_encoding = "1.8.0-rc.1"
bitcoin = "0.28.0-rc.1"
miniscript = { version = "6.0.1", git = "https://github.com/rust-bitcoin/rust-miniscript", optional = true }
slip132 = { version = "0.6.0-beta.1", path = "../slip132" }
serde_crate = { package = "serde", version = "1", features = ["derive"], optional = true }
serde_with = { version = "1.5", features = ["hex"], optional = true }

[features]
default = []
all = ["serde"]
all = ["serde", "miniscript"]
serde = ["serde_crate", "serde_with", "amplify/serde", "bitcoin/serde", "miniscript/serde", "slip132/serde"]
20 changes: 13 additions & 7 deletions hd/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ use bitcoin::util::bip32::{
ChildNumber, DerivationPath, ExtendedPrivKey, ExtendedPubKey, Fingerprint, KeySource,
};
use bitcoin::{OutPoint, XpubIdentifier};
#[cfg(feature = "miniscript")]
use miniscript::MiniscriptKey;
use slip132::{Error, FromSlip132};

use crate::{
AccountStep, DerivePatternError, DerivePublicKey, HardenedIndex, SegmentIndexes, TerminalStep,
UnhardenedIndex, XpubRef,
};
use crate::{AccountStep, HardenedIndex, SegmentIndexes, TerminalStep, UnhardenedIndex, XpubRef};
use crate::{DerivePatternError, DerivePublicKey};

/// Tracking HD wallet account guaranteeing key derivation without access to the
/// private keys.
Expand Down Expand Up @@ -106,12 +105,16 @@ impl TrackingAccount {

/// Returns fingerprint of the master key, if known
#[inline]
pub fn master_fingerprint(&self) -> Option<Fingerprint> { self.master.fingerprint() }
pub fn master_fingerprint(&self) -> Option<Fingerprint> {
self.master.fingerprint()
}

/// Returns fingerprint of the master key - or, if no master key present, of
/// the account key
#[inline]
pub fn account_fingerprint(&self) -> Fingerprint { self.account_xpub.fingerprint() }
pub fn account_fingerprint(&self) -> Fingerprint {
self.account_xpub.fingerprint()
}

/// Constructs [`DerivationPath`] for the account extended public key
#[inline]
Expand Down Expand Up @@ -322,10 +325,13 @@ impl FromStr for TrackingAccount {
}
}

#[cfg(feature = "miniscript")]
impl MiniscriptKey for TrackingAccount {
type Hash = Self;

fn to_pubkeyhash(&self) -> Self::Hash { self.clone() }
fn to_pubkeyhash(&self) -> Self::Hash {
self.clone()
}
}

#[cfg(test)]
Expand Down
38 changes: 26 additions & 12 deletions hd/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
// along with this software.
// If not, see <https://opensource.org/licenses/Apache-2.0>.

#[cfg(feature = "miniscript")]
use std::cell::Cell;

use bitcoin::secp256k1::{self, Secp256k1, Verification};
use bitcoin::{Address, Network, Script};
#[cfg(feature = "miniscript")]
use miniscript::{Descriptor, DescriptorTrait, ForEach, ForEachKey, TranslatePk2};

use crate::{SegmentIndexes, TrackingAccount, UnhardenedIndex};
use crate::UnhardenedIndex;
#[cfg(feature = "miniscript")]
use crate::{SegmentIndexes, TrackingAccount};

/// the provided derive pattern does not match descriptor derivation
/// wildcard
Expand All @@ -41,6 +45,15 @@ pub trait DerivePublicKey {
) -> Result<secp256k1::PublicKey, DerivePatternError>;
}

#[cfg(not(feature = "miniscript"))]
pub mod miniscript {
#[derive(
Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Display, Error
)]
#[display(Debug)]
pub enum Error {}
}

/// Errors during descriptor derivation
#[derive(Debug, Display, From)]
#[display(doc_comments)]
Expand Down Expand Up @@ -101,11 +114,12 @@ pub trait DescriptorDerive {
fn network(&self) -> Result<Network, DeriveError>;

/// Translate descriptor to a specifically-derived form
#[cfg(feature = "miniscript")]
fn derive_descriptor<C: Verification>(
&self,
secp: &Secp256k1<C>,
pat: impl AsRef<[UnhardenedIndex]>,
) -> Result<Descriptor<secp256k1::PublicKey>, DeriveError>;
) -> Result<Descriptor<bitcoin::PublicKey>, DeriveError>;

/// Create scriptPubkey for specific derive pattern
fn script_pubkey<C: Verification>(
Expand All @@ -122,7 +136,8 @@ pub trait DescriptorDerive {
) -> Result<Address, DeriveError>;
}

impl DescriptorDerive for miniscript::Descriptor<TrackingAccount> {
#[cfg(feature = "miniscript")]
impl DescriptorDerive for Descriptor<TrackingAccount> {
#[inline]
fn check_sanity(&self) -> Result<(), DeriveError> {
self.derive_pattern_len()?;
Expand Down Expand Up @@ -180,18 +195,17 @@ impl DescriptorDerive for miniscript::Descriptor<TrackingAccount> {
&self,
secp: &Secp256k1<C>,
pat: impl AsRef<[UnhardenedIndex]>,
) -> Result<Descriptor<secp256k1::PublicKey>, DeriveError> {
) -> Result<Descriptor<bitcoin::PublicKey>, DeriveError> {
let pat = pat.as_ref();
if pat.len() != self.derive_pattern_len()? {
return Err(DeriveError::DerivePatternMismatch);
}
let mut descriptor = self
.translate_pk2(|account| account.derive_public_key(secp, pat))
.map_err(DeriveError::from)?;
if let Descriptor::Tr(ref mut tr) = descriptor {
tr.spend_info(secp);
}
Ok(descriptor)
self.translate_pk2(|account| {
account
.derive_public_key(secp, pat)
.map(bitcoin::PublicKey::new)
})
.map_err(DeriveError::from)
}

#[inline]
Expand All @@ -201,7 +215,7 @@ impl DescriptorDerive for miniscript::Descriptor<TrackingAccount> {
pat: impl AsRef<[UnhardenedIndex]>,
) -> Result<Script, DeriveError> {
let d = self.derive_descriptor(secp, pat)?;
DescriptorTrait::script_pubkey(&d).map_err(DeriveError::from)
Ok(DescriptorTrait::script_pubkey(&d))
}

#[inline]
Expand Down
6 changes: 5 additions & 1 deletion hd/src/schemata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ use core::str::FromStr;
use std::convert::TryFrom;

use bitcoin::util::bip32::{ChildNumber, DerivationPath};
#[cfg(feature = "miniscript")]
use miniscript::descriptor::DescriptorType;

use crate::{HardenedIndex, SegmentIndexes, UnhardenedIndex};
#[cfg(feature = "miniscript")]
use crate::SegmentIndexes;
use crate::{HardenedIndex, UnhardenedIndex};

/// Errors in parsing derivation scheme string representation
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Error, Display)]
Expand Down Expand Up @@ -345,6 +348,7 @@ impl DerivationScheme {

/// Check whether provided descriptor type can be used with this derivation
/// scheme
#[cfg(feature = "miniscript")]
pub fn check_descriptor_type(&self, descriptor_type: DescriptorType) -> bool {
match (self, descriptor_type) {
(DerivationScheme::Bip44, DescriptorType::Pkh)
Expand Down
4 changes: 2 additions & 2 deletions libbitcoin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libbitcoin"
version = "0.3.0"
version = "0.3.0-rc.1"
license = "Apache-2.0"
authors = ["Dr Maxim Orlovsky <[email protected]>"]
description = "C library for building descriptor-based bitcoin wallets"
Expand All @@ -16,7 +16,7 @@ libc = "0.2"
lazy_static = "1.4"
amplify = "3"
amplify_derive = "2.4"
bitcoin = { version = "0.28", git = "https://github.com/LNP-BP/rust-bitcoin", branch = "taproot/all" }
bitcoin = "0.28.0-rc.1"
bip39 = "1.0.1"
rand = "0.8.3"
serde = { version = "1.0", features = ["derive"] }
Expand Down
19 changes: 10 additions & 9 deletions onchain/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bitcoin_onchain"
version = "0.6.0"
version = "0.6.0-beta.1"
license = "Apache-2.0"
authors = ["Dr. Maxim Orlovsky <[email protected]>"]
description = "Bitcoin descriptors library (part of descriptor-wallet)"
Expand All @@ -13,16 +13,17 @@ edition = "2018"
exclude = []

[dependencies]
amplify = "3.9.1"
bitcoin = { version = "0.28", git = "https://github.com/LNP-BP/rust-bitcoin", branch = "taproot/all" }
strict_encoding = { version = "1.8.0", git = "https://github.com/LNP-BP/client_side_validation", branch = "develop" }
descriptors = { version = "0.6.0-alpha.1", path = "../descriptors" }
bitcoin_hd = { version = "0.6.0-alpha.1", path = "../hd" }
miniscript = { version = "7", git = "https://github.com/LNP-BP/rust-miniscript", branch = "taproot", features = ["compiler"] }
amplify = "3.12.0"
bitcoin = "0.28.0-rc.1"
strict_encoding = "1.8.0-rc.1"
descriptors = { version = "0.6.0-beta.1", path = "../descriptors", default-features = false }
bitcoin_hd = { version = "0.6.0-beta.1", path = "../hd" }
miniscript_crate = { package = "miniscript", version = "6.0.1", git = "https://github.com/rust-bitcoin/rust-miniscript", optional = true }
electrum-client = { version = "0.9.0-rc.1", optional = true }
chrono = "0.4.19"
electrum-client = { version = "0.9", git = "https://github.com/LNP-BP/rust-electrum-client", branch = "taproot", optional = true }

[features]
default = []
all = ["electrum"]
all = ["electrum", "miniscript"]
miniscript = ["miniscript_crate", "descriptors/miniscript", "bitcoin_hd/miniscript"]
electrum = ["electrum-client"]
2 changes: 2 additions & 0 deletions onchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#[macro_use]
extern crate amplify;
#[cfg(feature = "miniscript")]
extern crate miniscript_crate as miniscript;

pub mod blockchain;
mod resolvers;
Expand Down
Loading

0 comments on commit ae60dc4

Please sign in to comment.