Skip to content

Commit 8b5ef27

Browse files
committed
SignPSBTInput wrapper function
The SignPSBTInput function takes a PSBTInput, SignatureData, SigningProvider, and other data necessary for signing. It fills the SignatureData with data from the PSBTInput, retrieves the UTXO from the PSBTInput, signs and finalizes the input if possible, and then extracts the results from the SignatureData and puts them back into the PSBTInput.
1 parent 58a8e28 commit 8b5ef27

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/script/sign.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,32 @@ bool ProduceSignature(const SigningProvider& provider, const BaseSignatureCreato
234234
return sigdata.complete;
235235
}
236236

237+
bool SignPSBTInput(const SigningProvider& provider, const CMutableTransaction& tx, PSBTInput& input, SignatureData& sigdata, int index, int sighash)
238+
{
239+
// if this input has a final scriptsig or scriptwitness, don't do anything with it
240+
if (!input.final_script_sig.empty() || !input.final_script_witness.IsNull()) {
241+
return true;
242+
}
243+
244+
// Fill SignatureData with input info
245+
input.FillSignatureData(sigdata);
246+
247+
// Get UTXO
248+
CTxOut utxo;
249+
if (input.non_witness_utxo) {
250+
utxo = input.non_witness_utxo->vout[tx.vin[index].prevout.n];
251+
} else if (!input.witness_utxo.IsNull()) {
252+
utxo = input.witness_utxo;
253+
} else {
254+
return false;
255+
}
256+
257+
MutableTransactionSignatureCreator creator(&tx, index, utxo.nValue, sighash);
258+
bool sig_complete = ProduceSignature(provider, creator, utxo.scriptPubKey, sigdata);
259+
input.FromSignatureData(sigdata);
260+
return sig_complete;
261+
}
262+
237263
class SignatureExtractorChecker final : public BaseSignatureChecker
238264
{
239265
private:

src/script/sign.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,9 @@ bool ProduceSignature(const SigningProvider& provider, const BaseSignatureCreato
634634
bool SignSignature(const SigningProvider &provider, const CScript& fromPubKey, CMutableTransaction& txTo, unsigned int nIn, const CAmount& amount, int nHashType);
635635
bool SignSignature(const SigningProvider &provider, const CTransaction& txFrom, CMutableTransaction& txTo, unsigned int nIn, int nHashType);
636636

637+
/** Signs a PSBTInput */
638+
bool SignPSBTInput(const SigningProvider& provider, const CMutableTransaction& tx, PSBTInput& input, SignatureData& sigdata, int index, int sighash = 1);
639+
637640
/** Extract signature data from a transaction input, and insert it. */
638641
SignatureData DataFromTransaction(const CMutableTransaction& tx, unsigned int nIn, const CTxOut& txout);
639642
void UpdateInput(CTxIn& input, const SignatureData& data);

0 commit comments

Comments
 (0)