forked from MetaMask/bdk-wasm
-
Notifications
You must be signed in to change notification settings - Fork 2
feat(wallet): expose finalize_psbt, cancel_tx, tx_details, descriptor_checksum, next_derivation_index #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
darioAnongba
wants to merge
3
commits into
main
Choose a base branch
from
feat/wallet-api-expansion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+229
−2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| use bdk_wallet::TxDetails as BdkTxDetails; | ||
| use wasm_bindgen::prelude::wasm_bindgen; | ||
|
|
||
| use super::{Amount, ChainPosition, FeeRate, Transaction, Txid}; | ||
|
|
||
| /// Detailed information about a wallet transaction. | ||
| /// | ||
| /// This type provides a comprehensive view of a transaction from the wallet's perspective, | ||
| /// including sent/received amounts, fees, fee rate, balance delta, and chain position. | ||
| /// | ||
| /// Obtain a `TxDetails` by calling `Wallet::tx_details(txid)`. | ||
| #[wasm_bindgen] | ||
| pub struct TxDetails { | ||
| txid: bitcoin::Txid, | ||
| sent: bitcoin::Amount, | ||
| received: bitcoin::Amount, | ||
| fee: Option<bitcoin::Amount>, | ||
| fee_rate: Option<bitcoin::FeeRate>, | ||
| balance_delta_sat: i64, | ||
| chain_position: bdk_wallet::chain::ChainPosition<bdk_wallet::chain::ConfirmationBlockTime>, | ||
| tx: bitcoin::Transaction, | ||
| } | ||
|
|
||
| #[wasm_bindgen] | ||
| impl TxDetails { | ||
| /// The transaction id. | ||
| #[wasm_bindgen(getter)] | ||
| pub fn txid(&self) -> Txid { | ||
| self.txid.into() | ||
| } | ||
|
|
||
| /// The sum of the transaction input amounts that spend from previous outputs | ||
| /// tracked by this wallet. | ||
| #[wasm_bindgen(getter)] | ||
| pub fn sent(&self) -> Amount { | ||
| self.sent.into() | ||
| } | ||
|
|
||
| /// The sum of the transaction outputs that send to script pubkeys tracked by | ||
| /// this wallet. | ||
| #[wasm_bindgen(getter)] | ||
| pub fn received(&self) -> Amount { | ||
| self.received.into() | ||
| } | ||
|
|
||
| /// The fee paid for the transaction, if known. | ||
| /// | ||
| /// This will be `None` if the transaction has inputs not owned by this wallet | ||
| /// and their `TxOut` values have not been inserted via `Wallet::insert_txout`. | ||
| #[wasm_bindgen(getter)] | ||
| pub fn fee(&self) -> Option<Amount> { | ||
| self.fee.map(Into::into) | ||
| } | ||
|
|
||
| /// The fee rate paid for the transaction, if known. | ||
| /// | ||
| /// Same conditions as `fee` for when this is `None`. | ||
| #[wasm_bindgen(getter)] | ||
| pub fn fee_rate(&self) -> Option<FeeRate> { | ||
| self.fee_rate.map(Into::into) | ||
| } | ||
|
|
||
| /// The net effect of the transaction on the wallet balance, in satoshis. | ||
| /// | ||
| /// Positive values mean the wallet received more than it spent (net inflow). | ||
| /// Negative values mean the wallet spent more than it received (net outflow). | ||
| #[wasm_bindgen(getter)] | ||
| pub fn balance_delta_sat(&self) -> i64 { | ||
| self.balance_delta_sat | ||
| } | ||
|
|
||
| /// The position of the transaction in the chain (confirmed or unconfirmed). | ||
| #[wasm_bindgen(getter)] | ||
| pub fn chain_position(&self) -> ChainPosition { | ||
| self.chain_position.into() | ||
| } | ||
|
|
||
| /// The complete transaction. | ||
| #[wasm_bindgen(getter)] | ||
| pub fn tx(&self) -> Transaction { | ||
| self.tx.clone().into() | ||
| } | ||
| } | ||
|
|
||
| impl From<BdkTxDetails> for TxDetails { | ||
| fn from(details: BdkTxDetails) -> Self { | ||
| TxDetails { | ||
| txid: details.txid, | ||
| sent: details.sent, | ||
| received: details.received, | ||
| fee: details.fee, | ||
| fee_rate: details.fee_rate, | ||
| balance_delta_sat: details.balance_delta.to_sat(), | ||
| chain_position: details.chain_position, | ||
| tx: details.tx.as_ref().clone(), | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -302,4 +302,78 @@ describe("Wallet", () => { | |
| expect(data.available).toBeDefined(); | ||
| } | ||
| }); | ||
|
|
||
| describe("descriptor_checksum", () => { | ||
| it("returns a non-empty checksum string", () => { | ||
| const checksum = wallet.descriptor_checksum("external"); | ||
|
|
||
| expect(typeof checksum).toBe("string"); | ||
| expect(checksum.length).toBeGreaterThan(0); | ||
| // Descriptor checksums are 8 characters of bech32 | ||
| expect(checksum.length).toBe(8); | ||
| }); | ||
|
|
||
| it("returns different checksums for external and internal keychains", () => { | ||
| const externalChecksum = wallet.descriptor_checksum("external"); | ||
| const internalChecksum = wallet.descriptor_checksum("internal"); | ||
|
|
||
| expect(externalChecksum).not.toBe(internalChecksum); | ||
| }); | ||
| }); | ||
|
|
||
| describe("next_derivation_index", () => { | ||
| it("returns 0 for a fresh wallet with no revealed addresses", () => { | ||
| const freshWallet = Wallet.create(network, externalDesc, internalDesc); | ||
| const index = freshWallet.next_derivation_index("external"); | ||
|
|
||
| expect(typeof index).toBe("number"); | ||
| expect(index).toBe(0); | ||
| }); | ||
|
|
||
| it("increments after revealing an address", () => { | ||
| const freshWallet = Wallet.create(network, externalDesc, internalDesc); | ||
| freshWallet.reveal_next_address("external"); | ||
| const index = freshWallet.next_derivation_index("external"); | ||
|
|
||
| expect(index).toBe(1); | ||
| }); | ||
|
|
||
| it("is consistent with derivation_index", () => { | ||
| const freshWallet = Wallet.create(network, externalDesc, internalDesc); | ||
| freshWallet.reveal_next_address("external"); | ||
| freshWallet.reveal_next_address("external"); | ||
|
|
||
| const derivIndex = freshWallet.derivation_index("external"); | ||
| const nextIndex = freshWallet.next_derivation_index("external"); | ||
|
|
||
| // next_derivation_index should be derivation_index + 1 | ||
| expect(nextIndex).toBe(derivIndex! + 1); | ||
| }); | ||
| }); | ||
|
|
||
| describe("cancel_tx", () => { | ||
| it("is callable on the wallet", () => { | ||
| // cancel_tx only unmarks change addresses; with an empty wallet it's a no-op. | ||
| // We verify the method exists and is callable. | ||
| expect(typeof wallet.cancel_tx).toBe("function"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("finalize_psbt", () => { | ||
| it("is callable with default SignOptions", () => { | ||
| expect(typeof wallet.finalize_psbt).toBe("function"); | ||
| // Full PSBT finalization is tested in esplora integration tests | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why add an empty test? where are these esplora tests using |
||
| // where we have funded wallets | ||
| }); | ||
| }); | ||
|
|
||
| describe("tx_details", () => { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need a more comprehensive test. |
||
| it("returns undefined for a non-existent txid", () => { | ||
| const unknownTxid = Txid.from_string( | ||
| "0000000000000000000000000000000000000000000000000000000000000000" | ||
| ); | ||
| const details = wallet.tx_details(unknownTxid); | ||
| expect(details).toBeUndefined(); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why add an empty test? where are these esplora tests using
cancel_tx?