Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix Taptree Strict Encode/Decode #95

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 5 additions & 19 deletions strict_encoding/src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
use std::io;
use std::io::{Read, Write};

use bitcoin::psbt::serialize::{Deserialize, Serialize};
use bitcoin::psbt::{self, PsbtSighashType, TapTree};
use bitcoin::secp256k1::{ecdsa, schnorr, Secp256k1};
use bitcoin::util::address::{self, Address, WitnessVersion};
use bitcoin::util::bip32;
use bitcoin::util::taproot::{
ControlBlock, FutureLeafVersion, LeafVersion, ScriptLeaf, TapBranchHash,
TapLeafHash, TapSighashHash, TapTweakHash, TaprootBuilder,
TaprootMerkleBranch,
TapLeafHash, TapSighashHash, TapTweakHash, TaprootMerkleBranch,
};
use bitcoin::{
schnorr as bip340, secp256k1, Amount, BlockHash, EcdsaSig,
Expand Down Expand Up @@ -742,28 +742,14 @@ impl Strategy for psbt::PartiallySignedTransaction {

impl StrictEncode for TapTree {
fn strict_encode<E: Write>(&self, e: E) -> Result<usize, Error> {
impl StrictEncode for &Script {
fn strict_encode<E: Write>(&self, e: E) -> Result<usize, Error> {
Script::strict_encode(self, e)
}
}
self.script_leaves()
.cloned()
.collect::<Vec<_>>()
.strict_encode(e)
self.serialize().strict_encode(e)
}
}

impl StrictDecode for TapTree {
fn strict_decode<D: Read>(d: D) -> Result<Self, Error> {
let builder = Vec::<(u8, Script)>::strict_decode(d)?
.into_iter()
.try_fold(TaprootBuilder::new(), |builder, (depth, script)| {
builder.add_leaf(depth, script)
})
.map_err(|err| Error::DataIntegrityError(err.to_string()))?;
TapTree::try_from(builder)
.map_err(|_| Error::DataIntegrityError(s!("incomplete tree")))
Ok(TapTree::deserialize(&Vec::<u8>::strict_decode(d)?)
.expect("incomplete tree"))
}
}

Expand Down