bitcoin/psbt/
serialize.rs

1// SPDX-License-Identifier: CC0-1.0
2
3//! PSBT serialization.
4//!
5//! Traits to serialize PSBT values to and from raw bytes
6//! according to the BIP-174 specification.
7//!
8
9use hashes::{hash160, ripemd160, sha256, sha256d, Hash};
10use hex::DisplayHex;
11use secp256k1::XOnlyPublicKey;
12
13use super::map::{Input, Map, Output, PsbtSighashType};
14use crate::bip32::{ChildNumber, Fingerprint, KeySource};
15use crate::blockdata::script::ScriptBuf;
16use crate::blockdata::transaction::{Transaction, TxOut};
17use crate::blockdata::witness::Witness;
18use crate::consensus::encode::{self, deserialize_partial, serialize, Decodable, Encodable};
19use crate::crypto::key::PublicKey;
20use crate::crypto::{ecdsa, taproot};
21use crate::io::Write;
22use crate::prelude::{String, Vec};
23use crate::psbt::{Error, Psbt};
24use crate::taproot::{
25    ControlBlock, LeafVersion, TapLeafHash, TapNodeHash, TapTree, TaprootBuilder,
26};
27use crate::VarInt;
28/// A trait for serializing a value as raw data for insertion into PSBT
29/// key-value maps.
30pub(crate) trait Serialize {
31    /// Serialize a value as raw data.
32    fn serialize(&self) -> Vec<u8>;
33}
34
35/// A trait for deserializing a value from raw data in PSBT key-value maps.
36pub(crate) trait Deserialize: Sized {
37    /// Deserialize a value from raw data.
38    fn deserialize(bytes: &[u8]) -> Result<Self, Error>;
39}
40
41impl Psbt {
42    /// Serialize a value as bytes in hex.
43    pub fn serialize_hex(&self) -> String { self.serialize().to_lower_hex_string() }
44
45    /// Serialize as raw binary data
46    pub fn serialize(&self) -> Vec<u8> {
47        let mut buf: Vec<u8> = Vec::new();
48        self.serialize_to_writer(&mut buf).expect("Writing to Vec can't fail");
49        buf
50    }
51
52    /// Serialize the PSBT into a writer.
53    pub fn serialize_to_writer(&self, w: &mut impl Write) -> io::Result<usize> {
54        let mut written_len = 0;
55
56        fn write_all(w: &mut impl Write, data: &[u8]) -> io::Result<usize> {
57            w.write_all(data).map(|_| data.len())
58        }
59
60        // magic
61        written_len += write_all(w, b"psbt")?;
62        // separator
63        written_len += write_all(w, &[0xff])?;
64
65        written_len += write_all(w, &self.serialize_map())?;
66
67        for i in &self.inputs {
68            written_len += write_all(w, &i.serialize_map())?;
69        }
70
71        for i in &self.outputs {
72            written_len += write_all(w, &i.serialize_map())?;
73        }
74
75        Ok(written_len)
76    }
77
78    /// Deserialize a value from raw binary data.
79    pub fn deserialize(mut bytes: &[u8]) -> Result<Self, Error> {
80        Self::deserialize_from_reader(&mut bytes)
81    }
82
83    /// Deserialize a value from raw binary data read from a `BufRead` object.
84    pub fn deserialize_from_reader<R: io::BufRead>(r: &mut R) -> Result<Self, Error> {
85        const MAGIC_BYTES: &[u8] = b"psbt";
86
87        let magic: [u8; 4] = Decodable::consensus_decode(r)?;
88        if magic != MAGIC_BYTES {
89            return Err(Error::InvalidMagic);
90        }
91
92        const PSBT_SERPARATOR: u8 = 0xff_u8;
93        let separator: u8 = Decodable::consensus_decode(r)?;
94        if separator != PSBT_SERPARATOR {
95            return Err(Error::InvalidSeparator);
96        }
97
98        let mut global = Psbt::decode_global(r)?;
99        global.unsigned_tx_checks()?;
100
101        let inputs: Vec<Input> = {
102            let inputs_len: usize = (global.unsigned_tx.input).len();
103
104            let mut inputs: Vec<Input> = Vec::with_capacity(inputs_len);
105
106            for _ in 0..inputs_len {
107                inputs.push(Input::decode(r)?);
108            }
109
110            inputs
111        };
112
113        let outputs: Vec<Output> = {
114            let outputs_len: usize = (global.unsigned_tx.output).len();
115
116            let mut outputs: Vec<Output> = Vec::with_capacity(outputs_len);
117
118            for _ in 0..outputs_len {
119                outputs.push(Output::decode(r)?);
120            }
121
122            outputs
123        };
124
125        global.inputs = inputs;
126        global.outputs = outputs;
127        Ok(global)
128    }
129}
130impl_psbt_de_serialize!(Transaction);
131impl_psbt_de_serialize!(TxOut);
132impl_psbt_de_serialize!(Witness);
133impl_psbt_hash_de_serialize!(ripemd160::Hash);
134impl_psbt_hash_de_serialize!(sha256::Hash);
135impl_psbt_hash_de_serialize!(TapLeafHash);
136impl_psbt_hash_de_serialize!(TapNodeHash);
137impl_psbt_hash_de_serialize!(hash160::Hash);
138impl_psbt_hash_de_serialize!(sha256d::Hash);
139
140// taproot
141impl_psbt_de_serialize!(Vec<TapLeafHash>);
142
143impl Serialize for ScriptBuf {
144    fn serialize(&self) -> Vec<u8> { self.to_bytes() }
145}
146
147impl Deserialize for ScriptBuf {
148    fn deserialize(bytes: &[u8]) -> Result<Self, Error> { Ok(Self::from(bytes.to_vec())) }
149}
150
151impl Serialize for PublicKey {
152    fn serialize(&self) -> Vec<u8> {
153        let mut buf = Vec::new();
154        self.write_into(&mut buf).expect("vecs don't error");
155        buf
156    }
157}
158
159impl Deserialize for PublicKey {
160    fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
161        PublicKey::from_slice(bytes).map_err(Error::InvalidPublicKey)
162    }
163}
164
165impl Serialize for secp256k1::PublicKey {
166    fn serialize(&self) -> Vec<u8> { self.serialize().to_vec() }
167}
168
169impl Deserialize for secp256k1::PublicKey {
170    fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
171        secp256k1::PublicKey::from_slice(bytes).map_err(Error::InvalidSecp256k1PublicKey)
172    }
173}
174
175impl Serialize for ecdsa::Signature {
176    fn serialize(&self) -> Vec<u8> { self.to_vec() }
177}
178
179impl Deserialize for ecdsa::Signature {
180    fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
181        // NB: Since BIP-174 says "the signature as would be pushed to the stack from
182        // a scriptSig or witness" we should ideally use a consensus deserialization and do
183        // not error on a non-standard values. However,
184        //
185        // 1) the current implementation of from_u32_consensus(`flag`) does not preserve
186        // the sighash byte `flag` mapping all unknown values to EcdsaSighashType::All or
187        // EcdsaSighashType::AllPlusAnyOneCanPay. Therefore, break the invariant
188        // EcdsaSig::from_slice(&sl[..]).to_vec = sl.
189        //
190        // 2) This would cause to have invalid signatures because the sighash message
191        // also has a field sighash_u32 (See BIP141). For example, when signing with non-standard
192        // 0x05, the sighash message would have the last field as 0x05u32 while, the verification
193        // would use check the signature assuming sighash_u32 as `0x01`.
194        ecdsa::Signature::from_slice(bytes).map_err(|e| match e {
195            ecdsa::Error::EmptySignature => Error::InvalidEcdsaSignature(e),
196            ecdsa::Error::SighashType(err) => Error::NonStandardSighashType(err.0),
197            ecdsa::Error::Secp256k1(..) => Error::InvalidEcdsaSignature(e),
198            ecdsa::Error::Hex(..) => unreachable!("Decoding from slice, not hex"),
199        })
200    }
201}
202
203impl Serialize for KeySource {
204    fn serialize(&self) -> Vec<u8> {
205        let mut rv: Vec<u8> = Vec::with_capacity(key_source_len(self));
206
207        rv.append(&mut self.0.to_bytes().to_vec());
208
209        for cnum in self.1.into_iter() {
210            rv.append(&mut serialize(&u32::from(*cnum)))
211        }
212
213        rv
214    }
215}
216
217impl Deserialize for KeySource {
218    fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
219        if bytes.len() < 4 {
220            return Err(io::Error::from(io::ErrorKind::UnexpectedEof).into());
221        }
222
223        let fprint: Fingerprint = bytes[0..4].try_into().expect("4 is the fingerprint length");
224        let mut dpath: Vec<ChildNumber> = Default::default();
225
226        let mut d = &bytes[4..];
227        while !d.is_empty() {
228            match u32::consensus_decode(&mut d) {
229                Ok(index) => dpath.push(index.into()),
230                Err(e) => return Err(e.into()),
231            }
232        }
233
234        Ok((fprint, dpath.into()))
235    }
236}
237
238// partial sigs
239impl Serialize for Vec<u8> {
240    fn serialize(&self) -> Vec<u8> { self.clone() }
241}
242
243impl Deserialize for Vec<u8> {
244    fn deserialize(bytes: &[u8]) -> Result<Self, Error> { Ok(bytes.to_vec()) }
245}
246
247impl Serialize for PsbtSighashType {
248    fn serialize(&self) -> Vec<u8> { serialize(&self.to_u32()) }
249}
250
251impl Deserialize for PsbtSighashType {
252    fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
253        let raw: u32 = encode::deserialize(bytes)?;
254        Ok(PsbtSighashType { inner: raw })
255    }
256}
257
258// Taproot related ser/deser
259impl Serialize for XOnlyPublicKey {
260    fn serialize(&self) -> Vec<u8> { XOnlyPublicKey::serialize(self).to_vec() }
261}
262
263impl Deserialize for XOnlyPublicKey {
264    fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
265        XOnlyPublicKey::from_slice(bytes).map_err(|_| Error::InvalidXOnlyPublicKey)
266    }
267}
268
269impl Serialize for taproot::Signature {
270    fn serialize(&self) -> Vec<u8> { self.to_vec() }
271}
272
273impl Deserialize for taproot::Signature {
274    fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
275        use taproot::SigFromSliceError::*;
276
277        taproot::Signature::from_slice(bytes).map_err(|e| match e {
278            SighashType(err) => Error::NonStandardSighashType(err.0),
279            InvalidSignatureSize(_) => Error::InvalidTaprootSignature(e),
280            Secp256k1(..) => Error::InvalidTaprootSignature(e),
281        })
282    }
283}
284
285impl Serialize for (XOnlyPublicKey, TapLeafHash) {
286    fn serialize(&self) -> Vec<u8> {
287        let ser_pk = self.0.serialize();
288        let mut buf = Vec::with_capacity(ser_pk.len() + self.1.as_byte_array().len());
289        buf.extend(&ser_pk);
290        buf.extend(self.1.as_byte_array());
291        buf
292    }
293}
294
295impl Deserialize for (XOnlyPublicKey, TapLeafHash) {
296    fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
297        if bytes.len() < 32 {
298            return Err(io::Error::from(io::ErrorKind::UnexpectedEof).into());
299        }
300        let a: XOnlyPublicKey = Deserialize::deserialize(&bytes[..32])?;
301        let b: TapLeafHash = Deserialize::deserialize(&bytes[32..])?;
302        Ok((a, b))
303    }
304}
305
306impl Serialize for ControlBlock {
307    fn serialize(&self) -> Vec<u8> { ControlBlock::serialize(self) }
308}
309
310impl Deserialize for ControlBlock {
311    fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
312        Self::decode(bytes).map_err(|_| Error::InvalidControlBlock)
313    }
314}
315
316// Versioned ScriptBuf
317impl Serialize for (ScriptBuf, LeafVersion) {
318    fn serialize(&self) -> Vec<u8> {
319        let mut buf = Vec::with_capacity(self.0.len() + 1);
320        buf.extend(self.0.as_bytes());
321        buf.push(self.1.to_consensus());
322        buf
323    }
324}
325
326impl Deserialize for (ScriptBuf, LeafVersion) {
327    fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
328        if bytes.is_empty() {
329            return Err(io::Error::from(io::ErrorKind::UnexpectedEof).into());
330        }
331        // The last byte is LeafVersion.
332        let script = ScriptBuf::deserialize(&bytes[..bytes.len() - 1])?;
333        let leaf_ver = LeafVersion::from_consensus(bytes[bytes.len() - 1])
334            .map_err(|_| Error::InvalidLeafVersion)?;
335        Ok((script, leaf_ver))
336    }
337}
338
339impl Serialize for (Vec<TapLeafHash>, KeySource) {
340    fn serialize(&self) -> Vec<u8> {
341        let mut buf = Vec::with_capacity(32 * self.0.len() + key_source_len(&self.1));
342        self.0.consensus_encode(&mut buf).expect("Vecs don't error allocation");
343        buf.extend(self.1.serialize());
344        buf
345    }
346}
347
348impl Deserialize for (Vec<TapLeafHash>, KeySource) {
349    fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
350        let (leafhash_vec, consumed) = deserialize_partial::<Vec<TapLeafHash>>(bytes)?;
351        let key_source = KeySource::deserialize(&bytes[consumed..])?;
352        Ok((leafhash_vec, key_source))
353    }
354}
355
356impl Serialize for TapTree {
357    fn serialize(&self) -> Vec<u8> {
358        let capacity = self
359            .script_leaves()
360            .map(|l| {
361                l.script().len() + VarInt::from(l.script().len()).size() // script version
362            + 1 // merkle branch
363            + 1 // leaf version
364            })
365            .sum::<usize>();
366        let mut buf = Vec::with_capacity(capacity);
367        for leaf_info in self.script_leaves() {
368            // # Cast Safety:
369            //
370            // TaprootMerkleBranch can only have len atmost 128(TAPROOT_CONTROL_MAX_NODE_COUNT).
371            // safe to cast from usize to u8
372            buf.push(leaf_info.merkle_branch().len() as u8);
373            buf.push(leaf_info.version().to_consensus());
374            leaf_info.script().consensus_encode(&mut buf).expect("Vecs dont err");
375        }
376        buf
377    }
378}
379
380impl Deserialize for TapTree {
381    fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
382        let mut builder = TaprootBuilder::new();
383        let mut bytes_iter = bytes.iter();
384        while let Some(depth) = bytes_iter.next() {
385            let version = bytes_iter.next().ok_or(Error::Taproot("Invalid Taproot Builder"))?;
386            let (script, consumed) = deserialize_partial::<ScriptBuf>(bytes_iter.as_slice())?;
387            if consumed > 0 {
388                bytes_iter.nth(consumed - 1);
389            }
390            let leaf_version =
391                LeafVersion::from_consensus(*version).map_err(|_| Error::InvalidLeafVersion)?;
392            builder = builder
393                .add_leaf_with_ver(*depth, script, leaf_version)
394                .map_err(|_| Error::Taproot("Tree not in DFS order"))?;
395        }
396        TapTree::try_from(builder).map_err(Error::TapTree)
397    }
398}
399
400// Helper function to compute key source len
401fn key_source_len(key_source: &KeySource) -> usize { 4 + 4 * (key_source.1).as_ref().len() }
402
403#[cfg(test)]
404mod tests {
405    use super::*;
406
407    // Composes tree matching a given depth map, filled with dumb script leafs,
408    // each of which consists of a single push-int op code, with int value
409    // increased for each consecutive leaf.
410    pub fn compose_taproot_builder<'map>(
411        opcode: u8,
412        depth_map: impl IntoIterator<Item = &'map u8>,
413    ) -> TaprootBuilder {
414        let mut val = opcode;
415        let mut builder = TaprootBuilder::new();
416        for depth in depth_map {
417            let script = ScriptBuf::from_hex(&format!("{:02x}", val)).unwrap();
418            builder = builder.add_leaf(*depth, script).unwrap();
419            let (new_val, _) = val.overflowing_add(1);
420            val = new_val;
421        }
422        builder
423    }
424
425    #[test]
426    fn taptree_hidden() {
427        let mut builder = compose_taproot_builder(0x51, &[2, 2, 2]);
428        builder = builder
429            .add_leaf_with_ver(
430                3,
431                ScriptBuf::from_hex("b9").unwrap(),
432                LeafVersion::from_consensus(0xC2).unwrap(),
433            )
434            .unwrap();
435        builder = builder.add_hidden_node(3, TapNodeHash::all_zeros()).unwrap();
436        assert!(TapTree::try_from(builder).is_err());
437    }
438
439    #[test]
440    fn taptree_roundtrip() {
441        let mut builder = compose_taproot_builder(0x51, &[2, 2, 2, 3]);
442        builder = builder
443            .add_leaf_with_ver(
444                3,
445                ScriptBuf::from_hex("b9").unwrap(),
446                LeafVersion::from_consensus(0xC2).unwrap(),
447            )
448            .unwrap();
449        let tree = TapTree::try_from(builder).unwrap();
450        let tree_prime = TapTree::deserialize(&tree.serialize()).unwrap();
451        assert_eq!(tree, tree_prime);
452    }
453
454    #[test]
455    fn can_deserialize_non_standard_psbt_sighash_type() {
456        let non_standard_sighash = [222u8, 0u8, 0u8, 0u8]; // 32 byte value.
457        let sighash = PsbtSighashType::deserialize(&non_standard_sighash);
458        assert!(sighash.is_ok())
459    }
460
461    #[test]
462    #[should_panic(expected = "InvalidMagic")]
463    fn invalid_vector_1() {
464        let hex_psbt = b"0200000001268171371edff285e937adeea4b37b78000c0566cbb3ad64641713ca42171bf6000000006a473044022070b2245123e6bf474d60c5b50c043d4c691a5d2435f09a34a7662a9dc251790a022001329ca9dacf280bdf30740ec0390422422c81cb45839457aeb76fc12edd95b3012102657d118d3357b8e0f4c2cd46db7b39f6d9c38d9a70abcb9b2de5dc8dbfe4ce31feffffff02d3dff505000000001976a914d0c59903c5bac2868760e90fd521a4665aa7652088ac00e1f5050000000017a9143545e6e33b832c47050f24d3eeb93c9c03948bc787b32e1300";
465        Psbt::deserialize(hex_psbt).unwrap();
466    }
467}