Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1001}1003const BaseSignatureCreator& DUMMY_SIGNATURE_CREATOR = DummySignatureCreator(32, 32);1004const BaseSignatureCreator& DUMMY_MAXIMUM_SIGNATURE_CREATOR = DummySignatureCreator(33, 32);1006bool IsSegWitOutput(const SigningProvider& provider, const CScript& script)1007{1008int version;1009valtype program;1010if (script.IsWitnessProgram(version, program)) return true;1011if (script.IsPayToScriptHash()) {1012std::vector<valtype> solutions;1013auto whichtype = Solver(script, solutions);1014if (whichtype == TxoutType::SCRIPTHASH) {1015auto h160 = uint160(solutions[0]);1016CScript subscript;1017if (provider.GetCScript(CScriptID{h160}, subscript)) {1018if (subscript.IsWitnessProgram(version, program)) return true;1019}1020}1021}1022return false;1023}1025bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore, const std::map<COutPoint, Coin>& coins, const SignOptions& options, std::map<int, bilingual_str>& input_errors)1026{1027bool fHashSingle = ((options.sighash_type & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);1029// Use CTransaction for the constant parts of the1030// transaction to avoid rehashing.1031const CTransaction txConst(mtx);1033PrecomputedTransactionData txdata;1034std::vector<CTxOut> spent_outputs;1035for (unsigned int i = 0; i < mtx.vin.size(); ++i) {1036CTxIn& txin = mtx.vin[i];1037auto coin = coins.find(txin.prevout);1038if (coin == coins.end() || coin->second.IsSpent()) {1039txdata.Init(txConst, /*spent_outputs=*/{}, /*force=*/true);1040break;1041} else {1042spent_outputs.emplace_back(coin->second.out.nValue, coin->second.out.scriptPubKey);1043}1044}1045if (spent_outputs.size() == mtx.vin.size()) {1046txdata.Init(txConst, std::move(spent_outputs), true);1047}1049// Sign what we can:1050for (unsigned int i = 0; i < mtx.vin.size(); ++i) {1051CTxIn& txin = mtx.vin[i];1052auto coin = coins.find(txin.prevout);1053if (coin == coins.end() || coin->second.IsSpent()) {1054input_errors[i] = _("Input not found or already spent");1055continue;1056}1057const CScript& prevPubKey = coin->second.out.scriptPubKey;1058const CAmount& amount = coin->second.out.nValue;1060SignatureData sigdata = DataFromTransaction(mtx, i, coin->second.out);1061// Only sign SIGHASH_SINGLE if there's a corresponding output:1062if (!fHashSingle || (i < mtx.vout.size())) {1063ProduceSignature(*keystore, MutableTransactionSignatureCreator(mtx, i, amount, &txdata, options), prevPubKey, sigdata);1064}1066UpdateInput(txin, sigdata);1068// amount must be specified for valid segwit signature1069if (amount == MAX_MONEY && !txin.scriptWitness.IsNull()) {1070input_errors[i] = _("Missing amount");1071continue;1072}1074ScriptError serror = SCRIPT_ERR_OK;1075if (!sigdata.complete && !VerifyScript(txin.scriptSig, prevPubKey, &txin.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, TransactionSignatureChecker(&txConst, i, amount, txdata, MissingDataBehavior::FAIL), &serror)) {1076if (serror == SCRIPT_ERR_INVALID_STACK_OPERATION) {1077// Unable to sign input and verification failed (possible attempt to partially sign).1078input_errors[i] = Untranslated("Unable to sign input, invalid stack size (possibly missing key)");1079} else if (serror == SCRIPT_ERR_SIG_NULLFAIL) {1080// Verification failed (possibly due to insufficient signatures).1081input_errors[i] = Untranslated("CHECK(MULTI)SIG failing with non-zero signature (possibly need more signatures)");1082} else {1083input_errors[i] = Untranslated(ScriptErrorString(serror));1084}1085} else {1086// If this input succeeds, make sure there is no error set for it1087input_errors.erase(i);1088}1089}1090return input_errors.empty();1091}