Skip to content

Commit

Permalink
feat: expose correct errors
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Aug 3, 2024
1 parent 7c9d997 commit d7dac1a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 23 deletions.
9 changes: 8 additions & 1 deletion bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ interface FromScriptError {
OtherFromScriptErr();
};

[Error]
interface LoadWithPersistError {
Persist(string error_message);
InvalidChangeSet(string error_message);
CouldNotLoad();
};

[Error]
interface ParseAmountError {
OutOfRange();
Expand Down Expand Up @@ -383,7 +390,7 @@ interface Wallet {
[Throws=CreateWithPersistError]
constructor(Descriptor descriptor, Descriptor change_descriptor, Network network, Connection connection);

[Name=load, Throws=FfiGenericError]
[Name=load, Throws=LoadWithPersistError]
constructor(Descriptor descriptor, Descriptor change_descriptor, Connection connection);

AddressInfo reveal_next_address(KeychainKind keychain);
Expand Down
17 changes: 17 additions & 0 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use bdk_wallet::miniscript::descriptor::DescriptorKeyParseError as BdkDescriptor
use bdk_wallet::signer::SignerError as BdkSignerError;
use bdk_wallet::tx_builder::AddUtxoError;
use bdk_wallet::CreateWithPersistError as BdkCreateWithPersistError;
use bdk_wallet::LoadWithPersistError as BdkLoadWithPersistError;
use bitcoin_internals::hex::display::DisplayHex;

use bdk_electrum::bdk_chain;
Expand Down Expand Up @@ -423,6 +424,9 @@ pub enum LoadWithPersistError {

#[error("the loaded changeset cannot construct wallet: {error_message}")]
InvalidChangeSet { error_message: String },

#[error("could not load")]
CouldNotLoad,
}

#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -1091,6 +1095,19 @@ impl From<BdkFromScriptError> for FromScriptError {
}
}

impl From<BdkLoadWithPersistError<bdk_chain::rusqlite::Error>> for LoadWithPersistError {
fn from(error: BdkLoadWithPersistError<bdk_chain::rusqlite::Error>) -> Self {
match error {
BdkLoadWithPersistError::Persist(e) => LoadWithPersistError::Persist {
error_message: e.to_string(),
},
BdkLoadWithPersistError::InvalidChangeSet(e) => LoadWithPersistError::InvalidChangeSet {
error_message: e.to_string(),
},
}
}
}

