| 743 | } |
| 744 | |
| 745 | bool ProduceSignature(const SigningProvider& provider, const BaseSignatureCreator& creator, const CScript& fromPubKey, SignatureData& sigdata) |
| 746 | { |
| 747 | if (sigdata.complete) return true; |
| 748 | |
| 749 | std::vector<valtype> result; |
| 750 | TxoutType whichType; |
| 751 | bool solved = SignStep(provider, creator, fromPubKey, result, whichType, SigVersion::BASE, sigdata); |
| 752 | bool P2SH = false; |
| 753 | CScript subscript; |
| 754 | |
| 755 | if (solved && whichType == TxoutType::SCRIPTHASH) |
| 756 | { |
| 757 | // Solver returns the subscript that needs to be evaluated; |
| 758 | // the final scriptSig is the signatures from that |
| 759 | // and then the serialized subscript: |
| 760 | subscript = CScript(result[0].begin(), result[0].end()); |
| 761 | sigdata.redeem_script = subscript; |
| 762 | solved = solved && SignStep(provider, creator, subscript, result, whichType, SigVersion::BASE, sigdata) && whichType != TxoutType::SCRIPTHASH; |
| 763 | P2SH = true; |
| 764 | } |
| 765 | |
| 766 | if (solved && whichType == TxoutType::WITNESS_V0_KEYHASH) |
| 767 | { |
| 768 | CScript witnessscript; |
| 769 | witnessscript << OP_DUP << OP_HASH160 << ToByteVector(result[0]) << OP_EQUALVERIFY << OP_CHECKSIG; |
| 770 | TxoutType subType; |
| 771 | solved = solved && SignStep(provider, creator, witnessscript, result, subType, SigVersion::WITNESS_V0, sigdata); |
| 772 | sigdata.scriptWitness.stack = result; |
| 773 | sigdata.witness = true; |
| 774 | result.clear(); |
| 775 | } |
| 776 | else if (solved && whichType == TxoutType::WITNESS_V0_SCRIPTHASH) |
| 777 | { |
| 778 | CScript witnessscript(result[0].begin(), result[0].end()); |
| 779 | sigdata.witness_script = witnessscript; |
| 780 | |
| 781 | TxoutType subType{TxoutType::NONSTANDARD}; |
| 782 | solved = solved && SignStep(provider, creator, witnessscript, result, subType, SigVersion::WITNESS_V0, sigdata) && subType != TxoutType::SCRIPTHASH && subType != TxoutType::WITNESS_V0_SCRIPTHASH && subType != TxoutType::WITNESS_V0_KEYHASH; |
| 783 | |
| 784 | // If we couldn't find a solution with the legacy satisfier, try satisfying the script using Miniscript. |
| 785 | // Note we need to check if the result stack is empty before, because it might be used even if the Script |
| 786 | // isn't fully solved. For instance the CHECKMULTISIG satisfaction in SignStep() pushes partial signatures |
| 787 | // and the extractor relies on this behaviour to combine witnesses. |
| 788 | if (!solved && result.empty()) { |
| 789 | WshSatisfier ms_satisfier{provider, sigdata, creator, witnessscript}; |
| 790 | const auto ms = miniscript::FromScript(witnessscript, ms_satisfier); |
| 791 | solved = ms && ms->Satisfy(ms_satisfier, result) == miniscript::Availability::YES; |
| 792 | } |
| 793 | result.emplace_back(witnessscript.begin(), witnessscript.end()); |
| 794 | |
| 795 | sigdata.scriptWitness.stack = result; |
| 796 | sigdata.witness = true; |
| 797 | result.clear(); |
| 798 | } else if (whichType == TxoutType::WITNESS_V1_TAPROOT && !P2SH) { |
| 799 | sigdata.witness = true; |
| 800 | if (solved) { |
| 801 | sigdata.scriptWitness.stack = std::move(result); |
| 802 | } |