Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[WIP] Impl conversion from v2::Psbt to v0::Psbt #33

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use bitcoin::hex::DisplayHex;

use crate::io::{self, BufRead, Write};
use crate::prelude::*;
use crate::serialize;
use crate::serialize::{Deserialize, Serialize};
use crate::{serialize, v0};

/// A PSBT key-value pair in its raw byte form.
///
Expand Down Expand Up @@ -112,6 +112,10 @@ impl Key {

Ok(Key { type_value, key })
}

pub(crate) fn into_v0(self) -> v0::bitcoin::raw::Key {
v0::bitcoin::raw::Key { type_value: self.type_value, key: self.key }
}
}

impl Serialize for Key {
Expand Down Expand Up @@ -160,6 +164,14 @@ where
{
/// Constructs full [Key] corresponding to this proprietary key type
pub fn to_key(&self) -> Key { Key { type_value: 0xFC, key: serialize(self) } }

pub(crate) fn into_v0(self) -> v0::bitcoin::raw::ProprietaryKey<Subtype> {
v0::bitcoin::raw::ProprietaryKey {
prefix: self.prefix,
subtype: self.subtype,
key: self.key,
}
}
}

impl<Subtype> TryFrom<Key> for ProprietaryKey<Subtype>
Expand Down
57 changes: 30 additions & 27 deletions src/v2/map/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::prelude::*;
use crate::serialize::{Deserialize, Serialize};
use crate::sighash_type::{InvalidSighashTypeError, PsbtSighashType};
use crate::v2::map::Map;
use crate::{raw, serialize};
use crate::{raw, serialize, v0};

/// A key-value map for an input of the corresponding index in the unsigned
/// transaction.
Expand Down Expand Up @@ -156,32 +156,35 @@ impl Input {
}
}

// /// Converts this `Input` to a `v0::Input`.
// pub(crate) fn into_v0(self) -> v0::Input {
// v0::Input {
// non_witness_utxo: self.non_witness_utxo,
// witness_utxo: self.witness_utxo,
// partial_sigs: self.partial_sigs,
// sighash_type: self.sighash_type,
// redeem_script: self.redeem_script,
// witness_script: self.witness_script,
// bip32_derivation: self.bip32_derivations,
// final_script_sig: self.final_script_sig,
// final_script_witness: self.final_script_witness,
// ripemd160_preimages: self.ripemd160_preimages,
// sha256_preimages: self.sha256_preimages,
// hash160_preimages: self.hash160_preimages,
// hash256_preimages: self.hash256_preimages,
// tap_key_sig: self.tap_key_sig,
// tap_script_sigs: self.tap_script_sigs,
// tap_scripts: self.tap_scripts,
// tap_key_origins: self.tap_key_origins,
// tap_internal_key: self.tap_internal_key,
// tap_merkle_root: self.tap_merkle_root,
// proprietary: self.proprietaries,
// unknown: self.unknowns,
// }
// }
/// Converts this `Input` to a `v0::Input`.
pub(crate) fn into_v0(self) -> v0::Input {
let proprietary = self.proprietaries.into_iter().map(|(k, v)| (k.into_v0(), v)).collect();
let unknown = self.unknowns.into_iter().map(|(k, v)| (k.into_v0(), v)).collect();

v0::Input {
non_witness_utxo: self.non_witness_utxo,
witness_utxo: self.witness_utxo,
partial_sigs: self.partial_sigs,
sighash_type: self.sighash_type,
redeem_script: self.redeem_script,
witness_script: self.witness_script,
bip32_derivation: self.bip32_derivations,
final_script_sig: self.final_script_sig,
final_script_witness: self.final_script_witness,
ripemd160_preimages: self.ripemd160_preimages,
sha256_preimages: self.sha256_preimages,
hash160_preimages: self.hash160_preimages,
hash256_preimages: self.hash256_preimages,
tap_key_sig: self.tap_key_sig,
tap_script_sigs: self.tap_script_sigs,
tap_scripts: self.tap_scripts,
tap_key_origins: self.tap_key_origins,
tap_internal_key: self.tap_internal_key,
tap_merkle_root: self.tap_merkle_root,
proprietary,
unknown,
}
}

