@@ -516,14 +516,14 @@ class Psbt {
516516 throw new Error ( 'Need validator function to validate signatures' ) ;
517517 pubkey = pubkey && ( 0 , bip371_js_1 . toXOnly ) ( pubkey ) ;
518518 const allHashses = pubkey
519- ? getTaprootHashesForSig (
519+ ? getTaprootHashesForSigValidation (
520520 inputIndex ,
521521 input ,
522522 this . data . inputs ,
523523 pubkey ,
524524 this . __CACHE ,
525525 )
526- : getAllTaprootHashesForSig (
526+ : getAllTaprootHashesForSigValidation (
527527 inputIndex ,
528528 input ,
529529 this . data . inputs ,
@@ -900,7 +900,7 @@ class Psbt {
900900 throw new Error (
901901 `Need Schnorr Signer to sign taproot input #${ inputIndex } .` ,
902902 ) ;
903- const hashesForSig = getTaprootHashesForSig (
903+ const hashesForSig = getTaprootHashesForSigning (
904904 inputIndex ,
905905 input ,
906906 this . data . inputs ,
@@ -1348,7 +1348,7 @@ function getHashForSig(inputIndex, input, cache, forValidate, sighashTypes) {
13481348 hash,
13491349 } ;
13501350}
1351- function getAllTaprootHashesForSig ( inputIndex , input , inputs , cache ) {
1351+ function getAllTaprootHashesForSigValidation ( inputIndex , input , inputs , cache ) {
13521352 const allPublicKeys = [ ] ;
13531353 if ( input . tapInternalKey ) {
13541354 const key = getPrevoutTaprootKey ( inputIndex , input , cache ) ;
@@ -1361,7 +1361,13 @@ function getAllTaprootHashesForSig(inputIndex, input, inputs, cache) {
13611361 allPublicKeys . push ( ...tapScriptPubkeys ) ;
13621362 }
13631363 const allHashes = allPublicKeys . map ( publicKey =>
1364- getTaprootHashesForSig ( inputIndex , input , inputs , publicKey , cache ) ,
1364+ getTaprootHashesForSigValidation (
1365+ inputIndex ,
1366+ input ,
1367+ inputs ,
1368+ publicKey ,
1369+ cache ,
1370+ ) ,
13651371 ) ;
13661372 return allHashes . flat ( ) ;
13671373}
@@ -1372,7 +1378,7 @@ function getPrevoutTaprootKey(inputIndex, input, cache) {
13721378function trimTaprootSig ( signature ) {
13731379 return signature . length === 64 ? signature : signature . subarray ( 0 , 64 ) ;
13741380}
1375- function getTaprootHashesForSig (
1381+ function getTaprootHashesForSigning (
13761382 inputIndex ,
13771383 input ,
13781384 inputs ,
@@ -1381,17 +1387,64 @@ function getTaprootHashesForSig(
13811387 tapLeafHashToSign ,
13821388 allowedSighashTypes ,
13831389) {
1384- const unsignedTx = cache . __TX ;
13851390 const sighashType =
13861391 input . sighashType || transaction_js_1 . Transaction . SIGHASH_DEFAULT ;
13871392 checkSighashTypeAllowed ( sighashType , allowedSighashTypes ) ;
1393+ const keySpend = Boolean ( input . tapInternalKey && ! tapLeafHashToSign ) ;
1394+ return getTaprootHashesForSig (
1395+ inputIndex ,
1396+ input ,
1397+ inputs ,
1398+ pubkey ,
1399+ cache ,
1400+ keySpend ,
1401+ sighashType ,
1402+ tapLeafHashToSign ,
1403+ ) ;
1404+ }
1405+ function getTaprootHashesForSigValidation (
1406+ inputIndex ,
1407+ input ,
1408+ inputs ,
1409+ pubkey ,
1410+ cache ,
1411+ ) {
1412+ const sighashType =
1413+ input . sighashType || transaction_js_1 . Transaction . SIGHASH_DEFAULT ;
1414+ const keySpend = Boolean ( input . tapKeySig ) ;
1415+ return getTaprootHashesForSig (
1416+ inputIndex ,
1417+ input ,
1418+ inputs ,
1419+ pubkey ,
1420+ cache ,
1421+ keySpend ,
1422+ sighashType ,
1423+ ) ;
1424+ }
1425+ /*
1426+ * This helper method is used for both generating a hash for signing as well for validating;
1427+ * thus depending on context key spend can be detected either with tapInternalKey with no leaves
1428+ * or tapKeySig (note tapKeySig is a signature to the prevout pk, so tapInternalKey is not needed).
1429+ */
1430+ function getTaprootHashesForSig (
1431+ inputIndex ,
1432+ input ,
1433+ inputs ,
1434+ pubkey ,
1435+ cache ,
1436+ keySpend ,
1437+ sighashType ,
1438+ tapLeafHashToSign ,
1439+ ) {
1440+ const unsignedTx = cache . __TX ;
13881441 const prevOuts = inputs . map ( ( i , index ) =>
13891442 getScriptAndAmountFromUtxo ( index , i , cache ) ,
13901443 ) ;
13911444 const signingScripts = prevOuts . map ( o => o . script ) ;
13921445 const values = prevOuts . map ( o => o . value ) ;
13931446 const hashes = [ ] ;
1394- if ( input . tapInternalKey && ! tapLeafHashToSign ) {
1447+ if ( keySpend ) {
13951448 const outputKey =
13961449 getPrevoutTaprootKey ( inputIndex , input , cache ) || Uint8Array . from ( [ ] ) ;
13971450 if ( tools . compare ( ( 0 , bip371_js_1 . toXOnly ) ( pubkey ) , outputKey ) === 0 ) {
0 commit comments