rust-bitcoin / rust-bitcoin

Rust Bitcoin library
Creative Commons Zero v1.0 Universal
2.13k stars 696 forks source link

Is there an API to validate arbitrary scripts? #1929

Closed panicfarm closed 1 year ago

panicfarm commented 1 year ago

Is there a Rust validator of arbitrary scripts (not just miniscripts)? A script verification (most often) involves verification of (pubKey, signature) pairs, given the sighash. Is there an API that allows to verify the script and to return all (pubKey, signature) pairs, that have been successfully verified for a given script (regardless of the script logic)?

In bitcoin core, there's btcdeb that allows to step through, replay and validate the scripts.

For example, given tx b6f7b0b6561c0a7f5f5563c9b257efff0c25ee5b3bf0122c2947e20e713d1fcc inp 0

btcdeb --tx=0200000000010144ae030a640d2774c298ce074768ae695b99a661a334e6606615f00c28fb6159010000000000000000011d3304000000000022002048eaa50566119433c394141b7e4d7132f5998f0ba24c13ad4de868ec2e657fcb05004730440220274268e39f0342ac34e2d4a679d7803d7b9c65e7dbe1a65e13a3bfa9106d9540022020654045f11303fbb877da7b0370191092384bbbe37b38b73d8f4c2ef75ddeca0147304402206fdefee135b05f02641877cc7f6295dc7493df6d3e6355a0752e35aa889f0900022028f7f2c3b30d96969524e81fd550918a5662dd8c5194d2161640b61410b7adb001008576a91456f2c7661463ea13646ba92892d63436301ed5b58763ac67210349a7db1b1fe5485c1ae893c23b093178ef1ebe6309b6a6fa2dd3d944d3a24dda7c820120876475527c21024dc66c3d3f31447e29e67ba2b0a06f0d47d4622bd69ca07ed2d9e70b014d086652ae67a914c044924e2bca55b66fdf8fad5de34b7c736d34da88ac6868e8050c00 --txin=0200000000010120b6dcf72111276a81f1c281bb5c3f429334ef34bfa93b12f1a6705cb87420a4010000000046b5358004c2af0000000000002200205b8c77ef2af589d7ca490c3c924db961818e4db3e313b9e343db60edd8e111e92c5b0400000000002200206f1912b349fae83f4e8b05205ff64f3983c886fd70870cf17987ba5aed94cbd3bdc715000000000016001415f006476518a2626eea2b6eec5d78c5b1d65d27ccc952000000000022002048eaa50566119433c394141b7e4d7132f5998f0ba24c13ad4de868ec2e657fcb04004730440220563b0220ca7ca84dec381c76a1fd60a821209a296fc44132c7d67f647732674502200afb70d900b936c70c14696846600d2f7d2cc13daafdf7e387f30a79783ecae301483045022100a19e7b34dbebf9e62a6e44a582d5241ed3bd475f1631da81631082819cdd2e0802201cb742becd0014f7928d7ba602511c76f53ea23a6ec8b1146d29321bf76dead101475221028d74efe884d2be900f22577b7c1500f1702ba4f2c85c646189cdc360aa6f8cb52103effff6a089bd5481713b532e1a540235049c7a02fffa5f0d5fad3d2d492f3a8452aec70d6d20

i did a screen grab showing how btcdeb verifies this non-trivial script.

Here is the screenshot of btcdeb successfully verifying (pubKey, signature) pairs within a complex script, that I am looking for: pubkeys

apoelstra commented 1 year ago

No, rust-bitcoin does not have a script debugger. I'm not sure what Rust-based options there are available for that.

panicfarm commented 1 year ago

No, rust-bitcoin does not have a script debugger. I'm not sure what Rust-based options there are available for that.

Perhaps I could link and wrap bitcoin core to Rust to verify scripts? Are there examples like that?

TheBlueMatt commented 1 year ago

There's no way to step through scripts, and doing so isn't something generally supported by Bitcoin Core either, but you can use rust-bitcoinconsensus to get a binary pass/fail given a script and transaction.

panicfarm commented 1 year ago

There's no way to step through scripts, and doing so isn't something generally supported by Bitcoin Core either, but you can use rust-bitcoinconsensus to get a binary pass/fail given a script and transaction.

I am not necessarily interested in stepping through the scripts, I just need pass/fail and all (pubKey, sig) pairs that have been successfully verified in the process.

stevenroose commented 1 year ago

@panicfarm if your script is valid miniscript, you can do that with the miniscript library. If not, there is more general pass/fail in rust-bitcoinconsensus but that really just pass/fail and can be really annoying, though I have in the past been able to use gdb to see what's actually happening on the C++ side.

stevenroose commented 1 year ago

If you would want to write a Script debugger in Rust, that would be super epic haha :)

Fwiw, there is https://ide.scriptwiz.app/ which is super helpful too. But tooling is in JS.

tcharding commented 1 year ago

Can we close this @panicfarm?

panicfarm commented 1 year ago

Yes