Skip to content

Commit 4b8feda

Browse files
authored
Add Snafu error types and proper Rust typed functions (#21)
1 parent 932decd commit 4b8feda

File tree

10 files changed

+428
-265
lines changed

10 files changed

+428
-265
lines changed

Cargo.lock

+63
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ bitcoin = { version = "0.31.2" }
1313
bitcoin_hashes = "0.14.0"
1414
hex = "0.4.3"
1515
miniscript = "11.0.0"
16+
snafu = "0.8.4"
1617

1718
[dev-dependencies]
1819
pretty_assertions = "1.4.0"

src/error.rs

+62-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,63 @@
1-
#[derive(Debug, PartialEq, Eq)]
2-
pub enum Bip322Error {
3-
InvalidAddress, // for legacy addresses 1... (p2pkh) not supported, also any non taproot
4-
Invalid, // Address no key; pubkey not recovered, invalid signature
5-
MalformedSignature, // wrong length, etc.
6-
InvalidSigHash, // only sighash All and Default supported
7-
NotKeyPathSpend, // only single key path spend supported
1+
use super::*;
2+
3+
#[derive(Debug, Snafu)]
4+
#[snafu(context(suffix(false)), visibility(pub))]
5+
pub enum Error {
6+
#[snafu(display("Failed to parse address `{address}`"))]
7+
AddressParse {
8+
source: bitcoin::address::ParseError,
9+
address: String,
10+
},
11+
#[snafu(display("Failed to parse private key"))]
12+
PrivateKeyParse { source: bitcoin::key::Error },
13+
#[snafu(display("Unsuported address `{address}`, only P2TR allowed"))]
14+
UnsupportedAddress { address: String },
15+
#[snafu(display("Decode error for signature `{signature}`"))]
16+
SignatureDecode {
17+
source: base64::DecodeError,
18+
signature: String,
19+
},
20+
#[snafu(display("Transaction encode error"))]
21+
TransactionEncode { source: std::io::Error },
22+
#[snafu(display("Transaction extract error"))]
23+
TransactionExtract {
24+
source: bitcoin::psbt::ExtractTxError,
25+
},
26+
#[snafu(display("To sign transaction invalid"))]
27+
ToSignInvalid,
28+
#[snafu(display("PSBT extract error"))]
29+
PsbtExtract { source: bitcoin::psbt::Error },
30+
#[snafu(display("Base64 decode error for transaction `{transaction}`"))]
31+
TransactionBase64Decode {
32+
source: base64::DecodeError,
33+
transaction: String,
34+
},
35+
#[snafu(display("Consensus decode error for transaction `{transaction}`"))]
36+
TransactionConsensusDecode {
37+
source: bitcoin::consensus::encode::Error,
38+
transaction: String,
39+
},
40+
#[snafu(display("Witness malformed"))]
41+
WitnessMalformed {
42+
source: bitcoin::consensus::encode::Error,
43+
},
44+
#[snafu(display("Witness empty"))]
45+
WitnessEmpty,
46+
#[snafu(display("Encode witness error"))]
47+
WitnessEncoding { source: std::io::Error },
48+
#[snafu(display("Signature of wrong length `{length}`"))]
49+
SignatureLength {
50+
length: usize,
51+
encoded_signature: Vec<u8>,
52+
},
53+
#[snafu(display("Invalid signature"))]
54+
SignatureInvalid { source: bitcoin::secp256k1::Error },
55+
#[snafu(display("Invalid sighash"))]
56+
SigHashTypeInvalid {
57+
source: bitcoin::sighash::InvalidSighashTypeError,
58+
},
59+
#[snafu(display("Unsupported sighash type `{sighash_type}`"))]
60+
SigHashTypeUnsupported { sighash_type: String },
61+
#[snafu(display("Not key path spend"))]
62+
NotKeyPathSpend,
863
}

0 commit comments

Comments
 (0)