impl From<BdkParseAmountError> for ParseAmountError {
fn from(error: BdkParseAmountError) -> Self {
match error {
Expand Down
10 changes: 5 additions & 5 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod error;
mod esplora;
mod keys;
mod store;
mod tx_builder;
mod types;
mod wallet;

Expand Down Expand Up @@ -33,6 +32,7 @@ use crate::error::ElectrumError;
use crate::error::EsploraError;
use crate::error::ExtractTxError;
use crate::error::FeeRateError;
use crate::types::Update;
use crate::error::FfiGenericError;
use crate::error::FromScriptError;
use crate::error::InspectError;
Expand All @@ -50,8 +50,8 @@ use crate::keys::DescriptorPublicKey;
use crate::keys::DescriptorSecretKey;
use crate::keys::Mnemonic;
use crate::store::Connection;
use crate::tx_builder::BumpFeeTxBuilder;
use crate::tx_builder::TxBuilder;
use crate::wallet::BumpFeeTxBuilder;
use crate::wallet::TxBuilder;
use crate::types::AddressInfo;
use crate::types::Balance;
use crate::types::BlockId;
Expand All @@ -62,12 +62,12 @@ use crate::types::FullScanRequest;
use crate::types::FullScanScriptInspector;
use crate::types::LocalOutput;
use crate::types::ScriptAmount;
use crate::types::SentAndReceivedValues;
use crate::wallet::SentAndReceivedValues;
use crate::types::SyncRequest;
use crate::types::SyncScriptInspector;
use crate::types::Update;
use crate::wallet::Wallet;
use crate::wallet::WalletNoPersist;
use crate::error::LoadWithPersistError;

use bdk_wallet::bitcoin::Network;
use bdk_wallet::keys::bip39::WordCount;
Expand Down
3 changes: 3 additions & 0 deletions bdk-ffi/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use bdk_wallet::AddressInfo as BdkAddressInfo;
use bdk_wallet::Balance as BdkBalance;
use bdk_wallet::KeychainKind;
use bdk_wallet::LocalOutput as BdkLocalOutput;
use bdk_wallet::Update as BdkUpdate;

use std::sync::{Arc, Mutex};

Expand Down Expand Up @@ -188,3 +189,5 @@ impl FullScanRequest {
}
}
}

pub struct Update(pub(crate) BdkUpdate);
37 changes: 20 additions & 17 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
use crate::bitcoin::Amount;
use crate::bitcoin::{FeeRate, Psbt, Script, Transaction};
use crate::bitcoin::{Amount, FeeRate, OutPoint, Psbt, Script, Transaction};
use crate::descriptor::Descriptor;
use crate::error::{
CalculateFeeError, CannotConnectError, CreateWithPersistError, DescriptorError,
FfiGenericError, LoadWithPersistError, SignerError, TxidParseError,
CalculateFeeError, CannotConnectError, CreateTxError, SignerError,
TxidParseError, CreateWithPersistError, DescriptorError, LoadWithPersistError,
};
use crate::store::Connection;
use crate::types::SentAndReceivedValues;
use crate::types::{
AddressInfo, Balance, CanonicalTx, FullScanRequest, LocalOutput, ScriptAmount, SyncRequest,
};
use crate::types::Update;
use crate::types::{AddressInfo, Balance, CanonicalTx, FullScanRequest, LocalOutput, SyncRequest};

use bdk_wallet::bitcoin::amount::Amount as BdkAmount;
use bdk_wallet::bitcoin::Network;
use bdk_wallet::bitcoin::Txid;
use bdk_wallet::bitcoin::Psbt as BdkPsbt;
use bdk_wallet::bitcoin::ScriptBuf as BdkScriptBuf;
use bdk_wallet::bitcoin::{OutPoint as BdkOutPoint, Sequence, Txid};
use bdk_wallet::chain::Persisted;
use bdk_wallet::rusqlite::Connection as BdkConnection;
use bdk_wallet::tx_builder::ChangeSpendPolicy;
use bdk_wallet::Wallet as BdkWallet;
use bdk_wallet::{KeychainKind, SignOptions};

use std::borrow::BorrowMut;
use std::collections::HashSet;
use std::str::FromStr;
use std::sync::{Arc, Mutex, MutexGuard};


pub struct Wallet {
inner_mutex: Mutex<Persisted<BdkWallet>>,
}
Expand Down Expand Up @@ -54,21 +60,20 @@ impl Wallet {
descriptor: Arc<Descriptor>,
change_descriptor: Arc<Descriptor>,
connection: Arc<Connection>,
) -> Result<Option<Wallet>, LoadWithPersistError> {
) -> Result<Wallet, LoadWithPersistError> {
let descriptor = descriptor.to_string_with_secret();
let change_descriptor = change_descriptor.to_string_with_secret();
let mut binding = connection.get_store();
let db: &mut BdkConnection = binding.borrow_mut();

let wallet: Option<Persisted<BdkWallet>> = BdkWallet::load()
let wallet: Persisted<BdkWallet> = BdkWallet::load()
.descriptors(descriptor, change_descriptor)
.load_wallet(db)?;
.load_wallet(db)?
.ok_or(LoadWithPersistError::CouldNotLoad)?;

wallet
.map(|w| Wallet {
inner_mutex: Mutex::new(Some(w)),
})
.ok_or(Ok(None))
Ok(Wallet {
inner_mutex: Mutex::new(wallet),
})
}

pub(crate) fn get_wallet(&self) -> MutexGuard<Persisted<BdkWallet>> {
Expand Down Expand Up @@ -214,8 +219,6 @@ pub struct SentAndReceivedValues {
pub received: Arc<Amount>,
}

pub struct Update(pub(crate) BdkUpdate);

#[derive(Clone, Debug)]
pub struct TxBuilder {
pub(crate) recipients: Vec<(BdkScriptBuf, BdkAmount)>,
Expand Down

0 comments on commit d7dac1a

Please sign in to comment.