Skip to content

Commit

Permalink
Move everything to v0 module
Browse files Browse the repository at this point in the history
All the "shared" stuff needs re-writing, just move it all to the `v0`
module so that that module stays functional. We can then go wild with
the new v2 stuff.
  • Loading branch information
tcharding committed Dec 7, 2023
1 parent 9dd343a commit a6fbd9d
Show file tree
Hide file tree
Showing 15 changed files with 305 additions and 305 deletions.
216 changes: 0 additions & 216 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,221 +1,5 @@
// SPDX-License-Identifier: CC0-1.0

use core::fmt;

use bitcoin::bip32::Xpub;
// TODO: This should be exposed like this in rust-bitcoin.
use bitcoin::consensus::encode as consensus;
use bitcoin::transaction::Transaction;
use bitcoin::{hashes, secp256k1, taproot};

use crate::prelude::*;
use crate::{io, raw};

/// Enum for marking psbt hash error.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum PsbtHash {
Ripemd,
Sha256,
Hash160,
Hash256,
}

/// Ways that a Partially Signed Transaction might fail.
// TODO: This general error needs splitting up into specific error types.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
/// Magic bytes for a PSBT must be the ASCII for "psbt" serialized in most
/// significant byte order.
InvalidMagic,
/// Missing both the witness and non-witness utxo.
MissingUtxo,
/// The separator for a PSBT must be `0xff`.
InvalidSeparator,
/// Returned when output index is out of bounds in relation to the output in non-witness UTXO.
PsbtUtxoOutOfbounds,
/// Known keys must be according to spec.
InvalidKey(raw::Key),
/// Non-proprietary key type found when proprietary key was expected
InvalidProprietaryKey,
/// Keys within key-value map should never be duplicated.
DuplicateKey(raw::Key),
/// 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 combine with a PSBT describing a different unsigned
/// transaction.
UnexpectedUnsignedTx {
/// Expected
expected: Box<Transaction>,
/// Actual
actual: Box<Transaction>,
},
/// Unable to parse as a standard sighash type.
NonStandardSighashType(u32),
/// Invalid hash when parsing slice.
InvalidHash(hashes::FromSliceError),
/// The pre-image must hash to the correponding psbt hash
InvalidPreimageHashPair {
/// Hash-type
hash_type: PsbtHash,
/// Pre-image
preimage: Box<[u8]>,
/// Hash value
hash: Box<[u8]>,
},
/// Conflicting data during combine procedure:
/// global extended public key has inconsistent key sources
CombineInconsistentKeySources(Box<Xpub>),
/// Serialization error in bitcoin consensus-encoded structures
ConsensusEncoding(consensus::Error),
/// Negative fee
NegativeFee,
/// Integer overflow in fee calculation
FeeOverflow,
/// Parsing error indicating invalid public keys
InvalidPublicKey(bitcoin::key::Error),
/// Parsing error indicating invalid secp256k1 public keys
InvalidSecp256k1PublicKey(secp256k1::Error),
/// Parsing error indicating invalid xonly public keys
InvalidXOnlyPublicKey,
/// Parsing error indicating invalid ECDSA signatures
InvalidEcdsaSignature(bitcoin::ecdsa::Error),
/// Parsing error indicating invalid taproot signatures
InvalidTaprootSignature(bitcoin::taproot::SigFromSliceError),
/// Parsing error indicating invalid control block
InvalidControlBlock,
/// Parsing error indicating invalid leaf version
InvalidLeafVersion,
/// Parsing error indicating a taproot error
Taproot(&'static str),
/// Taproot tree deserilaization error
TapTree(taproot::IncompleteBuilderError),
/// Error related to an xpub key
XPubKey(&'static str),
/// Error related to PSBT version
Version(&'static str),
/// PSBT data is not consumed entirely
PartialDataConsumption,
/// I/O error.
Io(io::Error),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use Error::*;

match *self {
InvalidMagic => f.write_str("invalid magic"),
MissingUtxo => f.write_str("UTXO information is not present in PSBT"),
InvalidSeparator => f.write_str("invalid separator"),
PsbtUtxoOutOfbounds =>
f.write_str("output index is out of bounds of non witness script output array"),
InvalidKey(ref rkey) => write!(f, "invalid key: {}", rkey),
InvalidProprietaryKey =>
write!(f, "non-proprietary key type found when proprietary key was expected"),
DuplicateKey(ref rkey) => write!(f, "duplicate key: {}", rkey),
UnsignedTxHasScriptSigs => f.write_str("the unsigned transaction has script sigs"),
UnsignedTxHasScriptWitnesses =>
f.write_str("the unsigned transaction has script witnesses"),
MustHaveUnsignedTx =>
f.write_str("partially signed transactions must have an unsigned transaction"),
NoMorePairs => f.write_str("no more key-value pairs for this psbt map"),
UnexpectedUnsignedTx { expected: ref e, actual: ref a } => write!(
f,
"different unsigned transaction: expected {}, actual {}",
e.txid(),
a.txid()
),
NonStandardSighashType(ref sht) => write!(f, "non-standard sighash type: {}", sht),
InvalidHash(ref e) => write_err!(f, "invalid hash when parsing slice"; e),
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)
}
CombineInconsistentKeySources(ref s) => {
write!(f, "combine conflict: {}", s)
}
ConsensusEncoding(ref e) => write_err!(f, "bitcoin consensus encoding error"; e),
NegativeFee => f.write_str("PSBT has a negative fee which is not allowed"),
FeeOverflow => f.write_str("integer overflow in fee calculation"),
InvalidPublicKey(ref e) => write_err!(f, "invalid public key"; e),
InvalidSecp256k1PublicKey(ref e) => write_err!(f, "invalid secp256k1 public key"; e),
InvalidXOnlyPublicKey => f.write_str("invalid xonly public key"),
InvalidEcdsaSignature(ref e) => write_err!(f, "invalid ECDSA signature"; e),
InvalidTaprootSignature(ref e) => write_err!(f, "invalid taproot signature"; e),
InvalidControlBlock => f.write_str("invalid control block"),
InvalidLeafVersion => f.write_str("invalid leaf version"),
Taproot(s) => write!(f, "taproot error - {}", s),
TapTree(ref e) => write_err!(f, "taproot tree error"; e),
XPubKey(s) => write!(f, "xpub key error - {}", s),
Version(s) => write!(f, "version error {}", s),
PartialDataConsumption =>
f.write_str("data not consumed entirely when explicitly deserializing"),
Io(ref e) => write_err!(f, "I/O error"; e),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use Error::*;

match *self {
InvalidHash(ref e) => Some(e),
ConsensusEncoding(ref e) => Some(e),
Io(ref e) => Some(e),
InvalidMagic
| MissingUtxo
| InvalidSeparator
| PsbtUtxoOutOfbounds
| InvalidKey(_)
| InvalidProprietaryKey
| DuplicateKey(_)
| UnsignedTxHasScriptSigs
| UnsignedTxHasScriptWitnesses
| MustHaveUnsignedTx
| NoMorePairs
| UnexpectedUnsignedTx { .. }
| NonStandardSighashType(_)
| InvalidPreimageHashPair { .. }
| CombineInconsistentKeySources(_)
| NegativeFee
| FeeOverflow
| InvalidPublicKey(_)
| InvalidSecp256k1PublicKey(_)
| InvalidXOnlyPublicKey
| InvalidEcdsaSignature(_)
| InvalidTaprootSignature(_)
| InvalidControlBlock
| InvalidLeafVersion
| Taproot(_)
| TapTree(_)
| XPubKey(_)
| Version(_)
| PartialDataConsumption => None,
}
}
}

impl From<hashes::FromSliceError> for Error {
fn from(e: hashes::FromSliceError) -> Error { Error::InvalidHash(e) }
}

impl From<consensus::Error> for Error {
fn from(e: consensus::Error) -> Self { Error::ConsensusEncoding(e) }
}

impl From<io::Error> for Error {
fn from(e: io::Error) -> Self { Error::Io(e) }
}

/// Formats error.
///
/// If `std` feature is OFF appends error source (delimited by `: `). We do this because
Expand Down
14 changes: 0 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ pub extern crate bitcoin;
pub extern crate miniscript;

mod error;
#[macro_use]
mod macros;
#[cfg(feature = "serde")]
mod serde_utils;
mod sighash_type;

pub mod raw;
pub mod serialize;
pub mod v0;

#[cfg(feature = "std")]
Expand All @@ -56,12 +48,6 @@ use std::io;
#[cfg(not(feature = "std"))]
use core2::io;

#[rustfmt::skip] // Keep pubic re-exports separate
pub use crate::{
error::Error,
sighash_type::PsbtSighashType,
};

#[rustfmt::skip]
mod prelude {
#[cfg(all(not(feature = "std"), not(test)))]
Expand Down
Loading

0 comments on commit a6fbd9d

Please sign in to comment.