Skip to content

feat: Add sign method for psbt - #1051

Open
ItoroD wants to merge 1 commit into
bitcoindevkit:masterfrom
ItoroD:keymap-psbt-sign-vec
Open

ItoroD wants to merge 1 commit into
bitcoindevkit:masterfrom
ItoroD:keymap-psbt-sign-vec

Conversation

@ItoroD

@ItoroD ItoroD commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

Addresses #866

Notes to the reviewers

See comment - #866 (comment)

Documentation

Changelog

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt and cargo clippy before committing
  • I've added exactly one changelog:* label
  • I've linked the relevant upstream docs or specs above

New Features:

  • I've added tests for the new feature
  • I've added docs for the new feature

@Test
fun createExtendedWPKHDescriptors() {
Descriptor("wpkh($TEST_EXTENDED_PRIVKEY/$BIP84_TEST_RECEIVE_PATH/*)", NetworkKind.TEST)
val dec = Descriptor("wpkh($TEST_EXTENDED_PRIVKEY/$BIP84_TEST_RECEIVE_PATH/*)", NetworkKind.TEST)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should be removed.

@ItoroD
ItoroD force-pushed the keymap-psbt-sign-vec branch from b9ef430 to 29cd018 Compare August 10, 2026 15:27
@ItoroD

ItoroD commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Just did a rebase on this one and I see errors. Because I am changing the structure of keymap in this PR any other PR that makes use of keymap will cause this PR to fail after rebase. I will fix this and alert you when its done.

But we might want to consider reviewing this and merging this soon, to reduce the number of iterations. Or I fix it when we are almost ready for it.

@reez

reez commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

Just did a rebase on this one and I see errors. Because I am changing the structure of keymap in this PR any other PR that makes use of keymap will cause this PR to fail after rebase. I will fix this and alert you when its done.

But we might want to consider reviewing this and merging this soon, to reduce the number of iterations. Or I fix it when we are almost ready for it.

yeah feel free to just ping me here when it gets un-drafted so I can try to review it

@ItoroD
ItoroD force-pushed the keymap-psbt-sign-vec branch from 29cd018 to 841c17d Compare August 11, 2026 15:58
@ItoroD
ItoroD marked this pull request as ready for review August 11, 2026 16:22
@ItoroD
ItoroD requested a review from thunderbiscuit as a code owner August 11, 2026 16:22
@reez

reez commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

Shaping up nice, a couple questions:
-Psbt::sign currently discards rust-bitcoin’s signing result and always returns Ok, is that intended?
-Should we keep KeyMap opaque and construct miniscript’s KeyMapWrapper internally so consumers get the direct psbt.sign(keyMap) workflow discussed in 866?

@ItoroD

ItoroD commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

-Should we keep KeyMap opaque and construct miniscript’s KeyMapWrapper internally so consumers get the direct psbt.sign(keyMap) workflow discussed in 866?

Yes, I agree. Theres nothing else users are using the wrapper for. I will update to reflect this.

Psbt::sign currently discards rust-bitcoin’s signing result and always returns Ok, is that intended?

No, that was not intended. At the very least error should be return if error (I guess I got too excited once i saw sign worked 😂). But I did intentionally discard the rust-bitcoins result. Just like keymapwrapper, do our users have use for SigningKeysMap (rust-bitcoin’s signing result) I was not sure.

@ItoroD
ItoroD force-pushed the keymap-psbt-sign-vec branch 3 times, most recently from c407880 to ef814ee Compare August 24, 2026 18:03
@ItoroD

ItoroD commented Aug 24, 2026

Copy link
Copy Markdown
Contributor Author

Updated error message

@thunderbiscuit thunderbiscuit added this to the 3.2.0 milestone Oct 13, 2026
Comment thread bdk-ffi/src/bitcoin.rs Outdated
);
}
let bdk_wrapper = bdk_wallet::miniscript::descriptor::KeyMapWrapper::from(bdk_map);
let sign_result = psbt.sign(&bdk_wrapper, &secp);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we fix the origin-path handling in the pinned KeyMapWrapper before exposing this? With tr([fingerprint/86h/1h/0h]account_xprv/0/*) and a matching PSBT, signing returns no errors but produces a tap_key_sig that fails Schnorr verification. Miniscript 12.3.7 derives the full origin path again from the account xprv; the equivalent master-xprv case verifies successfully. Please use a corrected key lookup and add a regression that verifies the signature rather than only checking its presence.

@ItoroD ItoroD Oct 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide example of valid psbt you used for sign test? Also how did you verify schnorr were you expecting schnorr signature after signing ?

@j-kon j-kon Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I expected Schnorr here because this is a Taproot (tr(...)) input. I verified tap_key_sig against the tweaked output key using the transaction’s Taproot sighash.

I reran the test: the account-xprv case fails verification, while the equivalent master-xprv case passes. Neither reports a signing error. Just to clarify, this is a synthetic unit-test PSBT, not an on-chain funded transaction.

PSBT and signing descriptor (disposable test key)

Unsigned PSBT:

cHNidP8BAF4CAAAAAU1jsXk14vinaw/FavxEf6I13DTTWcQ3W3qfEO45TulmAAAAAAD/////ASgjAAAAAAAAIlEgXnzxN1lKw2Se0nelATBRTNfinxJWqjQNKLqvwkspeREAAAAAAAEBKxAnAAAAAAAAIlEgXnzxN1lKw2Se0nelATBRTNfinxJWqjQNKLqvwkspeREhFtXKImYR1dqFkU+0ktlTKKLzI4vHT1o4Ukyt7xYanJ5zGQBRWcFzVgAAgAEAAIAAAACAAAAAAAAAAAABFyDVyiJmEdXahZFPtJLZUyii8yOLx09aOFJMre8WGpyecwAA

Account descriptor (checksum omitted):

tr([5159c173/86h/1h/0h]tprv8gfBCLYgHZLJLhg1DUSY6NN6eNAee3aqt3SE3M1hywpAiFWYGsm6U6B17VDKh8W1SQnatRpZujU5xseJ84nYZmkxPGCngNhmBMVRY2sAZhY/0/*)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Will try with your examples by tomorrow.

Maybe a good place for you to check is bdk-wallet. So we see the results there as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I am just getting back, its been a busy couple of weeks. After some digging, I have confirmed that your findings are correct. Account level xprv descriptor fails when the keyMapWrapper is used. It seems like getkey implementation in miniscript fails to recognise that the path as already walked up until 86h/1h/0h and recomputes the full path again creating a key for 86h/1h/0h/86h/1h/0h/0/0 which is wrong. Should be 86h/1h/0h/0/0

I have tried this in bdk-wallet too and got the same issue. The error shows up when we try to do a finalize on the psbt. I also tried a getkey implementation of Signer from bdk_tx and that worked fine. It is indeed a miniscript issue like you said (at least 12.3.7). I have a feeling its been updated/fixed in more recent versions of miniscript as I skimmed through its recent getkey implementations and it looked updated. Not withstanding, 12.3 is still the version our latest bdk-wallet is on.

I will open an issue on bdk-wallet as we use keymapwrapper a lot in examples and in tests on there. It shouldnt be the case that they (miniscript) are assuming we will always pass a master-xpr in our descriptor anytime we are using KeyMapWrapper. Only master-xpr seems to be working with KeyMapWrapper. Opens a bigger conversation about exposing bdk_tx::Signer and using that instead of KeyMapWrapper getkey implementation.

I shall link the issue here as soon as I open it.

@ItoroD

ItoroD commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

This is now ready for review

@ItoroD
ItoroD force-pushed the keymap-psbt-sign-vec branch 2 times, most recently from a364463 to 43908e6 Compare November 13, 2026 13:14

@thunderbiscuit thunderbiscuit left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the simplified API! This is much easier to work with at the cost of having to wrap a few things ourselves.

As is however I think it's hard to use. Here are some problems I suspect I'd be facing if I was using it:

  1. The sign method only take a single descriptor but a lot of wallets would have 2, and you don't really know if any given transaction has inputs from both keychains or only one, so you'd need to always call it twice. Let's pass a vector of Descriptors to the method instead of a single one.
  2. The signing result is not super usable and could be reworked. For example, which public keys signed which input (SigningKeys) is a debugging detail. It's untyped (Vec) and splits ECDSA from Schnorr in a way callers don't really care about I don't think. At the moment, you also don't know if your call signed anything. You might have passed the wrong descriptor, or passed a public descriptor.

How do you feel about this:

#[derive(uniffi::Record)]
pub struct PsbtSignResult {
    /// Inputs that received at least one new signature in this call.
    pub signed_inputs: Vec<u64>,
    /// Inputs that could not be processed, keyed by input index.
    pub errors: HashMap<u64, PsbtSignError>,
}

This gives you an error per-input, and number of inputs signed. You still need to check whether all inputs are signed and the psbt is ready for finalizing using psbt.finalize().

You could also throw for public descriptors: if every descriptor's key_map is empty, return Err(NoSecretKeys). So the signature would become Result<PsbtSignResult, PsbtSignError>.

Note: the API docs on Psbt::sign are copy/pasted from rust-bitcoin but they refer to things that don't make sense for us, so I'd write them for our flow instead.

@reez

reez commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

All the concerns I raised earlier are resolved. I’ll let Thunder’s review comments #1051 (review) get addressed. Otherwise this LGTM

@ItoroD

ItoroD commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

@thunderbiscuit

Thanks for the review! On the result type:

At the moment, you also don't know if your call signed anything.

signing_keys_map does tell us: an input with a non-empty key list was signed. One catch is that rust-bitcoin also inserts inputs with an empty list when no key matched, so callers have to check the list rather than the index. That's the thing signed_inputs would simplify, and I'm fine with that.
My concern is the case where a descriptor has secret keys but none match any input. rust-bitcoin doesn't treat that as an error, so with the proposed shape signed_inputs and errors are both empty, which looks the same as nothing having been attempted. Would you want that to be explicit, for example an unsigned_inputs list (processed without error but not signed)? Otherwise I'll document that empty signed_inputs and errors means no keys matched.
I'll also do the Vec<Descriptor> change.

@thunderbiscuit

thunderbiscuit commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

I think an empty vector of input signed makes sense for signalling that no keys or conditions in the descriptors matched the requirements in the PSBT. If the user expected some signatures, then they'd have to investigate input per input, but for the base case a check that signed_inputs.len() != 0 would be ok in my mind to check that the Psbt::sign method has signed at least some input.

I'd be ok with a method on the type that would return this more explicitly if you like, something like PsbtSignResult::nothing_was_signed() that would return a bool, basically doing the check that PsbtSignResult.signed_inputs is an empty list. But also it feels like maybe more noise than anything since you can do that check yourself? Let me know what you think.

@ItoroD
ItoroD force-pushed the keymap-psbt-sign-vec branch from 43908e6 to bd9a33e Compare November 16, 2026 17:59

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants