Skip to content

Commit 0a1d453

Browse files
committed
tapsend: add err test case for stxo proof
This commit adds a test case for the creation of suffix proofs where the stxo proofs are not created. This results in an specific error being returned. The test case ensures that the expected error message is returned.
1 parent 97d58a7 commit 0a1d453

File tree

1 file changed

+56
-9
lines changed

1 file changed

+56
-9
lines changed

tapsend/proof_test.go

+56-9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,32 @@ var (
2727
// TestCreateProofSuffix tests the creation of suffix proofs for a given anchor
2828
// transaction.
2929
func TestCreateProofSuffix(t *testing.T) {
30+
testCases := []struct {
31+
name string
32+
stxoProof bool
33+
expectedErr string
34+
}{
35+
{
36+
name: "Correct inclusion and exclusion proofs",
37+
stxoProof: true,
38+
},
39+
{
40+
name: "No stxo proof",
41+
stxoProof: false,
42+
expectedErr: "error verifying STXO proof: missing " +
43+
"asset proof",
44+
},
45+
}
46+
for _, tc := range testCases {
47+
tc := tc
48+
49+
t.Run(tc.name, func(tt *testing.T) {
50+
createProofSuffix(t, tc.stxoProof, tc.expectedErr)
51+
})
52+
}
53+
}
54+
55+
func createProofSuffix(t *testing.T, stxoProof bool, expectedErr string) {
3056
testAssets := []*asset.Asset{
3157
asset.RandAsset(t, asset.RandAssetType(t)),
3258
asset.RandAsset(t, asset.RandAssetType(t)),
@@ -83,7 +109,10 @@ func TestCreateProofSuffix(t *testing.T) {
83109
}
84110
outputCommitments := make(map[uint32]*commitment.TapCommitment)
85111

86-
addOutputCommitment(t, anchorTx, outputCommitments, testPackets...)
112+
addOutputCommitment(
113+
t, anchorTx, outputCommitments, stxoProof,
114+
testPackets...,
115+
)
87116
addBip86Output(t, anchorTx.FundedPsbt.Pkt)
88117

89118
// Create a proof suffix for all 4 packets now and validate it.
@@ -106,18 +135,34 @@ func TestCreateProofSuffix(t *testing.T) {
106135
)
107136

108137
// Checking the transfer witness is the very last step
109-
// of the proof verification. Since we didn't properly
138+
// of the proof verification. Since we don't properly
110139
// sign the transfer, we expect the witness to be
111140
// invalid. But if we get to that point, we know that
112-
// all inclusion and exclusion proofs are correct (which
113-
// is what this test is testing).
141+
// all inclusion and exclusion proofs are correct. So
142+
// for successful cases we still expect an error, namely
143+
// the invalid witness error.
144+
errCode := txscript.ErrTaprootSigInvalid
114145
invalidWitnessErr := vm.Error{
115146
Kind: vm.ErrInvalidTransferWitness,
116147
Inner: txscript.Error{
117-
ErrorCode: txscript.ErrTaprootSigInvalid,
148+
ErrorCode: errCode,
118149
},
119150
}
120-
require.ErrorIs(t, err, invalidWitnessErr)
151+
if expectedErr == "" {
152+
require.ErrorIs(t, err, invalidWitnessErr)
153+
154+
continue
155+
}
156+
157+
if vPkt.Outputs[outIdx].Asset.IsTransferRoot() {
158+
require.ErrorContains(
159+
t, err, expectedErr,
160+
)
161+
} else {
162+
require.ErrorIs(
163+
t, err, invalidWitnessErr,
164+
)
165+
}
121166
}
122167
}
123168
}
@@ -206,7 +251,7 @@ func createPacket(t *testing.T, a *asset.Asset, split bool,
206251

207252
func addOutputCommitment(t *testing.T, anchorTx *AnchorTransaction,
208253
outputCommitments map[uint32]*commitment.TapCommitment,
209-
vPackets ...*tappsbt.VPacket) {
254+
stxoProof bool, vPackets ...*tappsbt.VPacket) {
210255

211256
packet := anchorTx.FundedPsbt.Pkt
212257

@@ -245,8 +290,10 @@ func addOutputCommitment(t *testing.T, anchorTx *AnchorTransaction,
245290

246291
c, err := commitment.FromAssets(nil, assets...)
247292
require.NoError(t, err)
248-
err = c.MergeAltLeaves(stxoAssetsByOutput[idx1])
249-
require.NoError(t, err)
293+
if stxoProof {
294+
err = c.MergeAltLeaves(stxoAssetsByOutput[idx1])
295+
require.NoError(t, err)
296+
}
250297

251298
internalKey := keyByOutput[idx1]
252299
script, err := tapscript.PayToAddrScript(*internalKey, nil, *c)

0 commit comments

Comments
 (0)