Use a thread-safe MemoryDatabase

This commit is contained in:
Sudarsan Balaji 2021-10-14 03:53:22 +05:30
parent a66e8eb8ed
commit 23c17ca841
3 changed files with 16 additions and 15 deletions

View File

@ -9,10 +9,13 @@ edition = "2018"
crate-type = ["cdylib"]
[dependencies]
bdk = { version = "^0.11", features = ["all-keys"] }
bdk = { version = "^0.12.1-dev", features = ["all-keys"] }
uniffi_macros = "0.14.0"
uniffi = "0.14.0"
thiserror = "1.0"
[build-dependencies]
uniffi_build = "0.14.0"
[patch.crates-io]
bdk = { git = "https://github.com/artfuldev/bdk.git", branch = "use-send-and-sync-on-memory-database" }

View File

@ -12,13 +12,13 @@ class LibTest {
val desc =
"wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)"
// @Test
// fun walletNewAddress() {
// val wallet = OfflineWallet(desc)
// val address = wallet.getNewAddress()
// assertNotNull(address)
// assertEquals(address, "bcrt1qzg4mckdh50nwdm9hkzq06528rsu73hjxytqkxs")
// }
@Test
fun walletNewAddress() {
val wallet = OfflineWallet(desc)
val address = wallet.getNewAddress()
assertNotNull(address)
assertEquals(address, "bcrt1qzg4mckdh50nwdm9hkzq06528rsu73hjxytqkxs")
}
@Test(expected=BdkException.Descriptor::class)
fun invalidDescriptorExceptionIsThrown() {

View File

@ -1,28 +1,26 @@
use bdk::bitcoin::Network;
use bdk::sled;
use bdk::sled::Tree;
use bdk::database::MemoryDatabase;
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: Mutex<Wallet<(), MemoryDatabase>>,
}
impl OfflineWallet {
fn new(descriptor: String) -> Result<Self, BdkError> {
let database = sled::open("testdb").unwrap();
let tree = database.open_tree("test").unwrap();
let database = MemoryDatabase::default();
let wallet = Mutex::new(Wallet::new_offline(
&descriptor,
None,
Network::Regtest,
tree,
database,
)?);
Ok(OfflineWallet { wallet })
}