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/>.
|
|
|
|
//
|
|
|
|
|
2022-05-02 22:13:57 +00:00
|
|
|
use crate::prelude::*;
|
2021-06-09 10:34:44 +00:00
|
|
|
|
2021-06-09 10:40:41 +00:00
|
|
|
use core::fmt;
|
2018-08-08 06:36:51 +00:00
|
|
|
|
2022-05-02 22:13:57 +00:00
|
|
|
use crate::blockdata::transaction::Transaction;
|
|
|
|
use crate::consensus::encode;
|
|
|
|
use crate::util::psbt::raw;
|
2018-08-08 06:36:51 +00:00
|
|
|
|
2022-05-02 22:13:57 +00:00
|
|
|
use crate::hashes;
|
|
|
|
use crate::util::bip32::ExtendedPubKey;
|
2020-08-31 20:06:21 +00:00
|
|
|
|
2022-01-06 02:04:47 +00:00
|
|
|
/// Enum for marking psbt hash error.
|
2021-01-30 13:09:34 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
2020-08-31 20:06:21 +00:00
|
|
|
pub enum PsbtHash {
|
|
|
|
Ripemd,
|
|
|
|
Sha256,
|
|
|
|
Hash160,
|
|
|
|
Hash256,
|
|
|
|
}
|
2018-08-08 06:36:51 +00:00
|
|
|
/// Ways that a Partially Signed Transaction might fail.
|
2021-01-30 13:09:34 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, 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,
|
2022-02-28 14:01:02 +00:00
|
|
|
/// Missing both the witness and non-witness utxo.
|
|
|
|
MissingUtxo,
|
2018-08-08 06:36:51 +00:00
|
|
|
/// The separator for a PSBT must be `0xff`.
|
|
|
|
InvalidSeparator,
|
2022-02-28 14:01:02 +00:00
|
|
|
/// Returned when output index is out of bounds in relation to the output in non-witness UTXO.
|
|
|
|
PsbtUtxoOutOfbounds,
|
2018-08-08 06:36:51 +00:00
|
|
|
/// Known keys must be according to spec.
|
2018-08-10 15:28:48 +00:00
|
|
|
InvalidKey(raw::Key),
|
2020-12-05 14:10:44 +00:00
|
|
|
/// Non-proprietary key type found when proprietary key was expected
|
|
|
|
InvalidProprietaryKey,
|
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,
|
2022-02-22 13:41:20 +00:00
|
|
|
/// Attempting to combine with a PSBT describing a different unsigned
|
2018-08-08 06:36:51 +00:00
|
|
|
/// transaction.
|
|
|
|
UnexpectedUnsignedTx {
|
|
|
|
/// Expected
|
2020-11-18 09:33:36 +00:00
|
|
|
expected: Box<Transaction>,
|
2018-08-08 06:36:51 +00:00
|
|
|
/// Actual
|
2020-11-18 09:33:36 +00:00
|
|
|
actual: Box<Transaction>,
|
2018-08-08 06:36:51 +00:00
|
|
|
},
|
2022-03-28 23:45:46 +00:00
|
|
|
/// Unable to parse as a standard sighash type.
|
2022-03-28 21:56:36 +00:00
|
|
|
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
|
2020-11-18 09:48:29 +00:00
|
|
|
preimage: Box<[u8]>,
|
2020-08-31 20:06:21 +00:00
|
|
|
/// Hash value
|
2020-11-18 09:48:29 +00:00
|
|
|
hash: Box<[u8]>,
|
2020-11-28 12:49:50 +00:00
|
|
|
},
|
2022-02-22 13:41:20 +00:00
|
|
|
/// Conflicting data during combine procedure:
|
2021-01-30 13:32:44 +00:00
|
|
|
/// global extended public key has inconsistent key sources
|
2022-05-19 15:05:42 +00:00
|
|
|
CombineInconsistentKeySources(Box<ExtendedPubKey>),
|
2020-12-05 14:10:44 +00:00
|
|
|
/// Serialization error in bitcoin consensus-encoded structures
|
|
|
|
ConsensusEncoding,
|
2018-08-08 06:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
2022-05-04 05:02:18 +00:00
|
|
|
Error::InvalidMagic => f.write_str("invalid magic"),
|
|
|
|
Error::MissingUtxo => f.write_str("UTXO information is not present in PSBT"),
|
|
|
|
Error::InvalidSeparator => f.write_str("invalid separator"),
|
|
|
|
Error::PsbtUtxoOutOfbounds => f.write_str("output index is out of bounds of non witness script output array"),
|
2020-03-29 13:49:48 +00:00
|
|
|
Error::InvalidKey(ref rkey) => write!(f, "invalid key: {}", rkey),
|
2020-12-05 14:10:44 +00:00
|
|
|
Error::InvalidProprietaryKey => write!(f, "non-proprietary key type found when proprietary key was expected"),
|
2020-03-29 13:49:48 +00:00
|
|
|
Error::DuplicateKey(ref rkey) => write!(f, "duplicate key: {}", rkey),
|
|
|
|
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"),
|
2022-05-04 05:02:18 +00:00
|
|
|
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),
|
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 )
|
2022-05-04 05:02:18 +00:00
|
|
|
},
|
|
|
|
Error::CombineInconsistentKeySources(ref s) => { write!(f, "combine conflict: {}", s) },
|
2021-01-27 20:19:04 +00:00
|
|
|
Error::ConsensusEncoding => f.write_str("bitcoin consensus or BIP-174 encoding error"),
|
2018-08-08 06:36:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 10:34:44 +00:00
|
|
|
#[cfg(feature = "std")]
|
2022-05-04 05:56:24 +00:00
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
|
|
|
impl std::error::Error for Error {
|
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
|
|
use self::Error::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
HashParseError(e) => Some(e),
|
|
|
|
| InvalidMagic
|
|
|
|
| MissingUtxo
|
|
|
|
| InvalidSeparator
|
|
|
|
| PsbtUtxoOutOfbounds
|
|
|
|
| InvalidKey(_)
|
|
|
|
| InvalidProprietaryKey
|
|
|
|
| DuplicateKey(_)
|
|
|
|
| UnsignedTxHasScriptSigs
|
|
|
|
| UnsignedTxHasScriptWitnesses
|
|
|
|
| MustHaveUnsignedTx
|
|
|
|
| NoMorePairs
|
|
|
|
| UnexpectedUnsignedTx { .. }
|
|
|
|
| NonStandardSighashType(_)
|
|
|
|
| InvalidPreimageHashPair{ .. }
|
|
|
|
| CombineInconsistentKeySources(_)
|
|
|
|
| ConsensusEncoding => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|
2020-12-05 14:10:44 +00:00
|
|
|
|
|
|
|
impl From<encode::Error> for Error {
|
|
|
|
fn from(err: encode::Error) -> Self {
|
|
|
|
match err {
|
|
|
|
encode::Error::Psbt(err) => err,
|
|
|
|
_ => Error::ConsensusEncoding,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|