2022-06-29 04:05:31 +00:00
|
|
|
// SPDX-License-Identifier: CC0-1.0
|
2018-09-09 10:34:29 +00:00
|
|
|
|
2021-11-05 21:58:18 +00:00
|
|
|
//! PSBT serialization.
|
2018-09-09 04:07:07 +00:00
|
|
|
//!
|
2022-05-04 11:28:14 +00:00
|
|
|
//! Traits to serialize PSBT values to and from raw bytes
|
|
|
|
//! according to the BIP-174 specification.
|
2021-11-05 21:58:18 +00:00
|
|
|
//!
|
2018-09-09 04:07:07 +00:00
|
|
|
|
2022-12-23 00:28:17 +00:00
|
|
|
use core::convert::TryInto;
|
|
|
|
|
2022-05-02 22:13:57 +00:00
|
|
|
use crate::prelude::*;
|
2021-06-09 10:34:44 +00:00
|
|
|
|
2022-05-02 22:13:57 +00:00
|
|
|
use crate::io;
|
2018-09-09 04:20:29 +00:00
|
|
|
|
2022-07-30 12:22:18 +00:00
|
|
|
use crate::blockdata::script::ScriptBuf;
|
2022-05-02 22:13:57 +00:00
|
|
|
use crate::blockdata::witness::Witness;
|
|
|
|
use crate::blockdata::transaction::{Transaction, TxOut};
|
|
|
|
use crate::consensus::encode::{self, serialize, Decodable, Encodable, deserialize_partial};
|
2022-01-03 02:52:44 +00:00
|
|
|
use secp256k1::{self, XOnlyPublicKey};
|
2022-10-20 20:28:23 +00:00
|
|
|
use crate::bip32::{ChildNumber, Fingerprint, KeySource};
|
2022-05-02 22:13:57 +00:00
|
|
|
use crate::hashes::{hash160, ripemd160, sha256, sha256d, Hash};
|
2023-02-07 03:35:03 +00:00
|
|
|
use crate::crypto::{ecdsa, taproot};
|
2022-12-09 16:55:11 +00:00
|
|
|
use crate::psbt::{Error, PartiallySignedTransaction};
|
2022-12-15 15:54:32 +00:00
|
|
|
use crate::taproot::{TapNodeHash, TapLeafHash, ControlBlock, LeafVersion};
|
2022-11-08 00:36:52 +00:00
|
|
|
use crate::crypto::key::PublicKey;
|
2022-01-13 04:27:20 +00:00
|
|
|
|
2022-05-04 11:28:14 +00:00
|
|
|
use super::map::{Map, Input, Output, TapTree, PsbtSighashType};
|
2022-07-25 11:44:28 +00:00
|
|
|
use super::Psbt;
|
2018-09-09 04:07:07 +00:00
|
|
|
|
2022-11-07 23:32:52 +00:00
|
|
|
use crate::taproot::TaprootBuilder;
|
2018-09-09 04:07:07 +00:00
|
|
|
/// A trait for serializing a value as raw data for insertion into PSBT
|
2022-05-04 11:28:14 +00:00
|
|
|
/// key-value maps.
|
2022-07-25 11:44:28 +00:00
|
|
|
pub(crate) trait Serialize {
|
2018-09-09 04:07:07 +00:00
|
|
|
/// Serialize a value as raw data.
|
|
|
|
fn serialize(&self) -> Vec<u8>;
|
|
|
|
}
|
|
|
|
|
2022-05-04 11:28:14 +00:00
|
|
|
/// A trait for deserializing a value from raw data in PSBT key-value maps.
|
2022-12-08 23:16:56 +00:00
|
|
|
pub(crate) trait Deserialize: Sized {
|
2018-09-09 04:07:07 +00:00
|
|
|
/// Deserialize a value from raw data.
|
2022-12-09 16:55:11 +00:00
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, Error>;
|
2018-09-09 04:07:07 +00:00
|
|
|
}
|
|
|
|
|
2022-05-04 11:28:14 +00:00
|
|
|
impl PartiallySignedTransaction {
|
2022-07-25 11:44:28 +00:00
|
|
|
/// Serialize a value as bytes in hex.
|
|
|
|
pub fn serialize_hex(&self) -> String {
|
2023-01-07 15:39:11 +00:00
|
|
|
self.serialize().to_lower_hex_string()
|
2022-05-04 11:28:14 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 11:44:28 +00:00
|
|
|
/// Serialize as raw binary data
|
2022-05-04 11:28:14 +00:00
|
|
|
pub fn serialize(&self) -> Vec<u8> {
|
|
|
|
let mut buf: Vec<u8> = Vec::new();
|
|
|
|
|
2022-07-25 11:44:28 +00:00
|
|
|
// <magic>
|
2023-01-10 04:10:07 +00:00
|
|
|
buf.extend_from_slice(b"psbt");
|
2022-05-04 11:28:14 +00:00
|
|
|
|
|
|
|
buf.push(0xff_u8);
|
|
|
|
|
2022-07-25 11:44:28 +00:00
|
|
|
buf.extend(self.serialize_map());
|
2022-05-04 11:28:14 +00:00
|
|
|
|
|
|
|
for i in &self.inputs {
|
2022-07-25 11:44:28 +00:00
|
|
|
buf.extend(i.serialize_map());
|
2022-05-04 11:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i in &self.outputs {
|
2022-07-25 11:44:28 +00:00
|
|
|
buf.extend(i.serialize_map());
|
2022-05-04 11:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Deserialize a value from raw binary data.
|
2022-12-09 16:55:11 +00:00
|
|
|
pub fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
2022-05-04 11:28:14 +00:00
|
|
|
const MAGIC_BYTES: &[u8] = b"psbt";
|
|
|
|
if bytes.get(0..MAGIC_BYTES.len()) != Some(MAGIC_BYTES) {
|
2022-12-09 16:55:11 +00:00
|
|
|
return Err(Error::InvalidMagic);
|
2022-05-04 11:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const PSBT_SERPARATOR: u8 = 0xff_u8;
|
|
|
|
if bytes.get(MAGIC_BYTES.len()) != Some(&PSBT_SERPARATOR) {
|
2022-12-09 16:55:11 +00:00
|
|
|
return Err(Error::InvalidSeparator);
|
2022-05-04 11:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut d = bytes.get(5..).ok_or(Error::NoMorePairs)?;
|
|
|
|
|
2022-12-08 23:16:56 +00:00
|
|
|
let mut global = Psbt::decode_global(&mut d)?;
|
2022-05-04 11:28:14 +00:00
|
|
|
global.unsigned_tx_checks()?;
|
|
|
|
|
|
|
|
let inputs: Vec<Input> = {
|
2022-12-08 23:16:56 +00:00
|
|
|
let inputs_len: usize = (global.unsigned_tx.input).len();
|
2022-05-04 11:28:14 +00:00
|
|
|
|
|
|
|
let mut inputs: Vec<Input> = Vec::with_capacity(inputs_len);
|
|
|
|
|
|
|
|
for _ in 0..inputs_len {
|
2022-12-08 23:16:56 +00:00
|
|
|
inputs.push(Input::decode(&mut d)?);
|
2022-05-04 11:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inputs
|
|
|
|
};
|
|
|
|
|
|
|
|
let outputs: Vec<Output> = {
|
2022-12-08 23:16:56 +00:00
|
|
|
let outputs_len: usize = (global.unsigned_tx.output).len();
|
2022-05-04 11:28:14 +00:00
|
|
|
|
|
|
|
let mut outputs: Vec<Output> = Vec::with_capacity(outputs_len);
|
|
|
|
|
|
|
|
for _ in 0..outputs_len {
|
2022-12-08 23:16:56 +00:00
|
|
|
outputs.push(Output::decode(&mut d)?);
|
2022-05-04 11:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
outputs
|
|
|
|
};
|
|
|
|
|
|
|
|
global.inputs = inputs;
|
|
|
|
global.outputs = outputs;
|
|
|
|
Ok(global)
|
|
|
|
}
|
|
|
|
}
|
2018-09-09 04:07:07 +00:00
|
|
|
impl_psbt_de_serialize!(Transaction);
|
2018-09-09 04:30:38 +00:00
|
|
|
impl_psbt_de_serialize!(TxOut);
|
2022-01-11 09:10:28 +00:00
|
|
|
impl_psbt_de_serialize!(Witness);
|
2020-08-31 20:06:21 +00:00
|
|
|
impl_psbt_hash_de_serialize!(ripemd160::Hash);
|
|
|
|
impl_psbt_hash_de_serialize!(sha256::Hash);
|
2021-10-27 10:20:21 +00:00
|
|
|
impl_psbt_hash_de_serialize!(TapLeafHash);
|
2022-12-15 15:54:32 +00:00
|
|
|
impl_psbt_hash_de_serialize!(TapNodeHash);
|
2020-08-31 20:06:21 +00:00
|
|
|
impl_psbt_hash_de_serialize!(hash160::Hash);
|
|
|
|
impl_psbt_hash_de_serialize!(sha256d::Hash);
|
2018-09-09 04:20:29 +00:00
|
|
|
|
2021-10-27 10:20:21 +00:00
|
|
|
// taproot
|
|
|
|
impl_psbt_de_serialize!(Vec<TapLeafHash>);
|
|
|
|
|
2022-07-30 12:22:18 +00:00
|
|
|
impl Serialize for ScriptBuf {
|
2018-09-09 04:20:29 +00:00
|
|
|
fn serialize(&self) -> Vec<u8> {
|
|
|
|
self.to_bytes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-30 12:22:18 +00:00
|
|
|
impl Deserialize for ScriptBuf {
|
2022-12-09 16:55:11 +00:00
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
2018-09-09 04:20:29 +00:00
|
|
|
Ok(Self::from(bytes.to_vec()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 07:45:35 +00:00
|
|
|
impl Serialize for PublicKey {
|
|
|
|
fn serialize(&self) -> Vec<u8> {
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
self.write_into(&mut buf).expect("vecs don't error");
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for PublicKey {
|
2022-12-09 16:55:11 +00:00
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
2022-02-17 07:45:35 +00:00
|
|
|
PublicKey::from_slice(bytes)
|
2023-01-17 16:21:52 +00:00
|
|
|
.map_err(Error::InvalidPublicKey)
|
2022-02-17 07:45:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-12 12:09:02 +00:00
|
|
|
impl Serialize for secp256k1::PublicKey {
|
2018-09-09 04:20:29 +00:00
|
|
|
fn serialize(&self) -> Vec<u8> {
|
2021-04-12 12:09:02 +00:00
|
|
|
self.serialize().to_vec()
|
2018-09-09 04:20:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-12 12:09:02 +00:00
|
|
|
impl Deserialize for secp256k1::PublicKey {
|
2022-12-09 16:55:11 +00:00
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
2021-04-12 12:09:02 +00:00
|
|
|
secp256k1::PublicKey::from_slice(bytes)
|
2023-01-17 16:21:52 +00:00
|
|
|
.map_err(Error::InvalidSecp256k1PublicKey)
|
2018-09-09 04:20:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 00:36:52 +00:00
|
|
|
impl Serialize for ecdsa::Signature {
|
2022-01-06 10:51:33 +00:00
|
|
|
fn serialize(&self) -> Vec<u8> {
|
2022-01-13 04:27:20 +00:00
|
|
|
self.to_vec()
|
2022-01-06 10:51:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 00:36:52 +00:00
|
|
|
impl Deserialize for ecdsa::Signature {
|
2022-12-09 16:55:11 +00:00
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
2022-01-13 04:27:20 +00:00
|
|
|
// NB: Since BIP-174 says "the signature as would be pushed to the stack from
|
|
|
|
// a scriptSig or witness" we should ideally use a consensus deserialization and do
|
|
|
|
// not error on a non-standard values. However,
|
|
|
|
//
|
|
|
|
// 1) the current implementation of from_u32_consensus(`flag`) does not preserve
|
|
|
|
// the sighash byte `flag` mapping all unknown values to EcdsaSighashType::All or
|
2022-03-28 21:48:53 +00:00
|
|
|
// EcdsaSighashType::AllPlusAnyOneCanPay. Therefore, break the invariant
|
2022-01-13 04:27:20 +00:00
|
|
|
// EcdsaSig::from_slice(&sl[..]).to_vec = sl.
|
|
|
|
//
|
|
|
|
// 2) This would cause to have invalid signatures because the sighash message
|
|
|
|
// also has a field sighash_u32 (See BIP141). For example, when signing with non-standard
|
|
|
|
// 0x05, the sighash message would have the last field as 0x05u32 while, the verification
|
|
|
|
// would use check the signature assuming sighash_u32 as `0x01`.
|
2022-11-08 00:36:52 +00:00
|
|
|
ecdsa::Signature::from_slice(bytes)
|
2022-01-17 15:46:47 +00:00
|
|
|
.map_err(|e| match e {
|
2022-11-08 00:36:52 +00:00
|
|
|
ecdsa::Error::EmptySignature => {
|
2023-01-17 16:21:52 +00:00
|
|
|
Error::InvalidEcdsaSignature(e)
|
2022-01-17 15:46:47 +00:00
|
|
|
}
|
2022-11-08 00:36:52 +00:00
|
|
|
ecdsa::Error::NonStandardSighashType(flag) => {
|
2022-12-09 16:55:11 +00:00
|
|
|
Error::NonStandardSighashType(flag)
|
2022-01-17 15:46:47 +00:00
|
|
|
}
|
2022-11-08 00:36:52 +00:00
|
|
|
ecdsa::Error::Secp256k1(..) => {
|
2023-01-17 16:21:52 +00:00
|
|
|
Error::InvalidEcdsaSignature(e)
|
2022-01-17 15:46:47 +00:00
|
|
|
}
|
2022-11-08 00:36:52 +00:00
|
|
|
ecdsa::Error::HexEncoding(..) => {
|
2022-01-17 15:46:47 +00:00
|
|
|
unreachable!("Decoding from slice, not hex")
|
|
|
|
}
|
|
|
|
})
|
2022-01-06 10:51:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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> {
|
2022-05-25 03:21:25 +00:00
|
|
|
let mut rv: Vec<u8> = Vec::with_capacity(key_source_len(self));
|
2018-09-09 04:20:29 +00:00
|
|
|
|
|
|
|
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 {
|
2022-12-09 16:55:11 +00:00
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
2018-09-09 04:20:29 +00:00
|
|
|
if bytes.len() < 4 {
|
2023-02-02 17:34:49 +00:00
|
|
|
return Err(io::Error::from(io::ErrorKind::UnexpectedEof).into())
|
2018-09-09 04:20:29 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 00:28:17 +00:00
|
|
|
let fprint: Fingerprint = bytes[0..4].try_into().expect("4 is the fingerprint length");
|
2018-09-09 04:20:29 +00:00
|
|
|
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()),
|
2022-12-09 16:55:11 +00:00
|
|
|
Err(e) => return Err(e)?,
|
2018-09-09 04:20:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-13 22:37:29 +00:00
|
|
|
Ok((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> {
|
2022-12-09 16:55:11 +00:00
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
2018-09-09 04:30:38 +00:00
|
|
|
Ok(bytes.to_vec())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 21:58:07 +00:00
|
|
|
impl Serialize for PsbtSighashType {
|
2018-09-09 04:30:38 +00:00
|
|
|
fn serialize(&self) -> Vec<u8> {
|
2022-02-22 19:34:26 +00:00
|
|
|
serialize(&self.to_u32())
|
2018-09-09 04:30:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 21:58:07 +00:00
|
|
|
impl Deserialize for PsbtSighashType {
|
2022-12-09 16:55:11 +00:00
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
2018-09-09 04:30:38 +00:00
|
|
|
let raw: u32 = encode::deserialize(bytes)?;
|
2022-03-28 21:58:07 +00:00
|
|
|
Ok(PsbtSighashType { inner: raw })
|
2018-09-09 04:30:38 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-27 10:20:21 +00:00
|
|
|
|
|
|
|
// Taproot related ser/deser
|
2022-01-03 02:52:44 +00:00
|
|
|
impl Serialize for XOnlyPublicKey {
|
2021-10-27 10:20:21 +00:00
|
|
|
fn serialize(&self) -> Vec<u8> {
|
2022-05-25 03:21:25 +00:00
|
|
|
XOnlyPublicKey::serialize(self).to_vec()
|
2021-10-27 10:20:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 02:52:44 +00:00
|
|
|
impl Deserialize for XOnlyPublicKey {
|
2022-12-09 16:55:11 +00:00
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
2022-01-03 02:52:44 +00:00
|
|
|
XOnlyPublicKey::from_slice(bytes)
|
2023-01-17 16:21:52 +00:00
|
|
|
.map_err(|_| Error::InvalidXOnlyPublicKey)
|
2021-10-27 10:20:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 03:35:03 +00:00
|
|
|
impl Serialize for taproot::Signature {
|
2021-10-27 10:20:21 +00:00
|
|
|
fn serialize(&self) -> Vec<u8> {
|
|
|
|
self.to_vec()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 03:35:03 +00:00
|
|
|
impl Deserialize for taproot::Signature {
|
2022-12-09 16:55:11 +00:00
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
2023-02-07 03:35:03 +00:00
|
|
|
taproot::Signature::from_slice(bytes)
|
2022-01-17 15:46:47 +00:00
|
|
|
.map_err(|e| match e {
|
2023-02-07 03:35:03 +00:00
|
|
|
taproot::Error::InvalidSighashType(flag) => {
|
2022-12-09 16:55:11 +00:00
|
|
|
Error::NonStandardSighashType(flag as u32)
|
2022-01-17 15:46:47 +00:00
|
|
|
}
|
2023-02-07 03:35:03 +00:00
|
|
|
taproot::Error::InvalidSignatureSize(_) => {
|
|
|
|
Error::InvalidTaprootSignature(e)
|
2022-01-17 15:46:47 +00:00
|
|
|
}
|
2023-02-07 03:35:03 +00:00
|
|
|
taproot::Error::Secp256k1(..) => {
|
|
|
|
Error::InvalidTaprootSignature(e)
|
2022-01-17 15:46:47 +00:00
|
|
|
}
|
|
|
|
})
|
2021-10-27 10:20:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 02:52:44 +00:00
|
|
|
impl Serialize for (XOnlyPublicKey, TapLeafHash) {
|
2021-10-27 10:20:21 +00:00
|
|
|
fn serialize(&self) -> Vec<u8> {
|
|
|
|
let ser_pk = self.0.serialize();
|
2023-01-25 23:50:20 +00:00
|
|
|
let mut buf = Vec::with_capacity(ser_pk.len() + self.1.as_inner().len());
|
2021-10-27 10:20:21 +00:00
|
|
|
buf.extend(&ser_pk);
|
2023-01-25 23:50:20 +00:00
|
|
|
buf.extend(self.1.as_inner());
|
2021-10-27 10:20:21 +00:00
|
|
|
buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 02:52:44 +00:00
|
|
|
impl Deserialize for (XOnlyPublicKey, TapLeafHash) {
|
2022-12-09 16:55:11 +00:00
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
2021-10-27 10:20:21 +00:00
|
|
|
if bytes.len() < 32 {
|
2023-02-02 17:34:49 +00:00
|
|
|
return Err(io::Error::from(io::ErrorKind::UnexpectedEof).into())
|
2021-10-27 10:20:21 +00:00
|
|
|
}
|
2022-01-03 02:52:44 +00:00
|
|
|
let a: XOnlyPublicKey = Deserialize::deserialize(&bytes[..32])?;
|
2021-10-27 10:20:21 +00:00
|
|
|
let b: TapLeafHash = Deserialize::deserialize(&bytes[32..])?;
|
|
|
|
Ok((a, b))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for ControlBlock {
|
|
|
|
fn serialize(&self) -> Vec<u8> {
|
2022-05-25 03:21:25 +00:00
|
|
|
ControlBlock::serialize(self)
|
2021-10-27 10:20:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for ControlBlock {
|
2022-12-09 16:55:11 +00:00
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
2023-02-04 04:19:20 +00:00
|
|
|
Self::decode(bytes)
|
2023-01-17 16:21:52 +00:00
|
|
|
.map_err(|_| Error::InvalidControlBlock)
|
2021-10-27 10:20:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-30 12:22:18 +00:00
|
|
|
// Versioned ScriptBuf
|
|
|
|
impl Serialize for (ScriptBuf, LeafVersion) {
|
2021-10-27 10:20:21 +00:00
|
|
|
fn serialize(&self) -> Vec<u8> {
|
|
|
|
let mut buf = Vec::with_capacity(self.0.len() + 1);
|
|
|
|
buf.extend(self.0.as_bytes());
|
2022-01-19 02:46:51 +00:00
|
|
|
buf.push(self.1.to_consensus());
|
2021-10-27 10:20:21 +00:00
|
|
|
buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-30 12:22:18 +00:00
|
|
|
impl Deserialize for (ScriptBuf, LeafVersion) {
|
2022-12-09 16:55:11 +00:00
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
2021-10-27 10:20:21 +00:00
|
|
|
if bytes.is_empty() {
|
2023-02-02 17:34:49 +00:00
|
|
|
return Err(io::Error::from(io::ErrorKind::UnexpectedEof).into())
|
2021-10-27 10:20:21 +00:00
|
|
|
}
|
|
|
|
// The last byte is LeafVersion.
|
2022-07-30 12:22:18 +00:00
|
|
|
let script = ScriptBuf::deserialize(&bytes[..bytes.len() - 1])?;
|
2021-11-24 23:01:21 +00:00
|
|
|
let leaf_ver = LeafVersion::from_consensus(bytes[bytes.len() - 1])
|
2023-01-17 16:21:52 +00:00
|
|
|
.map_err(|_| Error::InvalidLeafVersion)?;
|
2021-10-27 10:20:21 +00:00
|
|
|
Ok((script, leaf_ver))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Serialize for (Vec<TapLeafHash>, KeySource) {
|
|
|
|
fn serialize(&self) -> Vec<u8> {
|
|
|
|
let mut buf = Vec::with_capacity( 32 * self.0.len() + key_source_len(&self.1));
|
|
|
|
self.0.consensus_encode(&mut buf).expect("Vecs don't error allocation");
|
|
|
|
// TODO: Add support for writing into a writer for key-source
|
|
|
|
buf.extend(self.1.serialize());
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for (Vec<TapLeafHash>, KeySource) {
|
2022-12-09 16:55:11 +00:00
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
2022-05-25 03:21:25 +00:00
|
|
|
let (leafhash_vec, consumed) = deserialize_partial::<Vec<TapLeafHash>>(bytes)?;
|
2021-10-27 10:20:21 +00:00
|
|
|
let key_source = KeySource::deserialize(&bytes[consumed..])?;
|
|
|
|
Ok((leafhash_vec, key_source))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for TapTree {
|
|
|
|
fn serialize(&self) -> Vec<u8> {
|
|
|
|
match (self.0.branch().len(), self.0.branch().last()) {
|
|
|
|
(1, Some(Some(root))) => {
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
for leaf_info in root.leaves.iter() {
|
|
|
|
// # Cast Safety:
|
|
|
|
//
|
|
|
|
// TaprootMerkleBranch can only have len atmost 128(TAPROOT_CONTROL_MAX_NODE_COUNT).
|
|
|
|
// safe to cast from usize to u8
|
2022-03-31 13:56:48 +00:00
|
|
|
buf.push(leaf_info.merkle_branch().as_inner().len() as u8);
|
|
|
|
buf.push(leaf_info.leaf_version().to_consensus());
|
|
|
|
leaf_info.script().consensus_encode(&mut buf).expect("Vecs dont err");
|
2021-10-27 10:20:21 +00:00
|
|
|
}
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
// This should be unreachable as we Taptree is already finalized
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for TapTree {
|
2022-12-09 16:55:11 +00:00
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
2021-10-27 10:20:21 +00:00
|
|
|
let mut builder = TaprootBuilder::new();
|
|
|
|
let mut bytes_iter = bytes.iter();
|
|
|
|
while let Some(depth) = bytes_iter.next() {
|
2023-01-17 16:21:52 +00:00
|
|
|
let version = bytes_iter.next().ok_or(Error::Taproot("Invalid Taproot Builder"))?;
|
2022-07-30 12:22:18 +00:00
|
|
|
let (script, consumed) = deserialize_partial::<ScriptBuf>(bytes_iter.as_slice())?;
|
2021-10-27 10:20:21 +00:00
|
|
|
if consumed > 0 {
|
|
|
|
bytes_iter.nth(consumed - 1);
|
|
|
|
}
|
2021-11-24 23:01:21 +00:00
|
|
|
let leaf_version = LeafVersion::from_consensus(*version)
|
2023-01-17 16:21:52 +00:00
|
|
|
.map_err(|_| Error::InvalidLeafVersion)?;
|
2022-03-31 13:12:05 +00:00
|
|
|
builder = builder.add_leaf_with_ver(*depth, script, leaf_version)
|
2023-01-17 16:21:52 +00:00
|
|
|
.map_err(|_| Error::Taproot("Tree not in DFS order"))?;
|
2021-10-27 10:20:21 +00:00
|
|
|
}
|
2022-07-29 00:33:26 +00:00
|
|
|
if builder.is_finalizable() && !builder.has_hidden_nodes() {
|
2021-10-27 10:20:21 +00:00
|
|
|
Ok(TapTree(builder))
|
|
|
|
} else {
|
2023-01-17 16:21:52 +00:00
|
|
|
Err(Error::Taproot("Incomplete taproot Tree"))
|
2021-10-27 10:20:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to compute key source len
|
|
|
|
fn key_source_len(key_source: &KeySource) -> usize {
|
|
|
|
4 + 4 * (key_source.1).as_ref().len()
|
|
|
|
}
|
2022-02-25 10:07:49 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-05-25 06:44:18 +00:00
|
|
|
use core::convert::TryFrom;
|
|
|
|
|
2022-02-25 10:07:49 +00:00
|
|
|
use super::*;
|
|
|
|
|
2022-04-01 06:09:50 +00:00
|
|
|
// Composes tree matching a given depth map, filled with dumb script leafs,
|
|
|
|
// each of which consists of a single push-int op code, with int value
|
|
|
|
// increased for each consecutive leaf.
|
|
|
|
pub fn compose_taproot_builder<'map>(opcode: u8, depth_map: impl IntoIterator<Item = &'map u8>) -> TaprootBuilder {
|
|
|
|
let mut val = opcode;
|
|
|
|
let mut builder = TaprootBuilder::new();
|
|
|
|
for depth in depth_map {
|
2022-07-30 12:22:18 +00:00
|
|
|
let script = ScriptBuf::from_hex(&format!("{:02x}", val)).unwrap();
|
2022-04-01 06:09:50 +00:00
|
|
|
builder = builder.add_leaf(*depth, script).unwrap();
|
|
|
|
let (new_val, _) = val.overflowing_add(1);
|
|
|
|
val = new_val;
|
|
|
|
}
|
|
|
|
builder
|
|
|
|
}
|
|
|
|
|
2022-04-05 20:43:52 +00:00
|
|
|
#[test]
|
|
|
|
fn taptree_hidden() {
|
|
|
|
let mut builder = compose_taproot_builder(0x51, &[2, 2, 2]);
|
2022-07-30 12:22:18 +00:00
|
|
|
builder = builder.add_leaf_with_ver(3, ScriptBuf::from_hex("b9").unwrap(), LeafVersion::from_consensus(0xC2).unwrap()).unwrap();
|
2022-12-15 16:17:39 +00:00
|
|
|
builder = builder.add_hidden_node(3, TapNodeHash::all_zeros()).unwrap();
|
2022-05-25 06:44:18 +00:00
|
|
|
assert!(TapTree::try_from(builder).is_err());
|
2022-04-05 20:43:52 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 06:09:50 +00:00
|
|
|
#[test]
|
|
|
|
fn taptree_roundtrip() {
|
|
|
|
let mut builder = compose_taproot_builder(0x51, &[2, 2, 2, 3]);
|
2022-07-30 12:22:18 +00:00
|
|
|
builder = builder.add_leaf_with_ver(3, ScriptBuf::from_hex("b9").unwrap(), LeafVersion::from_consensus(0xC2).unwrap()).unwrap();
|
2022-05-25 06:44:18 +00:00
|
|
|
let tree = TapTree::try_from(builder).unwrap();
|
2022-04-01 06:09:50 +00:00
|
|
|
let tree_prime = TapTree::deserialize(&tree.serialize()).unwrap();
|
|
|
|
assert_eq!(tree, tree_prime);
|
|
|
|
}
|
|
|
|
|
2022-02-25 10:07:49 +00:00
|
|
|
#[test]
|
2022-03-28 22:50:43 +00:00
|
|
|
fn can_deserialize_non_standard_psbt_sighash_type() {
|
2022-02-25 10:07:49 +00:00
|
|
|
let non_standard_sighash = [222u8, 0u8, 0u8, 0u8]; // 32 byte value.
|
2022-03-28 21:58:07 +00:00
|
|
|
let sighash = PsbtSighashType::deserialize(&non_standard_sighash);
|
2022-02-25 10:07:49 +00:00
|
|
|
assert!(sighash.is_ok())
|
|
|
|
}
|
2022-05-04 11:28:14 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic(expected = "InvalidMagic")]
|
|
|
|
fn invalid_vector_1() {
|
|
|
|
let hex_psbt = b"0200000001268171371edff285e937adeea4b37b78000c0566cbb3ad64641713ca42171bf6000000006a473044022070b2245123e6bf474d60c5b50c043d4c691a5d2435f09a34a7662a9dc251790a022001329ca9dacf280bdf30740ec0390422422c81cb45839457aeb76fc12edd95b3012102657d118d3357b8e0f4c2cd46db7b39f6d9c38d9a70abcb9b2de5dc8dbfe4ce31feffffff02d3dff505000000001976a914d0c59903c5bac2868760e90fd521a4665aa7652088ac00e1f5050000000017a9143545e6e33b832c47050f24d3eeb93c9c03948bc787b32e1300";
|
|
|
|
PartiallySignedTransaction::deserialize(hex_psbt).unwrap();
|
|
|
|
}
|
2022-02-25 10:07:49 +00:00
|
|
|
}
|