Skip to content

Commit

Permalink
temp 2: add generic alpha 3 error
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Jan 10, 2024
1 parent 71e9152 commit 1cb3a8b
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 137 deletions.
1 change: 1 addition & 0 deletions 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 @@ -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"] }
Expand Down
95 changes: 37 additions & 58 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
@@ -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
// ------------------------------------------------------------------------
Expand Down Expand Up @@ -36,7 +51,7 @@ dictionary Balance {
u64 total;
};

dictionary LocalUtxo {
dictionary LocalOutput {
OutPoint outpoint;
TxOut txout;
KeychainKind keychain;
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -165,7 +144,7 @@ interface TxBuilder {

TxBuilder enable_rbf_with_sequence(u32 nsequence);

[Throws=BdkError]
[Throws=Alpha3Error]
PartiallySignedTransaction finish([ByRef] Wallet wallet);
};

Expand All @@ -178,7 +157,7 @@ interface BumpFeeTxBuilder {

BumpFeeTxBuilder enable_rbf_with_sequence(u32 nsequence);

[Throws=BdkError]
[Throws=Alpha3Error]
PartiallySignedTransaction finish([ByRef] Wallet wallet);
};

Expand All @@ -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();
Expand All @@ -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]
Expand Down Expand Up @@ -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);
};

Expand Down Expand Up @@ -322,7 +301,7 @@ enum WordCount {
};

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

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

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

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

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

string serialize();
Expand All @@ -367,4 +346,4 @@ interface PartiallySignedTransaction {
dictionary OutPoint {
string txid;
u32 vout;
};
};
16 changes: 8 additions & 8 deletions bdk-ffi/src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 })
}

Expand Down Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions bdk-ffi/src/descriptor.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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())?;
Expand Down
Loading

0 comments on commit 1cb3a8b

Please sign in to comment.