Skip to content

Commit 4a1ce01

Browse files
committed
tapsend: error out instead of panicking
If there is no root locator defined when creating a split commitment, we return an error instead of running into a panic later on.
1 parent cb19dd6 commit 4a1ce01

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

tapsend/send.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ var (
124124
// in a virtual transaction that was already committed to an anchor
125125
// output.
126126
ErrAssetNotSigned = errors.New("asset not signed")
127+
128+
// ErrNoRootLocator is an error that is returned when a split commitment
129+
// is created without a split root output.
130+
ErrNoRootLocator = errors.New(
131+
"cannot create split commitment without split root output",
132+
)
127133
)
128134

129135
var (
@@ -590,6 +596,13 @@ func PrepareOutputAssets(ctx context.Context, vPkt *tappsbt.VPacket) error {
590596
splitLocators = append(splitLocators, &locator)
591597
}
592598

599+
// If we're creating a split commitment, there must be a split root.
600+
// If none of the outputs was designated as the split root, then
601+
// something was done incorrectly.
602+
if rootLocator == nil {
603+
return ErrNoRootLocator
604+
}
605+
593606
splitCommitment, err := commitment.NewSplitCommitment(
594607
ctx, splitCommitmentInputs, rootLocator, splitLocators...,
595608
)

tapsend/send_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,21 @@ var prepareOutputAssetsTestCases = []testCase{{
777777
return nil
778778
},
779779
err: nil,
780+
}, {
781+
name: "asset split with missing root locator",
782+
f: func(t *testing.T) error {
783+
state := initSpendScenario(t)
784+
785+
pkt := createPacket(
786+
state.address1, state.asset2PrevID,
787+
state, state.asset2InputAssets, false,
788+
)
789+
790+
pkt.Outputs[0].Type = tappsbt.TypeSimple
791+
792+
return tapsend.PrepareOutputAssets(context.Background(), pkt)
793+
},
794+
err: tapsend.ErrNoRootLocator,
780795
}, {
781796
name: "full value non-interactive send with un-spendable change",
782797
f: func(t *testing.T) error {

0 commit comments

Comments
 (0)