@@ -155,34 +155,32 @@ static const struct wally_tx_output *utxo_from_input(const struct wally_psbt *ps
155155 return NULL ;
156156}
157157
158- /* Try to determine if a PSBT input is taproot.
159- * TODO: We could verify that the script and field checks are in sync
160- * here, i.e. that an input with taproot fields has a taproot script,
161- * and return an error otherwise.
162- */
163- static bool is_taproot_input (const struct wally_psbt * psbt ,
164- const struct wally_psbt_input * inp )
158+ struct wally_psbt_input * psbt_get_input_signature_type (const struct wally_psbt * psbt ,
159+ size_t index , uint32_t * value_out )
165160{
166- if (!inp )
167- return false;
168- else {
169- const struct wally_tx_output * utxo = utxo_from_input (psbt , inp );
170- if (utxo ) {
171- /* Determine from the scriptpubkey if possible */
172- size_t script_type ;
173- int ret = wally_scriptpubkey_get_type (utxo -> script , utxo -> script_len ,
174- & script_type );
175- if (ret == WALLY_OK )
176- return script_type == WALLY_SCRIPT_TYPE_P2TR ;
177- }
178- /* No usable UTXO/script for this input, check for taproot fields */
179- return inp -> taproot_leaf_hashes .num_items ||
180- inp -> taproot_leaf_scripts .num_items ||
181- inp -> taproot_leaf_signatures .num_items ||
182- wally_map_get_integer (& inp -> psbt_fields , PSBT_IN_TAP_INTERNAL_KEY ) ||
183- wally_map_get_integer (& inp -> psbt_fields , PSBT_IN_TAP_MERKLE_ROOT ) ||
184- wally_map_get_integer (& inp -> psbt_fields , PSBT_IN_TAP_KEY_SIG );
161+ struct wally_psbt_input * inp = psbt_get_input (psbt , index );
162+ const struct wally_tx_output * utxo = utxo_from_input (psbt , inp );
163+
164+ if (value_out )
165+ * value_out = 0 ;
166+ if (!utxo || !value_out )
167+ return NULL ;
168+
169+ if (scriptpubkey_is_p2tr (utxo -> script , utxo -> script_len )) {
170+ * value_out = WALLY_SIGTYPE_SW_V1 ;
171+ return inp ;
185172 }
173+
174+ /* Otherwise, follow core and use whether a witness utxo is present */
175+ * value_out = inp -> witness_utxo ? WALLY_SIGTYPE_SW_V0 : WALLY_SIGTYPE_PRE_SW ;
176+ return inp ;
177+ }
178+
179+ int wally_psbt_get_input_signature_type (const struct wally_psbt * psbt ,
180+ size_t index , uint32_t * value_out )
181+ {
182+ struct wally_psbt_input * inp = psbt_get_input_signature_type (psbt , index , value_out );
183+ return inp ? WALLY_OK : WALLY_EINVAL ;
186184}
187185
188186/* Set a struct member on a parent struct */
@@ -4579,12 +4577,10 @@ int wally_psbt_get_input_signature_hash(struct wally_psbt *psbt, size_t index,
45794577 unsigned char * bytes_out , size_t len )
45804578{
45814579 struct wally_map scripts , assets , values , * assets_p ;
4582- const struct wally_psbt_input * inp = psbt_get_input (psbt , index );
45834580 size_t is_pset ;
45844581 uint32_t sighash , sighash_type ;
4585- const bool is_taproot = is_taproot_input (psbt , inp );
4586- /* FIXME: Determine segwitness in a smarter way (e.g. prevout script */
4587- const bool is_segwit = inp && inp -> witness_utxo != NULL ;
4582+ const struct wally_psbt_input * inp = psbt_get_input_signature_type (psbt , index , & sighash_type );
4583+ const bool is_taproot = sighash_type == WALLY_SIGTYPE_SW_V1 ;
45884584 int ret ;
45894585
45904586 if (!tx || !inp || flags )
@@ -4608,11 +4604,7 @@ int wally_psbt_get_input_signature_hash(struct wally_psbt *psbt, size_t index,
46084604 /* FIXME: Support script path spends */
46094605 script = NULL ;
46104606 script_len = 0 ;
4611- sighash_type = WALLY_SIGTYPE_SW_V1 ;
4612- } else if (is_segwit )
4613- sighash_type = WALLY_SIGTYPE_SW_V0 ;
4614- else
4615- sighash_type = WALLY_SIGTYPE_PRE_SW ;
4607+ }
46164608
46174609 ret = get_signing_data (psbt , & scripts , assets_p , & values );
46184610 if (ret == WALLY_OK )
@@ -4640,9 +4632,9 @@ int wally_psbt_sign_input_bip32(struct wally_psbt *psbt,
46404632 unsigned char sig [EC_SIGNATURE_LEN + 1 ], der [EC_SIGNATURE_DER_MAX_LEN + 1 ];
46414633 unsigned char signing_key [EC_PRIVATE_KEY_LEN ];
46424634 size_t sig_len = EC_SIGNATURE_LEN , der_len , pubkey_idx ;
4643- uint32_t sighash ;
4644- struct wally_psbt_input * inp = psbt_get_input (psbt , index );
4645- const bool is_taproot = is_taproot_input ( psbt , inp ) ;
4635+ uint32_t sighash , sighash_type ;
4636+ struct wally_psbt_input * inp = psbt_get_input_signature_type (psbt , index , & sighash_type );
4637+ const bool is_taproot = sighash_type == WALLY_SIGTYPE_SW_V1 ;
46464638 int ret ;
46474639
46484640 if (!inp || !hdkey || hdkey -> priv_key [0 ] != BIP32_FLAG_KEY_PRIVATE ||
0 commit comments