From c94295c3a9759f84edbe748345850f594a4cb638 Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Fri, 11 Sep 2020 11:31:10 +0000 Subject: [PATCH] Revert "Added hash Preimages to psbt" --- .gitignore | 3 - src/util/psbt/error.rs | 66 ------------ src/util/psbt/macros.rs | 54 ---------- src/util/psbt/map/input.rs | 61 +----------- src/util/psbt/mod.rs | 199 +------------------------------------ src/util/psbt/serialize.rs | 5 - 6 files changed, 7 insertions(+), 381 deletions(-) diff --git a/.gitignore b/.gitignore index 8aabe6be..93426a2c 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,3 @@ fuzz/hfuzz_workspace #IntelliJ project files .idea *.iml - -#Vscode project files -.vscode diff --git a/src/util/psbt/error.rs b/src/util/psbt/error.rs index a23f5156..15ab268d 100644 --- a/src/util/psbt/error.rs +++ b/src/util/psbt/error.rs @@ -18,17 +18,6 @@ use std::fmt; use blockdata::transaction::Transaction; use util::psbt::raw; -use hashes::{self, sha256, hash160, sha256d, ripemd160}; -use hash_types::Txid; - -/// Support hash-preimages in psbt -#[derive(Debug)] -pub enum PsbtHash{ - Ripemd160(ripemd160::Hash), - Sha256(sha256::Hash), - Hash256(sha256d::Hash), - Hash160(hash160::Hash), -} /// Ways that a Partially Signed Transaction might fail. #[derive(Debug)] pub enum Error { @@ -59,37 +48,6 @@ pub enum Error { }, /// Unable to parse as a standard SigHash type. NonStandardSigHashType(u32), - /// Parsing errors from bitcoin_hashes - HashParseError(hashes::Error), - /// The pre-image must hash to the correponding psbt hash - InvalidPreimageHashPair{ - /// Pre-image - preimage: Vec, - /// Hash value - hash: PsbtHash, - }, - /// If NonWitnessUtxo is used, the nonWitnessUtxo txid must - /// be the same of prevout txid - InvalidNonWitnessUtxo{ - /// Pre-image - prevout_txid: Txid, - /// Hash value - non_witness_utxo_txid: Txid, - }, - /// Incorrect P2sh/p2wsh script hash for the witness/redeem - /// script - InvalidWitnessScript{ - /// Expected Witness/Redeem Script Hash - // returns a vec to unify the p2wsh(sha2) and p2sh(hash160) - expected: Vec, - /// Actual Witness script Hash - actual: Vec, - }, - /// Currently only p2wpkh and p2wsh scripts are possible in segwit - UnrecognizedWitnessProgram, - /// The psbt input must either have an associated nonWitnessUtxo or - /// a WitnessUtxo - MustHaveSpendingUtxo, } impl fmt::Display for Error { @@ -107,23 +65,6 @@ impl fmt::Display for Error { f.write_str("partially signed transactions must have an unsigned transaction") } Error::NoMorePairs => f.write_str("no more key-value pairs for this psbt map"), - Error::HashParseError(e) => write!(f, "Hash Parse Error: {}", e), - Error::InvalidPreimageHashPair{ref preimage, ref hash} => { - // directly using debug forms of psbthash enums - write!(f, "Preimage {:?} does not match hash {:?}", preimage, hash ) - }, - Error::InvalidNonWitnessUtxo{ref prevout_txid, ref non_witness_utxo_txid} => { - write!(f, "NonWitnessUtxo txid {} must be the same as prevout txid {}", non_witness_utxo_txid, prevout_txid) - }, - Error::InvalidWitnessScript{ref expected, ref actual} => { - write!(f, "Invalid Witness/Redeem script: Expected {:?}, got {:?}", expected, actual) - } - Error::UnrecognizedWitnessProgram => { - f.write_str("Witness program must be p2wpkh/p2wsh") - } - Error::MustHaveSpendingUtxo => { - f.write_str("Input must either WitnessUtxo/ NonWitnessUtxo") - } } } } @@ -134,10 +75,3 @@ impl error::Error for Error { "description() is deprecated; use Display" } } - -#[doc(hidden)] -impl From for Error { - fn from(e: hashes::Error) -> Error { - Error::HashParseError(e) - } -} diff --git a/src/util/psbt/macros.rs b/src/util/psbt/macros.rs index 0901fcda..9c3bcab0 100644 --- a/src/util/psbt/macros.rs +++ b/src/util/psbt/macros.rs @@ -130,30 +130,6 @@ macro_rules! impl_psbt_insert_pair { }; } -#[cfg_attr(rustfmt, rustfmt_skip)] -macro_rules! impl_psbt_insert_hash_pair { - ($slf:ident.$keyed_name:ident <= <$raw_key:ident: $keyed_key_type:ty>|<$raw_value:ident: $keyed_value_type:ty>; $err_name: ident ) => { - if !$raw_key.key.is_empty() { - let key_val: $keyed_key_type = $crate::util::psbt::serialize::Deserialize::deserialize(&$raw_key.key)?; - match $slf.$keyed_name.entry(key_val) { - ::std::collections::btree_map::Entry::Vacant(empty_key) => { - let val: $keyed_value_type = $crate::util::psbt::serialize::Deserialize::deserialize(&$raw_value)?; - if <$keyed_key_type>::hash(&val) != key_val{ - return Err($crate::util::psbt::Error::InvalidPreimageHashPair{ - preimage: val, - hash: $crate::util::psbt::error::PsbtHash::$err_name(key_val), - }.into()); - } - empty_key.insert(val); - } - ::std::collections::btree_map::Entry::Occupied(_) => return Err($crate::util::psbt::Error::DuplicateKey($raw_key).into()), - } - } else { - return Err($crate::util::psbt::Error::InvalidKey($raw_key).into()); - } - }; -} - #[cfg_attr(rustfmt, rustfmt_skip)] macro_rules! impl_psbt_get_pair { ($rv:ident.push($slf:ident.$unkeyed_name:ident as <$unkeyed_typeval:expr, _>|<$unkeyed_value_type:ty>)) => { @@ -179,33 +155,3 @@ macro_rules! impl_psbt_get_pair { } }; } - -// macros for serde of hashes -macro_rules! impl_psbt_hash_de_serialize { - ($thing:ty) => { - impl_psbt_hash_serialize!($thing); - impl_psbt_hash_deserialize!($thing); - }; -} - -macro_rules! impl_psbt_hash_deserialize { - ($thing:ty) => { - impl $crate::util::psbt::serialize::Deserialize for $thing { - fn deserialize(bytes: &[u8]) -> Result { - <$thing>::from_slice(&bytes[..]).map_err(|e| { - $crate::util::psbt::Error::from(e).into() - }) - } - } - }; -} - -macro_rules! impl_psbt_hash_serialize { - ($thing:ty) => { - impl $crate::util::psbt::serialize::Serialize for $thing { - fn serialize(&self) -> Vec { - self.into_inner().to_vec() - } - } - }; -} diff --git a/src/util/psbt/map/input.rs b/src/util/psbt/map/input.rs index 9f22cbab..05a24432 100644 --- a/src/util/psbt/map/input.rs +++ b/src/util/psbt/map/input.rs @@ -17,13 +17,13 @@ use std::collections::BTreeMap; use blockdata::script::Script; use blockdata::transaction::{SigHashType, Transaction, TxOut}; use consensus::encode; -use hashes::{Hash, hash160, ripemd160, sha256, sha256d}; use util::bip32::{DerivationPath, Fingerprint}; use util::key::PublicKey; use util::psbt; use util::psbt::map::Map; use util::psbt::raw; use util::psbt::Error; + /// A key-value map for an input of the corresponding index in the unsigned /// transaction. #[derive(Clone, Default, Debug, PartialEq)] @@ -55,15 +55,6 @@ pub struct Input { /// The finalized, fully-constructed scriptWitness with signatures and any /// other scripts necessary for this input to pass validation. pub final_script_witness: Option>>, - /// TODO: Proof of reserves commitment - /// RIPEMD hash to preimage map - pub ripemd_preimages: BTreeMap>, - /// SHA256 hash to preimage map - pub sha256_preimages: BTreeMap>, - /// HSAH160 hash to preimage map - pub hash160_preimages: BTreeMap>, - /// HAS256 hash to preimage map - pub hash256_preimages: BTreeMap>, /// Unknown key-value pairs for this input. pub unknown: BTreeMap>, } @@ -121,34 +112,10 @@ impl Map for Input { self.hd_keypaths <= | } } - 10u8 => { - impl_psbt_insert_hash_pair! { - self.ripemd_preimages <= |>; Ripemd160 - } - } - 11u8 => { - impl_psbt_insert_hash_pair! { - self.sha256_preimages <= |>; Sha256 - } - } - 12u8 => { - impl_psbt_insert_hash_pair! { - self.hash160_preimages <= |>; Hash160 - } - } - 13u8 => { - impl_psbt_insert_hash_pair! { - self.hash256_preimages <= |>; Hash256 - } - } _ => match self.unknown.entry(raw_key) { - ::std::collections::btree_map::Entry::Vacant(empty_key) => { - empty_key.insert(raw_value); - } - ::std::collections::btree_map::Entry::Occupied(k) => { - return Err(Error::DuplicateKey(k.key().clone()).into()) - } - }, + ::std::collections::btree_map::Entry::Vacant(empty_key) => {empty_key.insert(raw_value);}, + ::std::collections::btree_map::Entry::Occupied(k) => return Err(Error::DuplicateKey(k.key().clone()).into()), + } } Ok(()) @@ -193,22 +160,6 @@ impl Map for Input { rv.push(self.final_script_witness as <8u8, _>|