Added Hash preimages to psbt
Added hash preimages to psbt as per updated bip174
This commit is contained in:
parent
926cff0741
commit
48151ef204
|
@ -18,6 +18,16 @@ use std::fmt;
|
||||||
use blockdata::transaction::Transaction;
|
use blockdata::transaction::Transaction;
|
||||||
use util::psbt::raw;
|
use util::psbt::raw;
|
||||||
|
|
||||||
|
use hashes;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
/// Enum for marking psbt hash error
|
||||||
|
pub enum PsbtHash {
|
||||||
|
Ripemd,
|
||||||
|
Sha256,
|
||||||
|
Hash160,
|
||||||
|
Hash256,
|
||||||
|
}
|
||||||
/// Ways that a Partially Signed Transaction might fail.
|
/// Ways that a Partially Signed Transaction might fail.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
@ -48,6 +58,17 @@ pub enum Error {
|
||||||
},
|
},
|
||||||
/// Unable to parse as a standard SigHash type.
|
/// Unable to parse as a standard SigHash type.
|
||||||
NonStandardSigHashType(u32),
|
NonStandardSigHashType(u32),
|
||||||
|
/// Parsing errors from bitcoin_hashes
|
||||||
|
HashParseError(hashes::Error),
|
||||||
|
/// The pre-image must hash to the correponding psbt hash
|
||||||
|
InvalidPreimageHashPair {
|
||||||
|
/// Hash-type
|
||||||
|
hash_type: PsbtHash,
|
||||||
|
/// Pre-image
|
||||||
|
preimage: Vec<u8>,
|
||||||
|
/// Hash value
|
||||||
|
hash: Vec<u8>,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
impl fmt::Display for Error {
|
||||||
|
@ -65,8 +86,20 @@ impl fmt::Display for Error {
|
||||||
f.write_str("partially signed transactions must have an unsigned transaction")
|
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::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, ref hash_type} => {
|
||||||
|
// directly using debug forms of psbthash enums
|
||||||
|
write!(f, "Preimage {:?} does not match {:?} hash {:?}", preimage, hash_type, hash )
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl error::Error for Error {}
|
impl error::Error for Error {}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
impl From<hashes::Error> for Error {
|
||||||
|
fn from(e: hashes::Error) -> Error {
|
||||||
|
Error::HashParseError(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -130,6 +130,7 @@ macro_rules! impl_psbt_insert_pair {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg_attr(rustfmt, rustfmt_skip)]
|
#[cfg_attr(rustfmt, rustfmt_skip)]
|
||||||
macro_rules! impl_psbt_get_pair {
|
macro_rules! impl_psbt_get_pair {
|
||||||
($rv:ident.push($slf:ident.$unkeyed_name:ident as <$unkeyed_typeval:expr, _>|<$unkeyed_value_type:ty>)) => {
|
($rv:ident.push($slf:ident.$unkeyed_name:ident as <$unkeyed_typeval:expr, _>|<$unkeyed_value_type:ty>)) => {
|
||||||
|
@ -155,3 +156,33 @@ macro_rules! impl_psbt_get_pair {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// macros for serde of hashes
|
||||||
|
macro_rules! impl_psbt_hash_de_serialize {
|
||||||
|
($hash_type:ty) => {
|
||||||
|
impl_psbt_hash_serialize!($hash_type);
|
||||||
|
impl_psbt_hash_deserialize!($hash_type);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! impl_psbt_hash_deserialize {
|
||||||
|
($hash_type:ty) => {
|
||||||
|
impl $crate::util::psbt::serialize::Deserialize for $hash_type {
|
||||||
|
fn deserialize(bytes: &[u8]) -> Result<Self, $crate::consensus::encode::Error> {
|
||||||
|
<$hash_type>::from_slice(&bytes[..]).map_err(|e| {
|
||||||
|
$crate::util::psbt::Error::from(e).into()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! impl_psbt_hash_serialize {
|
||||||
|
($hash_type:ty) => {
|
||||||
|
impl $crate::util::psbt::serialize::Serialize for $hash_type {
|
||||||
|
fn serialize(&self) -> Vec<u8> {
|
||||||
|
self.into_inner().to_vec()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
@ -12,18 +12,19 @@
|
||||||
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
|
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
|
||||||
//
|
//
|
||||||
|
|
||||||
use std::collections::BTreeMap;
|
use std::collections::btree_map::{Entry, BTreeMap};
|
||||||
|
|
||||||
use blockdata::script::Script;
|
use blockdata::script::Script;
|
||||||
use blockdata::transaction::{SigHashType, Transaction, TxOut};
|
use blockdata::transaction::{SigHashType, Transaction, TxOut};
|
||||||
use consensus::encode;
|
use consensus::encode;
|
||||||
use util::bip32::KeySource;
|
use util::bip32::KeySource;
|
||||||
|
use hashes::{self, hash160, ripemd160, sha256, sha256d};
|
||||||
use util::key::PublicKey;
|
use util::key::PublicKey;
|
||||||
use util::psbt;
|
use util::psbt;
|
||||||
use util::psbt::map::Map;
|
use util::psbt::map::Map;
|
||||||
use util::psbt::raw;
|
use util::psbt::raw;
|
||||||
use util::psbt::Error;
|
use util::psbt::serialize::Deserialize;
|
||||||
|
use util::psbt::{Error, error};
|
||||||
/// A key-value map for an input of the corresponding index in the unsigned
|
/// A key-value map for an input of the corresponding index in the unsigned
|
||||||
/// transaction.
|
/// transaction.
|
||||||
#[derive(Clone, Default, Debug, PartialEq)]
|
#[derive(Clone, Default, Debug, PartialEq)]
|
||||||
|
@ -55,6 +56,15 @@ pub struct Input {
|
||||||
/// The finalized, fully-constructed scriptWitness with signatures and any
|
/// The finalized, fully-constructed scriptWitness with signatures and any
|
||||||
/// other scripts necessary for this input to pass validation.
|
/// other scripts necessary for this input to pass validation.
|
||||||
pub final_script_witness: Option<Vec<Vec<u8>>>,
|
pub final_script_witness: Option<Vec<Vec<u8>>>,
|
||||||
|
/// TODO: Proof of reserves commitment
|
||||||
|
/// RIPEMD hash to preimage map
|
||||||
|
pub ripemd_preimages: BTreeMap<ripemd160::Hash, Vec<u8>>,
|
||||||
|
/// SHA256 hash to preimage map
|
||||||
|
pub sha256_preimages: BTreeMap<sha256::Hash, Vec<u8>>,
|
||||||
|
/// HSAH160 hash to preimage map
|
||||||
|
pub hash160_preimages: BTreeMap<hash160::Hash, Vec<u8>>,
|
||||||
|
/// HAS256 hash to preimage map
|
||||||
|
pub hash256_preimages: BTreeMap<sha256d::Hash, Vec<u8>>,
|
||||||
/// Unknown key-value pairs for this input.
|
/// Unknown key-value pairs for this input.
|
||||||
pub unknown: BTreeMap<raw::Key, Vec<u8>>,
|
pub unknown: BTreeMap<raw::Key, Vec<u8>>,
|
||||||
}
|
}
|
||||||
|
@ -112,10 +122,26 @@ impl Map for Input {
|
||||||
self.hd_keypaths <= <raw_key: PublicKey>|<raw_value: KeySource>
|
self.hd_keypaths <= <raw_key: PublicKey>|<raw_value: KeySource>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => match self.unknown.entry(raw_key) {
|
10u8 => {
|
||||||
::std::collections::btree_map::Entry::Vacant(empty_key) => {empty_key.insert(raw_value);},
|
psbt_insert_hash_pair(&mut self.ripemd_preimages, raw_key, raw_value, error::PsbtHash::Ripemd)?;
|
||||||
::std::collections::btree_map::Entry::Occupied(k) => return Err(Error::DuplicateKey(k.key().clone()).into()),
|
|
||||||
}
|
}
|
||||||
|
11u8 => {
|
||||||
|
psbt_insert_hash_pair(&mut self.sha256_preimages, raw_key, raw_value, error::PsbtHash::Sha256)?;
|
||||||
|
}
|
||||||
|
12u8 => {
|
||||||
|
psbt_insert_hash_pair(&mut self.hash160_preimages, raw_key, raw_value, error::PsbtHash::Hash160)?;
|
||||||
|
}
|
||||||
|
13u8 => {
|
||||||
|
psbt_insert_hash_pair(&mut self.hash256_preimages, raw_key, raw_value, error::PsbtHash::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())
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -160,6 +186,22 @@ impl Map for Input {
|
||||||
rv.push(self.final_script_witness as <8u8, _>|<Script>)
|
rv.push(self.final_script_witness as <8u8, _>|<Script>)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl_psbt_get_pair! {
|
||||||
|
rv.push(self.ripemd_preimages as <10u8, ripemd160::Hash>|<Vec<u8>>)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl_psbt_get_pair! {
|
||||||
|
rv.push(self.sha256_preimages as <11u8, sha256::Hash>|<Vec<u8>>)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl_psbt_get_pair! {
|
||||||
|
rv.push(self.hash160_preimages as <12u8, hash160::Hash>|<Vec<u8>>)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl_psbt_get_pair! {
|
||||||
|
rv.push(self.hash256_preimages as <13u8, sha256d::Hash>|<Vec<u8>>)
|
||||||
|
}
|
||||||
|
|
||||||
for (key, value) in self.unknown.iter() {
|
for (key, value) in self.unknown.iter() {
|
||||||
rv.push(raw::Pair {
|
rv.push(raw::Pair {
|
||||||
key: key.clone(),
|
key: key.clone(),
|
||||||
|
@ -180,6 +222,10 @@ impl Map for Input {
|
||||||
|
|
||||||
self.partial_sigs.extend(other.partial_sigs);
|
self.partial_sigs.extend(other.partial_sigs);
|
||||||
self.hd_keypaths.extend(other.hd_keypaths);
|
self.hd_keypaths.extend(other.hd_keypaths);
|
||||||
|
self.ripemd_preimages.extend(other.ripemd_preimages);
|
||||||
|
self.sha256_preimages.extend(other.sha256_preimages);
|
||||||
|
self.hash160_preimages.extend(other.hash160_preimages);
|
||||||
|
self.hash256_preimages.extend(other.hash256_preimages);
|
||||||
self.unknown.extend(other.unknown);
|
self.unknown.extend(other.unknown);
|
||||||
|
|
||||||
merge!(redeem_script, self, other);
|
merge!(redeem_script, self, other);
|
||||||
|
@ -192,3 +238,34 @@ impl Map for Input {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_psbtmap_consensus_enc_dec_oding!(Input);
|
impl_psbtmap_consensus_enc_dec_oding!(Input);
|
||||||
|
|
||||||
|
fn psbt_insert_hash_pair<H>(
|
||||||
|
map: &mut BTreeMap<H, Vec<u8>>,
|
||||||
|
raw_key: raw::Key,
|
||||||
|
raw_value: Vec<u8>,
|
||||||
|
hash_type: error::PsbtHash,
|
||||||
|
) -> Result<(), encode::Error>
|
||||||
|
where
|
||||||
|
H: hashes::Hash + Deserialize,
|
||||||
|
{
|
||||||
|
if raw_key.key.is_empty() {
|
||||||
|
return Err(psbt::Error::InvalidKey(raw_key).into());
|
||||||
|
}
|
||||||
|
let key_val: H = Deserialize::deserialize(&raw_key.key)?;
|
||||||
|
match map.entry(key_val) {
|
||||||
|
Entry::Vacant(empty_key) => {
|
||||||
|
let val: Vec<u8> = Deserialize::deserialize(&raw_value)?;
|
||||||
|
if <H as hashes::Hash>::hash(&val) != key_val {
|
||||||
|
return Err(psbt::Error::InvalidPreimageHashPair {
|
||||||
|
preimage: val,
|
||||||
|
hash: Vec::from(key_val.borrow()),
|
||||||
|
hash_type: hash_type,
|
||||||
|
}
|
||||||
|
.into());
|
||||||
|
}
|
||||||
|
empty_key.insert(val);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Entry::Occupied(_) => return Err(psbt::Error::DuplicateKey(raw_key).into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@ use blockdata::script::Script;
|
||||||
use blockdata::transaction::{SigHashType, Transaction, TxOut};
|
use blockdata::transaction::{SigHashType, Transaction, TxOut};
|
||||||
use consensus::encode::{self, serialize, Decodable};
|
use consensus::encode::{self, serialize, Decodable};
|
||||||
use util::bip32::{ChildNumber, Fingerprint, KeySource};
|
use util::bip32::{ChildNumber, Fingerprint, KeySource};
|
||||||
|
use hashes::{hash160, ripemd160, sha256, sha256d, Hash};
|
||||||
use util::key::PublicKey;
|
use util::key::PublicKey;
|
||||||
use util::psbt;
|
use util::psbt;
|
||||||
|
|
||||||
|
@ -42,6 +43,10 @@ pub trait Deserialize: Sized {
|
||||||
impl_psbt_de_serialize!(Transaction);
|
impl_psbt_de_serialize!(Transaction);
|
||||||
impl_psbt_de_serialize!(TxOut);
|
impl_psbt_de_serialize!(TxOut);
|
||||||
impl_psbt_de_serialize!(Vec<Vec<u8>>); // scriptWitness
|
impl_psbt_de_serialize!(Vec<Vec<u8>>); // scriptWitness
|
||||||
|
impl_psbt_hash_de_serialize!(ripemd160::Hash);
|
||||||
|
impl_psbt_hash_de_serialize!(sha256::Hash);
|
||||||
|
impl_psbt_hash_de_serialize!(hash160::Hash);
|
||||||
|
impl_psbt_hash_de_serialize!(sha256d::Hash);
|
||||||
|
|
||||||
impl Serialize for Script {
|
impl Serialize for Script {
|
||||||
fn serialize(&self) -> Vec<u8> {
|
fn serialize(&self) -> Vec<u8> {
|
||||||
|
|
Loading…
Reference in New Issue