@@ -2477,6 +2477,78 @@ bool CWallet::SignTransaction(CMutableTransaction& tx, const std::map<COutPoint,
24772477 return false ;
24782478}
24792479
2480+ TransactionError CWallet::FillPSBT (PartiallySignedTransaction& psbtx, bool & complete, int sighash_type, bool sign, bool bip32derivs) const
2481+ {
2482+ LOCK (cs_wallet);
2483+ // Get all of the previous transactions
2484+ complete = true ;
2485+ for (unsigned int i = 0 ; i < psbtx.tx ->vin .size (); ++i) {
2486+ const CTxIn& txin = psbtx.tx ->vin [i];
2487+ PSBTInput& input = psbtx.inputs .at (i);
2488+
2489+ if (PSBTInputSigned (input)) {
2490+ continue ;
2491+ }
2492+
2493+ // Verify input looks sane. This will check that we have at most one uxto, witness or non-witness.
2494+ if (!input.IsSane ()) {
2495+ return TransactionError::INVALID_PSBT;
2496+ }
2497+
2498+ // If we have no utxo, grab it from the wallet.
2499+ if (!input.non_witness_utxo && input.witness_utxo .IsNull ()) {
2500+ const uint256& txhash = txin.prevout .hash ;
2501+ const auto it = mapWallet.find (txhash);
2502+ if (it != mapWallet.end ()) {
2503+ const CWalletTx& wtx = it->second ;
2504+ // We only need the non_witness_utxo, which is a superset of the witness_utxo.
2505+ // The signing code will switch to the smaller witness_utxo if this is ok.
2506+ input.non_witness_utxo = wtx.tx ;
2507+ }
2508+ }
2509+
2510+ // Get the Sighash type
2511+ if (sign && input.sighash_type > 0 && input.sighash_type != sighash_type) {
2512+ return TransactionError::SIGHASH_MISMATCH;
2513+ }
2514+
2515+ // Get the scriptPubKey to know which SigningProvider to use
2516+ CScript script;
2517+ if (!input.witness_utxo .IsNull ()) {
2518+ script = input.witness_utxo .scriptPubKey ;
2519+ } else if (input.non_witness_utxo ) {
2520+ if (txin.prevout .n >= input.non_witness_utxo ->vout .size ()) {
2521+ return TransactionError::MISSING_INPUTS;
2522+ }
2523+ script = input.non_witness_utxo ->vout [txin.prevout .n ].scriptPubKey ;
2524+ } else {
2525+ // There's no UTXO so we can just skip this now
2526+ complete = false ;
2527+ continue ;
2528+ }
2529+ SignatureData sigdata;
2530+ input.FillSignatureData (sigdata);
2531+ std::unique_ptr<SigningProvider> provider = GetSigningProvider (script, sigdata);
2532+ if (!provider) {
2533+ complete = false ;
2534+ continue ;
2535+ }
2536+
2537+ complete &= SignPSBTInput (HidingSigningProvider (provider.get (), !sign, !bip32derivs), psbtx, i, sighash_type);
2538+ }
2539+
2540+ // Fill in the bip32 keypaths and redeemscripts for the outputs so that hardware wallets can identify change
2541+ for (unsigned int i = 0 ; i < psbtx.tx ->vout .size (); ++i) {
2542+ const CTxOut& out = psbtx.tx ->vout .at (i);
2543+ std::unique_ptr<SigningProvider> provider = GetSigningProvider (out.scriptPubKey );
2544+ if (provider) {
2545+ UpdatePSBTOutput (HidingSigningProvider (provider.get (), true , !bip32derivs), psbtx, i);
2546+ }
2547+ }
2548+
2549+ return TransactionError::OK;
2550+ }
2551+
24802552bool CWallet::FundTransaction (CMutableTransaction& tx, CAmount& nFeeRet, int & nChangePosInOut, std::string& strFailReason, bool lockUnspents, const std::set<int >& setSubtractFeeFromOutputs, CCoinControl coinControl)
24812553{
24822554 std::vector<CRecipient> vecSend;
0 commit comments