feat: add generic alpha 3 error

This commit is contained in:
thunderbiscuit 2024-01-10 14:04:06 -05:00
parent 4f6c198168
commit ccf5fbda6e
No known key found for this signature in database
GPG Key ID: 88253696EB836462
11 changed files with 177 additions and 137 deletions

1
bdk-ffi/Cargo.lock generated
View File

@ -163,6 +163,7 @@ dependencies = [
"assert_matches",
"bdk",
"bdk_esplora",
"thiserror",
"uniffi",
]

View File

@ -30,6 +30,7 @@ bdk = { version = "1.0.0-alpha.3", features = ["all-keys", "keys-bip39"] }
bdk_esplora = { version = "0.5.0", default-features = false, features = ["std", "blocking"] }
uniffi = { version = "=0.25.1" }
thiserror = "1.0.50"
[build-dependencies]
uniffi = { version = "=0.25.1", features = ["build"] }

View File

@ -1,5 +1,20 @@
namespace bdk {};
// ------------------------------------------------------------------------
// bdk crate - error module
// ------------------------------------------------------------------------
[Error]
enum Alpha3Error {
"Generic"
};
[Error]
interface CalculateFeeError {
MissingTxOut(sequence<OutPoint> out_points);
NegativeFee(i64 fee);
};
// ------------------------------------------------------------------------
// bdk crate - types module
// ------------------------------------------------------------------------
@ -36,7 +51,7 @@ dictionary Balance {
u64 total;
};
dictionary LocalUtxo {
dictionary LocalOutput {
OutPoint outpoint;
TxOut txout;
KeychainKind keychain;
@ -52,42 +67,6 @@ dictionary TxOut {
// bdk crate - wallet module
// ------------------------------------------------------------------------
[Error]
enum BdkError {
"Generic",
"NoRecipients",
"NoUtxosSelected",
"OutputBelowDustLimit",
"InsufficientFunds",
"BnBTotalTriesExceeded",
"BnBNoExactMatch",
"UnknownUtxo",
"TransactionNotFound",
"TransactionConfirmed",
"IrreplaceableTransaction",
"FeeRateTooLow",
"FeeTooLow",
"FeeRateUnavailable",
"MissingKeyOrigin",
"Key",
"ChecksumMismatch",
"SpendingPolicyRequired",
"InvalidPolicyPathError",
"Signer",
"InvalidOutpoint",
"Descriptor",
"Miniscript",
"MiniscriptPsbt",
"Bip32",
"Psbt",
};
[Error]
interface CalculateFeeError {
MissingTxOut(sequence<OutPoint> out_points);
NegativeFee(i64 fee);
};
interface FeeRate {
f32 as_sat_per_vb();
f32 sat_per_kwu();
@ -100,7 +79,7 @@ enum ChangeSpendPolicy {
};
interface Wallet {
[Name=new_no_persist, Throws=BdkError]
[Name=new_no_persist, Throws=Alpha3Error]
constructor(Descriptor descriptor, Descriptor? change_descriptor, Network network);
AddressInfo get_address(AddressIndex address_index);
@ -113,10 +92,10 @@ interface Wallet {
boolean is_mine([ByRef] Script script);
[Throws=BdkError]
[Throws=Alpha3Error]
void apply_update(Update update);
[Throws=BdkError]
[Throws=Alpha3Error]
boolean sign(PartiallySignedTransaction psbt);
SentAndReceivedValues sent_and_received([ByRef] Transaction tx);
@ -165,7 +144,7 @@ interface TxBuilder {
TxBuilder enable_rbf_with_sequence(u32 nsequence);
[Throws=BdkError]
[Throws=Alpha3Error]
PartiallySignedTransaction finish([ByRef] Wallet wallet);
};
@ -178,7 +157,7 @@ interface BumpFeeTxBuilder {
BumpFeeTxBuilder enable_rbf_with_sequence(u32 nsequence);
[Throws=BdkError]
[Throws=Alpha3Error]
PartiallySignedTransaction finish([ByRef] Wallet wallet);
};
@ -189,30 +168,30 @@ interface BumpFeeTxBuilder {
interface Mnemonic {
constructor(WordCount word_count);
[Name=from_string, Throws=BdkError]
[Name=from_string, Throws=Alpha3Error]
constructor(string mnemonic);
[Name=from_entropy, Throws=BdkError]
[Name=from_entropy, Throws=Alpha3Error]
constructor(sequence<u8> entropy);
string as_string();
};
interface DerivationPath {
[Throws=BdkError]
[Throws=Alpha3Error]
constructor(string path);
};
interface DescriptorSecretKey {
constructor(Network network, [ByRef] Mnemonic mnemonic, string? password);
[Name=from_string, Throws=BdkError]
[Name=from_string, Throws=Alpha3Error]
constructor(string secret_key);
[Throws=BdkError]
[Throws=Alpha3Error]
DescriptorSecretKey derive([ByRef] DerivationPath path);
[Throws=BdkError]
[Throws=Alpha3Error]
DescriptorSecretKey extend([ByRef] DerivationPath path);
DescriptorPublicKey as_public();
@ -223,20 +202,20 @@ interface DescriptorSecretKey {
};
interface DescriptorPublicKey {
[Name=from_string, Throws=BdkError]
[Name=from_string, Throws=Alpha3Error]
constructor(string public_key);
[Throws=BdkError]
[Throws=Alpha3Error]
DescriptorPublicKey derive([ByRef] DerivationPath path);
[Throws=BdkError]
[Throws=Alpha3Error]
DescriptorPublicKey extend([ByRef] DerivationPath path);
string as_string();
};
interface Descriptor {
[Throws=BdkError]
[Throws=Alpha3Error]
constructor(string descriptor, Network network);
[Name=new_bip44]
@ -275,10 +254,10 @@ interface Descriptor {
interface EsploraClient {
constructor(string url);
[Throws=BdkError]
[Throws=Alpha3Error]
Update scan(Wallet wallet, u64 stop_gap, u64 parallel_requests);
[Throws=BdkError]
[Throws=Alpha3Error]
void broadcast([ByRef] Transaction transaction);
};
@ -322,7 +301,7 @@ enum WordCount {
};
interface Address {
[Throws=BdkError]
[Throws=Alpha3Error]
constructor(string address, Network network);
Network network();
@ -337,7 +316,7 @@ interface Address {
};
interface Transaction {
[Throws=BdkError]
[Throws=Alpha3Error]
constructor(sequence<u8> transaction_bytes);
string txid();
@ -356,7 +335,7 @@ interface Transaction {
};
interface PartiallySignedTransaction {
[Throws=BdkError]
[Throws=Alpha3Error]
constructor(string psbt_base64);
string serialize();
@ -367,4 +346,4 @@ interface PartiallySignedTransaction {
dictionary OutPoint {
string txid;
u32 vout;
};
};

View File

@ -8,11 +8,11 @@ use bdk::bitcoin::Address as BdkAddress;
use bdk::bitcoin::OutPoint as BdkOutPoint;
use bdk::bitcoin::Transaction as BdkTransaction;
use bdk::bitcoin::Txid;
use bdk::Error as BdkError;
use std::io::Cursor;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use crate::error::Alpha3Error;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Script(pub(crate) BdkScriptBuf);
@ -71,14 +71,14 @@ pub struct Address {
}
impl Address {
pub fn new(address: String, network: Network) -> Result<Self, BdkError> {
pub fn new(address: String, network: Network) -> Result<Self, Alpha3Error> {
let parsed_address = address
.parse::<bdk::bitcoin::Address<NetworkUnchecked>>()
.map_err(|e| BdkError::Generic(e.to_string()))?;
.map_err(|e| Alpha3Error::Generic)?;
let network_checked_address = parsed_address
.require_network(network.into())
.map_err(|e| BdkError::Generic(e.to_string()))?;
.map_err(|e| Alpha3Error::Generic)?;
Ok(Address {
inner: network_checked_address,
@ -151,10 +151,10 @@ pub struct Transaction {
}
impl Transaction {
pub fn new(transaction_bytes: Vec<u8>) -> Result<Self, BdkError> {
pub fn new(transaction_bytes: Vec<u8>) -> Result<Self, Alpha3Error> {
let mut decoder = Cursor::new(transaction_bytes);
let tx: BdkTransaction = BdkTransaction::consensus_decode(&mut decoder)
.map_err(|e| BdkError::Generic(e.to_string()))?;
.map_err(|e| Alpha3Error::Generic)?;
Ok(Transaction { inner: tx })
}
@ -230,10 +230,10 @@ pub struct PartiallySignedTransaction {
}
impl PartiallySignedTransaction {
pub(crate) fn new(psbt_base64: String) -> Result<Self, BdkError> {
pub(crate) fn new(psbt_base64: String) -> Result<Self, Alpha3Error> {
let psbt: BdkPartiallySignedTransaction =
BdkPartiallySignedTransaction::from_str(&psbt_base64)
.map_err(|e| BdkError::Generic(e.to_string()))?;
.map_err(|e| Alpha3Error::Generic)?;
Ok(PartiallySignedTransaction {
inner: Mutex::new(psbt),

View File

@ -1,6 +1,7 @@
use crate::keys::DescriptorPublicKey;
use crate::keys::DescriptorSecretKey;
use crate::Network;
use crate::error::Alpha3Error;
use bdk::bitcoin::bip32::Fingerprint;
use bdk::bitcoin::key::Secp256k1;
@ -11,7 +12,6 @@ use bdk::template::{
Bip44, Bip44Public, Bip49, Bip49Public, Bip84, Bip84Public, Bip86, Bip86Public,
DescriptorTemplate,
};
use bdk::Error as BdkError;
use bdk::KeychainKind;
use std::str::FromStr;
@ -23,7 +23,7 @@ pub struct Descriptor {
}
impl Descriptor {
pub(crate) fn new(descriptor: String, network: Network) -> Result<Self, BdkError> {
pub(crate) fn new(descriptor: String, network: Network) -> Result<Self, Alpha3Error> {
let secp = Secp256k1::new();
let (extended_descriptor, key_map) =
descriptor.into_wallet_descriptor(&secp, network.into())?;

View File

@ -4,6 +4,83 @@ use bdk::chain::tx_graph::CalculateFeeError as BdkCalculateFeeError;
use std::fmt;
use bdk::descriptor::DescriptorError;
use bdk::wallet::error::{BuildFeeBumpError, CreateTxError};
use bdk::wallet::tx_builder::{AddUtxoError, AllowShrinkingError};
use bdk::wallet::{NewError, NewOrLoadError};
use std::convert::Infallible;
#[derive(Debug, thiserror::Error)]
pub enum Alpha3Error {
Generic,
}
impl fmt::Display for Alpha3Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Error in FFI")
}
}
impl From<DescriptorError> for Alpha3Error {
fn from(_: DescriptorError) -> Self {
Alpha3Error::Generic
}
}
impl From<AllowShrinkingError> for Alpha3Error {
fn from(_: AllowShrinkingError) -> Self {
Alpha3Error::Generic
}
}
impl From<BuildFeeBumpError> for Alpha3Error {
fn from(_: BuildFeeBumpError) -> Self {
Alpha3Error::Generic
}
}
impl From<CreateTxError<Infallible>> for Alpha3Error {
fn from(_: CreateTxError<Infallible>) -> Self {
Alpha3Error::Generic
}
}
impl From<AddUtxoError> for Alpha3Error {
fn from(_: AddUtxoError) -> Self {
Alpha3Error::Generic
}
}
impl From<bdk::bitcoin::bip32::Error> for Alpha3Error {
fn from(_: bdk::bitcoin::bip32::Error) -> Self {
Alpha3Error::Generic
}
}
// impl From<FileError<'_>> for TempFfiError {
// fn from(_: FileError<'_>) -> Self {
// TempFfiError::FfiError
// }
// }
impl From<NewError<std::io::Error>> for Alpha3Error {
fn from(_: NewError<std::io::Error>) -> Self {
Alpha3Error::Generic
}
}
// impl From<NewOrLoadError<std::io::Error, IterError>> for Alpha3Error {
// fn from(_: NewOrLoadError<std::io::Error, IterError>) -> Self {
// Alpha3Error::Alpha3Error
// }
// }
impl From<CreateTxError<std::io::Error>> for Alpha3Error {
fn from(_: CreateTxError<std::io::Error>) -> Self {
Alpha3Error::Generic
}
}
#[derive(Debug)]
pub enum CalculateFeeError {
MissingTxOut { out_points: Vec<OutPoint> },

View File

@ -1,8 +1,8 @@
use crate::wallet::{Update, Wallet};
use crate::error::Alpha3Error;
use bdk::bitcoin::Transaction as BdkTransaction;
use bdk::wallet::Update as BdkUpdate;
use bdk::Error as BdkError;
use bdk_esplora::esplora_client::{BlockingClient, Builder};
use bdk_esplora::EsploraExt;
@ -24,7 +24,7 @@ impl EsploraClient {
wallet: Arc<Wallet>,
stop_gap: u64,
parallel_requests: u64,
) -> Result<Arc<Update>, BdkError> {
) -> Result<Arc<Update>, Alpha3Error> {
let wallet = wallet.get_wallet();
let previous_tip = wallet.latest_checkpoint();
@ -32,10 +32,8 @@ impl EsploraClient {
let (update_graph, last_active_indices) = self
.0
.scan_txs_with_keychains(
.full_scan(
keychain_spks,
None,
None,
stop_gap as usize,
parallel_requests as usize,
)
@ -58,11 +56,11 @@ impl EsploraClient {
// pub fn sync();
pub fn broadcast(&self, transaction: &Transaction) -> Result<(), BdkError> {
pub fn broadcast(&self, transaction: &Transaction) -> Result<(), Alpha3Error> {
let bdk_transaction: BdkTransaction = transaction.into();
self.0
.broadcast(&bdk_transaction)
.map_err(|e| BdkError::Generic(e.to_string()))
.map_err(|e| Alpha3Error::Generic)
}
// pub fn estimate_fee();

View File

@ -1,4 +1,5 @@
use crate::Network;
use crate::error::Alpha3Error;
use bdk::bitcoin::bip32::DerivationPath as BdkDerivationPath;
use bdk::bitcoin::key::Secp256k1;
@ -12,7 +13,6 @@ use bdk::keys::{
};
use bdk::miniscript::descriptor::{DescriptorXKey, Wildcard};
use bdk::miniscript::BareCtx;
use bdk::Error as BdkError;
use std::ops::Deref;
use std::str::FromStr;
@ -35,16 +35,16 @@ impl Mnemonic {
Mnemonic { inner: mnemonic }
}
pub(crate) fn from_string(mnemonic: String) -> Result<Self, BdkError> {
pub(crate) fn from_string(mnemonic: String) -> Result<Self, Alpha3Error> {
BdkMnemonic::from_str(&mnemonic)
.map(|m| Mnemonic { inner: m })
.map_err(|e| BdkError::Generic(e.to_string()))
.map_err(|e| Alpha3Error::Generic)
}
pub(crate) fn from_entropy(entropy: Vec<u8>) -> Result<Self, BdkError> {
pub(crate) fn from_entropy(entropy: Vec<u8>) -> Result<Self, Alpha3Error> {
BdkMnemonic::from_entropy(entropy.as_slice())
.map(|m| Mnemonic { inner: m })
.map_err(|e| BdkError::Generic(e.to_string()))
.map_err(|e| Alpha3Error::Generic)
}
pub(crate) fn as_string(&self) -> String {
@ -57,12 +57,12 @@ pub(crate) struct DerivationPath {
}
impl DerivationPath {
pub(crate) fn new(path: String) -> Result<Self, BdkError> {
pub(crate) fn new(path: String) -> Result<Self, Alpha3Error> {
BdkDerivationPath::from_str(&path)
.map(|x| DerivationPath {
inner_mutex: Mutex::new(x),
})
.map_err(|e| BdkError::Generic(e.to_string()))
.map_err(|e| Alpha3Error::Generic)
}
}
@ -86,22 +86,20 @@ impl DescriptorSecretKey {
}
}
pub(crate) fn from_string(private_key: String) -> Result<Self, BdkError> {
pub(crate) fn from_string(private_key: String) -> Result<Self, Alpha3Error> {
let descriptor_secret_key = BdkDescriptorSecretKey::from_str(private_key.as_str())
.map_err(|e| BdkError::Generic(e.to_string()))?;
.map_err(|e| Alpha3Error::Generic)?;
Ok(Self {
inner: descriptor_secret_key,
})
}
pub(crate) fn derive(&self, path: &DerivationPath) -> Result<Arc<Self>, BdkError> {
pub(crate) fn derive(&self, path: &DerivationPath) -> Result<Arc<Self>, Alpha3Error> {
let secp = Secp256k1::new();
let descriptor_secret_key = &self.inner;
let path = path.inner_mutex.lock().unwrap().deref().clone();
match descriptor_secret_key {
BdkDescriptorSecretKey::Single(_) => Err(BdkError::Generic(
"Cannot derive from a single key".to_string(),
)),
BdkDescriptorSecretKey::Single(_) => Err(Alpha3Error::Generic),
BdkDescriptorSecretKey::XPrv(descriptor_x_key) => {
let derived_xprv = descriptor_x_key.xkey.derive_priv(&secp, &path)?;
let key_source = match descriptor_x_key.origin.clone() {
@ -118,19 +116,15 @@ impl DescriptorSecretKey {
inner: derived_descriptor_secret_key,
}))
}
BdkDescriptorSecretKey::MultiXPrv(_) => Err(BdkError::Generic(
"Cannot derive from a multi key".to_string(),
)),
BdkDescriptorSecretKey::MultiXPrv(_) => Err(Alpha3Error::Generic),
}
}
pub(crate) fn extend(&self, path: &DerivationPath) -> Result<Arc<Self>, BdkError> {
pub(crate) fn extend(&self, path: &DerivationPath) -> Result<Arc<Self>, Alpha3Error> {
let descriptor_secret_key = &self.inner;
let path = path.inner_mutex.lock().unwrap().deref().clone();
match descriptor_secret_key {
BdkDescriptorSecretKey::Single(_) => Err(BdkError::Generic(
"Cannot extend from a single key".to_string(),
)),
BdkDescriptorSecretKey::Single(_) => Err(Alpha3Error::Generic),
BdkDescriptorSecretKey::XPrv(descriptor_x_key) => {
let extended_path = descriptor_x_key.derivation_path.extend(path);
let extended_descriptor_secret_key = BdkDescriptorSecretKey::XPrv(DescriptorXKey {
@ -143,9 +137,7 @@ impl DescriptorSecretKey {
inner: extended_descriptor_secret_key,
}))
}
BdkDescriptorSecretKey::MultiXPrv(_) => Err(BdkError::Generic(
"Cannot derive from a multi key".to_string(),
)),
BdkDescriptorSecretKey::MultiXPrv(_) => Err(Alpha3Error::Generic),
}
}
@ -185,23 +177,21 @@ pub struct DescriptorPublicKey {
}
impl DescriptorPublicKey {
pub(crate) fn from_string(public_key: String) -> Result<Self, BdkError> {
pub(crate) fn from_string(public_key: String) -> Result<Self, Alpha3Error> {
let descriptor_public_key = BdkDescriptorPublicKey::from_str(public_key.as_str())
.map_err(|e| BdkError::Generic(e.to_string()))?;
.map_err(|e| Alpha3Error::Generic)?;
Ok(Self {
inner: descriptor_public_key,
})
}
pub(crate) fn derive(&self, path: &DerivationPath) -> Result<Arc<Self>, BdkError> {
pub(crate) fn derive(&self, path: &DerivationPath) -> Result<Arc<Self>, Alpha3Error> {
let secp = Secp256k1::new();
let descriptor_public_key = &self.inner;
let path = path.inner_mutex.lock().unwrap().deref().clone();
match descriptor_public_key {
BdkDescriptorPublicKey::Single(_) => Err(BdkError::Generic(
"Cannot derive from a single key".to_string(),
)),
BdkDescriptorPublicKey::Single(_) => Err(Alpha3Error::Generic),
BdkDescriptorPublicKey::XPub(descriptor_x_key) => {
let derived_xpub = descriptor_x_key.xkey.derive_pub(&secp, &path)?;
let key_source = match descriptor_x_key.origin.clone() {
@ -218,19 +208,15 @@ impl DescriptorPublicKey {
inner: derived_descriptor_public_key,
}))
}
BdkDescriptorPublicKey::MultiXPub(_) => Err(BdkError::Generic(
"Cannot derive from a multi xpub".to_string(),
)),
BdkDescriptorPublicKey::MultiXPub(_) => Err(Alpha3Error::Generic),
}
}
pub(crate) fn extend(&self, path: &DerivationPath) -> Result<Arc<Self>, BdkError> {
pub(crate) fn extend(&self, path: &DerivationPath) -> Result<Arc<Self>, Alpha3Error> {
let descriptor_public_key = &self.inner;
let path = path.inner_mutex.lock().unwrap().deref().clone();
match descriptor_public_key {
BdkDescriptorPublicKey::Single(_) => Err(BdkError::Generic(
"Cannot extend from a single key".to_string(),
)),
BdkDescriptorPublicKey::Single(_) => Err(Alpha3Error::Generic),
BdkDescriptorPublicKey::XPub(descriptor_x_key) => {
let extended_path = descriptor_x_key.derivation_path.extend(path);
let extended_descriptor_public_key = BdkDescriptorPublicKey::XPub(DescriptorXKey {
@ -243,9 +229,7 @@ impl DescriptorPublicKey {
inner: extended_descriptor_public_key,
}))
}
BdkDescriptorPublicKey::MultiXPub(_) => Err(BdkError::Generic(
"Cannot derive from a multi xpub".to_string(),
)),
BdkDescriptorPublicKey::MultiXPub(_) => Err(Alpha3Error::Generic),
}
}
@ -257,10 +241,10 @@ impl DescriptorPublicKey {
#[cfg(test)]
mod test {
use crate::keys::{DerivationPath, DescriptorPublicKey, DescriptorSecretKey, Mnemonic};
use crate::BdkError;
// use bdk::bitcoin::hashes::hex::ToHex;
use bdk::bitcoin::Network;
use std::sync::Arc;
use crate::error::Alpha3Error;
fn get_inner() -> 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();
@ -270,7 +254,7 @@ mod test {
fn derive_dsk(
key: &DescriptorSecretKey,
path: &str,
) -> Result<Arc<DescriptorSecretKey>, BdkError> {
) -> Result<Arc<DescriptorSecretKey>, Alpha3Error> {
let path = DerivationPath::new(path.to_string()).unwrap();
key.derive(&path)
}
@ -278,7 +262,7 @@ mod test {
fn extend_dsk(
key: &DescriptorSecretKey,
path: &str,
) -> Result<Arc<DescriptorSecretKey>, BdkError> {
) -> Result<Arc<DescriptorSecretKey>, Alpha3Error> {
let path = DerivationPath::new(path.to_string()).unwrap();
key.extend(&path)
}
@ -286,7 +270,7 @@ mod test {
fn derive_dpk(
key: &DescriptorPublicKey,
path: &str,
) -> Result<Arc<DescriptorPublicKey>, BdkError> {
) -> Result<Arc<DescriptorPublicKey>, Alpha3Error> {
let path = DerivationPath::new(path.to_string()).unwrap();
key.derive(&path)
}
@ -294,7 +278,7 @@ mod test {
fn extend_dpk(
key: &DescriptorPublicKey,
path: &str,
) -> Result<Arc<DescriptorPublicKey>, BdkError> {
) -> Result<Arc<DescriptorPublicKey>, Alpha3Error> {
let path = DerivationPath::new(path.to_string()).unwrap();
key.extend(&path)
}

View File

@ -22,9 +22,10 @@ use crate::keys::DescriptorSecretKey;
use crate::keys::Mnemonic;
use crate::types::AddressIndex;
use crate::types::AddressInfo;
use crate::error::Alpha3Error;
use crate::types::Balance;
use crate::types::FeeRate;
use crate::types::LocalUtxo;
use crate::types::LocalOutput;
use crate::types::ScriptAmount;
use crate::wallet::BumpFeeTxBuilder;
use crate::wallet::SentAndReceivedValues;
@ -34,7 +35,6 @@ use crate::wallet::Wallet;
use bdk::keys::bip39::WordCount;
use bdk::wallet::tx_builder::ChangeSpendPolicy;
use bdk::Error as BdkError;
use bdk::KeychainKind;
uniffi::include_scaffolding!("bdk");

View File

@ -5,7 +5,7 @@ use bdk::wallet::AddressInfo as BdkAddressInfo;
use bdk::wallet::Balance as BdkBalance;
use bdk::KeychainKind;
use bdk::LocalUtxo as BdkLocalUtxo;
use bdk::LocalOutput as BdkLocalOutput;
use bdk::FeeRate as BdkFeeRate;
@ -113,16 +113,16 @@ impl From<BdkBalance> for Balance {
}
}
pub struct LocalUtxo {
pub struct LocalOutput {
pub outpoint: OutPoint,
pub txout: TxOut,
pub keychain: KeychainKind,
pub is_spent: bool,
}
impl From<BdkLocalUtxo> for LocalUtxo {
fn from(local_utxo: BdkLocalUtxo) -> Self {
LocalUtxo {
impl From<BdkLocalOutput> for LocalOutput {
fn from(local_utxo: BdkLocalOutput) -> Self {
LocalOutput {
outpoint: OutPoint {
txid: local_utxo.outpoint.txid.to_string(),
vout: local_utxo.outpoint.vout,

View File

@ -1,6 +1,6 @@
use crate::bitcoin::{OutPoint, PartiallySignedTransaction, Transaction};
use crate::descriptor::Descriptor;
use crate::error::CalculateFeeError;
use crate::error::{Alpha3Error, CalculateFeeError};
use crate::types::ScriptAmount;
use crate::types::{Balance, FeeRate};
use crate::Script;
@ -11,7 +11,7 @@ use bdk::bitcoin::psbt::PartiallySignedTransaction as BdkPartiallySignedTransact
use bdk::bitcoin::{OutPoint as BdkOutPoint, Sequence, Txid};
use bdk::wallet::tx_builder::ChangeSpendPolicy;
use bdk::wallet::Update as BdkUpdate;
use bdk::{Error as BdkError, FeeRate as BdkFeeRate};
use bdk::{FeeRate as BdkFeeRate};
use bdk::{SignOptions, Wallet as BdkWallet};
use std::collections::HashSet;
@ -29,7 +29,7 @@ impl Wallet {
descriptor: Arc<Descriptor>,
change_descriptor: Option<Arc<Descriptor>>,
network: Network,
) -> Result<Self, BdkError> {
) -> Result<Self, Alpha3Error> {
let descriptor = descriptor.as_string_private();
let change_descriptor = change_descriptor.map(|d| d.as_string_private());
@ -65,10 +65,10 @@ impl Wallet {
Balance::from(bdk_balance)
}
pub fn apply_update(&self, update: Arc<Update>) -> Result<(), BdkError> {
pub fn apply_update(&self, update: Arc<Update>) -> Result<(), Alpha3Error> {
self.get_wallet()
.apply_update(update.0.clone())
.map_err(|e| BdkError::Generic(e.to_string()))
.map_err(|e| Alpha3Error::Generic)
}
pub fn is_mine(&self, script: &Script) -> bool {
@ -81,11 +81,11 @@ impl Wallet {
&self,
psbt: Arc<PartiallySignedTransaction>,
// sign_options: Option<SignOptions>,
) -> Result<bool, BdkError> {
) -> Result<bool, Alpha3Error> {
let mut psbt = psbt.inner.lock().unwrap();
self.get_wallet()
.sign(&mut psbt, SignOptions::default())
.map_err(|e| BdkError::Generic(e.to_string()))
.map_err(|e| Alpha3Error::Generic)
}
pub fn sent_and_received(&self, tx: &Transaction) -> SentAndReceivedValues {
@ -465,7 +465,7 @@ impl TxBuilder {
pub(crate) fn finish(
&self,
wallet: &Wallet,
) -> Result<Arc<PartiallySignedTransaction>, BdkError> {
) -> Result<Arc<PartiallySignedTransaction>, Alpha3Error> {
// TODO: I had to change the wallet here to be mutable. Why is that now required with the 1.0 API?
let mut wallet = wallet.get_wallet();
let mut tx_builder = wallet.build_tx();
@ -560,9 +560,9 @@ impl BumpFeeTxBuilder {
pub(crate) fn finish(
&self,
wallet: &Wallet,
) -> Result<Arc<PartiallySignedTransaction>, BdkError> {
) -> Result<Arc<PartiallySignedTransaction>, Alpha3Error> {
let txid =
Txid::from_str(self.txid.as_str()).map_err(|e| BdkError::Generic(e.to_string()))?;
Txid::from_str(self.txid.as_str()).map_err(|e| Alpha3Error::Generic)?;
let mut wallet = wallet.get_wallet();
let mut tx_builder = wallet.build_fee_bump(txid)?;
tx_builder.fee_rate(BdkFeeRate::from_sat_per_vb(self.fee_rate));
@ -581,7 +581,7 @@ impl BumpFeeTxBuilder {
}
let psbt: BdkPartiallySignedTransaction = tx_builder
.finish()
.map_err(|e| BdkError::Generic(e.to_string()))?;
.map_err(|e| Alpha3Error::Generic)?;
Ok(Arc::new(psbt.into()))
}