2018-09-09 10:34:29 +00:00
|
|
|
// Rust Bitcoin Library
|
|
|
|
// Written by
|
|
|
|
// The Rust Bitcoin developers
|
|
|
|
//
|
|
|
|
// To the extent possible under law, the author(s) have dedicated all
|
|
|
|
// copyright and related and neighboring rights to this software to
|
|
|
|
// the public domain worldwide. This software is distributed without
|
|
|
|
// any warranty.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the CC0 Public Domain Dedication
|
|
|
|
// along with this software.
|
|
|
|
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
//
|
|
|
|
|
2018-09-09 04:07:07 +00:00
|
|
|
//! # PSBT Serialization
|
|
|
|
//!
|
|
|
|
//! Defines traits used for (de)serializing PSBT values into/from raw
|
|
|
|
//! bytes in PSBT key-value pairs.
|
|
|
|
|
2019-07-11 17:06:42 +00:00
|
|
|
use std::io;
|
2018-09-09 04:20:29 +00:00
|
|
|
|
|
|
|
use blockdata::script::Script;
|
2018-09-09 04:30:38 +00:00
|
|
|
use blockdata::transaction::{SigHashType, Transaction, TxOut};
|
2018-09-09 04:20:29 +00:00
|
|
|
use consensus::encode::{self, serialize, Decodable};
|
2020-09-13 20:49:52 +00:00
|
|
|
use util::bip32::{ChildNumber, Fingerprint, KeySource};
|
2018-09-09 04:20:29 +00:00
|
|
|
use util::key::PublicKey;
|
|
|
|
use util::psbt;
|
2018-09-09 04:07:07 +00:00
|
|
|
|
|
|
|
/// A trait for serializing a value as raw data for insertion into PSBT
|
|
|
|
/// key-value pairs.
|
|
|
|
pub trait Serialize {
|
|
|
|
/// Serialize a value as raw data.
|
|
|
|
fn serialize(&self) -> Vec<u8>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A trait for deserializing a value from raw data in PSBT key-value pairs.
|
|
|
|
pub trait Deserialize: Sized {
|
|
|
|
/// Deserialize a value from raw data.
|
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, encode::Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_psbt_de_serialize!(Transaction);
|
2018-09-09 04:30:38 +00:00
|
|
|
impl_psbt_de_serialize!(TxOut);
|
|
|
|
impl_psbt_de_serialize!(Vec<Vec<u8>>); // scriptWitness
|
2018-09-09 04:20:29 +00:00
|
|
|
|
|
|
|
impl Serialize for Script {
|
|
|
|
fn serialize(&self) -> Vec<u8> {
|
|
|
|
self.to_bytes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for Script {
|
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, encode::Error> {
|
|
|
|
Ok(Self::from(bytes.to_vec()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for PublicKey {
|
|
|
|
fn serialize(&self) -> Vec<u8> {
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
self.write_into(&mut buf);
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for PublicKey {
|
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, encode::Error> {
|
|
|
|
PublicKey::from_slice(bytes)
|
|
|
|
.map_err(|_| encode::Error::ParseFailed("invalid public key"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-13 20:49:52 +00:00
|
|
|
impl Serialize for KeySource {
|
2018-09-09 04:20:29 +00:00
|
|
|
fn serialize(&self) -> Vec<u8> {
|
|
|
|
let mut rv: Vec<u8> = Vec::with_capacity(4 + 4 * (self.1).as_ref().len());
|
|
|
|
|
|
|
|
rv.append(&mut self.0.to_bytes().to_vec());
|
|
|
|
|
|
|
|
for cnum in self.1.into_iter() {
|
|
|
|
rv.append(&mut serialize(&u32::from(*cnum)))
|
|
|
|
}
|
|
|
|
|
|
|
|
rv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-13 20:49:52 +00:00
|
|
|
impl Deserialize for KeySource {
|
2018-09-09 04:20:29 +00:00
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, encode::Error> {
|
|
|
|
if bytes.len() < 4 {
|
|
|
|
return Err(io::Error::from(io::ErrorKind::UnexpectedEof).into())
|
|
|
|
}
|
|
|
|
|
|
|
|
let fprint: Fingerprint = Fingerprint::from(&bytes[0..4]);
|
|
|
|
let mut dpath: Vec<ChildNumber> = Default::default();
|
|
|
|
|
2019-07-11 17:06:42 +00:00
|
|
|
let mut d = &bytes[4..];
|
|
|
|
while !d.is_empty() {
|
|
|
|
match u32::consensus_decode(&mut d) {
|
|
|
|
Ok(index) => dpath.push(index.into()),
|
2018-09-09 04:20:29 +00:00
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-13 20:49:52 +00:00
|
|
|
Ok(KeySource(fprint, dpath.into()))
|
2018-09-09 04:20:29 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-09 04:30:38 +00:00
|
|
|
|
|
|
|
// partial sigs
|
|
|
|
impl Serialize for Vec<u8> {
|
|
|
|
fn serialize(&self) -> Vec<u8> {
|
|
|
|
self.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for Vec<u8> {
|
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, encode::Error> {
|
|
|
|
Ok(bytes.to_vec())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for SigHashType {
|
|
|
|
fn serialize(&self) -> Vec<u8> {
|
|
|
|
serialize(&self.as_u32())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for SigHashType {
|
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, encode::Error> {
|
|
|
|
let raw: u32 = encode::deserialize(bytes)?;
|
|
|
|
let rv: SigHashType = SigHashType::from_u32(raw);
|
|
|
|
|
|
|
|
if rv.as_u32() == raw {
|
|
|
|
Ok(rv)
|
|
|
|
} else {
|
|
|
|
Err(psbt::Error::NonStandardSigHashType(raw).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|