diff --git a/Cargo.toml b/Cargo.toml index b49fdab..1ca4b61 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -118,7 +118,9 @@ electrum = [ "bitcoin_onchain/electrum" ] strict_encoding = [ - "slip132/strict_encoding" + "slip132/strict_encoding", + "psbt/strict_encoding", + "descriptors/strict_encoding", ] sign = ["psbt/sign"] construct = ["psbt/construct"] diff --git a/descriptors/Cargo.toml b/descriptors/Cargo.toml index ceedfa6..0c8731c 100644 --- a/descriptors/Cargo.toml +++ b/descriptors/Cargo.toml @@ -15,7 +15,6 @@ exclude = [] [dependencies] amplify = { workspace = true } -strict_encoding = { workspace = true } bitcoin = { workspace = true } bitcoin_scripts = { workspace = true } bitcoin_blockchain = { workspace = true } @@ -24,12 +23,14 @@ miniscript_crate = { workspace = true, features = ["compiler"], optional = true chrono = { workspace = true } serde_crate = { package = "serde", version = "1", optional = true } serde_with = { version = "2.3", features = ["hex"], optional = true } +strict_encoding = { workspace = true, optional = true } [features] all = [ "rand", "miniscript", - "serde" + "serde", + "strict_encoding" ] default = [] rand = [ diff --git a/descriptors/src/descriptor.rs b/descriptors/src/descriptor.rs index 8e5d549..bd9446e 100644 --- a/descriptors/src/descriptor.rs +++ b/descriptors/src/descriptor.rs @@ -12,7 +12,6 @@ use std::fmt::{self, Display, Formatter}; use std::str::FromStr; -use amplify::Wrapper; use bitcoin::hashes::Hash; use bitcoin::schnorr::{TweakedPublicKey, UntweakedPublicKey}; use bitcoin::secp256k1::{self, Secp256k1, Verification}; @@ -32,7 +31,7 @@ use miniscript::policy::compiler::CompilerError; use miniscript::{Descriptor, MiniscriptKey, Terminal}; #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)] -#[derive(StrictEncode, StrictDecode)] +#[cfg_attr(feature = "strict_encoding", derive(StrictEncode, StrictDecode))] #[cfg_attr( feature = "serde", derive(Serialize, Deserialize), @@ -97,7 +96,7 @@ impl DescriptorClass { #[derive( Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash, Default, Debug, Display )] -#[derive(StrictEncode, StrictDecode)] +#[cfg_attr(feature = "strict_encoding", derive(StrictEncode, StrictDecode))] #[repr(u8)] pub enum SpkClass { #[display("bare")] @@ -203,7 +202,7 @@ impl FromStr for SpkClass { serde(crate = "serde_crate") )] #[derive(Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)] -#[derive(StrictEncode, StrictDecode)] +#[cfg_attr(feature = "strict_encoding", derive(StrictEncode, StrictDecode))] #[repr(u8)] pub enum CompositeDescrType { #[display("bare")] @@ -328,7 +327,7 @@ impl FromStr for CompositeDescrType { serde(crate = "serde_crate") )] #[derive(Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)] -#[derive(StrictEncode, StrictDecode)] +#[cfg_attr(feature = "strict_encoding", derive(StrictEncode, StrictDecode))] #[repr(u8)] pub enum OuterDescrType { #[display("bare")] @@ -410,8 +409,8 @@ impl FromStr for OuterDescrType { derive(Serialize, Deserialize), serde(crate = "serde_crate") )] +#[cfg_attr(feature = "strict_encoding", derive(StrictEncode, StrictDecode))] #[derive(Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)] -#[derive(StrictEncode, StrictDecode)] #[repr(u8)] pub enum InnerDescrType { #[display("bare")] @@ -494,7 +493,7 @@ impl FromStr for InnerDescrType { serde(crate = "serde_crate") )] #[derive(Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Default)] -#[derive(StrictEncode, StrictDecode)] +#[cfg_attr(feature = "strict_encoding", derive(StrictEncode, StrictDecode))] #[repr(C)] pub struct DescrVariants { pub bare: bool, @@ -566,7 +565,7 @@ impl DescrVariants { } #[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug, Display)] -#[derive(StrictEncode, StrictDecode)] +#[cfg_attr(feature = "strict_encoding", derive(StrictEncode, StrictDecode))] #[non_exhaustive] pub enum ScriptPubkeyDescr { #[display("bare({0})", alt = "bare({0:#})")] @@ -663,7 +662,7 @@ impl TryFrom for ScriptPubkeyDescr { type Error = UnsupportedScriptPubkey; fn try_from(spk: PubkeyScript) -> Result { - let script = spk.as_inner(); + let script = spk.clone(); let bytes = script.as_bytes(); match (&spk, spk.witness_version()) { (spk, _) if spk.is_p2pk() && script.len() == 67 => Ok(ScriptPubkeyDescr::Pk( @@ -707,7 +706,7 @@ impl TryFrom for ScriptPubkeyDescr { /// Descriptors exposing bare scripts (unlike [`miniscript::Descriptor`] which /// uses miniscript representation of the scripts). #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] -#[derive(StrictEncode, StrictDecode)] +#[cfg_attr(feature = "strict_encoding", derive(StrictEncode, StrictDecode))] #[non_exhaustive] pub enum BareDescriptor { Bare(PubkeyScript), diff --git a/descriptors/src/input.rs b/descriptors/src/input.rs index 2873369..7f108f1 100644 --- a/descriptors/src/input.rs +++ b/descriptors/src/input.rs @@ -21,7 +21,6 @@ use bitcoin_blockchain::locks::{self, SeqNo}; use bitcoin_hd::{DerivationSubpath, UnhardenedIndex}; #[derive(Clone, PartialEq, Eq, Debug)] -#[derive(StrictEncode, StrictDecode)] pub struct InputDescriptor { pub outpoint: OutPoint, pub terminal: DerivationSubpath, diff --git a/descriptors/src/lib.rs b/descriptors/src/lib.rs index 2e63e89..fd6e655 100644 --- a/descriptors/src/lib.rs +++ b/descriptors/src/lib.rs @@ -22,6 +22,7 @@ #[macro_use] extern crate amplify; +#[cfg(feature = "strict_encoding")] #[macro_use] extern crate strict_encoding; #[cfg(feature = "miniscript")] diff --git a/descriptors/src/templates.rs b/descriptors/src/templates.rs index 10edd1b..0dd9c2e 100644 --- a/descriptors/src/templates.rs +++ b/descriptors/src/templates.rs @@ -9,130 +9,269 @@ // along with this software. // If not, see . -use std::fmt::{self, Display, Formatter}; -use std::str::FromStr; - -use amplify::Wrapper; -use bitcoin::blockdata::opcodes; -use bitcoin::blockdata::script::Builder; -use bitcoin::secp256k1::{Secp256k1, Verification}; -use bitcoin::Script; -use bitcoin_hd::account::DerivePublicKey; -use bitcoin_hd::{DerivePatternError, UnhardenedIndex}; -use miniscript::MiniscriptKey; -#[cfg(feature = "serde")] -use serde_with::{hex::Hex, As, DisplayFromStr}; -use strict_encoding::{self, StrictDecode, StrictEncode}; - -/// Allows creating templates for native bitcoin scripts with embedded -/// key generator templates. May be useful for creating descriptors in -/// situations where target script can't be deterministically represented by -/// miniscript, for instance for Lightning network-specific transaction outputs -#[cfg_attr( - feature = "serde", - derive(Serialize, Deserialize), - serde(crate = "serde_crate", rename = "lowercase") -)] -#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Debug, Hash, Display)] -#[derive(StrictEncode, StrictDecode)] -pub enum OpcodeTemplate -where - Pk: MiniscriptKey + StrictEncode + StrictDecode + FromStr, - ::Err: Display, -{ - /// Normal script command (OP_CODE) - #[display("opcode({0})")] - OpCode(u8), - - /// Binary data (follows push commands) - #[display("data({0:#x?})")] - Data(#[cfg_attr(feature = "serde", serde(with = "As::"))] Box<[u8]>), - - /// Key template - #[display("key({0})")] - Key(#[cfg_attr(feature = "serde", serde(with = "As::"))] Pk), -} +#[cfg(feature = "strict_encoding")] +pub use strict::{OpcodeTemplate, ScriptTemplate}; + +#[cfg(feature = "strict_encoding")] +mod strict { + use std::fmt::{self, Display, Formatter}; + use std::str::FromStr; + + use amplify::Wrapper; + use bitcoin::blockdata::opcodes; + use bitcoin::blockdata::script::Builder; + use bitcoin::secp256k1::{Secp256k1, Verification}; + use bitcoin::Script; + use bitcoin_hd::account::DerivePublicKey; + use bitcoin_hd::{DerivePatternError, UnhardenedIndex}; + use miniscript::MiniscriptKey; + #[cfg(feature = "serde")] + use serde_with::{hex::Hex, As, DisplayFromStr}; + use strict_encoding::{self, StrictDecode, StrictEncode}; + + /// Allows creating templates for native bitcoin scripts with embedded + /// key generator templates. May be useful for creating descriptors in + /// situations where target script can't be deterministically represented by + /// miniscript, for instance for Lightning network-specific transaction + /// outputs + #[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(crate = "serde_crate", rename = "lowercase") + )] + #[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Debug, Hash, Display)] + pub enum OpcodeTemplate + where + Pk: MiniscriptKey + FromStr + StrictEncode + StrictDecode, + ::Err: Display, + { + /// Normal script command (OP_CODE) + #[display("opcode({0})")] + OpCode(u8), + + /// Binary data (follows push commands) + #[display("data({0:#x?})")] + Data(#[cfg_attr(feature = "serde", serde(with = "As::"))] Box<[u8]>), + + /// Key template + #[display("key({0})")] + Key(#[cfg_attr(feature = "serde", serde(with = "As::"))] Pk), + } + + impl OpcodeTemplate + where + Pk: MiniscriptKey + DerivePublicKey + StrictEncode + StrictDecode + FromStr, + ::Err: Display, + { + fn translate_pk( + &self, + ctx: &Secp256k1, + pat: impl IntoIterator>, + ) -> Result, DerivePatternError> { + Ok(match self { + OpcodeTemplate::OpCode(code) => OpcodeTemplate::OpCode(*code), + OpcodeTemplate::Data(data) => OpcodeTemplate::Data(data.clone()), + OpcodeTemplate::Key(key) => { + OpcodeTemplate::Key(bitcoin::PublicKey::new(key.derive_public_key(ctx, pat)?)) + } + }) + } + } + + /// Allows creating templates for native bitcoin scripts with embedded + /// key generator templates. May be useful for creating descriptors in + /// situations where target script can't be deterministically represented by + /// miniscript, for instance for Lightning network-specific transaction + /// outputs + #[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(crate = "serde_crate", transparent) + )] + #[derive(Wrapper, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)] + #[wrap(Index, IndexMut, IndexFull, IndexFrom, IndexTo, IndexInclusive)] + pub struct ScriptTemplate(Vec>) + where + Pk: MiniscriptKey + StrictEncode + StrictDecode + FromStr, + ::Err: Display; -impl OpcodeTemplate -where - Pk: MiniscriptKey + DerivePublicKey + StrictEncode + StrictDecode + FromStr, - ::Err: Display, -{ - fn translate_pk( - &self, - ctx: &Secp256k1, - pat: impl IntoIterator>, - ) -> Result, DerivePatternError> { - Ok(match self { - OpcodeTemplate::OpCode(code) => OpcodeTemplate::OpCode(*code), - OpcodeTemplate::Data(data) => OpcodeTemplate::Data(data.clone()), - OpcodeTemplate::Key(key) => { - OpcodeTemplate::Key(bitcoin::PublicKey::new(key.derive_public_key(ctx, pat)?)) + impl Display for ScriptTemplate + where + Pk: MiniscriptKey + StrictEncode + StrictDecode + FromStr, + ::Err: Display, + { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + for instruction in &self.0 { + Display::fmt(instruction, f)?; } - }) + Ok(()) + } } -} -/// Allows creating templates for native bitcoin scripts with embedded -/// key generator templates. May be useful for creating descriptors in -/// situations where target script can't be deterministically represented by -/// miniscript, for instance for Lightning network-specific transaction outputs -#[cfg_attr( - feature = "serde", - derive(Serialize, Deserialize), - serde(crate = "serde_crate", transparent) -)] -#[derive(Wrapper, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)] -#[derive(StrictEncode, StrictDecode)] -#[wrap(Index, IndexMut, IndexFull, IndexFrom, IndexTo, IndexInclusive)] -pub struct ScriptTemplate(Vec>) -where - Pk: MiniscriptKey + StrictEncode + StrictDecode + FromStr, - ::Err: Display; - -impl Display for ScriptTemplate -where - Pk: MiniscriptKey + StrictEncode + StrictDecode + FromStr, - ::Err: Display, -{ - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - for instruction in &self.0 { - Display::fmt(instruction, f)?; + impl ScriptTemplate + where + Pk: MiniscriptKey + DerivePublicKey + StrictEncode + StrictDecode + FromStr, + ::Err: Display, + { + pub fn translate_pk( + &self, + ctx: &Secp256k1, + pat: impl AsRef<[UnhardenedIndex]>, + ) -> Result, DerivePatternError> { + let pat = pat.as_ref(); + Ok(self + .0 + .iter() + .map(|op| op.translate_pk(ctx, pat)) + .collect::, _>>()? + .into()) } - Ok(()) } -} -impl ScriptTemplate -where - Pk: MiniscriptKey + DerivePublicKey + StrictEncode + StrictDecode + FromStr, - ::Err: Display, -{ - pub fn translate_pk( - &self, - ctx: &Secp256k1, - pat: impl AsRef<[UnhardenedIndex]>, - ) -> Result, DerivePatternError> { - let pat = pat.as_ref(); - Ok(self - .0 - .iter() - .map(|op| op.translate_pk(ctx, pat)) - .collect::, _>>()? - .into()) + impl From> for Script { + fn from(template: ScriptTemplate) -> Self { + let mut builder = Builder::new(); + for op in template.into_inner() { + builder = match op { + OpcodeTemplate::OpCode(code) => builder.push_opcode(opcodes::All::from(code)), + OpcodeTemplate::Data(data) => builder.push_slice(&data), + OpcodeTemplate::Key(key) => builder.push_key(&key), + }; + } + builder.into_script() + } } } -impl From> for Script { - fn from(template: ScriptTemplate) -> Self { - let mut builder = Builder::new(); - for op in template.into_inner() { - builder = match op { - OpcodeTemplate::OpCode(code) => builder.push_opcode(opcodes::All::from(code)), - OpcodeTemplate::Data(data) => builder.push_slice(&data), - OpcodeTemplate::Key(key) => builder.push_key(&key), - }; +#[cfg(not(feature = "strict_encoding"))] +pub use no_strict::{OpcodeTemplate, ScriptTemplate}; + +#[cfg(not(feature = "strict_encoding"))] +mod no_strict { + use std::fmt::{self, Display, Formatter}; + use std::str::FromStr; + + use amplify::Wrapper; + use bitcoin::blockdata::opcodes; + use bitcoin::blockdata::script::Builder; + use bitcoin::secp256k1::{Secp256k1, Verification}; + use bitcoin::Script; + use bitcoin_hd::account::DerivePublicKey; + use bitcoin_hd::{DerivePatternError, UnhardenedIndex}; + use miniscript::MiniscriptKey; + #[cfg(feature = "serde")] + use serde_with::{hex::Hex, As, DisplayFromStr}; + + /// Allows creating templates for native bitcoin scripts with embedded + /// key generator templates. May be useful for creating descriptors in + /// situations where target script can't be deterministically represented by + /// miniscript, for instance for Lightning network-specific transaction + /// outputs + #[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(crate = "serde_crate", rename = "lowercase") + )] + #[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Debug, Hash, Display)] + pub enum OpcodeTemplate + where + Pk: MiniscriptKey + FromStr, + ::Err: Display, + { + /// Normal script command (OP_CODE) + #[display("opcode({0})")] + OpCode(u8), + + /// Binary data (follows push commands) + #[display("data({0:#x?})")] + Data(#[cfg_attr(feature = "serde", serde(with = "As::"))] Box<[u8]>), + + /// Key template + #[display("key({0})")] + Key(#[cfg_attr(feature = "serde", serde(with = "As::"))] Pk), + } + + impl OpcodeTemplate + where + Pk: MiniscriptKey + DerivePublicKey + FromStr, + ::Err: Display, + { + fn translate_pk( + &self, + ctx: &Secp256k1, + pat: impl IntoIterator>, + ) -> Result, DerivePatternError> { + Ok(match self { + OpcodeTemplate::OpCode(code) => OpcodeTemplate::OpCode(*code), + OpcodeTemplate::Data(data) => OpcodeTemplate::Data(data.clone()), + OpcodeTemplate::Key(key) => { + OpcodeTemplate::Key(bitcoin::PublicKey::new(key.derive_public_key(ctx, pat)?)) + } + }) + } + } + + /// Allows creating templates for native bitcoin scripts with embedded + /// key generator templates. May be useful for creating descriptors in + /// situations where target script can't be deterministically represented by + /// miniscript, for instance for Lightning network-specific transaction + /// outputs + #[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(crate = "serde_crate", transparent) + )] + #[derive(Wrapper, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)] + #[wrap(Index, IndexMut, IndexFull, IndexFrom, IndexTo, IndexInclusive)] + pub struct ScriptTemplate(Vec>) + where + Pk: MiniscriptKey + FromStr, + ::Err: Display; + + impl Display for ScriptTemplate + where + Pk: MiniscriptKey + FromStr, + ::Err: Display, + { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + for instruction in &self.0 { + Display::fmt(instruction, f)?; + } + Ok(()) + } + } + + impl ScriptTemplate + where + Pk: MiniscriptKey + DerivePublicKey + FromStr, + ::Err: Display, + { + pub fn translate_pk( + &self, + ctx: &Secp256k1, + pat: impl AsRef<[UnhardenedIndex]>, + ) -> Result, DerivePatternError> { + let pat = pat.as_ref(); + Ok(self + .0 + .iter() + .map(|op| op.translate_pk(ctx, pat)) + .collect::, _>>()? + .into()) + } + } + + impl From> for Script { + fn from(template: ScriptTemplate) -> Self { + let mut builder = Builder::new(); + for op in template.into_inner() { + builder = match op { + OpcodeTemplate::OpCode(code) => builder.push_opcode(opcodes::All::from(code)), + OpcodeTemplate::Data(data) => builder.push_slice(&data), + OpcodeTemplate::Key(key) => builder.push_key(&key), + }; + } + builder.into_script() } - builder.into_script() } } diff --git a/hd/src/account.rs b/hd/src/account.rs index 17cb5ef..e03a8e0 100644 --- a/hd/src/account.rs +++ b/hd/src/account.rs @@ -69,7 +69,7 @@ pub trait DerivePublicKey { /// HD wallet account guaranteeing key derivation without access to the /// private keys. #[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)] -#[derive(StrictEncode, StrictDecode)] +#[cfg_attr(feature = "strict_encoding", derive(StrictEncode, StrictDecode))] pub struct DerivationAccount { /// Reference to the extended master public key, if known pub master: XpubRef, diff --git a/hd/src/indexes.rs b/hd/src/indexes.rs index 4ceb2bc..53e93ab 100644 --- a/hd/src/indexes.rs +++ b/hd/src/indexes.rs @@ -738,7 +738,7 @@ impl TryFrom for HardenedIndex { serde(crate = "serde_crate", rename_all = "camelCase") )] #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Display, From)] -#[derive(StrictEncode, StrictDecode)] +#[cfg_attr(feature = "strict_encoding", derive(StrictEncode, StrictDecode))] pub enum TerminalStep { /// Specific unhardened index #[from] diff --git a/hd/src/path.rs b/hd/src/path.rs index 6e47e85..6bb865e 100644 --- a/hd/src/path.rs +++ b/hd/src/path.rs @@ -12,10 +12,12 @@ use core::fmt::{self, Display, Formatter}; use core::str::FromStr; use std::borrow::{Borrow, BorrowMut}; +#[cfg(feature = "strict_encoding")] use std::io; use std::ops::{Deref, DerefMut}; use bitcoin::util::bip32; +#[cfg(feature = "strict_encoding")] use strict_encoding::{self, StrictDecode, StrictEncode}; use crate::SegmentIndexes; @@ -99,16 +101,18 @@ where fn borrow_mut(&mut self) -> &mut [Segment] { &mut self.0 } } +#[cfg(feature = "strict_encoding")] impl StrictEncode for DerivationSubpath where Segment: SegmentIndexes + StrictEncode, { #[inline] - fn strict_encode(&self, e: E) -> Result { - self.0.strict_encode(e) + fn strict_encoding(&self, e: E) -> Result { + self.0.strict_encoding(e) } } +#[cfg(feature = "strict_encoding")] impl StrictDecode for DerivationSubpath where Segment: SegmentIndexes + StrictDecode, diff --git a/hd/src/ranges.rs b/hd/src/ranges.rs index cd888a3..5cbff24 100644 --- a/hd/src/ranges.rs +++ b/hd/src/ranges.rs @@ -12,12 +12,14 @@ use std::cmp::Ordering; use std::collections::BTreeSet; use std::fmt::{self, Display, Formatter}; +#[cfg(feature = "strict_encoding")] use std::io; use std::ops::RangeInclusive; use std::str::FromStr; use amplify::Wrapper; use bitcoin::util::bip32; +#[cfg(feature = "strict_encoding")] use strict_encoding::{self, StrictDecode, StrictEncode}; use crate::SegmentIndexes; @@ -156,17 +158,19 @@ where fn is_hardened(&self) -> bool { self.first_range().is_hardened() } } +#[cfg(feature = "strict_encoding")] impl StrictEncode for IndexRangeList where Index: SegmentIndexes + StrictEncode, BTreeSet>: StrictEncode, { #[inline] - fn strict_encode(&self, e: E) -> Result { - self.0.strict_encode(e) + fn strict_encoding(&self, e: E) -> Result { + self.0.strict_encoding(e) } } +#[cfg(feature = "strict_encoding")] impl StrictDecode for IndexRangeList where Index: SegmentIndexes + StrictDecode, @@ -425,15 +429,17 @@ where } } +#[cfg(feature = "strict_encoding")] impl StrictEncode for IndexRange where Index: SegmentIndexes + StrictEncode, { - fn strict_encode(&self, mut e: E) -> Result { - Ok(strict_encode_list!(e; self.first_index(), self.last_index())) + fn strict_encoding(&self, mut e: E) -> Result { + Ok(strict_encoding_list!(e; self.first_index(), self.last_index())) } } +#[cfg(feature = "strict_encoding")] impl StrictDecode for IndexRange where Index: SegmentIndexes + StrictDecode, diff --git a/psbt/Cargo.toml b/psbt/Cargo.toml index 262209b..babe4c9 100644 --- a/psbt/Cargo.toml +++ b/psbt/Cargo.toml @@ -15,7 +15,6 @@ exclude = [] [dependencies] amplify = { workspace = true } -strict_encoding = { workspace = true } bitcoin = { workspace = true, features = ["base64"] } bitcoin_scripts = { workspace = true } bitcoin_blockchain = { workspace = true } @@ -25,6 +24,7 @@ descriptors = { workspace = true, optional = true } miniscript_crate = { workspace = true, optional = true } serde_crate = { package = "serde", version = "1", optional = true } serde_with = { version = "2.3", features = ["hex"], optional = true } +strict_encoding = { workspace = true, optional = true} [dev-dependencies] strict_encoding_test = "0.9.0" @@ -34,7 +34,8 @@ default = [] all = [ "serde", "construct", - "sign" + "sign", + "strict_encoding" ] miniscript = ["miniscript_crate"] construct = [ diff --git a/psbt/src/global.rs b/psbt/src/global.rs index 1c566ba..a95797f 100644 --- a/psbt/src/global.rs +++ b/psbt/src/global.rs @@ -27,7 +27,7 @@ use crate::{raw, Error, FeeError, Input, Output, PsbtVersion, TxError}; // TODO: Do manual serde and strict encoding implementation to check the // deserialized values #[derive(Clone, Eq, PartialEq, Debug, Default)] -#[derive(StrictEncode, StrictDecode)] +#[cfg_attr(feature = "strict_encoding", derive(StrictEncode, StrictDecode))] #[cfg_attr( feature = "serde", derive(Serialize, Deserialize), diff --git a/psbt/src/input.rs b/psbt/src/input.rs index 401b027..c5febcd 100644 --- a/psbt/src/input.rs +++ b/psbt/src/input.rs @@ -31,7 +31,7 @@ use crate::{raw, InputMatchError, TxinError}; // TODO: Do manual serde implementation to check the deserialized values #[derive(Clone, Eq, PartialEq, Debug, Default)] -#[derive(StrictEncode, StrictDecode)] +#[cfg_attr(feature = "strict_encoding", derive(StrictEncode, StrictDecode))] #[cfg_attr( feature = "serde", derive(Serialize, Deserialize), diff --git a/psbt/src/lib.rs b/psbt/src/lib.rs index 6e1b494..57ecac0 100644 --- a/psbt/src/lib.rs +++ b/psbt/src/lib.rs @@ -30,6 +30,7 @@ extern crate amplify; #[cfg(feature = "serde")] #[macro_use] extern crate serde_crate as serde; +#[cfg(feature = "strict_encoding")] #[macro_use] extern crate strict_encoding; #[cfg(feature = "miniscript")] @@ -66,8 +67,11 @@ pub use proprietary::{ /// Version of the PSBT (V0 stands for BIP174-defined version; V2 - for BIP370). #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Default)] -#[derive(StrictEncode, StrictDecode)] -#[strict_encoding(repr = u32)] +#[cfg_attr( + feature = "strict_encoding", + derive(StrictEncode, StrictDecode), + strict_encoding(repr = u32), +)] #[cfg_attr( feature = "serde", derive(Serialize, Deserialize), diff --git a/psbt/src/output.rs b/psbt/src/output.rs index 4cdbd28..75fabeb 100644 --- a/psbt/src/output.rs +++ b/psbt/src/output.rs @@ -24,7 +24,7 @@ use crate::v0::OutputV0; // TODO: Do manual serde implementation to check the deserialized values #[derive(Clone, Eq, PartialEq, Debug, Default)] -#[derive(StrictEncode, StrictDecode)] +#[cfg_attr(feature = "strict_encoding", derive(StrictEncode, StrictDecode))] #[cfg_attr( feature = "serde", derive(Serialize, Deserialize), diff --git a/psbt/src/proprietary.rs b/psbt/src/proprietary.rs index d48387f..11adef7 100644 --- a/psbt/src/proprietary.rs +++ b/psbt/src/proprietary.rs @@ -37,9 +37,9 @@ pub enum ProprietaryKeyError { /// parts: /// 1) key location, in form of `input(no)`, `output(no)`, or `global`; /// 2) key type, in form of `prefix(no)`; - /// 3) key-value pair, in form of `key:value`, where both key and value - /// must be hexadecimal bytestrings; one of them may be omitted - /// (for instance, `:value` or `key:`). + /// 3) key-value pair, in form of `key:value`, where both key and value must + /// be hexadecimal bytestrings; one of them may be omitted (for instance, + /// `:value` or `key:`). /// /// If the proprietary key does not have associated data, the third part of /// the descriptor must be fully omitted. diff --git a/psbt/src/sign/signer.rs b/psbt/src/sign/signer.rs index 6092c9b..cc98b56 100644 --- a/psbt/src/sign/signer.rs +++ b/psbt/src/sign/signer.rs @@ -340,7 +340,7 @@ impl Input { let spent_value = prevout.value; // Check script_pubkey match and requirements - let script_pubkey = PubkeyScript::from_inner(prevout.script_pubkey.clone()); + let script_pubkey = PubkeyScript::from(prevout.script_pubkey.clone()); let witness_script = self.witness_script.as_ref(); let redeem_script = self.redeem_script.as_ref(); @@ -441,7 +441,7 @@ impl Input { let index = self.index(); // Check script_pubkey match - let script_pubkey = PubkeyScript::from_inner(self.input_prevout()?.script_pubkey.clone()); + let script_pubkey = PubkeyScript::from(self.input_prevout()?.script_pubkey.clone()); if let Some(internal_key) = self.tap_internal_key { if script_pubkey != Script::new_v1_p2tr(provider.secp_context(), internal_key, self.tap_merkle_root) diff --git a/slip132/src/lib.rs b/slip132/src/lib.rs index 4e29a82..f7d97ee 100644 --- a/slip132/src/lib.rs +++ b/slip132/src/lib.rs @@ -19,8 +19,7 @@ #[macro_use] extern crate amplify; #[cfg(feature = "strict_encoding")] -#[macro_use] -extern crate strict_encoding; +use strict_encoding::{self, StrictDecode, StrictEncode}; #[cfg(feature = "serde")] #[macro_use]