support BIP342 script-path taproot sighash#1
Conversation
hashForTaprootSignature only handled key-path (BIP341) spends. Allow a non-null scriptCode, interpreted as the 32-byte tapleaf hash, to produce the script-path sighash: set spend_type to 0x02 and append the ext_flag=1 extension (tapleaf_hash || key_version || codesep_pos). Key-path behaviour is unchanged. Add testSigHashTaprootScriptPath, whose golden value is cross-checked against btcd's txscript.CalcTapscriptSignaturehash (the same oracle reproduces the existing key-path vector exactly).
| final List<TransactionOutput> prevOuts = new ArrayList<>(); | ||
| prevOuts.add(new TransactionOutput( | ||
| RegTestParams.get(), null, Coin.valueOf(2_236_761L), p2trScript)); | ||
| prevOuts.add(new TransactionOutput( | ||
| RegTestParams.get(), null, Coin.valueOf(3_275_592L), p2trScript)); |
There was a problem hiding this comment.
This test is failing because the value of 2_236_761L is incorrect (with 2_236_249L the test passes). Maybe we can reuse the parsing from the test that is above this one:
bitcoinj/core/src/test/java/org/bitcoinj/core/TransactionTest.java
Lines 484 to 504 in 67d8a58
There was a problem hiding this comment.
Fixed by using the parsing logic 8701562
| public synchronized Sha256Hash hashForTaprootSignature( | ||
| int inputIndex, | ||
| byte[] scriptCode, | ||
| List<TransactionOutput> prevOutputs, | ||
| byte sigHashType){ |
There was a problem hiding this comment.
Considering we don't modify this code often and for future readability I think we should modify the argument scriptCode to tapLeafHash. Also I would add a nice doc comment to this function documenting that we don't support OP_CODESEPARATOR and what each argument is similar to what the library has for other functions:
bitcoinj/core/src/main/java/org/bitcoinj/core/Transaction.java
Lines 1302 to 1317 in 67d8a58
hashForTaprootSignature only handled key-path (BIP341) spends. Allow a non-null scriptCode, interpreted as the 32-byte tapleaf hash, to produce the script-path sighash: set spend_type to 0x02 and append the ext_flag=1 extension (tapleaf_hash || key_version || codesep_pos). Key-path behaviour is unchanged.
Add testSigHashTaprootScriptPath, whose golden value is cross-checked against btcd's txscript.CalcTapscriptSignaturehash (the same oracle reproduces the existing key-path vector exactly).