/// Creates a new finalized input.
///
Expand Down
31 changes: 17 additions & 14 deletions src/v2/map/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::error::write_err;
use crate::prelude::*;
use crate::serialize::{Deserialize, Serialize};
use crate::v2::map::Map;
use crate::{raw, serialize};
use crate::{raw, serialize, v0};

/// A key-value map for an output of the corresponding index in the unsigned
/// transaction.
Expand Down Expand Up @@ -72,19 +72,22 @@ impl Output {
}
}

// /// Converts this `Output` to a `v0::Output`.
// pub(crate) fn into_v0(self) -> v0::Output {
// v0::Output {
// redeem_script: self.redeem_script,
// witness_script: self.witness_script,
// bip32_derivation: self.bip32_derivations,
// tap_internal_key: self.tap_internal_key,
// tap_tree: self.tap_tree,
// tap_key_origins: self.tap_key_origins,
// proprietary: self.proprietaries,
// unknown: self.unknowns,
// }
// }
/// Converts this `Output` to a `v0::Output`.
pub(crate) fn into_v0(self) -> v0::Output {
let proprietary = self.proprietaries.into_iter().map(|(k, v)| (k.into_v0(), v)).collect();
let unknown = self.unknowns.into_iter().map(|(k, v)| (k.into_v0(), v)).collect();

v0::Output {
redeem_script: self.redeem_script,
witness_script: self.witness_script,
bip32_derivation: self.bip32_derivations,
tap_internal_key: self.tap_internal_key,
tap_tree: self.tap_tree,
tap_key_origins: self.tap_key_origins,
proprietary,
unknown,
}
}

/// Creates the [`TxOut`] associated with this `Output`.
pub(crate) fn tx_out(&self) -> TxOut {
Expand Down
38 changes: 26 additions & 12 deletions src/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use bitcoin::{ecdsa, transaction, Amount, Sequence, Transaction, TxOut, Txid};
use crate::error::{write_err, FeeError, FundingUtxoError};
use crate::prelude::*;
use crate::v2::map::Map;
use crate::{v0, Version};

#[rustfmt::skip] // Keep public exports separate.
#[doc(inline)]
Expand Down Expand Up @@ -404,18 +405,31 @@ impl Updater {
Ok(self)
}

// /// Converts the inner PSBT v2 to a PSBT v0.
// pub fn into_psbt_v0(self) -> v0::Psbt {
// let unsigned_tx =
// self.0.unsigned_tx().expect("Updater guarantees lock time can be determined");
// let psbt = self.psbt();

// let global = psbt.global.into_v0(unsigned_tx);
// let inputs = psbt.inputs.into_iter().map(|input| input.into_v0()).collect();
// let outputs = psbt.outputs.into_iter().map(|output| output.into_v0()).collect();

// v0::Psbt { global, inputs, outputs }
// }
/// Converts the inner PSBT v2 to a PSBT v0.
///
/// Conversion is lossy because PSBT v2 introduced global types not present in v0.
/// See [BIP-370] for a list of differences.
///
/// [BIP-370]: <https://github.com/bitcoin/bips/blob/master/bip-0370.mediawiki#specification>
pub fn into_psbt_v0(self) -> v0::Psbt {
let unsigned_tx =
self.0.unsigned_tx().expect("Updater guarantees lock time can be determined");
let inputs = self.0.inputs.into_iter().map(|input| input.into_v0()).collect();
let outputs = self.0.outputs.into_iter().map(|output| output.into_v0()).collect();
let proprietary =
self.0.global.proprietaries.into_iter().map(|(k, v)| (k.into_v0(), v)).collect();
let unknown = self.0.global.unknowns.into_iter().map(|(k, v)| (k.into_v0(), v)).collect();

v0::Psbt {
unsigned_tx,
inputs,
outputs,
version: Version::ZERO.to_u32(),
xpub: self.0.global.xpubs,
proprietary,
unknown,
}
}

/// Returns the inner [`Psbt`].
pub fn psbt(self) -> Psbt { self.0 }
Expand Down
Loading