MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / SignTransaction

Function SignTransaction

src/script/sign.cpp:1025–1091  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1023}
1024
1025bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore, const std::map<COutPoint, Coin>& coins, const SignOptions& options, std::map<int, bilingual_str>& input_errors)
1026{
1027 bool fHashSingle = ((options.sighash_type & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);
1028
1029 // Use CTransaction for the constant parts of the
1030 // transaction to avoid rehashing.
1031 const CTransaction txConst(mtx);
1032
1033 PrecomputedTransactionData txdata;
1034 std::vector<CTxOut> spent_outputs;
1035 for (unsigned int i = 0; i < mtx.vin.size(); ++i) {
1036 CTxIn& txin = mtx.vin[i];
1037 auto coin = coins.find(txin.prevout);
1038 if (coin == coins.end() || coin->second.IsSpent()) {
1039 txdata.Init(txConst, /*spent_outputs=*/{}, /*force=*/true);
1040 break;
1041 } else {
1042 spent_outputs.emplace_back(coin->second.out.nValue, coin->second.out.scriptPubKey);
1043 }
1044 }
1045 if (spent_outputs.size() == mtx.vin.size()) {
1046 txdata.Init(txConst, std::move(spent_outputs), true);
1047 }
1048
1049 // Sign what we can:
1050 for (unsigned int i = 0; i < mtx.vin.size(); ++i) {
1051 CTxIn& txin = mtx.vin[i];
1052 auto coin = coins.find(txin.prevout);
1053 if (coin == coins.end() || coin->second.IsSpent()) {
1054 input_errors[i] = _("Input not found or already spent");
1055 continue;
1056 }
1057 const CScript& prevPubKey = coin->second.out.scriptPubKey;
1058 const CAmount& amount = coin->second.out.nValue;
1059
1060 SignatureData sigdata = DataFromTransaction(mtx, i, coin->second.out);
1061 // Only sign SIGHASH_SINGLE if there's a corresponding output:
1062 if (!fHashSingle || (i < mtx.vout.size())) {
1063 ProduceSignature(*keystore, MutableTransactionSignatureCreator(mtx, i, amount, &txdata, options), prevPubKey, sigdata);
1064 }
1065
1066 UpdateInput(txin, sigdata);
1067
1068 // amount must be specified for valid segwit signature
1069 if (amount == MAX_MONEY && !txin.scriptWitness.IsNull()) {
1070 input_errors[i] = _("Missing amount");
1071 continue;
1072 }
1073
1074 ScriptError serror = SCRIPT_ERR_OK;
1075 if (!sigdata.complete && !VerifyScript(txin.scriptSig, prevPubKey, &txin.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, TransactionSignatureChecker(&txConst, i, amount, txdata, MissingDataBehavior::FAIL), &serror)) {
1076 if (serror == SCRIPT_ERR_INVALID_STACK_OPERATION) {
1077 // Unable to sign input and verification failed (possible attempt to partially sign).
1078 input_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).
1081 input_errors[i] = Untranslated("CHECK(MULTI)SIG failing with non-zero signature (possibly need more signatures)");
1082 } else {

Callers 4

script_sign.cppFile · 0.50
VerifyScriptBenchFunction · 0.50

Calls 15

_Function · 0.85
DataFromTransactionFunction · 0.85
ProduceSignatureFunction · 0.85
UpdateInputFunction · 0.85
VerifyScriptFunction · 0.85
UntranslatedFunction · 0.85
ScriptErrorStringFunction · 0.85
findMethod · 0.80
sizeMethod · 0.45
endMethod · 0.45
IsSpentMethod · 0.45

Tested by 1