Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose a whole lot of errors #485

Merged
merged 4 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion bdk-ffi/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 bdk-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ bdk_esplora = { version = "0.10.0", default-features = false, features = ["std",
bdk_file_store = { version = "0.8.0" }

uniffi = { version = "=0.26.1" }
bitcoin-internals = { version = "0.2.0", features = ["alloc"] }
thiserror = "1.0.58"

[build-dependencies]
Expand Down
60 changes: 56 additions & 4 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,58 @@ enum FeeRateError {
"ArithmeticOverflow"
};

[Error]
interface AddressError {
Base58();
Bech32();
EmptyBech32Payload();
InvalidBech32Variant();
InvalidWitnessVersion(u8 version);
UnparsableWitnessVersion();
MalformedWitnessVersion();
InvalidWitnessProgramLength(u64 length);
InvalidSegwitV0ProgramLength(u64 length);
UncompressedPubkey();
ExcessiveScriptSize();
UnrecognizedScript();
UnknownAddressType(string s);
NetworkValidation(Network required, Network found, string address);
OtherAddressError();
};

[Error]
interface TransactionError {
Io();
OversizedVectorAllocation();
InvalidChecksum(string expected, string actual);
NonMinimalVarInt();
ParseFailed();
UnsupportedSegwitFlag(u8 flag);
OtherTransactionError();
};

[Error]
interface PsbtParseError {
PsbtEncoding(string e);
Base64Encoding(string e);
};

[Error]
interface DescriptorError {
InvalidHdKeyPath();
InvalidDescriptorChecksum();
HardenedDerivationXpub();
MultiPath();
Key(string e);
Policy(string e);
InvalidDescriptorCharacter(string char);
Bip32(string e);
Base58(string e);
Pk(string e);
Miniscript(string e);
Hex(string e);
};

// ------------------------------------------------------------------------
// bdk crate - types module
// ------------------------------------------------------------------------
Expand Down Expand Up @@ -273,7 +325,7 @@ interface DescriptorPublicKey {
};

interface Descriptor {
[Throws=Alpha3Error]
[Throws=DescriptorError]
constructor(string descriptor, Network network);

[Name=new_bip44]
Expand Down Expand Up @@ -360,7 +412,7 @@ enum WordCount {
};

interface Address {
[Throws=Alpha3Error]
[Throws=AddressError]
constructor(string address, Network network);

Network network();
Expand All @@ -375,7 +427,7 @@ interface Address {
};

interface Transaction {
[Throws=Alpha3Error]
[Throws=TransactionError]
constructor(sequence<u8> transaction_bytes);

string txid();
Expand All @@ -394,7 +446,7 @@ interface Transaction {
};

interface PartiallySignedTransaction {
[Throws=Alpha3Error]
[Throws=PsbtParseError]
constructor(string psbt_base64);

string serialize();
Expand Down
23 changes: 8 additions & 15 deletions bdk-ffi/src/bitcoin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error::Alpha3Error;
use crate::error::{AddressError, PsbtParseError, TransactionError};

use bdk::bitcoin::address::{NetworkChecked, NetworkUnchecked};
use bdk::bitcoin::blockdata::script::ScriptBuf as BdkScriptBuf;
Expand Down Expand Up @@ -41,14 +41,9 @@ pub struct Address {
}

impl Address {
pub fn new(address: String, network: Network) -> Result<Self, Alpha3Error> {
let parsed_address = address
.parse::<bdk::bitcoin::Address<NetworkUnchecked>>()
.map_err(|_| Alpha3Error::Generic)?;

let network_checked_address = parsed_address
.require_network(network)
.map_err(|_| Alpha3Error::Generic)?;
pub fn new(address: String, network: Network) -> Result<Self, AddressError> {
let parsed_address = address.parse::<bdk::bitcoin::Address<NetworkUnchecked>>()?;
let network_checked_address = parsed_address.require_network(network)?;

Ok(Address {
inner: network_checked_address,
Expand Down Expand Up @@ -121,10 +116,9 @@ pub struct Transaction {
}

impl Transaction {
pub fn new(transaction_bytes: Vec<u8>) -> Result<Self, Alpha3Error> {
pub fn new(transaction_bytes: Vec<u8>) -> Result<Self, TransactionError> {
let mut decoder = Cursor::new(transaction_bytes);
let tx: BdkTransaction =
BdkTransaction::consensus_decode(&mut decoder).map_err(|_| Alpha3Error::Generic)?;
let tx: BdkTransaction = BdkTransaction::consensus_decode(&mut decoder)?;
Ok(Transaction { inner: tx })
}

Expand Down Expand Up @@ -200,10 +194,9 @@ pub struct PartiallySignedTransaction {
}

impl PartiallySignedTransaction {
pub(crate) fn new(psbt_base64: String) -> Result<Self, Alpha3Error> {
pub(crate) fn new(psbt_base64: String) -> Result<Self, PsbtParseError> {
let psbt: BdkPartiallySignedTransaction =
BdkPartiallySignedTransaction::from_str(&psbt_base64)
.map_err(|_| Alpha3Error::Generic)?;
BdkPartiallySignedTransaction::from_str(&psbt_base64)?;

Ok(PartiallySignedTransaction {
inner: Mutex::new(psbt),
Expand Down
10 changes: 4 additions & 6 deletions bdk-ffi/src/descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error::Alpha3Error;
use crate::error::DescriptorError;
use crate::keys::DescriptorPublicKey;
use crate::keys::DescriptorSecretKey;

Expand All @@ -23,7 +23,7 @@ pub struct Descriptor {
}

impl Descriptor {
pub(crate) fn new(descriptor: String, network: Network) -> Result<Self, Alpha3Error> {
pub(crate) fn new(descriptor: String, network: Network) -> Result<Self, DescriptorError> {
let secp = Secp256k1::new();
let (extended_descriptor, key_map) = descriptor.into_wallet_descriptor(&secp, network)?;
Ok(Self {
Expand Down Expand Up @@ -276,8 +276,6 @@ mod test {
use crate::*;
use assert_matches::assert_matches;

use crate::Alpha3Error;

fn get_descriptor_secret_key() -> DescriptorSecretKey {
let mnemonic = Mnemonic::from_string("chaos fabric time speed sponsor all flat solution wisdom trophy crack object robot pave observe combine where aware bench orient secret primary cable detect".to_string()).unwrap();
DescriptorSecretKey::new(Network::Testnet, &mnemonic, None)
Expand Down Expand Up @@ -392,8 +390,8 @@ mod test {
fn test_descriptor_from_string() {
let descriptor1 = Descriptor::new("wpkh(tprv8hwWMmPE4BVNxGdVt3HhEERZhondQvodUY7Ajyseyhudr4WabJqWKWLr4Wi2r26CDaNCQhhxEftEaNzz7dPGhWuKFU4VULesmhEfZYyBXdE/0/*)".to_string(), Network::Testnet);
let descriptor2 = Descriptor::new("wpkh(tprv8hwWMmPE4BVNxGdVt3HhEERZhondQvodUY7Ajyseyhudr4WabJqWKWLr4Wi2r26CDaNCQhhxEftEaNzz7dPGhWuKFU4VULesmhEfZYyBXdE/0/*)".to_string(), Network::Bitcoin);
// Creating a Descriptor using an extended key that doesn't match the network provided will throw and InvalidNetwork Error
// Creating a Descriptor using an extended key that doesn't match the network provided will throw a DescriptorError::Key with inner InvalidNetwork error
assert!(descriptor1.is_ok());
assert_matches!(descriptor2.unwrap_err(), Alpha3Error::Generic)
assert_matches!(descriptor2.unwrap_err(), DescriptorError::Key { .. });
}
}
Loading
Loading