2018-09-09 10:34:29 +00:00
|
|
|
// Rust Bitcoin Library
|
|
|
|
// Written by
|
|
|
|
// The Rust Bitcoin developers
|
|
|
|
//
|
|
|
|
// To the extent possible under law, the author(s) have dedicated all
|
|
|
|
// copyright and related and neighboring rights to this software to
|
|
|
|
// the public domain worldwide. This software is distributed without
|
|
|
|
// any warranty.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the CC0 Public Domain Dedication
|
|
|
|
// along with this software.
|
|
|
|
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
//
|
|
|
|
|
2018-08-08 06:36:51 +00:00
|
|
|
use std::error;
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
use blockdata::transaction::Transaction;
|
2018-08-10 15:28:48 +00:00
|
|
|
use util::psbt::raw;
|
2018-08-08 06:36:51 +00:00
|
|
|
|
2020-08-31 20:06:21 +00:00
|
|
|
use hashes;
|
|
|
|
|
2020-11-28 13:41:52 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
2020-08-31 20:06:21 +00:00
|
|
|
/// Enum for marking psbt hash error
|
|
|
|
pub enum PsbtHash {
|
|
|
|
Ripemd,
|
|
|
|
Sha256,
|
|
|
|
Hash160,
|
|
|
|
Hash256,
|
|
|
|
}
|
2018-08-08 06:36:51 +00:00
|
|
|
/// Ways that a Partially Signed Transaction might fail.
|
2020-11-28 13:41:52 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
2018-08-08 06:36:51 +00:00
|
|
|
pub enum Error {
|
|
|
|
/// Magic bytes for a PSBT must be the ASCII for "psbt" serialized in most
|
|
|
|
/// significant byte order.
|
|
|
|
InvalidMagic,
|
|
|
|
/// The separator for a PSBT must be `0xff`.
|
|
|
|
InvalidSeparator,
|
|
|
|
/// Known keys must be according to spec.
|
2018-08-10 15:28:48 +00:00
|
|
|
InvalidKey(raw::Key),
|
2018-08-08 06:36:51 +00:00
|
|
|
/// Keys within key-value map should never be duplicated.
|
2018-08-10 15:28:48 +00:00
|
|
|
DuplicateKey(raw::Key),
|
2018-08-08 06:36:51 +00:00
|
|
|
/// The scriptSigs for the unsigned transaction must be empty.
|
|
|
|
UnsignedTxHasScriptSigs,
|
|
|
|
/// The scriptWitnesses for the unsigned transaction must be empty.
|
|
|
|
UnsignedTxHasScriptWitnesses,
|
|
|
|
/// A PSBT must have an unsigned transaction.
|
|
|
|
MustHaveUnsignedTx,
|
|
|
|
/// Signals that there are no more key-value pairs in a key-value map.
|
|
|
|
NoMorePairs,
|
|
|
|
/// Attempting to merge with a PSBT describing a different unsigned
|
|
|
|
/// transaction.
|
|
|
|
UnexpectedUnsignedTx {
|
|
|
|
/// Expected
|
|
|
|
expected: Transaction,
|
|
|
|
/// Actual
|
|
|
|
actual: Transaction,
|
|
|
|
},
|
|
|
|
/// Unable to parse as a standard SigHash type.
|
|
|
|
NonStandardSigHashType(u32),
|
2020-08-31 20:06:21 +00:00
|
|
|
/// 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>,
|
2020-11-28 12:49:50 +00:00
|
|
|
},
|
|
|
|
/// Data inconsistency/conflicting data during merge procedure
|
|
|
|
MergeConflict(String),
|
2018-08-08 06:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
2020-03-29 13:49:48 +00:00
|
|
|
Error::InvalidKey(ref rkey) => write!(f, "invalid key: {}", rkey),
|
|
|
|
Error::DuplicateKey(ref rkey) => write!(f, "duplicate key: {}", rkey),
|
|
|
|
Error::UnexpectedUnsignedTx { expected: ref e, actual: ref a } => write!(f, "different unsigned transaction: expected {}, actual {}", e.txid(), a.txid()),
|
|
|
|
Error::NonStandardSigHashType(ref sht) => write!(f, "non-standard sighash type: {}", sht),
|
|
|
|
Error::InvalidMagic => f.write_str("invalid magic"),
|
|
|
|
Error::InvalidSeparator => f.write_str("invalid separator"),
|
|
|
|
Error::UnsignedTxHasScriptSigs => f.write_str("the unsigned transaction has script sigs"),
|
|
|
|
Error::UnsignedTxHasScriptWitnesses => f.write_str("the unsigned transaction has script witnesses"),
|
|
|
|
Error::MustHaveUnsignedTx => {
|
|
|
|
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"),
|
2020-08-31 20:06:21 +00:00
|
|
|
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 )
|
|
|
|
}
|
2020-11-28 12:49:50 +00:00
|
|
|
Error::MergeConflict(ref s) => { write!(f, "Merge conflict: {}", s) }
|
2018-08-08 06:36:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 14:56:46 +00:00
|
|
|
impl error::Error for Error {}
|
2020-08-31 20:06:21 +00:00
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
impl From<hashes::Error> for Error {
|
|
|
|
fn from(e: hashes::Error) -> Error {
|
|
|
|
Error::HashParseError(e)
|
|
|
|
}
|
|
|
|
}
|