Skip to content

Commit 7b56804

Browse files
committed
multi: move label validation to rpc and simplify validation function
Previously labels with reserved prefixes were added to provide us with a way to identify automatically dispatched loops. This commit moves the validation of these labels to the rpc level so that it will only apply to user-initiated swaps.
1 parent 15a400b commit 7b56804

File tree

5 files changed

+16
-21
lines changed

5 files changed

+16
-21
lines changed

labels/labels.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package labels
22

33
import (
44
"errors"
5+
"strings"
56
)
67

78
const (
@@ -29,16 +30,10 @@ func Validate(label string) error {
2930
return ErrLabelTooLong
3031
}
3132

32-
// If the label is shorter than our reserved prefix, it cannot contain
33-
// it.
34-
if len(label) < len(Reserved) {
35-
return nil
36-
}
37-
3833
// Check if our label begins with our reserved prefix. We don't mind if
3934
// it has our reserved prefix in another case, we just need to be able
4035
// to reserve a subset of labels with this prefix.
41-
if label[0:len(Reserved)] == Reserved {
36+
if strings.HasPrefix(label, Reserved) {
4237
return ErrReservedPrefix
4338
}
4439

loopd/swapclient_server.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/btcsuite/btcutil"
1212
"github.com/lightninglabs/lndclient"
1313
"github.com/lightninglabs/loop"
14+
"github.com/lightninglabs/loop/labels"
1415
"github.com/lightninglabs/loop/liquidity"
1516
"github.com/lightninglabs/loop/loopdb"
1617
"github.com/lightninglabs/loop/looprpc"
@@ -79,6 +80,11 @@ func (s *swapClientServer) LoopOut(ctx context.Context,
7980
}
8081
}
8182

83+
// Check that the label is valid.
84+
if err := labels.Validate(in.Label); err != nil {
85+
return nil, err
86+
}
87+
8288
req := &loop.OutRequest{
8389
Amount: btcutil.Amount(in.Amt),
8490
DestAddr: sweepAddr,
@@ -477,6 +483,11 @@ func (s *swapClientServer) LoopIn(ctx context.Context,
477483
return nil, err
478484
}
479485

486+
// Check that the label is valid.
487+
if err := labels.Validate(in.Label); err != nil {
488+
return nil, err
489+
}
490+
480491
req := &loop.LoopInRequest{
481492
Amount: btcutil.Amount(in.Amt),
482493
MaxMinerFee: btcutil.Amount(in.MaxMinerFee),

loopdb/loopin.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,9 @@ func putLabel(bucket *bbolt.Bucket, label string) error {
125125
return nil
126126
}
127127

128-
if err := labels.Validate(label); err != nil {
129-
return err
128+
// Check that the label does not exceed our maximum length.
129+
if len(label) > labels.MaxLength {
130+
return labels.ErrLabelTooLong
130131
}
131132

132133
return bucket.Put(labelKey, []byte(label))

loopin.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/btcsuite/btcd/chaincfg/chainhash"
1515
"github.com/btcsuite/btcd/wire"
1616
"github.com/lightninglabs/lndclient"
17-
"github.com/lightninglabs/loop/labels"
1817
"github.com/lightninglabs/loop/loopdb"
1918
"github.com/lightninglabs/loop/swap"
2019
"github.com/lightningnetwork/lnd/chainntnfs"
@@ -79,11 +78,6 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig,
7978
currentHeight int32, request *LoopInRequest) (*loopInInitResult,
8079
error) {
8180

82-
// Before we start, check that the label is valid.
83-
if err := labels.Validate(request.Label); err != nil {
84-
return nil, err
85-
}
86-
8781
// Request current server loop in terms and use these to calculate the
8882
// swap fee that we should subtract from the swap amount in the payment
8983
// request that we send to the server.

loopout.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/btcsuite/btcd/wire"
1414
"github.com/btcsuite/btcutil"
1515
"github.com/lightninglabs/lndclient"
16-
"github.com/lightninglabs/loop/labels"
1716
"github.com/lightninglabs/loop/loopdb"
1817
"github.com/lightninglabs/loop/swap"
1918
"github.com/lightninglabs/loop/sweep"
@@ -88,11 +87,6 @@ type loopOutInitResult struct {
8887
func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
8988
currentHeight int32, request *OutRequest) (*loopOutInitResult, error) {
9089

91-
// Before we start, check that the label is valid.
92-
if err := labels.Validate(request.Label); err != nil {
93-
return nil, err
94-
}
95-
9690
// Generate random preimage.
9791
var swapPreimage [32]byte
9892
if _, err := rand.Read(swapPreimage[:]); err != nil {

0 commit comments

Comments
 (0)