Skip to content

Commit

Permalink
new errors
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Aug 3, 2024
1 parent d1b7c05 commit 7c9d997
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 75 deletions.
12 changes: 9 additions & 3 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ interface CreateTxError {
MiniscriptPsbt(string error_message);
};

[Error]
interface CreateWithPersistError {
Persist(string error_message);
Descriptor(string error_message);
};

[Error]
interface DescriptorError {
InvalidHdKeyPath();
Expand Down Expand Up @@ -374,7 +380,7 @@ enum ChangeSpendPolicy {
};

interface Wallet {
[Throws=FfiGenericError]
[Throws=CreateWithPersistError]
constructor(Descriptor descriptor, Descriptor change_descriptor, Network network, Connection connection);

[Name=load, Throws=FfiGenericError]
Expand Down Expand Up @@ -419,7 +425,7 @@ interface Wallet {
};

interface WalletNoPersist {
[Throws=FfiGenericError]
[Throws=DescriptorError]
constructor(Descriptor descriptor, Descriptor change_descriptor, Network network);

AddressInfo reveal_next_address(KeychainKind keychain);
Expand Down Expand Up @@ -519,7 +525,7 @@ interface BumpFeeTxBuilder {
// ------------------------------------------------------------------------

interface Connection {
[Throws=FfiGenericError]
[Throws=SqliteError]
constructor(string path);
};

Expand Down
2 changes: 1 addition & 1 deletion bdk-ffi/src/electrum.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::bitcoin::Transaction;
use crate::error::ElectrumError;
use crate::types::Update;
use crate::types::{FullScanRequest, SyncRequest};
use crate::wallet::Update;

use bdk_electrum::bdk_chain::spk_client::SyncResult;
use bdk_electrum::BdkElectrumClient as BdkBdkElectrumClient;
Expand Down
74 changes: 42 additions & 32 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use bdk_wallet::bitcoin::psbt::Error as BdkPsbtError;
use bdk_wallet::bitcoin::psbt::ExtractTxError as BdkExtractTxError;
use bdk_wallet::bitcoin::psbt::PsbtParseError as BdkPsbtParseError;
use bdk_wallet::chain::local_chain::CannotConnectError as BdkCannotConnectError;
use bdk_wallet::chain::rusqlite::Error as BdkSqliteError;
use bdk_wallet::chain::tx_graph::CalculateFeeError as BdkCalculateFeeError;
use bdk_wallet::descriptor::DescriptorError as BdkDescriptorError;
use bdk_wallet::error::BuildFeeBumpError;
Expand All @@ -20,8 +21,10 @@ use bdk_wallet::keys::bip39::Error as BdkBip39Error;
use bdk_wallet::miniscript::descriptor::DescriptorKeyParseError as BdkDescriptorKeyParseError;
use bdk_wallet::signer::SignerError as BdkSignerError;
use bdk_wallet::tx_builder::AddUtxoError;
use bdk_wallet::CreateWithPersistError as BdkCreateWithPersistError;
use bitcoin_internals::hex::display::DisplayHex;

use bdk_electrum::bdk_chain;
use std::convert::TryInto;

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -209,6 +212,15 @@ pub enum CreateTxError {
MiniscriptPsbt { error_message: String },
}

#[derive(Debug, thiserror::Error)]
pub enum CreateWithPersistError {
#[error("sqlite persistence error: {error_message}")]
Persist { error_message: String },

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

#[derive(Debug, thiserror::Error)]
pub enum DescriptorError {
#[error("invalid hd key path")]
Expand Down Expand Up @@ -404,6 +416,15 @@ pub enum InspectError {
RequestAlreadyConsumed,
}

#[derive(Debug, thiserror::Error)]
pub enum LoadWithPersistError {
#[error("sqlite persistence error: {error_message}")]
Persist { error_message: String },

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

#[derive(Debug, thiserror::Error)]
pub enum ParseAmountError {
#[error("amount out of range")]
Expand Down Expand Up @@ -601,9 +622,7 @@ pub enum SignerError {

#[derive(Debug, thiserror::Error)]
pub enum SqliteError {
// NOTE: This error is renamed from Network to InvalidNetwork to avoid conflict with the Network
// enum in uniffi.
#[error("SQLite error: {rusqlite_error}")]
#[error("sqlite error: {rusqlite_error}")]
Sqlite { rusqlite_error: String },
}

Expand Down Expand Up @@ -862,6 +881,19 @@ impl From<BdkCreateTxError> for CreateTxError {
}
}

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

impl From<AddUtxoError> for CreateTxError {
fn from(error: AddUtxoError) -> Self {
match error {
Expand Down Expand Up @@ -1217,13 +1249,13 @@ impl From<BdkEncodeError> for TransactionError {
}
}

// impl From<BdkSqliteError> for SqliteError {
// fn from(error: BdkSqliteError) -> Self {
// SqliteError::Sqlite {
// rusqlite_error: error.to_string(),
// }
// }
// }
impl From<BdkSqliteError> for SqliteError {
fn from(error: BdkSqliteError) -> Self {
SqliteError::Sqlite {
rusqlite_error: error.to_string(),
}
}
}

impl From<DescriptorError> for WalletCreationError {
fn from(error: DescriptorError) -> Self {
Expand All @@ -1233,28 +1265,6 @@ impl From<DescriptorError> for WalletCreationError {
}
}

// impl From<LoadError> for WalletCreationError {
// fn from(error: LoadError) -> Self {
// match error {
// LoadError::Descriptor(e) => WalletCreationError::Descriptor {
// error_message: e.to_string(),
// },
// LoadError::MissingGenesis => {
// WalletCreationError::MissingGenesis
// }
// LoadError::LoadedNetworkDoesNotMatch { expected, got } => {
// WalletCreationError::LoadedNetworkDoesNotMatch { expected, got }
// }
// LoadError::LoadedDescriptorDoesNotMatch { got, keychain } => {
// WalletCreationError::LoadedDescriptorDoesNotMatch {
// got: format!("{:?}", got),
// keychain,
// }
// }
// }
// }
// }

// ------------------------------------------------------------------------
// error tests
// ------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion bdk-ffi/src/esplora.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::bitcoin::Transaction;
use crate::error::EsploraError;
use crate::types::Update;
use crate::types::{FullScanRequest, SyncRequest};
use crate::wallet::Update;

use bdk_esplora::esplora_client::{BlockingClient, Builder};
use bdk_esplora::EsploraExt;
Expand Down
10 changes: 6 additions & 4 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod error;
mod esplora;
mod keys;
mod store;
mod tx_builder;
mod types;
mod wallet;

Expand All @@ -25,6 +26,7 @@ use crate::error::Bip39Error;
use crate::error::CalculateFeeError;
use crate::error::CannotConnectError;
use crate::error::CreateTxError;
use crate::error::CreateWithPersistError;
use crate::error::DescriptorError;
use crate::error::DescriptorKeyError;
use crate::error::ElectrumError;
Expand All @@ -48,6 +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::types::AddressInfo;
use crate::types::Balance;
use crate::types::BlockId;
Expand All @@ -58,12 +62,10 @@ use crate::types::FullScanRequest;
use crate::types::FullScanScriptInspector;
use crate::types::LocalOutput;
use crate::types::ScriptAmount;
use crate::types::SentAndReceivedValues;
use crate::types::SyncRequest;
use crate::types::SyncScriptInspector;
use crate::wallet::BumpFeeTxBuilder;
use crate::wallet::SentAndReceivedValues;
use crate::wallet::TxBuilder;
use crate::wallet::Update;
use crate::types::Update;
use crate::wallet::Wallet;
use crate::wallet::WalletNoPersist;

Expand Down
8 changes: 5 additions & 3 deletions bdk-ffi/src/store.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use crate::error::FfiGenericError;
use crate::error::SqliteError;

use bdk_wallet::rusqlite::Connection as BdkConnection;

use std::sync::Mutex;
use std::sync::MutexGuard;

pub struct Connection(Mutex<BdkConnection>);

impl Connection {
pub fn new(path: String) -> Result<Self, FfiGenericError> {
let connection = BdkConnection::open(path).expect("must open connection");
pub fn new(path: String) -> Result<Self, SqliteError> {
let connection = BdkConnection::open(path)?;
Ok(Self(Mutex::new(connection)))
}

Expand Down
53 changes: 22 additions & 31 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
use crate::bitcoin::Amount;
use crate::bitcoin::{FeeRate, OutPoint, Psbt, Script, Transaction};
use crate::bitcoin::{FeeRate, Psbt, Script, Transaction};
use crate::descriptor::Descriptor;
use crate::error::{
CalculateFeeError, CannotConnectError, CreateTxError, FfiGenericError, SignerError,
TxidParseError,
CalculateFeeError, CannotConnectError, CreateWithPersistError, DescriptorError,
FfiGenericError, LoadWithPersistError, SignerError, TxidParseError,
};
use crate::store::Connection;
use crate::types::{
AddressInfo, Balance, CanonicalTx, FullScanRequest, LocalOutput, ScriptAmount, SyncRequest,
};
use crate::types::SentAndReceivedValues;
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::Psbt as BdkPsbt;
use bdk_wallet::bitcoin::ScriptBuf as BdkScriptBuf;
use bdk_wallet::bitcoin::{OutPoint as BdkOutPoint, Sequence, Txid};
use bdk_wallet::bitcoin::Txid;
use bdk_wallet::chain::Persisted;
use bdk_wallet::rusqlite::Connection as BdkConnection;
use bdk_wallet::tx_builder::ChangeSpendPolicy;
use bdk_wallet::Update as BdkUpdate;
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};

Expand All @@ -41,16 +35,15 @@ impl Wallet {
change_descriptor: Arc<Descriptor>,
network: Network,
connection: Arc<Connection>,
) -> Result<Self, FfiGenericError> {
) -> Result<Self, CreateWithPersistError> {
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: Persisted<BdkWallet> = BdkWallet::create(descriptor, change_descriptor)
.network(network)
.create_wallet(db)
.expect("wallet creation failed");
.create_wallet(db)?;

Ok(Wallet {
inner_mutex: Mutex::new(wallet),
Expand All @@ -61,22 +54,21 @@ impl Wallet {
descriptor: Arc<Descriptor>,
change_descriptor: Arc<Descriptor>,
connection: Arc<Connection>,
) -> Result<Wallet, FfiGenericError> {
) -> Result<Option<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: Persisted<BdkWallet> = BdkWallet::load()
let wallet: Option<Persisted<BdkWallet>> = BdkWallet::load()
.descriptors(descriptor, change_descriptor)
.load_wallet(db)
.map_err(|_| FfiGenericError::GenericError)
.unwrap()
.ok_or(FfiGenericError::GenericError)?;
.load_wallet(db)?;

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

pub(crate) fn get_wallet(&self) -> MutexGuard<Persisted<BdkWallet>> {
Expand All @@ -96,14 +88,13 @@ impl WalletNoPersist {
descriptor: Arc<Descriptor>,
change_descriptor: Arc<Descriptor>,
network: Network,
) -> Result<Self, FfiGenericError> {
) -> Result<Self, DescriptorError> {
let descriptor = descriptor.to_string_with_secret();
let change_descriptor = change_descriptor.to_string_with_secret();

let wallet: BdkWallet = BdkWallet::create(descriptor, change_descriptor)
.network(network)
.create_wallet_no_persist()
.expect("wallet creation failed");
.create_wallet_no_persist()?;

Ok(WalletNoPersist {
inner_mutex: Mutex::new(wallet),
Expand Down Expand Up @@ -425,9 +416,9 @@ macro_rules! impl_tx_finish {
}
}
}

let psbt = tx_builder.finish().map_err(CreateTxError::from)?;

Ok(Arc::new(psbt.into()))
}
}
Expand Down Expand Up @@ -489,7 +480,7 @@ macro_rules! impl_tx_bump {
}
}
let psbt: BdkPsbt = tx_builder.finish()?;

Ok(Arc::new(psbt.into()))
}
}
Expand Down

0 comments on commit 7c9d997

Please sign in to comment.