@@ -1974,9 +1974,71 @@ SigningResult DescriptorScriptPubKeyMan::SignMessage(const std::string& message,
19741974 return SigningResult::OK;
19751975}
19761976
1977- TransactionError DescriptorScriptPubKeyMan::FillPSBT (PartiallySignedTransaction& psbt , int sighash_type, bool sign, bool bip32derivs) const
1977+ TransactionError DescriptorScriptPubKeyMan::FillPSBT (PartiallySignedTransaction& psbtx , int sighash_type, bool sign, bool bip32derivs) const
19781978{
1979- return TransactionError::INVALID_PSBT;
1979+ for (unsigned int i = 0 ; i < psbtx.tx ->vin .size (); ++i) {
1980+ const CTxIn& txin = psbtx.tx ->vin [i];
1981+ PSBTInput& input = psbtx.inputs .at (i);
1982+
1983+ if (PSBTInputSigned (input)) {
1984+ continue ;
1985+ }
1986+
1987+ // Verify input looks sane. This will check that we have at most one uxto, witness or non-witness.
1988+ if (!input.IsSane ()) {
1989+ return TransactionError::INVALID_PSBT;
1990+ }
1991+
1992+ // Get the Sighash type
1993+ if (sign && input.sighash_type > 0 && input.sighash_type != sighash_type) {
1994+ return TransactionError::SIGHASH_MISMATCH;
1995+ }
1996+
1997+ // Get the scriptPubKey to know which SigningProvider to use
1998+ CScript script;
1999+ if (!input.witness_utxo .IsNull ()) {
2000+ script = input.witness_utxo .scriptPubKey ;
2001+ } else if (input.non_witness_utxo ) {
2002+ if (txin.prevout .n >= input.non_witness_utxo ->vout .size ()) {
2003+ return TransactionError::MISSING_INPUTS;
2004+ }
2005+ script = input.non_witness_utxo ->vout [txin.prevout .n ].scriptPubKey ;
2006+ } else {
2007+ // There's no UTXO so we can just skip this now
2008+ continue ;
2009+ }
2010+ SignatureData sigdata;
2011+ input.FillSignatureData (sigdata);
2012+
2013+ std::unique_ptr<FlatSigningProvider> keys = MakeUnique<FlatSigningProvider>();
2014+ std::unique_ptr<FlatSigningProvider> script_keys = GetSigningProvider (script, sign);
2015+ if (script_keys) {
2016+ *keys = Merge (*keys, *script_keys);
2017+ } else {
2018+ // Maybe there are pubkeys listed that we can sign for
2019+ script_keys = MakeUnique<FlatSigningProvider>();
2020+ for (const auto & pk_pair : input.hd_keypaths ) {
2021+ const CPubKey& pubkey = pk_pair.first ;
2022+ std::unique_ptr<FlatSigningProvider> pk_keys = GetSigningProvider (pubkey);
2023+ if (pk_keys) {
2024+ *keys = Merge (*keys, *pk_keys);
2025+ }
2026+ }
2027+ }
2028+
2029+ SignPSBTInput (HidingSigningProvider (keys.get (), !sign, !bip32derivs), psbtx, i, sighash_type);
2030+ }
2031+
2032+ // Fill in the bip32 keypaths and redeemscripts for the outputs so that hardware wallets can identify change
2033+ for (unsigned int i = 0 ; i < psbtx.tx ->vout .size (); ++i) {
2034+ std::unique_ptr<SigningProvider> keys = GetSolvingProvider (psbtx.tx ->vout .at (i).scriptPubKey );
2035+ if (!keys) {
2036+ continue ;
2037+ }
2038+ UpdatePSBTOutput (HidingSigningProvider (keys.get (), true , !bip32derivs), psbtx, i);
2039+ }
2040+
2041+ return TransactionError::OK;
19802042}
19812043
19822044const CKeyMetadata* DescriptorScriptPubKeyMan::GetMetadata (const CTxDestination& dest) const
0 commit comments