txscript: add more detail to invalid tapscript merkle proof error

In this commit, we add more detail to the invalid tapscript merkle proof
error. Before this commit, the error was blank, making such a case hard
to debug. We'll now log the expected witness program, what we derived,
and also the passed in tapscript root.
This commit is contained in:
Olaoluwa Osuntokun 2024-12-03 16:42:30 +01:00
parent 25c804f13f
commit a155d466fc
No known key found for this signature in database
GPG key ID: 90525F7DEEE0AD86
2 changed files with 23 additions and 9 deletions

View file

@ -353,7 +353,10 @@ func VerifyTaprootLeafCommitment(controlBlock *ControlBlock,
expectedWitnessProgram := schnorr.SerializePubKey(taprootKey)
if !bytes.Equal(expectedWitnessProgram, taprootWitnessProgram) {
return scriptError(ErrTaprootMerkleProofInvalid, "")
str := fmt.Sprintf("derived witness program: %x, expected: "+
"%x, using tapscript_root: %x", expectedWitnessProgram,
taprootWitnessProgram, rootHash)
return scriptError(ErrTaprootMerkleProofInvalid, str)
}
// Finally, we'll verify that the parity of the y coordinate of the

View file

@ -293,13 +293,15 @@ func TestTapscriptCommitmentVerification(t *testing.T) {
// make from 0 to 1 leaf
// ensure verifies properly
testCases := []struct {
treeMutateFunc func(*IndexedTapScriptTree)
ctrlBlockMutateFunc func(*ControlBlock)
numLeaves int
valid bool
treeMutateFunc func(*IndexedTapScriptTree)
ctrlBlockMutateFunc func(*ControlBlock)
expectedErr ErrorCode
}{
// A valid merkle proof of a single leaf.
{
@ -324,9 +326,11 @@ func TestTapscriptCommitmentVerification(t *testing.T) {
{
numLeaves: 4,
valid: false,
expectedErr: ErrTaprootMerkleProofInvalid,
treeMutateFunc: func(t *IndexedTapScriptTree) {
for _, leafProof := range t.LeafMerkleProofs {
leafProof.InclusionProof[0] ^= 1
proofLen := len(leafProof.InclusionProof)
leafProof.InclusionProof[proofLen-1] ^= 1
}
},
},
@ -337,6 +341,7 @@ func TestTapscriptCommitmentVerification(t *testing.T) {
// key commitment.
numLeaves: 2,
valid: false,
expectedErr: ErrTaprootOutputKeyParityMismatch,
ctrlBlockMutateFunc: func(c *ControlBlock) {
c.OutputKeyYIsOdd = !c.OutputKeyYIsOdd
},
@ -391,6 +396,12 @@ func TestTapscriptCommitmentVerification(t *testing.T) {
"valid=%v, got valid=%v", testCase.valid,
valid)
}
if !valid {
if !IsErrorCode(err, testCase.expectedErr) {
t.Fatalf("expected error code %v, got %v", testCase.expectedErr, err)
}
}
}
// TODO(roasbeef): index correctness