Add a little bit of error handling

This commit is contained in:
Sudarsan Balaji 2021-10-14 00:05:50 +05:30
parent 64fbf60a6a
commit 279f304d5d
2 changed files with 55 additions and 13 deletions

View File

@ -2,7 +2,50 @@ namespace bdk {
};
[Error]
enum BdkError {
"InvalidU32Bytes",
"Generic",
"ScriptDoesntHaveAddressForm",
"NoRecipients",
"NoUtxosSelected",
"OutputBelowDustLimit",
"InsufficientFunds",
"BnBTotalTriesExceeded",
"BnBNoExactMatch",
"UnknownUtxo",
"TransactionNotFound",
"TransactionConfirmed",
"IrreplaceableTransaction",
"FeeRateTooLow",
"FeeTooLow",
"FeeRateUnavailable",
"MissingKeyOrigin",
"Key",
"ChecksumMismatch",
"SpendingPolicyRequired",
"InvalidPolicyPathError",
"Signer",
"InvalidNetwork",
"InvalidProgressValue",
"ProgressUpdateError",
"InvalidOutpoint",
"Descriptor",
"AddressValidator",
"Encode",
"Miniscript",
"Bip32",
"Secp256k1",
"Json",
"Hex",
"Psbt",
"PsbtParse",
"Electrum",
"Sled",
};
interface OfflineWallet {
[Throws=BdkError]
constructor(string descriptor);
string get_new_address();
};

View File

@ -2,30 +2,29 @@ use bdk::bitcoin::Network;
use bdk::sled;
use bdk::sled::Tree;
use bdk::wallet::AddressIndex;
use bdk::Error;
use bdk::Wallet;
use std::sync::Mutex;
type BdkError = Error;
uniffi_macros::include_scaffolding!("bdk");
struct OfflineWallet {
wallet: Mutex<Wallet<(), Tree>>,
//wallet: RwLock<Vec<String>>
}
impl OfflineWallet {
fn new(descriptor: String) -> Self {
fn new(descriptor: String) -> Result<Self, BdkError> {
let database = sled::open("testdb").unwrap();
let tree = database.open_tree("test").unwrap();
let wallet = Wallet::new_offline(&descriptor, None, Network::Regtest, tree).unwrap();
OfflineWallet {
wallet: Mutex::new(wallet),
}
// OfflineWallet {
// wallet: RwLock::new(Vec::new())
// }
let wallet = Mutex::new(Wallet::new_offline(
&descriptor,
None,
Network::Regtest,
tree,
)?);
Ok(OfflineWallet { wallet })
}
fn get_new_address(&self) -> String {