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
Add psbt BIP174 test vectors tests #935
Conversation
I stole that error type, and a bunch of the code for signing PSBTs from the bdk project. By 'clean up' I meant remove all the variants that were not being used. Also, in general, all the error handling code is incomplete i.e., usage of
That's a PR you can do if you would like to. Conceptually simple but likely to get some argument/discussion if you like that sort of thing :) Otherwise I'll do it. Cheers, will review. EDIT: By the way, this should probably be a draft PR, its not ready for merge yet. EDIT EDIT: So cool to wake up on Monday morning and find all your problems from last week solved! Thanks man. |
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 going to push a commit to this branch @DanGould, want to take a look at it and squash it in if you agree with the changes?
examples/psbt.rs
Outdated
// OP_CHECKMULTISIG bug pops +1 value when evaluating. Need extra OP_0 byte in front | ||
// Why is OP_0 called `OP_PUSHBYTES_0` in rust-bitcoin? |
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 did some digging, this is because in rust-bitcoin we map OP_1 - OP_16 to all::OP_PUSHNUM_0
- all::OP_PUSHNUM_16
. Bitcoin Core does not define opcodes for the byte values 0x01 - 0x4b (at least not in enum opcodetype
) but in rust-bitcoin we define all::OP_PUSHBYTES_1
- all::OP_PUSHBYTES_75
, when these are serialized they are simply the byte values 0x01 - 0x4b. So, OP_0 could have been defined as OP_PUSHNUM_0
I guess but OP_PUSHBYTES_0
was chosen.
@apoelstra do you remember why this was done or why we did not explicitly have an OP_0
defined or aliased as we do for OP_FALSE
?
This seem so obvious I feel like I'm missing something?
/// OP_0, the zero byte, the empty stack.
pub static OP_ZERO: All = all::OP_PUSHBYTES_0;
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 think it just didn't occur to me. I have never needed to push a zero to the stack, only FALSE 🤷
I'd be happy to accept a new alias.
Nice work man! We have the seed passing issue to resolve still. Also, I'm starting to think that this should not be an example but rather should be an integration test. Then, the remaining TODOs are changes to |
Locally this builds fine but CI is including recent changes to the sighash types, no idea what is going on with that. Its your branch @DanGould so I won't mess with it but my blunt guess would be update your local |
Draft because I'm stuck. We are currently working on PSBT workflow that uses the test vectors from BIP174, issue rust-bitcoin#892 describes the problem. PR rust-bitcoin#935 implements a solution using the test vectors but the code is cluttered because of all the hex strings, also the code does not really show how a user of `rust-bitcoin` would go about using the `psbt` module. I propose that rust-bitcoin#935 goes into `src/tests`, this PR adds `src/examples/psbt.rs`. Add an example PSBT workflow based on a cold-storage signer and a watch-only online wallet. The watch-only wallet is the Creator, Updater, and Finalizer. The cold-storage wallet is the Signer.
Draft because I'm stuck. We are currently working on PSBT workflow that uses the test vectors from BIP174, issue rust-bitcoin#892 describes the problem. PR rust-bitcoin#935 implements a solution using the test vectors but the code is cluttered because of all the hex strings, also the code does not really show how a user of `rust-bitcoin` would go about using the `psbt` module. I propose that rust-bitcoin#935 goes into `src/tests`, this PR adds `src/examples/psbt.rs`. Add an example PSBT workflow based on a cold-storage signer and a watch-only online wallet. The watch-only wallet is the Creator, Updater, and Finalizer. The cold-storage wallet is the Signer.
concept ack to that and your psbt watch-only example as an example. I'll turn these TODOs into PRs. |
For your CI fixes you can check out #940 or
|
dea0872
to
4bde605
Compare
Why the heck aren't I seeing these errors when running |
Don't beat yourself up about it, it took me months on this project to work out how to see the Rust 1.29 errors. I don't use
And the shell function
|
@tcharding would it make sense to add this script to contributing.md? I wonder why this doesn't show up with act on its own. I get that this will change when we upgrade rust. this would have saved me 3 pushes at this point |
Yeah go for it. |
All green! I rekon this is pretty close to ready-for-review. I still rekon we should be asserting against PSBTs not against hex, do you have an opinion on that? |
31da281
to
eff0d9a
Compare
We're asserting against PSBTs now 👍 Sincere thanks for all the guidance. I'm thrilled to be able to contribute :D edit: if this is going to be an integration test it looks like we'll need a home for them. |
There were a few missed, I pushed a wip patch, you can squash if you like it. I also pushed a commit that moves the example to |
I'm glad you are enjoying it, this is a mad project with good people working on it. I'm learning heaps hacking on this repo. |
Oh and incase you want to know why its in |
Now this uses the same signer as #940, is rebased on master, and satisfies the clippy gods. I pair programmed the latest with @0xBEEFCAF3 and added him as a co-author. |
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.
This looks good, a few trivial whitespace issues to fix.
With the whitespace fixes: ACK 210f5ec
tests/psbt.rs
Outdated
psbt | ||
} | ||
|
||
/// Finalizes a PSBT accord to the Input Finalizer role described in BIP 174. |
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.
Perhaps we should comment here that finalizing can better be done with miniscript
?
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.
If we do we should add the same comment in examples/ecdsa-psbt.rs
.
tests/psbt.rs
Outdated
let mut inputs = psbt.inputs.clone(); | ||
|
||
for (i, input) in inputs.iter_mut().enumerate() { | ||
for sk in keys { | ||
let mut required_pks = input.bip32_derivation.keys(); | ||
let pk = sk.public_key(secp); | ||
if required_pks.any(|required| (*required) == pk.inner) { | ||
psbt_sign::sign(&mut psbt, sk, i, secp).expect("failed to sign input with key"); | ||
} | ||
} | ||
} |
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 applied the patches from this PR to the sign PSBT (#957) PR and was able to implement GetKey for &[PriavateKey]
based on this code. If/when both this PR and #957 merges this function can become
/// Signs `psbt` with `keys` if required.
fn sign(mut psbt: Psbt, keys: &[PrivateKey]) -> Psbt {
let secp = SECP256K1;
psbt.sign(&keys, &secp);
psbt
}
which smells like success :)
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.
This took a little massaging since &keys
needs to be GetKeys
. I think it works now
Thanks for sticking at this one @DanGould! |
Hey @DanGould, now that the |
I re-named the PR to remove the word "example" because this adds tests not example. |
62259a2
to
421fe92
Compare
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've said it before but this test is hot. All my suggestions are just nits though I'd like to see util
removed from the import paths if that's ok.
ACK 421fe92
bitcoin/tests/psbt.rs
Outdated
vout: input_0.index, | ||
}, | ||
script_sig: Script::new(), | ||
sequence: bitcoin::Sequence(0xFFFFFFFF), // Disable nSequence. |
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.
sequence: bitcoin::Sequence(0xFFFFFFFF), // Disable nSequence. | |
sequence: Sequence::MAX, // Disable nSequence. |
bitcoin/tests/psbt.rs
Outdated
|
||
Transaction { | ||
version: 2, | ||
lock_time: bitcoin::absolute::PackedLockTime::ZERO, |
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: With the addition of use bitcoin::locktime::absolute;
lock_time: bitcoin::absolute::PackedLockTime::ZERO, | |
lock_time: absolute::PackedLockTime::ZERO, |
bitcoin/tests/psbt.rs
Outdated
use bitcoin::util::bip32::{ExtendedPrivKey, ExtendedPubKey, Fingerprint, IntoDerivationPath, KeySource}; | ||
use bitcoin::util::psbt::{Psbt, PsbtSighashType}; |
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: We are in the process of flattening util
, would be better if the import path did not use util
(both psbt
and bip32
are in scope at the crate root).
Will need to be rebased once #1345 goes in. |
421fe92
to
5459c4f
Compare
added nits & |
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.
3ad55bf
to
12714f8
Compare
In preparation for adding integration tests in the standard Rust `tests/` directroy; move the contents of `test_data` to `tests/data`.
Implement Test Vectors from BIP 174 Co-authored-by: Tobin Harding <me@tobin.cc> Co-authored-by: Armin Sabouri <armins88@gmail.com>
12714f8
to
b8bd31d
Compare
cherry-picked #1347 so we have a clean history and pass CI before @tcharding wakes up |
ACK b8bd31d Thanks man! |
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.
resolves #892