diff --git a/Cargo.lock b/Cargo.lock index 040fd424..c6e11c73 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -132,6 +132,7 @@ name = "concurrent-psbt" version = "0.1.0" dependencies = [ "bitcoin", + "pastey", "proptest", "psbt-v2", "rand", @@ -249,6 +250,12 @@ version = "1.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" +[[package]] +name = "pastey" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ee67f1008b1ba2321834326597b8e186293b049a023cdef258527550b9935b4" + [[package]] name = "ppv-lite86" version = "0.2.21" diff --git a/crates/concurrent-psbt/Cargo.toml b/crates/concurrent-psbt/Cargo.toml index d68f8317..509439fd 100644 --- a/crates/concurrent-psbt/Cargo.toml +++ b/crates/concurrent-psbt/Cargo.toml @@ -10,6 +10,7 @@ prop-tests = [] [dependencies] bitcoin = "0.32" +pastey = "0.2.3" psbt-v2 = "0.3.0" rand = "0.9" diff --git a/crates/concurrent-psbt/src/psbt/conflict_macro.rs b/crates/concurrent-psbt/src/psbt/conflict_macro.rs deleted file mode 100644 index c28266a9..00000000 --- a/crates/concurrent-psbt/src/psbt/conflict_macro.rs +++ /dev/null @@ -1,60 +0,0 @@ -//! Generates typed conflict enums for the external PSBT types. -//! -//! Temporary until `joinable_struct!` defines the source structs and generates -//! their conflict API from the same field list. - -macro_rules! record_conflicts { - ( - $Result:ident, $Conflict:ident, $Field:ident { - $( $Variant:ident => $field:ident $( [ $Key:ty ] )? : $Value:ty ),+ $(,)? - } - ) => { - #[derive(Debug, Clone, Copy, PartialEq, Eq)] - pub enum $Field<'a> { - $( $Variant $( (&'a $Key) )?, )+ - } - - #[derive(Debug, Clone, Copy)] - pub enum $Conflict<'a> { - $( $Variant { $( key: &'a $Key, )? values: &'a $crate::Conflict<$Value> }, )+ - } - - impl<'a> $Conflict<'a> { - pub fn path(&self) -> $Field<'a> { - match self { - $( Self::$Variant { $( key: record_conflicts!(@key $Key, key), )? .. } => { - record_conflicts!(@path $Field $Variant key $( [ $Key ] )?) - }, )+ - } - } - } - - impl $Result { - /// Borrow each conflicted field, retaining its keydata and value type. - pub fn conflicts(&self) -> impl Iterator> { - // Exhaustive destructuring requires a mapping for every field. - let Self { $( $field, )+ } = self; - std::iter::empty() - $( .chain($crate::lattice::conflicts::Conflicts::conflicts($field) - .map(record_conflicts!(@item $Conflict $Variant $( [ $Key ] )?))) )+ - } - } - - impl $crate::lattice::conflicts::Conflicts for $Result { - type Item<'a> = $Conflict<'a>; - - fn conflicts(&self) -> impl Iterator> { - Self::conflicts(self) - } - } - }; - (@key $Key:ty, $key:ident) => { $key }; - (@path $Field:ident $Variant:ident $key:ident [ $Key:ty ]) => { $Field::$Variant(*$key) }; - (@path $Field:ident $Variant:ident $key:ident) => { $Field::$Variant }; - (@item $Conflict:ident $Variant:ident [ $Key:ty ]) => { - |(key, values)| $Conflict::$Variant { key, values } - }; - (@item $Conflict:ident $Variant:ident) => { - |values| $Conflict::$Variant { values } - }; -} diff --git a/crates/concurrent-psbt/src/psbt/global.rs b/crates/concurrent-psbt/src/psbt/global.rs index 1fbf8d95..8fed465f 100644 --- a/crates/concurrent-psbt/src/psbt/global.rs +++ b/crates/concurrent-psbt/src/psbt/global.rs @@ -1,10 +1,13 @@ #![allow(clippy::result_large_err)] - -//! PSBT global field merging. +#![cfg_attr( + not(test), + expect(dead_code, reason = "global serialization is used by UnorderedPsbt") +)] +//! PSBT global fields and their merging. //! -//! [`ResultGlobal`] wraps each global field in the internal result domain, -//! enabling field-by-field merging that accumulates conflicts without short-circuiting. -//! [`GlobalExt::wrap`] lifts a clean [`Global`] into the result domain. +//! [`Global`] holds the global fields of a PSBT, and [`ResultGlobal`] wraps +//! each of them in the internal result domain, enabling field-by-field merging +//! that accumulates conflicts without short-circuiting. use std::collections::BTreeMap; @@ -12,40 +15,57 @@ use bitcoin::bip32::{KeySource, Xpub}; use bitcoin::locktime::absolute; use bitcoin::transaction; -use psbt_v2::Version; use psbt_v2::raw; -pub use psbt_v2::v2::Global; joinable_struct! { - /// Result-domain wrapper around a BIP 370 [`Global`]. + /// The global fields of a PSBT. + /// + /// Mirrors the BIP 370 global map except `PSBT_GLOBAL_VERSION`, which is + /// always 2, and the input and output counts, which are filled in when + /// converting to a BIP 370 PSBT. + source: Global, + /// Result-domain counterpart of [`Global`]. /// - /// Produced by joining two [`Global`] values via [`GlobalExt::wrap`]. + /// Produced by joining [`Global`] values lifted with [`Global::wrap`]. /// Use [`ResultGlobal::is_ok`] to check for conflicts and /// [`ResultGlobal::try_unwrap`] to extract. - source: Global, result: ResultGlobal, - conflicts: GlobalConflict, GlobalField { - Version => version: Version, - InputCount => input_count: usize, - OutputCount => output_count: usize, - TxVersion => tx_version: transaction::Version, - FallbackLockTime => fallback_lock_time: absolute::LockTime, - TxModifiableFlags => tx_modifiable_flags: u8, - Xpub => xpubs[Xpub]: KeySource, - Proprietary => proprietaries[raw::ProprietaryKey]: Vec, - Unknown => unknowns[raw::Key]: Vec, - }, - ext: GlobalExt, + conflicts: GlobalConflict, GlobalField, + v2: psbt_v2::v2::Global, fields: { - version: Version, - tx_version: transaction::Version, - fallback_lock_time: Option, - tx_modifiable_flags: u8, - input_count: usize, - output_count: usize, - xpubs: BTreeMap, - proprietaries: BTreeMap>, - unknowns: BTreeMap>, + tx_version: transaction::Version => TxVersion, + fallback_lock_time: Option => FallbackLockTime(absolute::LockTime), + tx_modifiable_flags: u8 => TxModifiableFlags, + xpubs: BTreeMap => Xpub[Xpub](KeySource), + proprietaries: BTreeMap> => Proprietary[raw::ProprietaryKey](Vec), + unknowns: BTreeMap> => Unknown[raw::Key](Vec), + }, + proprietary: {}, +} + +impl Default for Global { + fn default() -> Self { + Self::from_v2_fields(psbt_v2::v2::Global::default()) + } +} + +/// Drops the version and the input and output counts. +impl From for Global { + fn from(global: psbt_v2::v2::Global) -> Self { + let Ok(global) = Self::decode_v2::(global); + global + } +} + +impl Global { + /// Convert to a BIP 370 global map for a PSBT with `input_count` inputs + /// and `output_count` outputs. + pub(crate) fn into_v2(self, input_count: usize, output_count: usize) -> psbt_v2::v2::Global { + self.encode_v2(psbt_v2::v2::Global { + input_count, + output_count, + ..psbt_v2::v2::Global::default() + }) } } @@ -70,6 +90,14 @@ mod tests { assert!(joined.try_unwrap().is_err()); } + #[test] + fn default_is_an_empty_v2_global() { + assert_eq!( + Global::default().into_v2(0, 0), + psbt_v2::v2::Global::default() + ); + } + #[test] fn conflicts_identify_the_field() { let mut a = Global::default(); @@ -87,6 +115,9 @@ mod tests { #[cfg(feature = "prop-tests")] mod prop { use super::*; + use bitcoin::Network; + use bitcoin::bip32::{DerivationPath, Xpriv}; + use bitcoin::secp256k1::Secp256k1; use proptest::prelude::*; fn arb_proprietary_key() -> impl Strategy { @@ -102,13 +133,19 @@ mod tests { }) } + /// The master xpub of `seed`, with its own fingerprint as key source. + fn xpub_from_seed(seed: u8) -> (Xpub, KeySource) { + let xpriv = Xpriv::new_master(Network::Bitcoin, &[seed; 16]).expect("valid seed"); + let xpub = Xpub::from_priv(&Secp256k1::new(), &xpriv); + (xpub, (xpub.fingerprint(), DerivationPath::master())) + } + fn arb_global() -> impl Strategy { ( proptest::bool::ANY, 0u8..4, proptest::bool::ANY, - 0usize..4, - 0usize..4, + proptest::option::of(0u8..2), proptest::collection::btree_map( arb_proprietary_key(), proptest::collection::vec(0u8..=255, 0..=8), @@ -122,33 +159,21 @@ mod tests { ), ) .prop_map( - |( - use_v1, - flags, - has_lock_time, - in_count, - out_count, + |(use_v1, flags, has_lock_time, xpub, proprietaries, unknowns)| Global { + tx_version: if use_v1 { + transaction::Version::ONE + } else { + transaction::Version::TWO + }, + tx_modifiable_flags: flags, + fallback_lock_time: if has_lock_time { + Some(absolute::LockTime::from_consensus(500_000)) + } else { + None + }, + xpubs: xpub.map(xpub_from_seed).into_iter().collect(), proprietaries, unknowns, - )| { - Global { - tx_version: if use_v1 { - transaction::Version::ONE - } else { - transaction::Version::TWO - }, - tx_modifiable_flags: flags, - fallback_lock_time: if has_lock_time { - Some(absolute::LockTime::from_consensus(500_000)) - } else { - None - }, - input_count: in_count, - output_count: out_count, - proprietaries, - unknowns, - ..Global::default() - } }, ) } @@ -170,8 +195,21 @@ mod tests { prop_assert_eq!(unwrapped.wrap(), wrapped); } + #[test] + fn v2_roundtrip_derives_version_and_counts( + a in arb_global(), + input_count in 0usize..4, + output_count in 0usize..4, + ) { + let v2 = a.clone().into_v2(input_count, output_count); + prop_assert_eq!(v2.version, psbt_v2::Version::TWO); + prop_assert_eq!((v2.input_count, v2.output_count), (input_count, output_count)); + prop_assert_eq!(Global::from(v2), a); + } + #[test] fn is_ok_consistency(a in arb_result_global()) { + prop_assert_eq!(a.conflicts().next().is_none(), a.is_ok()); if a.is_ok() { prop_assert!(a.try_unwrap().is_ok()); } else { diff --git a/crates/concurrent-psbt/src/psbt/input.rs b/crates/concurrent-psbt/src/psbt/input.rs index 8e138126..bb4d9b1d 100644 --- a/crates/concurrent-psbt/src/psbt/input.rs +++ b/crates/concurrent-psbt/src/psbt/input.rs @@ -1,5 +1,10 @@ #![allow(clippy::result_large_err)] +//! PSBT input fields. +//! +//! [`ResultInput`] joins fields independently, retaining incompatible values +//! as conflicts. + use std::collections::BTreeMap; use bitcoin::bip32::KeySource; @@ -8,86 +13,73 @@ use bitcoin::key::{PublicKey, XOnlyPublicKey}; use bitcoin::locktime::absolute; use bitcoin::taproot::{ControlBlock, LeafVersion, TapLeafHash, TapNodeHash}; use bitcoin::{OutPoint, ScriptBuf, Sequence, Transaction, TxOut, Txid, Witness, ecdsa, taproot}; +use psbt_v2::PsbtSighashType; use psbt_v2::raw; -use psbt_v2::{PsbtSighashType, v2::Input}; joinable_struct! { - /// Result-domain representation of a PSBT input. - /// - /// Each field mirrors its counterpart in [`psbt_v2::v2::Input`] but is - /// represented in the internal result domain, recording either a clean value - /// or an accumulated conflict. - /// Use [`InputExt::wrap`] to lift a clean [`psbt_v2::v2::Input`] into this type, and - /// [`ResultInput::try_unwrap`] to recover the clean value once all conflicts are resolved. + /// A PSBT input, mirroring the BIP 370 per-input map. source: Input, + /// Result-domain counterpart of [`Input`]. + /// + /// Each field holds a clean value or a conflict. [`Input::wrap`] lifts an + /// [`Input`] into this type, and [`ResultInput::try_unwrap`] returns it if + /// no field conflicts. result: ResultInput, - conflicts: InputConflict, InputField { - PreviousTxid => previous_txid: Txid, - SpentOutputIndex => spent_output_index: u32, - Sequence => sequence: Sequence, - MinTime => min_time: absolute::Time, - MinHeight => min_height: absolute::Height, - NonWitnessUtxo => non_witness_utxo: Transaction, - WitnessUtxo => witness_utxo: TxOut, - PartialSig => partial_sigs[PublicKey]: ecdsa::Signature, - SighashType => sighash_type: PsbtSighashType, - RedeemScript => redeem_script: ScriptBuf, - WitnessScript => witness_script: ScriptBuf, - Bip32Derivation => bip32_derivations[PublicKey]: KeySource, - FinalScriptSig => final_script_sig: ScriptBuf, - FinalScriptWitness => final_script_witness: Witness, - Ripemd160Preimage => ripemd160_preimages[ripemd160::Hash]: Vec, - Sha256Preimage => sha256_preimages[sha256::Hash]: Vec, - Hash160Preimage => hash160_preimages[hash160::Hash]: Vec, - Hash256Preimage => hash256_preimages[sha256d::Hash]: Vec, - TapKeySig => tap_key_sig: taproot::Signature, - TapScriptSig => tap_script_sigs[(XOnlyPublicKey, TapLeafHash)]: taproot::Signature, - TapScript => tap_scripts[ControlBlock]: (ScriptBuf, LeafVersion), - TapKeyOrigin => tap_key_origins[XOnlyPublicKey]: (Vec, KeySource), - TapInternalKey => tap_internal_key: XOnlyPublicKey, - TapMerkleRoot => tap_merkle_root: TapNodeHash, - Proprietary => proprietaries[raw::ProprietaryKey]: Vec, - Unknown => unknowns[raw::Key]: Vec, - }, - ext: InputExt, + conflicts: InputConflict, InputField, + v2: psbt_v2::v2::Input, fields: { - previous_txid: Txid, - spent_output_index: u32, - sequence: Option, - min_time: Option, - min_height: Option, - non_witness_utxo: Option, - witness_utxo: Option, - partial_sigs: BTreeMap, - sighash_type: Option, - redeem_script: Option, - witness_script: Option, - bip32_derivations: BTreeMap, - final_script_sig: Option, - final_script_witness: Option, - ripemd160_preimages: BTreeMap>, - sha256_preimages: BTreeMap>, - hash160_preimages: BTreeMap>, - hash256_preimages: BTreeMap>, - tap_key_sig: Option, - tap_script_sigs: BTreeMap<(XOnlyPublicKey, TapLeafHash), taproot::Signature>, - tap_scripts: BTreeMap, - tap_key_origins: BTreeMap, KeySource)>, - tap_internal_key: Option, - tap_merkle_root: Option, - proprietaries: BTreeMap>, - unknowns: BTreeMap>, + previous_txid: Txid => PreviousTxid, + spent_output_index: u32 => SpentOutputIndex, + sequence: Option => Sequence(Sequence), + min_time: Option => MinTime(absolute::Time), + min_height: Option => MinHeight(absolute::Height), + non_witness_utxo: Option => NonWitnessUtxo(Transaction), + witness_utxo: Option => WitnessUtxo(TxOut), + partial_sigs: BTreeMap => PartialSig[PublicKey](ecdsa::Signature), + sighash_type: Option => SighashType(PsbtSighashType), + redeem_script: Option => RedeemScript(ScriptBuf), + witness_script: Option => WitnessScript(ScriptBuf), + bip32_derivations: BTreeMap => Bip32Derivation[PublicKey](KeySource), + final_script_sig: Option => FinalScriptSig(ScriptBuf), + final_script_witness: Option => FinalScriptWitness(Witness), + ripemd160_preimages: BTreeMap> => Ripemd160Preimage[ripemd160::Hash](Vec), + sha256_preimages: BTreeMap> => Sha256Preimage[sha256::Hash](Vec), + hash160_preimages: BTreeMap> => Hash160Preimage[hash160::Hash](Vec), + hash256_preimages: BTreeMap> => Hash256Preimage[sha256d::Hash](Vec), + tap_key_sig: Option => TapKeySig(taproot::Signature), + tap_script_sigs: BTreeMap<(XOnlyPublicKey, TapLeafHash), taproot::Signature> => TapScriptSig[(XOnlyPublicKey, TapLeafHash)](taproot::Signature), + tap_scripts: BTreeMap => TapScript[ControlBlock]((ScriptBuf, LeafVersion)), + tap_key_origins: BTreeMap, KeySource)> => TapKeyOrigin[XOnlyPublicKey]((Vec, KeySource)), + tap_internal_key: Option => TapInternalKey(XOnlyPublicKey), + tap_merkle_root: Option => TapMerkleRoot(TapNodeHash), + proprietaries: BTreeMap> => Proprietary[raw::ProprietaryKey](Vec), + unknowns: BTreeMap> => Unknown[raw::Key](Vec), + }, + proprietary: {}, +} + +impl Input { + /// Create an input spending `outpoint`, with every other field unset. + pub fn new(outpoint: &OutPoint) -> Self { + Self::from_v2_fields(psbt_v2::v2::Input::new(outpoint)) + } +} + +impl From for Input { + fn from(input: psbt_v2::v2::Input) -> Self { + let Ok(input) = Self::decode_v2::(input); + input + } +} + +impl From for psbt_v2::v2::Input { + fn from(input: Input) -> Self { + let base = psbt_v2::v2::Input::new(&out_point(&input)); + input.encode_v2(base) } } /// Extract the [`OutPoint`] this input spends (previous txid + output index). -#[cfg_attr( - not(all(test, feature = "unit-tests")), - expect( - dead_code, - reason = "used by InputSet once inputs are keyed by spent outpoint" - ) -)] #[cfg_attr(coverage_nightly, coverage(off))] pub(crate) fn out_point(input: &Input) -> OutPoint { OutPoint { @@ -98,16 +90,20 @@ pub(crate) fn out_point(input: &Input) -> OutPoint { #[cfg(test)] #[cfg_attr(coverage_nightly, coverage(off))] -mod tests { +pub(crate) mod tests { use super::*; use crate::lattice::join::Join; #[cfg(feature = "unit-tests")] - mod unit { + pub(crate) mod unit { use super::*; + use bitcoin::bip32::{DerivationPath, Fingerprint}; use bitcoin::hashes::Hash; + use bitcoin::secp256k1::{Keypair, Message, Secp256k1, SecretKey}; + use bitcoin::sighash::TapSighashType; + use std::collections::BTreeMap; - fn make_input(txid_byte: u8, vout: u32) -> Input { + pub(crate) fn make_input(txid_byte: u8, vout: u32) -> Input { let txid = bitcoin::Txid::from_byte_array([txid_byte; 32]); Input::new(&bitcoin::OutPoint { txid, vout }) } @@ -115,11 +111,145 @@ mod tests { #[test] fn inputs_spending_different_outpoints_conflict() { let joined = make_input(1, 0).wrap().join(make_input(2, 0).wrap()); - let fields: Vec<_> = joined.conflicts().map(|c| c.path()).collect(); - assert_eq!(fields, [InputField::PreviousTxid]); + assert_eq!( + joined.conflicts().map(|c| c.path()).collect::>(), + [InputField::PreviousTxid] + ); assert_eq!(joined.clone().try_unwrap(), Err(joined)); } + /// An input with every field set, so a conversion that drops one + /// changes the input. + fn populated_input() -> Input { + let secp = Secp256k1::new(); + let secret_key = SecretKey::from_slice(&[1; 32]).expect("valid secret key"); + let keypair = Keypair::from_secret_key(&secp, &secret_key); + let public_key = PublicKey::new(secret_key.public_key(&secp)); + let (x_only_public_key, _) = keypair.x_only_public_key(); + let message = Message::from_digest([2; 32]); + let ecdsa_signature = + ecdsa::Signature::sighash_all(secp.sign_ecdsa(&message, &secret_key)); + let taproot_signature = taproot::Signature { + signature: secp.sign_schnorr_no_aux_rand(&message, &keypair), + sighash_type: TapSighashType::Default, + }; + let leaf = TapLeafHash::from_byte_array([3; 32]); + let source = (Fingerprint::from([4; 4]), DerivationPath::master()); + let mut control_block = vec![0xc0]; + control_block.extend(x_only_public_key.serialize()); + let control_block = ControlBlock::decode(&control_block).expect("valid control block"); + let script = ScriptBuf::from_bytes(vec![5]); + Input { + sequence: Some(Sequence(6)), + min_time: Some(absolute::Time::MIN), + min_height: Some(absolute::Height::MIN), + non_witness_utxo: Some(Transaction { + version: bitcoin::transaction::Version::TWO, + lock_time: absolute::LockTime::ZERO, + input: vec![], + output: vec![], + }), + witness_utxo: Some(TxOut { + value: bitcoin::Amount::from_sat(7), + script_pubkey: script.clone(), + }), + partial_sigs: BTreeMap::from([(public_key, ecdsa_signature)]), + sighash_type: Some(PsbtSighashType::from_u32(1)), + redeem_script: Some(script.clone()), + witness_script: Some(script.clone()), + bip32_derivations: BTreeMap::from([(public_key, source.clone())]), + final_script_sig: Some(script.clone()), + final_script_witness: Some(Witness::from_slice(&[[8]])), + ripemd160_preimages: BTreeMap::from([( + ripemd160::Hash::from_byte_array([9; 20]), + vec![9], + )]), + sha256_preimages: BTreeMap::from([( + sha256::Hash::from_byte_array([10; 32]), + vec![10], + )]), + hash160_preimages: BTreeMap::from([( + hash160::Hash::from_byte_array([11; 20]), + vec![11], + )]), + hash256_preimages: BTreeMap::from([( + sha256d::Hash::from_byte_array([12; 32]), + vec![12], + )]), + tap_key_sig: Some(taproot_signature), + tap_script_sigs: BTreeMap::from([((x_only_public_key, leaf), taproot_signature)]), + tap_scripts: BTreeMap::from([(control_block, (script, LeafVersion::TapScript))]), + tap_key_origins: BTreeMap::from([(x_only_public_key, (vec![leaf], source))]), + tap_internal_key: Some(x_only_public_key), + tap_merkle_root: Some(TapNodeHash::from_byte_array([13; 32])), + proprietaries: BTreeMap::from([( + raw::ProprietaryKey { + prefix: vec![14], + subtype: 15, + key: vec![], + }, + vec![16], + )]), + unknowns: BTreeMap::from([( + raw::Key { + type_value: 0xfe, + key: vec![17], + }, + vec![18], + )]), + ..make_input(19, 20) + } + } + + #[test] + fn compound_and_proprietary_keydata_identify_conflicts() { + let input = populated_input(); + let mut other = input.clone(); + let tap_key = *input.tap_script_sigs.keys().next().unwrap(); + other + .tap_script_sigs + .get_mut(&tap_key) + .unwrap() + .sighash_type = TapSighashType::All; + let proprietary_key = input.proprietaries.keys().next().unwrap().clone(); + other + .proprietaries + .get_mut(&proprietary_key) + .unwrap() + .push(99); + let joined = input.clone().wrap().join(other.wrap()); + let conflicts: Vec<_> = joined.conflicts().collect(); + assert_eq!( + conflicts.iter().map(|c| c.path()).collect::>(), + [ + InputField::TapScriptSig(&tap_key), + InputField::Proprietary(&proprietary_key), + ] + ); + let InputConflict::TapScriptSig { key, values } = conflicts[0] else { + panic!("taproot signature"); + }; + assert_eq!(key, &tap_key); + assert_eq!( + values + .into_iter() + .map(|signature| signature.sighash_type) + .collect::>(), + [TapSighashType::Default, TapSighashType::All] + ); + let InputConflict::Proprietary { key, values } = conflicts[1] else { + panic!("proprietary field"); + }; + assert_eq!(key, &proprietary_key); + assert_eq!(values.into_iter().next(), input.proprietaries.get(key)); + } + + #[test] + fn v2_conversion_round_trips_a_populated_input() { + let input = populated_input(); + assert_eq!(Input::from(psbt_v2::v2::Input::from(input.clone())), input); + } + #[test] fn out_point_extraction() { let op = out_point(&make_input(1, 42)); @@ -129,58 +259,94 @@ mod tests { } #[cfg(feature = "prop-tests")] - mod prop { + pub(crate) mod prop { use super::*; use bitcoin::hashes::Hash; use proptest::prelude::*; - fn arb_outpoint() -> impl Strategy { + // Small domain for outpoints to ensure collisions + pub(crate) fn arb_outpoint() -> impl Strategy { (0u8..3, 0u32..2).prop_map(|(txid_byte, vout)| OutPoint { txid: Txid::from_byte_array([txid_byte; 32]), vout, }) } - fn arb_input() -> impl Strategy { - ( - arb_outpoint(), - proptest::option::of(0u32..8), - proptest::option::of(1u64..10_000), - proptest::option::of(proptest::collection::vec(0u8..255, 0..=8)), - proptest::option::of(proptest::collection::vec(0u8..255, 0..=8)), - ) - .prop_map(|(op, seq, utxo_val, redeem, witness)| Input { - sequence: seq.map(Sequence), - witness_utxo: utxo_val.map(|v| TxOut { - value: bitcoin::Amount::from_sat(v), - script_pubkey: ScriptBuf::new(), - }), - redeem_script: redeem.map(ScriptBuf::from_bytes), - witness_script: witness.map(ScriptBuf::from_bytes), - ..Input::new(&op) - }) + pub(crate) fn arb_input() -> impl Strategy { + arb_outpoint().prop_map(|op| Input::new(&op)) } - fn arb_result_input() -> impl Strategy { + /// Wrapped inputs, and joins of two, drawn with optional and map fields + /// populated so the join laws exercise every field category. + pub(crate) fn arb_result_input() -> impl Strategy { prop_oneof![ - arb_input().prop_map(|i| i.wrap()), - (arb_input(), arb_input()).prop_map(|(a, b)| a.wrap().join(b.wrap())), + arb_input_with_fields().prop_map(|i| i.wrap()), + (arb_input_with_fields(), arb_input_with_fields()) + .prop_map(|(a, b)| a.wrap().join(b.wrap())), ] } - assert_join_laws!(arb_result_input()); + // An input with some optional and proprietary fields populated. + pub(crate) fn arb_input_with_fields() -> impl Strategy { + ( + arb_outpoint(), + proptest::option::of(0u32..8), // sequence + proptest::option::of(1u64..10_000), // witness_utxo value (sats) + proptest::option::of(proptest::collection::vec(0u8..255, 0..=8)), // redeem_script + proptest::option::of(proptest::collection::vec(0u8..255, 0..=8)), // witness_script + proptest::option::of(proptest::collection::vec(0u8..255, 0..=8)), // final_script_sig + proptest::collection::btree_map( + proptest::collection::vec(0u8..255, 1..=4), + proptest::collection::vec(0u8..255, 0..=4), + 0..=2, + ), // proprietaries + ) + .prop_map( + |(op, seq, utxo_val, redeem, witness_s, final_sig, props)| { + let mut input = Input { + sequence: seq.map(Sequence), + witness_utxo: utxo_val.map(|v| TxOut { + value: bitcoin::Amount::from_sat(v), + script_pubkey: ScriptBuf::new(), + }), + redeem_script: redeem.map(ScriptBuf::from_bytes), + witness_script: witness_s.map(ScriptBuf::from_bytes), + final_script_sig: final_sig.map(ScriptBuf::from_bytes), + ..Input::new(&op) + }; + for (k, v) in props { + input.proprietaries.insert( + raw::ProprietaryKey { + prefix: b"test".to_vec(), + subtype: k[0], + key: k, + }, + v, + ); + } + input + }, + ) + } proptest! { #[test] - fn wrap_try_unwrap_roundtrip(i in arb_input()) { + fn wrap_try_unwrap_roundtrip(i in prop_oneof![arb_input(), arb_input_with_fields()]) { prop_assert_eq!(i.clone().wrap().try_unwrap(), Ok(i)); } + #[test] + fn v2_roundtrip(i in arb_input_with_fields()) { + prop_assert_eq!(Input::from(psbt_v2::v2::Input::from(i.clone())), i); + } + #[test] fn is_ok_iff_try_unwrap_succeeds(r in arb_result_input()) { + prop_assert_eq!(r.conflicts().next().is_none(), r.is_ok()); prop_assert_eq!(r.is_ok(), r.try_unwrap().is_ok()); } - } + + assert_join_laws!(arb_result_input()); } } diff --git a/crates/concurrent-psbt/src/psbt/joinable_struct_macro.rs b/crates/concurrent-psbt/src/psbt/joinable_struct_macro.rs index 9862b8b0..98bd75fc 100644 --- a/crates/concurrent-psbt/src/psbt/joinable_struct_macro.rs +++ b/crates/concurrent-psbt/src/psbt/joinable_struct_macro.rs @@ -1,5 +1,5 @@ -/// Generate a result-domain product type and its lattice operations from a -/// single field list using standard Rust type syntax. +/// Generate a product type, its result-domain counterpart, and their lattice +/// operations from a single field list using standard Rust type syntax. /// /// Each field's result-domain type is `::Result`, so a field /// joins the way its type [`Lift`](crate::lattice::lift::Lift)s: a @@ -7,26 +7,157 @@ /// [`JoinResult`](crate::JoinResult), and `Option`, `BTreeMap` and `HashMap` by lifting /// their contents. Paths and type aliases resolve like any other type. /// +/// Mirrored fields are private, with public getters and setters by default. +/// Prefix a field with `[accessors(pub(crate))]` to restrict its accessors. +/// Fields of `$Result` are `pub(crate)`. +/// `=> Variant` names its conflict variant. Container fields also name the +/// conflicted value type in parentheses and, for maps, the key type in brackets. +/// /// Generates: -/// 1. `$Result` struct with the lifted field types -/// 2. `$Ext` trait with `wrap` method on the source type -/// 3. `Join for $Result` -/// 4. `$Result::is_ok` and `$Result::try_unwrap` -/// 5. `Lift for $Source` and `ResultWrapped for $Result` +/// 1. `$Source` struct with the fields as written +/// 2. `$Result` struct with the lifted field types +/// 3. `$Source::wrap` +/// 4. `Join for $Result` +/// 5. `$Result::is_ok` and `$Result::try_unwrap` +/// 6. `Lift for $Source` and `ResultWrapped for $Result` +/// 7. Typed conflict paths and `Conflicts for $Result` +/// +/// Each entry in `fields` mirrors the field of the same name in the `v2` type. +/// Each entry in `proprietary` is written +/// `name: Type = Codec`, and is stored in a proprietary key of `v2` by the +/// [`ProprietaryField`](crate::psbt::proprietary::ProprietaryField) impl +/// `Codec`. The struct then has private conversions: /// -/// The field list must name at least one field: `is_ok` is generated as the -/// conjunction of the per-field checks. +/// - `from_v2_fields(v2, proprietary...)` moves the mirrored fields out of +/// `v2` and takes the proprietary fields as given; +/// - `decode_v2(v2)` decodes the proprietary fields in order, removing their +/// keys, then moves the mirrored fields; +/// - `encode_v2(self, base)` moves the mirrored fields into `base` and +/// encodes the proprietary fields into its proprietary keys. Fields of +/// `base` that are not mirrored are kept. macro_rules! joinable_struct { ( + $(#[$source_meta:meta])* + source: $Source:ident, $(#[$result_meta:meta])* + result: $Result:ident, + conflicts: $Conflict:ident, $Field:ident, + v2: $V2:path, + fields: { + $( + $(#[$field_meta:meta])* + $( [accessors($accessor_vis:vis)] )? + $field:ident : $T:ty + => $Variant:ident $( [ $Key:ty ] )? $( ( $Value:ty ) )? + ),+ $(,)? + }, + proprietary: { + $( + $(#[$proprietary_meta:meta])* $proprietary_vis:vis + $proprietary:ident : $P:ty = $Codec:ty + => $ProprietaryVariant:ident $( [ $ProprietaryKey:ty ] )? $( ( $ProprietaryValue:ty ) )? + ),* $(,)? + } $(,)? + ) => { + joinable_struct! { + $(#[$source_meta])* + source: $Source, + $(#[$result_meta])* + result: $Result, + conflicts: $Conflict, $Field, + fields: { + $( $(#[$field_meta])* $field: $T => $Variant $( [ $Key ] )? $( ( $Value ) )?, )+ + $( $(#[$proprietary_meta])* $proprietary_vis $proprietary: $P => $ProprietaryVariant $( [ $ProprietaryKey ] )? $( ( $ProprietaryValue ) )?, )* + } + } + + impl $Source { + $( joinable_struct!(@accessors $(($accessor_vis))? $field: $T); )+ + } + + impl $Source { + /// The mirrored fields of `v2`, with these proprietary fields. + fn from_v2_fields(v2: $V2, $( $proprietary: $P ),*) -> Self { + Self { + $( $field: v2.$field, )+ + $( $proprietary, )* + } + } + + /// Decode the proprietary fields of `v2`, removing their keys, and + /// move its mirrored fields. + /// + /// # Errors + /// Returns the error of the first proprietary field that does not + /// decode. + fn decode_v2(v2: $V2) -> Result + where + $( E: From<<$Codec as $crate::psbt::proprietary::ProprietaryField>::Error>, )* + { + joinable_struct!(@decode v2 [ $( $proprietary $Codec ),* ]) + } + + /// Move the mirrored fields into `base`, and encode the proprietary + /// fields into its proprietary keys. + /// + /// If a proprietary field encodes to `None`, its key is removed from + /// `base`, even if `self.proprietaries` has it. + fn encode_v2(self, mut base: $V2) -> $V2 { + $( base.$field = self.$field; )+ + $( + let key = $crate::psbt::proprietary::key( + <$Codec as $crate::psbt::proprietary::ProprietaryField>::SUBTYPE, + ); + base.proprietaries.remove(&key); + base.proprietaries.extend( + <$Codec as $crate::psbt::proprietary::ProprietaryField>::encode( + self.$proprietary, + ) + .map(|value| (key, value)), + ); + )* + base + } + } + }; + + (@decode $v2:ident []) => { + Ok(Self::from_v2_fields($v2)) + }; + + (@decode $v2:ident [ $( $proprietary:ident $Codec:ty ),+ ]) => {{ + let mut $v2 = $v2; + $( + let $proprietary = + <$Codec as $crate::psbt::proprietary::ProprietaryField>::decode( + $v2.proprietaries.remove(&$crate::psbt::proprietary::key( + <$Codec as $crate::psbt::proprietary::ProprietaryField>::SUBTYPE, + )), + )?; + )+ + Ok(Self::from_v2_fields($v2, $( $proprietary ),+)) + }}; + + ( + $(#[$source_meta:meta])* source: $Source:ident, + $(#[$result_meta:meta])* result: $Result:ident, - conflicts: $Conflict:ident, $Field:ident { $($conflict_fields:tt)* }, - ext: $Ext:ident, + conflicts: $Conflict:ident, $Field:ident, fields: { - $( $field:ident : $T:ty ),+ $(,)? + $( $(#[$field_meta:meta])* $vis:vis $field:ident : $T:ty + => $Variant:ident $( [ $Key:ty ] )? $( ( $Value:ty ) )? ),+ $(,)? } ) => { + $(#[$source_meta])* + #[derive(Debug, Clone, PartialEq, Eq)] + pub struct $Source { + $( + $(#[$field_meta])* + $vis $field: $T, + )+ + } + $(#[$result_meta])* #[derive(Debug, Clone, PartialEq)] pub struct $Result { @@ -35,7 +166,16 @@ macro_rules! joinable_struct { )+ } - pub use $crate::Lift as $Ext; + impl $Source { + #[doc = concat!( + "Lift into the result domain as a conflict-free [`", + stringify!($Result), + "`]." + )] + pub fn wrap(self) -> $Result { + $crate::lattice::lift::Lift::wrap(self) + } + } impl $crate::lattice::lift::Lift for $Source { type Result = $Result; @@ -82,8 +222,43 @@ macro_rules! joinable_struct { } } - record_conflicts! { - $Result, $Conflict, $Field { $($conflict_fields)* } + #[derive(Debug, Clone, Copy, PartialEq, Eq)] + pub enum $Field<'a> { + $( $Variant $( (&'a $Key) )?, )+ + } + + #[derive(Debug, Clone, Copy)] + pub enum $Conflict<'a> { + $( $Variant { $( key: &'a $Key, )? values: &'a $crate::Conflict }, )+ + } + + impl<'a> $Conflict<'a> { + pub fn path(&self) -> $Field<'a> { + match self { + $( Self::$Variant { $( key: joinable_struct!(@conflict_key $Key, key), )? .. } => { + joinable_struct!(@conflict_path $Field $Variant key $( [ $Key ] )?) + }, )+ + } + } + } + + impl $Result { + /// Borrow each conflicted field, retaining its keydata and value type. + pub fn conflicts(&self) -> impl Iterator> { + // Exhaustive destructuring requires a mapping for every field. + let Self { $( $field, )+ } = self; + std::iter::empty() + $( .chain($crate::lattice::conflicts::Conflicts::conflicts($field) + .map(joinable_struct!(@conflict_item $Conflict $Variant $( [ $Key ] )?))) )+ + } + } + + impl $crate::lattice::conflicts::Conflicts for $Result { + type Item<'a> = $Conflict<'a>; + + fn conflicts(&self) -> impl Iterator> { + Self::conflicts(self) + } } impl $crate::lattice::lift::ResultWrapped for $Result { @@ -98,6 +273,35 @@ macro_rules! joinable_struct { } } }; + (@accessors $field:ident: $T:ty) => { + joinable_struct!(@accessors (pub) $field: $T); + }; + (@accessors ($vis:vis) $field:ident: $T:ty) => { + pastey::paste! { + #[doc = concat!("The `", stringify!($field), "` field.")] + $vis fn $field(&self) -> &$T { + &self.$field + } + + #[doc = concat!("Replace the `", stringify!($field), "` field.")] + $vis fn [](&mut self, $field: $T) { + self.$field = $field; + } + } + }; + + (@conflict_key $Key:ty, $key:ident) => { $key }; + (@conflict_path $Field:ident $Variant:ident $key:ident [ $Key:ty ]) => { $Field::$Variant(*$key) }; + (@conflict_path $Field:ident $Variant:ident $key:ident) => { $Field::$Variant }; + (@conflict_item $Conflict:ident $Variant:ident [ $Key:ty ]) => { + |(key, values)| $Conflict::$Variant { key, values } + }; + (@conflict_item $Conflict:ident $Variant:ident) => { + |values| $Conflict::$Variant { values } + }; + (@conflict_value $T:ty) => { $T }; + (@conflict_value $T:ty, $Value:ty) => { $Value }; + } // Outside the `coverage(off)` test module, so that this expansion of the @@ -106,30 +310,23 @@ macro_rules! joinable_struct { mod sample { use std::collections::BTreeMap; - /// One field per kind of lifting, the option spelled as a path. - #[derive(Debug, PartialEq)] - pub struct Sample { - pub scalar: u8, - pub option: std::option::Option, - pub map: BTreeMap, - } - joinable_struct! { - /// Result-domain form of [`Sample`]. + /// One field per kind of lifting, the option spelled as a path. source: Sample, + /// Result-domain form of [`Sample`]. result: ResultSample, - conflicts: SampleConflict, SampleField { - Scalar => scalar: u8, - Option => option: u8, - Map => map[u8]: u8, - }, - ext: SampleExt, + conflicts: SampleConflict, SampleField, fields: { - scalar: u8, - option: std::option::Option, - map: BTreeMap, + pub scalar: u8 => Scalar, + pub option: std::option::Option => Option(u8), + pub map: BTreeMap => Map[u8](u8), } } + + impl Sample { + joinable_struct!(@accessors scalar: u8); + joinable_struct!(@accessors (pub(crate)) option: std::option::Option); + } } #[cfg(all(test, feature = "unit-tests"))] @@ -137,9 +334,9 @@ mod sample { mod tests { use std::collections::BTreeMap; - use super::sample::{Sample, SampleExt}; + use super::sample::Sample; use crate::lattice::join::Join; - use crate::lattice::lift::ResultWrapped; + use crate::lattice::lift::{Lift, ResultWrapped}; fn sample(scalar: u8, option: u8, value: u8) -> Sample { Sample { @@ -149,6 +346,17 @@ mod tests { } } + #[test] + fn default_and_restricted_accessors_read_and_update_fields() { + let mut value = sample(1, 2, 3); + assert_eq!(value.scalar(), &1); + assert_eq!(value.option(), &Some(2)); + value.set_scalar(4); + value.set_option(None); + assert_eq!(value.scalar(), &4); + assert_eq!(value.option(), &None); + } + #[test] fn structs_lift_inside_a_map() { let clean = std::collections::HashMap::from([(0u8, sample(1, 2, 3))]); diff --git a/crates/concurrent-psbt/src/psbt/mod.rs b/crates/concurrent-psbt/src/psbt/mod.rs index 9602d15c..67951359 100644 --- a/crates/concurrent-psbt/src/psbt/mod.rs +++ b/crates/concurrent-psbt/src/psbt/mod.rs @@ -1,8 +1,7 @@ #[macro_use] -mod conflict_macro; -#[macro_use] mod joinable_struct_macro; pub mod global; pub mod input; pub mod output; +pub(crate) mod proprietary; mod values; diff --git a/crates/concurrent-psbt/src/psbt/output.rs b/crates/concurrent-psbt/src/psbt/output.rs index c2f13b2f..6b5a1e8b 100644 --- a/crates/concurrent-psbt/src/psbt/output.rs +++ b/crates/concurrent-psbt/src/psbt/output.rs @@ -1,57 +1,116 @@ #![allow(clippy::result_large_err)] +//! PSBT output fields. +//! +//! [`ResultOutput`] joins fields independently, retaining incompatible values +//! as conflicts. + use std::collections::BTreeMap; use bitcoin::bip32::KeySource; use bitcoin::key::{PublicKey, XOnlyPublicKey}; -use bitcoin::taproot::{TapLeafHash, TapTree}; -use bitcoin::{Amount, ScriptBuf}; +use bitcoin::taproot::TapTree; +use bitcoin::{Amount, ScriptBuf, TapLeafHash}; use psbt_v2::raw; -use psbt_v2::v2::Output; +use super::proprietary::ProprietaryField; joinable_struct! { - /// Result-domain representation of a PSBT output. + /// A PSBT output, mirroring the BIP 370 per-output map, identified by its + /// [`UniqueId`]. /// - /// Each field mirrors its counterpart in [`psbt_v2::v2::Output`] but is - /// represented in the internal result domain, recording either a clean value - /// or an accumulated conflict. + /// The unique ID is set when the output is created with [`Output::new`] + /// or converted from a BIP 370 output carrying `PSBT_OUT_UNIQUE_ID`, and + /// cannot change afterwards. source: Output, + /// Result-domain counterpart of [`Output`]. + /// + /// Each field holds a clean value or a conflict. result: ResultOutput, - conflicts: OutputConflict, OutputField { - Amount => amount: Amount, - ScriptPubkey => script_pubkey: ScriptBuf, - RedeemScript => redeem_script: ScriptBuf, - WitnessScript => witness_script: ScriptBuf, - Bip32Derivation => bip32_derivations[PublicKey]: KeySource, - TapInternalKey => tap_internal_key: XOnlyPublicKey, - TapTree => tap_tree: TapTree, - TapKeyOrigin => tap_key_origins[XOnlyPublicKey]: (Vec, KeySource), - Proprietary => proprietaries[raw::ProprietaryKey]: Vec, - Unknown => unknowns[raw::Key]: Vec, - }, - ext: OutputExt, + conflicts: OutputConflict, OutputField, + v2: psbt_v2::v2::Output, fields: { - amount: Amount, - script_pubkey: ScriptBuf, - redeem_script: Option, - witness_script: Option, - bip32_derivations: BTreeMap, - tap_internal_key: Option, - tap_tree: Option, - tap_key_origins: BTreeMap, KeySource)>, - proprietaries: BTreeMap>, - unknowns: BTreeMap>, + amount: Amount => Amount, + script_pubkey: ScriptBuf => ScriptPubkey, + redeem_script: Option => RedeemScript(ScriptBuf), + witness_script: Option => WitnessScript(ScriptBuf), + bip32_derivations: BTreeMap => Bip32Derivation[PublicKey](KeySource), + tap_internal_key: Option => TapInternalKey(XOnlyPublicKey), + tap_tree: Option => TapTree(TapTree), + tap_key_origins: BTreeMap, KeySource)> => TapKeyOrigin[XOnlyPublicKey]((Vec, KeySource)), + proprietaries: BTreeMap> => Proprietary[raw::ProprietaryKey](Vec), + unknowns: BTreeMap> => Unknown[raw::Key](Vec), + }, + proprietary: { + unique_id: UniqueId = UniqueIdField => UniqueId, + }, +} + +impl Output { + /// Create an output paying `amount` to `script_pubkey`, identified by + /// `unique_id`, with every other field unset. + pub fn new(unique_id: UniqueId, amount: Amount, script_pubkey: ScriptBuf) -> Self { + Self::from_v2_fields( + psbt_v2::v2::Output { + amount, + script_pubkey, + ..psbt_v2::v2::Output::default() + }, + unique_id, + ) + } + + /// The [`UniqueId`] identifying this output. + pub fn unique_id(&self) -> &UniqueId { + &self.unique_id + } +} + +/// Why a BIP 370 output cannot be converted into an [`Output`]. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum InvalidOutput { + /// It has no `PSBT_OUT_UNIQUE_ID`. + MissingUniqueId, + /// Its `PSBT_OUT_UNIQUE_ID` is empty or longer than [`UniqueId::MAX_LEN`] + /// bytes. + InvalidUniqueId(InvalidUniqueId), +} + +impl std::fmt::Display for InvalidOutput { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::MissingUniqueId => write!(f, "output missing PSBT_OUT_UNIQUE_ID"), + Self::InvalidUniqueId(error) => write!(f, "output {error}"), + } + } +} + +impl std::error::Error for InvalidOutput {} + +/// Reads the unique ID from `PSBT_OUT_UNIQUE_ID`. +impl TryFrom for Output { + type Error = InvalidOutput; + + fn try_from(output: psbt_v2::v2::Output) -> Result { + Self::decode_v2(output) + } +} + +/// Writes the unique ID to `PSBT_OUT_UNIQUE_ID`. +impl From for psbt_v2::v2::Output { + fn from(output: Output) -> Self { + output.encode_v2(psbt_v2::v2::Output::default()) } } /// Opaque byte-vector identifier for a PSBT output. /// -/// Computed from the `PSBT_OUT_UNIQUE_ID` proprietary field, keyed by subtype -/// [`PSBT_OUT_UNIQUE_ID_SUBTYPE`]. Used as the map key in `OutputSet` -/// so that outputs can be merged in an order-independent way. +/// Stored in the `PSBT_OUT_UNIQUE_ID` proprietary field, subtype +/// [`PSBT_OUT_UNIQUE_ID_SUBTYPE`]. Identifies an output independently of its +/// position in a transaction. /// -/// Built with [`TryFrom`], a unique ID is at most [`UniqueId::MAX_LEN`] -/// bytes long. One read from an output is not checked. +/// A unique ID built with [`TryFrom`] or read from a BIP 370 output is 1 to +/// [`UniqueId::MAX_LEN`] bytes long. An empty ID would make unrelated outputs +/// join as one. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct UniqueId(Vec); @@ -96,7 +155,8 @@ impl UniqueId { impl crate::values::IdempotentValue for UniqueId {} -/// The bytes are too long for a [`UniqueId`]; they are returned unchanged. +/// Bytes that are not a valid [`UniqueId`]: empty, or longer than +/// [`UniqueId::MAX_LEN`]. #[derive(Debug, Clone, PartialEq, Eq)] pub struct InvalidUniqueId(pub Vec); @@ -104,7 +164,7 @@ impl TryFrom> for UniqueId { type Error = InvalidUniqueId; fn try_from(bytes: Vec) -> Result { - if bytes.len() > Self::MAX_LEN { + if bytes.is_empty() || bytes.len() > Self::MAX_LEN { return Err(InvalidUniqueId(bytes)); } Ok(Self(bytes)) @@ -115,7 +175,7 @@ impl std::fmt::Display for InvalidUniqueId { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, - "unique ID is {} bytes, more than {}", + "unique ID is {} bytes, not 1 to {}", self.0.len(), UniqueId::MAX_LEN ) @@ -126,36 +186,39 @@ impl std::error::Error for InvalidUniqueId {} pub const PSBT_OUT_UNIQUE_ID_SUBTYPE: u8 = 0x01; -/// Extension trait on [`psbt_v2::v2::Output`] for accessing the unique-ID proprietary field. -pub trait OutputUniqueIdExt { - /// Return the [`UniqueId`] stored in the `PSBT_OUT_UNIQUE_ID` proprietary field, if present. - fn unique_id(&self) -> Option; - /// Return `true` if this output carries a [`UniqueId`]. - fn has_unique_id(&self) -> bool { - self.unique_id().is_some() +/// Stores an output's unique ID in `PSBT_OUT_UNIQUE_ID`. +struct UniqueIdField; + +impl ProprietaryField for UniqueIdField { + const SUBTYPE: u8 = PSBT_OUT_UNIQUE_ID_SUBTYPE; + type Value = UniqueId; + type Error = InvalidOutput; + + fn decode(value: Option>) -> Result { + let bytes = value.ok_or(InvalidOutput::MissingUniqueId)?; + UniqueId::try_from(bytes).map_err(InvalidOutput::InvalidUniqueId) + } + + fn encode(unique_id: UniqueId) -> Option> { + Some(unique_id.into_bytes()) } +} + +/// Sets `PSBT_OUT_UNIQUE_ID` on BIP 370 outputs, for tests that parse them. +#[cfg(test)] +pub(crate) trait OutputUniqueIdExt { /// Store `id` in the `PSBT_OUT_UNIQUE_ID` proprietary field, replacing /// any existing value. fn set_unique_id(&mut self, id: UniqueId); } -impl OutputUniqueIdExt for Output { - fn unique_id(&self) -> Option { - let key = psbt_v2::raw::ProprietaryKey { - prefix: b"concurrent-psbt".to_vec(), - subtype: PSBT_OUT_UNIQUE_ID_SUBTYPE, - key: vec![], - }; - self.proprietaries.get(&key).map(|v| UniqueId(v.clone())) - } - +#[cfg(test)] +impl OutputUniqueIdExt for psbt_v2::v2::Output { fn set_unique_id(&mut self, id: UniqueId) { - let key = psbt_v2::raw::ProprietaryKey { - prefix: b"concurrent-psbt".to_vec(), - subtype: PSBT_OUT_UNIQUE_ID_SUBTYPE, - key: vec![], - }; - self.proprietaries.insert(key, id.into_bytes()); + self.proprietaries.insert( + super::proprietary::key(PSBT_OUT_UNIQUE_ID_SUBTYPE), + id.into_bytes(), + ); } } @@ -165,21 +228,84 @@ mod tests { use super::*; use crate::lattice::join::Join; + /// An output with unique ID `[tag]` and every other field unset. + fn output(tag: u8) -> Output { + Output::new(UniqueId::new(vec![tag]), Amount::ZERO, ScriptBuf::new()) + } + #[cfg(feature = "unit-tests")] mod unit { use super::*; + use bitcoin::bip32::{DerivationPath, Fingerprint}; + use bitcoin::hashes::Hash; + use bitcoin::secp256k1::{Secp256k1, SecretKey}; + use bitcoin::taproot::TaprootBuilder; + use std::collections::BTreeMap; #[test] fn outputs_with_different_amounts_conflict() { let with_amount = |sats| Output { amount: Amount::from_sat(sats), - ..Output::default() + ..output(1) }; let joined = with_amount(100).wrap().join(with_amount(200).wrap()); - let fields: Vec<_> = joined.conflicts().map(|c| c.path()).collect(); - assert_eq!(fields, [OutputField::Amount]); + assert_eq!( + joined.conflicts().map(|c| c.path()).collect::>(), + [OutputField::Amount] + ); assert_eq!(joined.clone().try_unwrap(), Err(joined)); } + + /// An output with every field set, so a conversion that drops one + /// changes the output. + fn populated_output() -> Output { + let secret_key = SecretKey::from_slice(&[1; 32]).expect("valid secret key"); + let public_key = PublicKey::new(secret_key.public_key(&Secp256k1::new())); + let (x_only_public_key, _) = public_key.inner.x_only_public_key(); + let source = (Fingerprint::from([2; 4]), DerivationPath::master()); + let script = ScriptBuf::from_bytes(vec![3]); + let tap_tree = TaprootBuilder::new() + .add_leaf(0, script.clone()) + .expect("valid leaf"); + Output { + amount: bitcoin::Amount::from_sat(4), + script_pubkey: script.clone(), + redeem_script: Some(script.clone()), + witness_script: Some(script), + bip32_derivations: BTreeMap::from([(public_key, source.clone())]), + tap_internal_key: Some(x_only_public_key), + tap_tree: Some(TapTree::try_from(tap_tree).expect("complete tree")), + tap_key_origins: BTreeMap::from([( + x_only_public_key, + (vec![TapLeafHash::from_byte_array([5; 32])], source), + )]), + proprietaries: BTreeMap::from([( + raw::ProprietaryKey { + prefix: vec![6], + subtype: 7, + key: vec![], + }, + vec![8], + )]), + unknowns: BTreeMap::from([( + raw::Key { + type_value: 0xfe, + key: vec![9], + }, + vec![10], + )]), + ..output(11) + } + } + + #[test] + fn v2_conversion_keeps_every_field() { + let output = populated_output(); + assert_eq!( + Output::try_from(psbt_v2::v2::Output::from(output.clone())), + Ok(output) + ); + } } #[cfg(feature = "prop-tests")] @@ -198,7 +324,7 @@ mod tests { script_pubkey: ScriptBuf::new(), redeem_script: redeem.map(ScriptBuf::from_bytes), witness_script: witness.map(ScriptBuf::from_bytes), - ..Output::default() + ..output(1) }) } @@ -219,9 +345,20 @@ mod tests { #[test] fn is_ok_iff_try_unwrap_succeeds(r in arb_result_output()) { + prop_assert_eq!(r.conflicts().next().is_none(), r.is_ok()); prop_assert_eq!(r.is_ok(), r.try_unwrap().is_ok()); } + #[test] + fn a_set_field_reads_back( + mut o in arb_output(), + redeem in proptest::option::of(proptest::collection::vec(0u8..255, 0..=8)), + ) { + let redeem = redeem.map(ScriptBuf::from_bytes); + o.set_redeem_script(redeem.clone()); + prop_assert_eq!(o.redeem_script(), &redeem); + } + } } @@ -232,22 +369,55 @@ mod tests { proptest! { #[test] - fn unique_id_roundtrip(uid in proptest::collection::vec(0u8..=255, 1..=40)) { + fn unique_id_roundtrip(uid in proptest::collection::vec(0u8..=255, 0..=40)) { let Ok(id) = UniqueId::try_from(uid.clone()) else { - prop_assert!(uid.len() > UniqueId::MAX_LEN); + prop_assert!(uid.is_empty() || uid.len() > UniqueId::MAX_LEN); let error = InvalidUniqueId(uid.clone()); - prop_assert!(error.to_string().contains("more than 32")); + prop_assert!(error.to_string().contains("not 1 to 32")); prop_assert_eq!(UniqueId::try_from(uid), Err(error)); return Ok(()); }; prop_assert_eq!(&id, &UniqueId::new(uid.clone())); prop_assert_eq!(id.as_bytes(), uid.as_slice()); + prop_assert_eq!(id.into_bytes(), uid); + } - let mut output = Output::default(); - prop_assert!(!output.has_unique_id()); - output.set_unique_id(id); - prop_assert!(output.has_unique_id()); - prop_assert_eq!(output.unique_id().unwrap().into_bytes(), uid); + #[test] + fn v2_conversion_reads_and_writes_the_unique_id( + uid in proptest::collection::vec(0u8..=255, 0..=40), + amount in any::(), + unknown in proptest::option::of(proptest::collection::vec(0u8..=255, 0..=4)), + ) { + let mut v2 = psbt_v2::v2::Output { + amount: Amount::from_sat(amount), + ..psbt_v2::v2::Output::default() + }; + if let Some(unknown) = unknown { + v2.unknowns.insert(raw::Key { type_value: 0xfc, key: vec![] }, unknown); + } + prop_assert_eq!( + Output::try_from(v2.clone()), + Err(InvalidOutput::MissingUniqueId) + ); + + v2.set_unique_id(UniqueId::new(uid.clone())); + match Output::try_from(v2.clone()) { + Ok(output) => { + prop_assert!((1..=UniqueId::MAX_LEN).contains(&uid.len())); + prop_assert_eq!(output.unique_id().as_bytes(), uid.as_slice()); + prop_assert!(output.proprietaries.is_empty()); + prop_assert_eq!(output.amount, Amount::from_sat(amount)); + prop_assert_eq!(psbt_v2::v2::Output::from(output), v2); + } + Err(error) => { + prop_assert!(uid.is_empty() || uid.len() > UniqueId::MAX_LEN); + prop_assert!(error.to_string().starts_with("output unique ID is")); + prop_assert_eq!( + error, + InvalidOutput::InvalidUniqueId(InvalidUniqueId(uid)) + ); + } + } } } } @@ -256,42 +426,56 @@ mod tests { mod unit_uid { use super::*; - fn output_with_uid(uid: &[u8]) -> Output { - let mut output = Output::default(); - let key = psbt_v2::raw::ProprietaryKey { + #[test] + fn unique_id_is_encoded_under_the_crate_prefix() { + let key = raw::ProprietaryKey { prefix: b"concurrent-psbt".to_vec(), - subtype: PSBT_OUT_UNIQUE_ID_SUBTYPE, + subtype: 0x01, key: vec![], }; - output.proprietaries.insert(key, uid.to_vec()); - output + let v2 = psbt_v2::v2::Output::from(output(7)); + assert_eq!(v2.proprietaries.get(&key), Some(&vec![7])); + let mut set = psbt_v2::v2::Output::default(); + set.set_unique_id(UniqueId::new(vec![7])); + assert_eq!(set, v2); + assert_eq!( + InvalidOutput::MissingUniqueId.to_string(), + "output missing PSBT_OUT_UNIQUE_ID" + ); } #[test] - fn unique_id_present() { - let output = output_with_uid(&[1, 2, 3]); - assert!(output.has_unique_id()); - assert_eq!(output.unique_id().unwrap().into_bytes(), vec![1, 2, 3]); - } - - #[test] - fn unique_id_absent() { - let output = Output::default(); - assert!(!output.has_unique_id()); - assert!(output.unique_id().is_none()); + fn unique_id_is_1_to_32_bytes() { + for len in [1, 32] { + let mut v2 = psbt_v2::v2::Output::default(); + v2.set_unique_id(UniqueId::new(vec![0; len])); + let output = Output::try_from(v2).expect("a valid unique ID"); + assert_eq!(output.unique_id(), &UniqueId::new(vec![0; len])); + } + for len in [0, 33] { + let error = UniqueId::try_from(vec![0; len]).unwrap_err(); + assert_eq!(error, InvalidUniqueId(vec![0; len])); + assert_eq!( + error.to_string(), + format!("unique ID is {len} bytes, not 1 to 32") + ); + let mut v2 = psbt_v2::v2::Output::default(); + v2.set_unique_id(UniqueId::new(vec![0; len])); + let error = Output::try_from(v2).unwrap_err(); + assert_eq!( + error.to_string(), + format!("output unique ID is {len} bytes, not 1 to 32") + ); + } } #[test] - fn unique_id_construction_generation_and_setting() { - let id = UniqueId::new(vec![1, 2, 3]); - assert_eq!(id.as_bytes(), &[1, 2, 3]); - + fn unique_id_construction_and_generation() { let generated = UniqueId::generate(); assert_eq!(generated.as_bytes().len(), 16); - - let mut output = Output::default(); - output.set_unique_id(id); - assert_eq!(output.unique_id().unwrap().into_bytes(), vec![1, 2, 3]); + let output = Output::new(generated.clone(), Amount::ONE_SAT, ScriptBuf::new()); + assert_eq!(output.unique_id(), &generated); + assert_eq!(output.amount, Amount::ONE_SAT); } } } diff --git a/crates/concurrent-psbt/src/psbt/proprietary.rs b/crates/concurrent-psbt/src/psbt/proprietary.rs new file mode 100644 index 00000000..2e687f92 --- /dev/null +++ b/crates/concurrent-psbt/src/psbt/proprietary.rs @@ -0,0 +1,34 @@ +//! Fields this crate defines, stored under its own proprietary prefix until +//! they are assigned standard keys. + +use psbt_v2::raw; + +/// The prefix of every proprietary key this crate defines. +pub(crate) const PREFIX: &[u8] = b"concurrent-psbt"; + +/// The proprietary key with `subtype` under [`PREFIX`]. +pub(crate) fn key(subtype: u8) -> raw::ProprietaryKey { + raw::ProprietaryKey { + prefix: PREFIX.to_vec(), + subtype, + key: vec![], + } +} + +/// How a field this crate defines is stored in a proprietary key. +/// +/// A field's key and encoding are defined by its codec, so changing them does +/// not change the field's type or join. +pub(crate) trait ProprietaryField { + /// The subtype of the field's key under [`PREFIX`]. + const SUBTYPE: u8; + /// The decoded field. + type Value; + /// Why a field cannot be decoded. + type Error; + /// Decode the field from its key's value, or from `None` if the key is + /// absent. + fn decode(value: Option>) -> Result; + /// Encode the field as its key's value, or `None` to leave the key out. + fn encode(value: Self::Value) -> Option>; +}