From 3e8e6d9aa14f6eeffb77961326bd6b4c055b82ee Mon Sep 17 00:00:00 2001 From: Daniel Roberts Date: Thu, 1 May 2025 10:52:50 -0500 Subject: [PATCH] Add BIP-373 PSBT_{IN,OUT}_MUSIG2_PARTICIPANT_PUBKEYS serialization and deserialization --- bitcoin/src/psbt/map/input.rs | 15 +++++++++++++++ bitcoin/src/psbt/map/output.rs | 14 ++++++++++++++ bitcoin/src/psbt/serialize.rs | 22 ++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/bitcoin/src/psbt/map/input.rs b/bitcoin/src/psbt/map/input.rs index 287c7f362..2422c57d2 100644 --- a/bitcoin/src/psbt/map/input.rs +++ b/bitcoin/src/psbt/map/input.rs @@ -59,6 +59,8 @@ const PSBT_IN_TAP_BIP32_DERIVATION: u64 = 0x16; const PSBT_IN_TAP_INTERNAL_KEY: u64 = 0x17; /// Type: Taproot Merkle Root PSBT_IN_TAP_MERKLE_ROOT = 0x18 const PSBT_IN_TAP_MERKLE_ROOT: u64 = 0x18; +/// Type: MuSig2 Public Keys Participating in Aggregate Input PSBT_IN_MUSIG2_PARTICIPANT_PUBKEYS = 0x1a +const PSBT_IN_MUSIG2_PARTICIPANT_PUBKEYS: u64 = 0x1a; /// Type: Proprietary Use Type PSBT_IN_PROPRIETARY = 0xFC const PSBT_IN_PROPRIETARY: u64 = 0xFC; @@ -113,6 +115,8 @@ pub struct Input { pub tap_internal_key: Option, /// Taproot Merkle root. pub tap_merkle_root: Option, + /// Mapping from MuSig2 aggregate keys to the participant keys from which they were aggregated. + pub musig2_participant_pubkeys: BTreeMap>, /// Proprietary key-value pairs for this input. pub proprietary: BTreeMap>, /// Unknown key-value pairs for this input. @@ -352,6 +356,11 @@ impl Input { self.tap_merkle_root <= |< raw_value: TapNodeHash> } } + PSBT_IN_MUSIG2_PARTICIPANT_PUBKEYS => { + impl_psbt_insert_pair! { + self.musig2_participant_pubkeys <= |< raw_value: Vec > + } + } PSBT_IN_PROPRIETARY => { let key = raw::ProprietaryKey::try_from(raw_key.clone())?; match self.proprietary.entry(key) { @@ -390,6 +399,7 @@ impl Input { self.tap_script_sigs.extend(other.tap_script_sigs); self.tap_scripts.extend(other.tap_scripts); self.tap_key_origins.extend(other.tap_key_origins); + self.musig2_participant_pubkeys.extend(other.musig2_participant_pubkeys); self.proprietary.extend(other.proprietary); self.unknown.extend(other.unknown); @@ -482,6 +492,11 @@ impl Map for Input { impl_psbt_get_pair! { rv.push(self.tap_merkle_root, PSBT_IN_TAP_MERKLE_ROOT) } + + impl_psbt_get_pair! { + rv.push_map(self.musig2_participant_pubkeys, PSBT_IN_MUSIG2_PARTICIPANT_PUBKEYS) + } + for (key, value) in self.proprietary.iter() { rv.push(raw::Pair { key: key.to_key(), value: value.clone() }); } diff --git a/bitcoin/src/psbt/map/output.rs b/bitcoin/src/psbt/map/output.rs index 7f86bdb65..37e853f81 100644 --- a/bitcoin/src/psbt/map/output.rs +++ b/bitcoin/src/psbt/map/output.rs @@ -20,6 +20,8 @@ const PSBT_OUT_TAP_INTERNAL_KEY: u64 = 0x05; const PSBT_OUT_TAP_TREE: u64 = 0x06; /// Type: Taproot Key BIP 32 Derivation Path PSBT_OUT_TAP_BIP32_DERIVATION = 0x07 const PSBT_OUT_TAP_BIP32_DERIVATION: u64 = 0x07; +/// Type: MuSig2 Public Keys Participating in Aggregate Output PSBT_OUT_MUSIG2_PARTICIPANT_PUBKEYS = 0x08 +const PSBT_OUT_MUSIG2_PARTICIPANT_PUBKEYS: u64 = 0x08; /// Type: Proprietary Use Type PSBT_IN_PROPRIETARY = 0xFC const PSBT_OUT_PROPRIETARY: u64 = 0xFC; @@ -40,6 +42,8 @@ pub struct Output { pub tap_tree: Option, /// Map of tap root x only keys to origin info and leaf hashes contained in it. pub tap_key_origins: BTreeMap, KeySource)>, + /// Mapping from MuSig2 aggregate keys to the participant keys from which they were aggregated. + pub musig2_participant_pubkeys: BTreeMap>, /// Proprietary key-value pairs for this output. pub proprietary: BTreeMap>, /// Unknown key-value pairs for this output. @@ -90,6 +94,11 @@ impl Output { self.tap_key_origins <= |< raw_value: (Vec, KeySource)> } } + PSBT_OUT_MUSIG2_PARTICIPANT_PUBKEYS => { + impl_psbt_insert_pair! { + self.musig2_participant_pubkeys <= |< raw_value: Vec > + } + } _ => match self.unknown.entry(raw_key) { btree_map::Entry::Vacant(empty_key) => { empty_key.insert(raw_value); @@ -107,6 +116,7 @@ impl Output { self.proprietary.extend(other.proprietary); self.unknown.extend(other.unknown); self.tap_key_origins.extend(other.tap_key_origins); + self.musig2_participant_pubkeys.extend(other.musig2_participant_pubkeys); combine!(redeem_script, self, other); combine!(witness_script, self, other); @@ -143,6 +153,10 @@ impl Map for Output { rv.push_map(self.tap_key_origins, PSBT_OUT_TAP_BIP32_DERIVATION) } + impl_psbt_get_pair! { + rv.push_map(self.musig2_participant_pubkeys, PSBT_OUT_MUSIG2_PARTICIPANT_PUBKEYS) + } + for (key, value) in self.proprietary.iter() { rv.push(raw::Pair { key: key.to_key(), value: value.clone() }); } diff --git a/bitcoin/src/psbt/serialize.rs b/bitcoin/src/psbt/serialize.rs index 8f6193fd2..270f8a75e 100644 --- a/bitcoin/src/psbt/serialize.rs +++ b/bitcoin/src/psbt/serialize.rs @@ -172,6 +172,28 @@ impl Deserialize for secp256k1::PublicKey { } } +impl Serialize for Vec { + fn serialize(&self) -> Vec { + let mut result: Vec = Vec::with_capacity(secp256k1::constants::PUBLIC_KEY_SIZE * self.len()); + + for pubkey in self.iter() { + result.extend(Serialize::serialize(pubkey)); + } + + result + } +} + +impl Deserialize for Vec { + fn deserialize(bytes: &[u8]) -> Result { + bytes.chunks(secp256k1::constants::PUBLIC_KEY_SIZE) + .map(|pubkey_bytes| { + secp256k1::PublicKey::deserialize(pubkey_bytes) + }) + .collect() + } +} + impl Serialize for ecdsa::Signature { fn serialize(&self) -> Vec { self.to_vec() } }