From 7eab35fe9e8d7c3cf04213ddfd5acbe071665cb9 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 15 Nov 2023 14:52:30 +1100 Subject: [PATCH] raw: Refactor module Refactor the `raw` module by moving code around and improving the docs. Refactor only, no logic changes. --- src/raw.rs | 173 ++++++++++++++++++++++++++++------------------------- 1 file changed, 91 insertions(+), 82 deletions(-) diff --git a/src/raw.rs b/src/raw.rs index b6e2e49..e936afc 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -2,9 +2,12 @@ //! Raw PSBT key-value pairs. //! -//! Raw PSBT key-value pairs as defined at -//! . +//! [BIP-174] defines the following: //! +//! - ` := ` +//! - ` := ` +//! +//! [BIP-174]: . use core::convert::TryFrom; use core::fmt; @@ -19,54 +22,26 @@ use crate::prelude::*; use crate::serialize::{Deserialize, Serialize}; use crate::{io, Error}; -/// A PSBT key in its raw byte form. -#[derive(Debug, PartialEq, Hash, Eq, Clone, Ord, PartialOrd)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[cfg_attr(feature = "serde", serde(crate = "actual_serde"))] -pub struct Key { - /// The type of this PSBT key. - pub type_value: u8, - /// The key itself in raw byte form. - /// ` := ` - #[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::hex_bytes"))] - pub key: Vec, -} - /// A PSBT key-value pair in its raw byte form. -/// ` := ` +/// +/// - ` := ` #[derive(Debug, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(crate = "actual_serde"))] pub struct Pair { /// The key of this key-value pair. pub key: Key, - /// The value data of this key-value pair in raw byte form. - /// ` := ` + /// The value of this key-value pair in raw byte form. + /// + /// - ` := ` #[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::hex_bytes"))] pub value: Vec, } -/// Default implementation for proprietary key subtyping -pub type ProprietaryType = u8; - -/// Proprietary keys (i.e. keys starting with 0xFC byte) with their internal -/// structure according to BIP 174. -#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[cfg_attr(feature = "serde", serde(crate = "actual_serde"))] -pub struct ProprietaryKey -where - Subtype: Copy + From + Into, -{ - /// Proprietary type prefix used for grouping together keys under some - /// application and avoid namespace collision - #[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::hex_bytes"))] - pub prefix: Vec, - /// Custom proprietary subtype - pub subtype: Subtype, - /// Additional key bytes (like serialized public key data etc) - #[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::hex_bytes"))] - pub key: Vec, +impl Pair { + pub(crate) fn decode(r: &mut R) -> Result { + Ok(Pair { key: Key::decode(r)?, value: Decodable::consensus_decode(r)? }) + } } impl fmt::Display for Key { @@ -75,6 +50,40 @@ impl fmt::Display for Key { } } +impl Serialize for Pair { + fn serialize(&self) -> Vec { + let mut buf = Vec::new(); + buf.extend(self.key.serialize()); + // := + self.value.consensus_encode(&mut buf).unwrap(); + buf + } +} + +impl Deserialize for Pair { + fn deserialize(bytes: &[u8]) -> Result { + let mut decoder = bytes; + Pair::decode(&mut decoder) + } +} + +/// The key of a key-value PSBT pair, in its raw byte form. +/// +/// - ` := ` +/// +/// We do not carry the `keylen` around, we just create the `VarInt` length when serializing and +/// deserializing. +#[derive(Debug, PartialEq, Hash, Eq, Clone, Ord, PartialOrd)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", serde(crate = "actual_serde"))] +pub struct Key { + /// The `keytype` of this PSBT map key (`keytype`). + pub type_value: u8, + /// The `keydata` itself in raw byte form. + #[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::hex_bytes"))] + pub key: Vec, +} + impl Key { pub(crate) fn decode(r: &mut R) -> Result { let VarInt(byte_size): VarInt = Decodable::consensus_decode(r)?; @@ -121,53 +130,27 @@ impl Serialize for Key { } } -impl Serialize for Pair { - fn serialize(&self) -> Vec { - let mut buf = Vec::new(); - buf.extend(self.key.serialize()); - // := - self.value.consensus_encode(&mut buf).unwrap(); - buf - } -} - -impl Deserialize for Pair { - fn deserialize(bytes: &[u8]) -> Result { - let mut decoder = bytes; - Pair::decode(&mut decoder) - } -} - -impl Pair { - pub(crate) fn decode(r: &mut R) -> Result { - Ok(Pair { key: Key::decode(r)?, value: Decodable::consensus_decode(r)? }) - } -} - -impl Encodable for ProprietaryKey -where - Subtype: Copy + From + Into, -{ - fn consensus_encode(&self, w: &mut W) -> Result { - let mut len = self.prefix.consensus_encode(w)? + 1; - w.emit_u8(self.subtype.into())?; - w.write_all(&self.key)?; - len += self.key.len(); - Ok(len) - } -} +/// Default implementation for proprietary key subtyping +pub type ProprietaryType = u8; -impl Decodable for ProprietaryKey +/// Proprietary keys (i.e. keys starting with 0xFC byte) with their internal +/// structure according to BIP 174. +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", serde(crate = "actual_serde"))] +pub struct ProprietaryKey where Subtype: Copy + From + Into, { - fn consensus_decode(r: &mut R) -> Result { - let prefix = Vec::::consensus_decode(r)?; - let subtype = Subtype::from(r.read_u8()?); - let key = read_to_end(r)?; - - Ok(ProprietaryKey { prefix, subtype, key }) - } + /// Proprietary type prefix used for grouping together keys under some + /// application and avoid namespace collision + #[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::hex_bytes"))] + pub prefix: Vec, + /// Custom proprietary subtype + pub subtype: Subtype, + /// Additional key bytes (like serialized public key data etc) + #[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::hex_bytes"))] + pub key: Vec, } impl ProprietaryKey @@ -197,6 +180,32 @@ where } } +impl Encodable for ProprietaryKey +where + Subtype: Copy + From + Into, +{ + fn consensus_encode(&self, w: &mut W) -> Result { + let mut len = self.prefix.consensus_encode(w)? + 1; + w.emit_u8(self.subtype.into())?; + w.write_all(&self.key)?; + len += self.key.len(); + Ok(len) + } +} + +impl Decodable for ProprietaryKey +where + Subtype: Copy + From + Into, +{ + fn consensus_decode(r: &mut R) -> Result { + let prefix = Vec::::consensus_decode(r)?; + let subtype = Subtype::from(r.read_u8()?); + let key = read_to_end(r)?; + + Ok(ProprietaryKey { prefix, subtype, key }) + } +} + // core2 doesn't have read_to_end pub(crate) fn read_to_end(mut d: D) -> Result, io::Error> { let mut result = vec![];