Skip to content

Commit

Permalink
feat: expose commit method on wallet type
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed May 8, 2024
1 parent 72b5bfd commit e9a7628
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class LiveTxBuilderTest {
val fullScanRequest: FullScanRequest = wallet.startFullScan()
val update = esploraClient.fullScan(fullScanRequest, 10uL, 1uL)
wallet.applyUpdate(update)
wallet.commit()
println("Balance: ${wallet.getBalance().total}")

assert(wallet.getBalance().total > 0uL)
Expand All @@ -52,6 +53,7 @@ class LiveTxBuilderTest {
val fullScanRequest: FullScanRequest = wallet.startFullScan()
val update = esploraClient.fullScan(fullScanRequest, 10uL, 1uL)
wallet.applyUpdate(update)
wallet.commit()
println("Balance: ${wallet.getBalance().total}")

assert(wallet.getBalance().total > 0uL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class LiveWalletTest {
val fullScanRequest: FullScanRequest = wallet.startFullScan()
val update = esploraClient.fullScan(fullScanRequest, 10uL, 1uL)
wallet.applyUpdate(update)
wallet.commit()
println("Balance: ${wallet.getBalance().total}")
val balance: Balance = wallet.getBalance()
println("Balance: $balance")
Expand All @@ -52,8 +53,8 @@ class LiveWalletTest {
val esploraClient = EsploraClient("https://esplora.testnet.kuutamo.cloud/")
val fullScanRequest: FullScanRequest = wallet.startFullScan()
val update = esploraClient.fullScan(fullScanRequest, 10uL, 1uL)

wallet.applyUpdate(update)
wallet.commit()
println("Balance: ${wallet.getBalance().total}")
println("New address: ${wallet.revealNextAddress(KeychainKind.EXTERNAL).address}")

Expand Down
3 changes: 3 additions & 0 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ interface Wallet {
[Throws=CannotConnectError]
void apply_update(Update update);

[Throws=PersistenceError]
boolean commit();

boolean is_mine([ByRef] Script script);

[Throws=SignerError]
Expand Down
19 changes: 7 additions & 12 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,13 @@ impl Wallet {
.map_err(CannotConnectError::from)
}

// TODO: This is the fallible version of get_internal_address; should I rename it to get_internal_address?
// It's a slight change of the API, the other option is to rename the get_address to try_get_address
// pub fn try_get_internal_address(
// &self,
// address_index: AddressIndex,
// ) -> Result<AddressInfo, PersistenceError> {
// let address_info = self
// .get_wallet()
// .try_get_internal_address(address_index.into())?
// .into();
// Ok(address_info)
// }
pub fn commit(&self) -> Result<bool, PersistenceError> {
self.get_wallet()
.commit()
.map_err(|e| PersistenceError::Write {
error_message: e.to_string(),
})
}

pub fn network(&self) -> Network {
self.get_wallet().network()
Expand Down
18 changes: 11 additions & 7 deletions bdk-jvm/lib/src/test/kotlin/org/bitcoindevkit/LiveTxBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ class LiveTxBuilderTest {
@Test
fun testTxBuilder() {
val descriptor = Descriptor("wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)", Network.TESTNET)
val wallet = Wallet(descriptor, null, persistenceFilePath, Network.TESTNET)
val esploraClient = EsploraClient("https://esplora.testnet.kuutamo.cloud/")
val wallet = Wallet(descriptor, null, persistenceFilePath, Network.SIGNET)
// val esploraClient = EsploraClient("https://esplora.testnet.kuutamo.cloud/")
val esploraClient = EsploraClient("http://signet.bitcoindevkit.net")
val fullScanRequest: FullScanRequest = wallet.startFullScan()
val update = esploraClient.fullScan(fullScanRequest, 10uL, 1uL)
wallet.applyUpdate(update)
wallet.commit()
println("Balance: ${wallet.getBalance().total}")

assert(wallet.getBalance().total > 0uL)

val recipient: Address = Address("tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", Network.TESTNET)
val recipient: Address = Address("tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", Network.SIGNET)
val psbt: Psbt = TxBuilder()
.addRecipient(recipient.scriptPubkey(), 4200uL)
.feeRate(FeeRate.fromSatPerVb(2uL))
Expand All @@ -46,17 +48,19 @@ class LiveTxBuilderTest {
fun complexTxBuilder() {
val externalDescriptor = Descriptor("wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)", Network.TESTNET)
val changeDescriptor = Descriptor("wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/1/*)", Network.TESTNET)
val wallet = Wallet(externalDescriptor, changeDescriptor, persistenceFilePath, Network.TESTNET)
val esploraClient = EsploraClient("https://esplora.testnet.kuutamo.cloud/")
val wallet = Wallet(externalDescriptor, changeDescriptor, persistenceFilePath, Network.SIGNET)
// val esploraClient = EsploraClient("https://esplora.testnet.kuutamo.cloud/")
val esploraClient = EsploraClient("http://signet.bitcoindevkit.net")
val fullScanRequest: FullScanRequest = wallet.startFullScan()
val update = esploraClient.fullScan(fullScanRequest, 10uL, 1uL)
wallet.applyUpdate(update)
wallet.commit()
println("Balance: ${wallet.getBalance().total}")

assert(wallet.getBalance().total > 0uL)

val recipient1: Address = Address("tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", Network.TESTNET)
val recipient2: Address = Address("tb1qw2c3lxufxqe2x9s4rdzh65tpf4d7fssjgh8nv6", Network.TESTNET)
val recipient1: Address = Address("tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", Network.SIGNET)
val recipient2: Address = Address("tb1qw2c3lxufxqe2x9s4rdzh65tpf4d7fssjgh8nv6", Network.SIGNET)
val allRecipients: List<ScriptAmount> = listOf(
ScriptAmount(recipient1.scriptPubkey(), 4200uL),
ScriptAmount(recipient2.scriptPubkey(), 4200uL),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class LiveWalletTest {
val fullScanRequest: FullScanRequest = wallet.startFullScan()
val update = esploraClient.fullScan(fullScanRequest, 10uL, 1uL)
wallet.applyUpdate(update)
wallet.commit()
println("Balance: ${wallet.getBalance().total}")

assert(wallet.getBalance().total > 0uL)
Expand All @@ -51,6 +52,7 @@ class LiveWalletTest {
val fullScanRequest: FullScanRequest = wallet.startFullScan()
val update = esploraClient.fullScan(fullScanRequest, 10uL, 1uL)
wallet.applyUpdate(update)
wallet.commit()
println("Balance: ${wallet.getBalance().total}")
println("New address: ${wallet.revealNextAddress(KeychainKind.EXTERNAL).address.asString()}")

Expand Down
2 changes: 2 additions & 0 deletions bdk-python/tests/test_live_tx_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def test_tx_builder(self):
parallel_requests=1
)
wallet.apply_update(update)
wallet.commit()

self.assertGreater(wallet.get_balance().total, 0)

Expand Down Expand Up @@ -62,6 +63,7 @@ def complex_tx_builder(self):
parallel_requests=1
)
wallet.apply_update(update)
wallet.commit()

self.assertGreater(wallet.get_balance().total, 0)

Expand Down
2 changes: 2 additions & 0 deletions bdk-python/tests/test_live_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def test_synced_balance(self):
parallel_requests=1
)
wallet.apply_update(update)
wallet.commit()

self.assertGreater(wallet.get_balance().total, 0)

Expand Down Expand Up @@ -58,6 +59,7 @@ def test_broadcast_transaction(self):
parallel_requests=1
)
wallet.apply_update(update)
wallet.commit()

self.assertGreater(wallet.get_balance().total, 0)

Expand Down
4 changes: 3 additions & 1 deletion bdk-swift/Tests/BitcoinDevKitTests/LiveTxBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ final class LiveTxBuilderTests: XCTestCase {
parallelRequests: 1
)
try wallet.applyUpdate(update: update)
try wallet.commit()

XCTAssertGreaterThan(wallet.getBalance().total, UInt64(0), "Wallet must have positive balance, please add funds")

Expand Down Expand Up @@ -78,7 +79,8 @@ final class LiveTxBuilderTests: XCTestCase {
parallelRequests: 1
)
try wallet.applyUpdate(update: update)

try wallet.commit()

XCTAssertGreaterThan(wallet.getBalance().total, UInt64(0), "Wallet must have positive balance, please add funds")

let recipient1: Address = try Address(address: "tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", network: .testnet)
Expand Down
2 changes: 2 additions & 0 deletions bdk-swift/Tests/BitcoinDevKitTests/LiveWalletTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ final class LiveWalletTests: XCTestCase {
parallelRequests: 1
)
try wallet.applyUpdate(update: update)
try wallet.commit()

XCTAssertGreaterThan(wallet.getBalance().total, UInt64(0))

Expand Down Expand Up @@ -74,6 +75,7 @@ final class LiveWalletTests: XCTestCase {
parallelRequests: 1
)
try wallet.applyUpdate(update: update)
try wallet.commit()

XCTAssertGreaterThan(wallet.getBalance().total, UInt64(0), "Wallet must have positive balance, please add funds")

Expand Down

0 comments on commit e9a7628

Please sign in to comment.