-
Notifications
You must be signed in to change notification settings - Fork 89
feat(wallet): add Wallet::sign_psbt<K> wrapping bitcoin::Psbt::sign #438
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
EliteCoder18
wants to merge
3
commits into
bitcoindevkit:master
Choose a base branch
from
EliteCoder18:feat/sign-psbt-wrapper
base: master
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.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| use bdk_wallet::bitcoin::bip32::Xpriv; | ||
| use bdk_wallet::bitcoin::psbt::SigningKeys; | ||
| use bdk_wallet::bitcoin::{Amount, FeeRate, Psbt, TxIn}; | ||
| use bdk_wallet::test_utils::*; | ||
| use bdk_wallet::{psbt, KeychainKind, SignOptions}; | ||
|
|
@@ -221,3 +223,82 @@ fn test_psbt_multiple_internalkey_signers() { | |
| let verify_res = secp.verify_schnorr(&signature, &message, &xonlykey); | ||
| assert!(verify_res.is_ok(), "The wrong internal key was used"); | ||
| } | ||
|
|
||
| // wpkh PSBT with bip32_derivation populated, derived from | ||
| // tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L | ||
| const WPKH_PSBT_WITH_DERIVATION: &str = "cHNidP8BAHECAAAAAbOlV/kRKdNVk6Wn2cay5JUvpFw4tEsKWylqu+HfPKDyAAAAAAD9////ArObAAAAAAAAFgAU2+Ijq+8PDcPUGgHQqOPg9+6n9h8QJwAAAAAAABYAFKENkldInmhd2gMGYjkNwXeFL68T0AcAAAABAHEBAAAAATCzgdcz18YZK+8oNpJzqjM8ErFYW3hJLi+bO4bjmQrRAAAAAAD/////AlDDAAAAAAAAFgAUoQ2SV0ieaF3aAwZiOQ3Bd4UvrxOoYQAAAAAAABYAFIgWLNSQrRaGsRyWtCHaqeauCPYsAAAAAAEBH1DDAAAAAAAAFgAUoQ2SV0ieaF3aAwZiOQ3Bd4UvrxMiBgLOtp4iMz+DVWxdHvunWgM0a/PVLPvTn8XSTe0DTvfZ9Bjic/5CVAAAgAEAAIAAAACAAAAAAAAAAAAAIgIDxWYngmqPgL3saGZ4NTgcy5W/XINU8lkqnqjKC+oJuRwY4nP+QlQAAIABAACAAAAAgAEAAAAAAAAAACICAs62niIzP4NVbF0e+6daAzRr89Us+9OfxdJN7QNO99n0GOJz/kJUAACAAQAAgAAAAIAAAAAAAAAAAAA="; | ||
|
|
||
| #[test] | ||
| fn test_sign_psbt_with_xpriv() { | ||
| let mut psbt = Psbt::from_str(WPKH_PSBT_WITH_DERIVATION).unwrap(); | ||
| let (desc, change_desc) = get_test_wpkh_and_change_desc(); | ||
| let (wallet, _) = get_funded_wallet(desc, change_desc); | ||
|
|
||
| let xpriv = Xpriv::from_str( | ||
| "tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L", | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| let signing_keys = wallet | ||
| .sign_psbt(&mut psbt, &xpriv) | ||
| .expect("sign_psbt should succeed with the correct xpriv"); | ||
|
|
||
| assert!( | ||
| !signing_keys.is_empty(), | ||
| "expected at least one input to be signed" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_sign_psbt_with_wrong_key_signs_nothing() { | ||
| let mut psbt = Psbt::from_str(WPKH_PSBT_WITH_DERIVATION).unwrap(); | ||
| let (desc, change_desc) = get_test_wpkh_and_change_desc(); | ||
| let (wallet, _) = get_funded_wallet(desc, change_desc); | ||
|
|
||
| let wrong_xpriv = Xpriv::from_str( | ||
| "tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS", | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| let signing_keys = wallet | ||
| .sign_psbt(&mut psbt, &wrong_xpriv) | ||
| .expect("sign_psbt returns Ok even when no keys matched"); | ||
|
|
||
| let keys_used: usize = signing_keys | ||
| .values() | ||
| .map(|keys| match keys { | ||
| SigningKeys::Ecdsa(v) => v.len(), | ||
| SigningKeys::Schnorr(v) => v.len(), | ||
| }) | ||
| .sum(); | ||
| assert_eq!( | ||
| keys_used, 0, | ||
| "expected zero keys used when signing with an unrelated xpriv" | ||
| ); | ||
| for input in &psbt.inputs { | ||
| assert!( | ||
| input.partial_sigs.is_empty(), | ||
| "expected no partial signatures when signing with an unrelated key" | ||
| ); | ||
| } | ||
| } | ||
|
Collaborator
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. nit: you could also add a test that uses the |
||
|
|
||
| #[test] | ||
| fn test_sign_psbt_with_wallet_keymap() { | ||
| use miniscript::descriptor::KeyMapWrapper; | ||
| let mut psbt = Psbt::from_str(WPKH_PSBT_WITH_DERIVATION).unwrap(); | ||
| let (desc, change_desc) = get_test_wpkh_and_change_desc(); | ||
| let (wallet, _) = get_funded_wallet(desc, change_desc); | ||
|
|
||
| let keymap = wallet.get_keymap(); | ||
| let wrapper = KeyMapWrapper::from(keymap); | ||
|
|
||
| let signing_keys = wallet | ||
| .sign_psbt(&mut psbt, &wrapper) | ||
| .expect("sign_psbt should succeed with wallet keymap"); | ||
|
|
||
| assert!( | ||
| !signing_keys.is_empty(), | ||
| "expected at least one input to be signed using wallet keymap" | ||
| ); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
nit: What do you think of those docs changes? I added that it's used for software signing and pointed to the correct approach for external signing.
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.
I'm not sure if we should refer to
TransactionSigner, as it'll be later removed with signers module.The hardware signers should handle the PSBT and signing keys on their own, considering that we had archived https://github.com/bitcoindevkit/rust-hwi and it's considered unmaintained.
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.
Anyway, we can add it now and remove it when removing the module.
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.
Yep thanks, was thinking about it too.
From my understanding we added
sign_with_signersas a transition step to keep supporting the bdk signer for some more time while moving to psbt.sign.Which means yeah agree we shouldn't refer to it.