Skip to content

Commit eaf4f88

Browse files
committed
Abstract out IsSegWitOutput from utxoupdatepsbt
This is not a pure refactor; additional functionality is added in IsSegWitOutput which lets it recurse into P2SH when a SigningProvider is provided that knows about the inner script.
1 parent e79bbb7 commit eaf4f88

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/rpc/rawtransaction.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,9 +1540,7 @@ UniValue utxoupdatepsbt(const JSONRPCRequest& request)
15401540

15411541
const Coin& coin = view.AccessCoin(psbtx.tx->vin[i].prevout);
15421542

1543-
std::vector<std::vector<unsigned char>> solutions_data;
1544-
txnouttype which_type = Solver(coin.out.scriptPubKey, solutions_data);
1545-
if (which_type == TX_WITNESS_V0_SCRIPTHASH || which_type == TX_WITNESS_V0_KEYHASH || which_type == TX_WITNESS_UNKNOWN) {
1543+
if (IsSegWitOutput(DUMMY_SIGNING_PROVIDER, coin.out.scriptPubKey)) {
15461544
input.witness_utxo = coin.out;
15471545
}
15481546
}

src/script/sign.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,3 +505,19 @@ FlatSigningProvider Merge(const FlatSigningProvider& a, const FlatSigningProvide
505505
ret.origins.insert(b.origins.begin(), b.origins.end());
506506
return ret;
507507
}
508+
509+
bool IsSegWitOutput(const SigningProvider& provider, const CScript& script)
510+
{
511+
std::vector<valtype> solutions;
512+
auto whichtype = Solver(script, solutions);
513+
if (whichtype == TX_WITNESS_V0_SCRIPTHASH || whichtype == TX_WITNESS_V0_KEYHASH || whichtype == TX_WITNESS_UNKNOWN) return true;
514+
if (whichtype == TX_SCRIPTHASH) {
515+
auto h160 = uint160(solutions[0]);
516+
CScript subscript;
517+
if (provider.GetCScript(h160, subscript)) {
518+
whichtype = Solver(subscript, solutions);
519+
if (whichtype == TX_WITNESS_V0_SCRIPTHASH || whichtype == TX_WITNESS_V0_KEYHASH || whichtype == TX_WITNESS_UNKNOWN) return true;
520+
}
521+
}
522+
return false;
523+
}

src/script/sign.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,7 @@ void UpdateInput(CTxIn& input, const SignatureData& data);
232232
* Solvability is unrelated to whether we consider this output to be ours. */
233233
bool IsSolvable(const SigningProvider& provider, const CScript& script);
234234

235+
/** Check whether a scriptPubKey is known to be segwit. */
236+
bool IsSegWitOutput(const SigningProvider& provider, const CScript& script);
237+
235238
#endif // BITCOIN_SCRIPT_SIGN_H

0 commit comments

Comments
 (0)