Skip to content

feat(wallet): expose finalize_psbt, cancel_tx, tx_details, descriptor_checksum, next_derivation_index#43

Merged
darioAnongba merged 12 commits intomainfrom
feat/wallet-api-expansion
Mar 25, 2026
Merged

feat(wallet): expose finalize_psbt, cancel_tx, tx_details, descriptor_checksum, next_derivation_index#43
darioAnongba merged 12 commits intomainfrom
feat/wallet-api-expansion

Conversation

@darioAnongba
Copy link
Copy Markdown
Collaborator

@darioAnongba darioAnongba commented Mar 17, 2026

Summary

Exposes five bdk_wallet::Wallet methods that were available upstream but not yet wrapped for WASM consumers. Continues the API surface expansion tracked in #21.

New Wallet Methods

Method Description
finalize_psbt(psbt, options) Finalize a PSBT (add finalized scripts/witnesses to inputs). Essential for multi-sig and watch-only workflows.
cancel_tx(tx) Release reserved change addresses when a transaction won't be broadcast.
tx_details(txid) Get comprehensive transaction details: sent, received, fee, fee rate, balance delta, chain position.
descriptor_checksum(keychain) Get the descriptor checksum string (8-char bech32) for a keychain.
next_derivation_index(keychain) Get the next derivation index (always returns a value, unlike derivation_index which returns None for fresh wallets).

New Type

  • TxDetails — WASM-compatible wrapper for bdk_wallet::TxDetails with getters for txid, sent, received, fee, fee_rate, balance_delta_sat (i64), chain_position, and tx.
    • balance_delta is exposed as i64 satoshis since SignedAmount has no existing WASM wrapper.

Tests

  • descriptor_checksum: Validates non-empty 8-char string, different for external/internal keychains.
  • next_derivation_index: Tests fresh wallet (returns 0), increment after reveal, consistency with derivation_index.
  • cancel_tx: Method existence and callability.
  • finalize_psbt: Method existence check (full PSBT finalization requires funded wallet, covered in esplora integration tests).
  • tx_details: Returns undefined for unknown txid.

Toshi added 2 commits March 17, 2026 10:07
…_checksum, next_derivation_index

Add five new Wallet methods that were available in bdk_wallet but not
yet exposed through the WASM bindings:

- finalize_psbt: Finalize a PSBT by adding finalized scripts and
  witnesses to inputs. Essential for multi-sig and watch-only workflows.

- cancel_tx: Inform the wallet that a transaction will not be broadcast,
  freeing reserved change addresses for future transactions.

- tx_details: Get comprehensive transaction details including sent,
  received, fee, fee rate, balance delta, and chain position.

- descriptor_checksum: Get the checksum portion of the descriptor string
  for a given keychain.

- next_derivation_index: Get the next unused derivation index, always
  returning a value (0 if nothing derived yet), unlike derivation_index
  which returns None.

Also adds the TxDetails WASM-compatible wrapper type with getters for
all fields, exposing balance_delta as i64 satoshis since SignedAmount
has no existing wrapper.

Closes #21
@darioAnongba darioAnongba marked this pull request as ready for review March 17, 2026 16:00
@darioAnongba darioAnongba self-assigned this Mar 17, 2026
Toshi added 9 commits March 24, 2026 10:03
Address review feedback on PR #43:

- tx_details: Add esplora integration test that verifies all TxDetails
  fields (txid, sent, received, fee, fee_rate, balance_delta_sat,
  chain_position, tx) for a known wallet transaction after self-send.
  Add unit test for fresh wallet returning undefined.

- finalize_psbt: Add esplora integration test that signs a PSBT with
  try_finalize=false, then calls finalize_psbt separately and verifies
  the PSBT is extractable. Replaces empty stub in unit tests.

- cancel_tx: Add esplora integration test that builds a tx (reserving
  a change address), cancels it, and verifies the internal derivation
  index stays unchanged. Improve unit test with index assertions.
…t, cancel_tx

- tx_details: Find the self-send tx (sent > 0) instead of assuming
  txs[0] is the self-send (it's the faucet funding tx on regtest)
- finalize_psbt/cancel_tx: Create new FeeRate instances instead of
  reusing the consumed feeRate variable (wasm-bindgen ownership)
- Remove unused dummyTxid variable (ESLint no-unused-vars)
SentAndReceived is a tuple struct without named field getters in the
wasm-bindgen TypeScript definitions. Use tx_details() which exposes
named sent/received getters to find the self-send transaction.
- finalize_psbt: sign() returns false with try_finalize=false because
  it reports finalization status, not signing success. Assert false
  then verify finalize_psbt succeeds separately.
- cancel_tx: next_derivation_index tracks derived count (not usage).
  Use list_unused_addresses to verify change addr returns to unused
  pool after cancel.
- cancel_tx test: use next_derivation_index and list_unused_addresses
  correctly to verify the change address is freed after cancellation,
  without depending on shared wallet state from prior tests
- events test: wait for the specific funding tx to be indexed by Esplora
  before running the full scan, fixing intermittent race condition where
  the block was indexed but the tx wasn't yet queryable
cancel_tx unmarks the change address so it can be reused. The test now
verifies this by building a tx (which advances the internal derivation
index), cancelling it, then building another tx and confirming the
derivation index does not advance further (proving the change address
was reused instead of revealing a new one).
…ming

- cancel_tx test: create fresh Recipient instances for the second
  build_tx call to avoid null pointer from wasm-bindgen ownership
  consumption of ScriptBuf and Amount objects
- events test beforeAll: wait for address-level Esplora indexing
  instead of just tx endpoint, since full_scan queries by script pubkey
- events test TxConfirmed after send: wait for tx to be confirmed in
  Esplora before syncing wallet to avoid race condition
…nge output

The cancel_tx test assumed that building a tx with 600 sats would
always produce a change output. With small UTXOs, the remaining
amount after send + fee can fall below the dust threshold (546 sats
for P2WPKH), causing BDK to fold it into fees instead of creating
a change output. This meant no new internal address was revealed,
failing the derivation index assertion.

Fix: send 25% of balance instead of a fixed 600 sats, guaranteeing
the remaining ~75% always exceeds the dust threshold and produces
a change output.
@darioAnongba darioAnongba merged commit 66bec32 into main Mar 25, 2026
12 checks passed
@darioAnongba darioAnongba deleted the feat/wallet-api-expansion branch March 25, 2026 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant