// SPDX-License-Identifier: CC0-1.0 //! Raw PSBT key-value pairs. //! //! Raw PSBT key-value pairs as defined at //! . //! use core::convert::TryFrom; use core::fmt; use super::serialize::{Deserialize, Serialize}; use crate::consensus::encode::{ self, deserialize, serialize, Decodable, Encodable, ReadExt, VarInt, WriteExt, MAX_VEC_SIZE, }; use crate::io; use crate::prelude::*; use crate::psbt::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. /// ` := ` #[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 fmt::Display for Key { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "type: {:#x}, key: {:x}", self.type_value, self.key.as_hex()) } } impl Key { pub(crate) fn decode(r: &mut R) -> Result { let VarInt(byte_size): VarInt = Decodable::consensus_decode(r)?; if byte_size == 0 { return Err(Error::NoMorePairs); } let key_byte_size: u64 = byte_size - 1; if key_byte_size > MAX_VEC_SIZE as u64 { return Err(encode::Error::OversizedVectorAllocation { requested: key_byte_size as usize, max: MAX_VEC_SIZE, })?; } let type_value: u8 = Decodable::consensus_decode(r)?; let mut key = Vec::with_capacity(key_byte_size as usize); for _ in 0..key_byte_size { key.push(Decodable::consensus_decode(r)?); } Ok(Key { type_value, key }) } } impl Serialize for Key { fn serialize(&self) -> Vec { let mut buf = Vec::new(); VarInt((self.key.len() + 1) as u64) .consensus_encode(&mut buf) .expect("in-memory writers don't error"); self.type_value.consensus_encode(&mut buf).expect("in-memory writers don't error"); for key in &self.key { key.consensus_encode(&mut buf).expect("in-memory writers don't error"); } buf } } 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) } } 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 }) } } impl ProprietaryKey where Subtype: Copy + From + Into, { /// Constructs full [Key] corresponding to this proprietary key type pub fn to_key(&self) -> Key { Key { type_value: 0xFC, key: serialize(self) } } } impl TryFrom for ProprietaryKey where Subtype: Copy + From + Into, { type Error = Error; /// Constructs a [`ProprietaryKey`] from a [`Key`]. /// /// # Errors /// Returns [`Error::InvalidProprietaryKey`] if `key` does not start with `0xFC` byte. fn try_from(key: Key) -> Result { if key.type_value != 0xFC { return Err(Error::InvalidProprietaryKey); } Ok(deserialize(&key.key)?) } } // core2 doesn't have read_to_end pub(crate) fn read_to_end(mut d: D) -> Result, io::Error> { let mut result = vec![]; let mut buf = [0u8; 64]; loop { match d.read(&mut buf) { Ok(0) => break, Ok(n) => result.extend_from_slice(&buf[0..n]), Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {} Err(e) => return Err(e), }; } Ok(result) }