BlockchainCommons / Learning-Bitcoin-from-the-Command-Line

A complete course for learning Bitcoin programming and usage from the command
3.12k stars 754 forks source link

touble with the second round of P2SH validation #554

Closed zerotobtc closed 2 years ago

zerotobtc commented 2 years ago

I'm having trouble with the second round of P2SH validation in 10.4 Scripting a multisig.

There it says:

Irgendwie funktioniert das nicht bei mir... Hat sich da was geändert und die Macher haben das noch nicht aktualisiert? Wenn ich das mit btcdeb durchlaufen lasse, bekomme ich den Fehler Somehow that doesnt work for me... Has something changed in the way to validate this with btcdeb? When I run this with btcdeb I get the error:

stack has 7 entries [require 1]
stack has 7 entries [require 4]
stack has 7 entries [require 7]
error: Signature is found in scriptCode

and all parameters are still on the stack. That means that OP_CHECKMULTISIG does not run properly, doesn't it? In the documentation of btc opcode it says:

Compares the first signature against each public key until it finds an ECDSA match. Starting with the subsequent public key, it compares the second signature against each remaining public key until it finds an ECDSA match. The process is repeated until all signatures have been checked or not enough public keys remain to produce a successful result. All signatures need to match a public key. Because public keys are not checked again if they fail any signature comparison, signatures must be placed in the scriptSig using the same order as their corresponding public keys were placed in the scriptPubKey or redeemScript. If all signatures are valid, 1 is returned, 0 otherwise. Due to a bug, one extra unused value is removed from the stack.

Source: https://en.bitcoin.it/wiki/Script

shannona commented 2 years ago

I double-checked, and the CHECKMULTISIG code is correct.

We don't use btcdeb in that section mainly because trying to check signatures with any scripting tools is very complex.

See: https://github.com/bitcoin-core/btcdeb/blob/master/doc/btcdeb.md#signature-checking

This command offers an example of pulling a multisig out of a transaction:

btcdeb --tx=010000000001019086ce64fce1bb086395faf6fac37c73f32ba4ea89330432bf8ee8035e9315aa0100000000ffffffff021353b9030000000017a914c3f413d0918853a8e23766678d2e3c2e5c8138bb8725e4973100000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d040047304402207f874ef00f11dcc9a621acad9354f3fca1bf90c43878f607b7e2d358088487e7022052a01b47b8eef5e1c96a6affdc3dac46fdc11b60612464dc8c5921a852090d2701483045022100c56ab2abb17fdf565417228763bc9f2940a6465042fd62fbd9f4c7406345d7f702201cb1a56b45181f8347713627b325ec5df48fc1aee6bdaf937cbb804d7409b10c016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000 --txin=0100000000010170a6ee35199eae2d8ea659561374fa704f8fd95188ff5931157e4598dd0c44020100000000ffffffff0280f0fa02000000001976a914eec426a744f7a3b2ffd346925ac832e248834dd788ac4013543500000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d04004730440220054c0b331a31496d9123aeabe8415b8d2f877f1cf67709120af4eb1e09de59e002206cdf84e733e23be531aff202f868d200773e22aa0037033a74fc6752df2fd19601483045022100b54fa12828d13b58cb654dd910b9e8b36d471d644d8f66516577990ca099ee19022048ea2ac78f964d1b823af70c13c5607a29b14bb2348022190b3c280f51ec5df2016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000

Note that the 2-of-3 multisig is input as the script and the signatures are input as the stack, which is the correct methodology for btcdeb.