Skip to content

Commit

Permalink
Add Snafu error types and proper Rust typed functions (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Aug 10, 2024
1 parent 932decd commit 4b8feda
Show file tree
Hide file tree
Showing 10 changed files with 428 additions and 265 deletions.
63 changes: 63 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ bitcoin = { version = "0.31.2" }
bitcoin_hashes = "0.14.0"
hex = "0.4.3"
miniscript = "11.0.0"
snafu = "0.8.4"

[dev-dependencies]
pretty_assertions = "1.4.0"
69 changes: 62 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,63 @@
#[derive(Debug, PartialEq, Eq)]
pub enum Bip322Error {
InvalidAddress, // for legacy addresses 1... (p2pkh) not supported, also any non taproot
Invalid, // Address no key; pubkey not recovered, invalid signature
MalformedSignature, // wrong length, etc.
InvalidSigHash, // only sighash All and Default supported
NotKeyPathSpend, // only single key path spend supported
use super::*;

#[derive(Debug, Snafu)]
#[snafu(context(suffix(false)), visibility(pub))]
pub enum Error {
#[snafu(display("Failed to parse address `{address}`"))]
AddressParse {
source: bitcoin::address::ParseError,
address: String,
},
#[snafu(display("Failed to parse private key"))]
PrivateKeyParse { source: bitcoin::key::Error },
#[snafu(display("Unsuported address `{address}`, only P2TR allowed"))]
UnsupportedAddress { address: String },
#[snafu(display("Decode error for signature `{signature}`"))]
SignatureDecode {
source: base64::DecodeError,
signature: String,
},
#[snafu(display("Transaction encode error"))]
TransactionEncode { source: std::io::Error },
#[snafu(display("Transaction extract error"))]
TransactionExtract {
source: bitcoin::psbt::ExtractTxError,
},
#[snafu(display("To sign transaction invalid"))]
ToSignInvalid,
#[snafu(display("PSBT extract error"))]
PsbtExtract { source: bitcoin::psbt::Error },
#[snafu(display("Base64 decode error for transaction `{transaction}`"))]
TransactionBase64Decode {
source: base64::DecodeError,
transaction: String,
},
#[snafu(display("Consensus decode error for transaction `{transaction}`"))]
TransactionConsensusDecode {
source: bitcoin::consensus::encode::Error,
transaction: String,
},
#[snafu(display("Witness malformed"))]
WitnessMalformed {
source: bitcoin::consensus::encode::Error,
},
#[snafu(display("Witness empty"))]
WitnessEmpty,
#[snafu(display("Encode witness error"))]
WitnessEncoding { source: std::io::Error },
#[snafu(display("Signature of wrong length `{length}`"))]
SignatureLength {
length: usize,
encoded_signature: Vec<u8>,
},
#[snafu(display("Invalid signature"))]
SignatureInvalid { source: bitcoin::secp256k1::Error },
#[snafu(display("Invalid sighash"))]
SigHashTypeInvalid {
source: bitcoin::sighash::InvalidSighashTypeError,
},
#[snafu(display("Unsupported sighash type `{sighash_type}`"))]
SigHashTypeUnsupported { sighash_type: String },
#[snafu(display("Not key path spend"))]
NotKeyPathSpend,
}
Loading

0 comments on commit 4b8feda

Please sign in to comment.