// Copyright (c) 2009-2018 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include bool PartiallySignedTransaction::IsNull() const { return !tx && inputs.empty() && outputs.empty() && unknown.empty(); } bool PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt) { // Prohibited to merge two PSBTs over different transactions if (tx->GetHash() != psbt.tx->GetHash()) { return false; } for (unsigned int i = 0; i < inputs.size(); ++i) { inputs[i].Merge(psbt.inputs[i]); } for (unsigned int i = 0; i < outputs.size(); ++i) { outputs[i].Merge(psbt.outputs[i]); } unknown.insert(psbt.unknown.begin(), psbt.unknown.end()); return true; } bool PartiallySignedTransaction::IsSane() const { for (PSBTInput input : inputs) { if (!input.IsSane()) return false; } return true; } bool PartiallySignedTransaction::AddInput(const CTxIn& txin, PSBTInput& psbtin) { if (std::find(tx->vin.begin(), tx->vin.end(), txin) != tx->vin.end()) { return false; } tx->vin.push_back(txin); psbtin.partial_sigs.clear(); psbtin.final_script_sig.clear(); inputs.push_back(psbtin); return true; } bool PartiallySignedTransaction::AddOutput(const CTxOut& txout, const PSBTOutput& psbtout) { tx->vout.push_back(txout); outputs.push_back(psbtout); return true; } bool PartiallySignedTransaction::GetInputUTXO(CTxOut& utxo, int input_index) const { PSBTInput input = inputs[input_index]; int prevout_index = tx->vin[input_index].prevout.n; if (input.non_witness_utxo) { utxo = input.non_witness_utxo->vout[prevout_index]; } else { return false; } return true; } bool PSBTInput::IsNull() const { return !non_witness_utxo && partial_sigs.empty() && unknown.empty() && hd_keypaths.empty() && redeem_script.empty(); } void PSBTInput::FillSignatureData(SignatureData& sigdata) const { if (!final_script_sig.empty()) { sigdata.scriptSig = final_script_sig; sigdata.complete = true; } if (sigdata.complete) { return; } sigdata.signatures.insert(partial_sigs.begin(), partial_sigs.end()); if (!redeem_script.empty()) { sigdata.redeem_script = redeem_script; } for (const auto& key_pair : hd_keypaths) { sigdata.misc_pubkeys.emplace(key_pair.first.GetID(), key_pair); } } void PSBTInput::FromSignatureData(const SignatureData& sigdata) { if (sigdata.complete) { partial_sigs.clear(); hd_keypaths.clear(); redeem_script.clear(); if (!sigdata.scriptSig.empty()) { final_script_sig = sigdata.scriptSig; } return; } partial_sigs.insert(sigdata.signatures.begin(), sigdata.signatures.end()); if (redeem_script.empty() && !sigdata.redeem_script.empty()) { redeem_script = sigdata.redeem_script; } for (const auto& entry : sigdata.misc_pubkeys) { hd_keypaths.emplace(entry.second); } } void PSBTInput::Merge(const PSBTInput& input) { if (!non_witness_utxo && input.non_witness_utxo) non_witness_utxo = input.non_witness_utxo; partial_sigs.insert(input.partial_sigs.begin(), input.partial_sigs.end()); hd_keypaths.insert(input.hd_keypaths.begin(), input.hd_keypaths.end()); unknown.insert(input.unknown.begin(), input.unknown.end()); if (redeem_script.empty() && !input.redeem_script.empty()) redeem_script = input.redeem_script; if (final_script_sig.empty() && !input.final_script_sig.empty()) final_script_sig = input.final_script_sig; } bool PSBTInput::IsSane() const { return true; } void PSBTOutput::FillSignatureData(SignatureData& sigdata) const { if (!redeem_script.empty()) { sigdata.redeem_script = redeem_script; } for (const auto& key_pair : hd_keypaths) { sigdata.misc_pubkeys.emplace(key_pair.first.GetID(), key_pair); } } void PSBTOutput::FromSignatureData(const SignatureData& sigdata) { if (redeem_script.empty() && !sigdata.redeem_script.empty()) { redeem_script = sigdata.redeem_script; } for (const auto& entry : sigdata.misc_pubkeys) { hd_keypaths.emplace(entry.second); } } bool PSBTOutput::IsNull() const { return redeem_script.empty() && hd_keypaths.empty() && unknown.empty(); } void PSBTOutput::Merge(const PSBTOutput& output) { hd_keypaths.insert(output.hd_keypaths.begin(), output.hd_keypaths.end()); unknown.insert(output.unknown.begin(), output.unknown.end()); if (redeem_script.empty() && !output.redeem_script.empty()) redeem_script = output.redeem_script; } bool SignPSBTInput(const SigningProvider& provider, const CMutableTransaction& tx, PSBTInput& input, int index, int sighash) { // if this input has a final scriptsig, don't do anything with it if (!input.final_script_sig.empty()) { return true; } // Fill SignatureData with input info SignatureData sigdata; input.FillSignatureData(sigdata); // Get UTXO CTxOut utxo; if (input.non_witness_utxo) { // If we're taking our information from a non-witness UTXO, verify that it matches the prevout. if (input.non_witness_utxo->GetHash() != tx.vin[index].prevout.hash) return false; utxo = input.non_witness_utxo->vout[tx.vin[index].prevout.n]; } else { return false; } MutableTransactionSignatureCreator creator(&tx, index, utxo.nValue, sighash); bool sig_complete = ProduceSignature(provider, creator, utxo.scriptPubKey, sigdata); input.FromSignatureData(sigdata); return sig_complete; } bool FinalizePSBT(PartiallySignedTransaction& psbtx) { // Finalize input signatures -- in case we have partial signatures that add up to a complete // signature, but have not combined them yet (e.g. because the combiner that created this // PartiallySignedTransaction did not understand them), this will combine them into a final // script. bool complete = true; for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) { PSBTInput& input = psbtx.inputs.at(i); complete &= SignPSBTInput(DUMMY_SIGNING_PROVIDER, *psbtx.tx, input, i, 1); } return complete; } bool FinalizeAndExtractPSBT(PartiallySignedTransaction& psbtx, CMutableTransaction& result) { // It's not safe to extract a PSBT that isn't finalized, and there's no easy way to check // whether a PSBT is finalized without finalizing it, so we just do this. if (!FinalizePSBT(psbtx)) { return false; } result = *psbtx.tx; for (unsigned int i = 0; i < result.vin.size(); ++i) { result.vin[i].scriptSig = psbtx.inputs[i].final_script_sig; } return true; } TransactionError CombinePSBTs(PartiallySignedTransaction& out, const std::vector& psbtxs) { out = psbtxs[0]; // Copy the first one // Merge for (auto it = std::next(psbtxs.begin()); it != psbtxs.end(); ++it) { if (!out.Merge(*it)) { return TransactionError::PSBT_MISMATCH; } } if (!out.IsSane()) { return TransactionError::INVALID_PSBT; } return TransactionError::OK; }