diff --git a/labels/labels.go b/labels/labels.go index b2137f4..d75376d 100644 --- a/labels/labels.go +++ b/labels/labels.go @@ -2,6 +2,7 @@ package labels import ( "errors" + "strings" ) const ( @@ -29,16 +30,10 @@ func Validate(label string) error { return ErrLabelTooLong } - // If the label is shorter than our reserved prefix, it cannot contain - // it. - if len(label) < len(Reserved) { - return nil - } - // Check if our label begins with our reserved prefix. We don't mind if // it has our reserved prefix in another case, we just need to be able // to reserve a subset of labels with this prefix. - if label[0:len(Reserved)] == Reserved { + if strings.HasPrefix(label, Reserved) { return ErrReservedPrefix } diff --git a/loopd/swapclient_server.go b/loopd/swapclient_server.go index 7dc1008..af0feed 100644 --- a/loopd/swapclient_server.go +++ b/loopd/swapclient_server.go @@ -11,6 +11,7 @@ import ( "github.com/btcsuite/btcutil" "github.com/lightninglabs/lndclient" "github.com/lightninglabs/loop" + "github.com/lightninglabs/loop/labels" "github.com/lightninglabs/loop/liquidity" "github.com/lightninglabs/loop/loopdb" "github.com/lightninglabs/loop/looprpc" @@ -79,6 +80,11 @@ func (s *swapClientServer) LoopOut(ctx context.Context, } } + // Check that the label is valid. + if err := labels.Validate(in.Label); err != nil { + return nil, err + } + req := &loop.OutRequest{ Amount: btcutil.Amount(in.Amt), DestAddr: sweepAddr, @@ -477,6 +483,11 @@ func (s *swapClientServer) LoopIn(ctx context.Context, return nil, err } + // Check that the label is valid. + if err := labels.Validate(in.Label); err != nil { + return nil, err + } + req := &loop.LoopInRequest{ Amount: btcutil.Amount(in.Amt), MaxMinerFee: btcutil.Amount(in.MaxMinerFee), diff --git a/loopdb/loopin.go b/loopdb/loopin.go index 756656c..e5fccb4 100644 --- a/loopdb/loopin.go +++ b/loopdb/loopin.go @@ -125,8 +125,9 @@ func putLabel(bucket *bbolt.Bucket, label string) error { return nil } - if err := labels.Validate(label); err != nil { - return err + // Check that the label does not exceed our maximum length. + if len(label) > labels.MaxLength { + return labels.ErrLabelTooLong } return bucket.Put(labelKey, []byte(label)) diff --git a/loopin.go b/loopin.go index 22dd056..0276412 100644 --- a/loopin.go +++ b/loopin.go @@ -14,7 +14,6 @@ import ( "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" "github.com/lightninglabs/lndclient" - "github.com/lightninglabs/loop/labels" "github.com/lightninglabs/loop/loopdb" "github.com/lightninglabs/loop/swap" "github.com/lightningnetwork/lnd/chainntnfs" @@ -79,11 +78,6 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig, currentHeight int32, request *LoopInRequest) (*loopInInitResult, error) { - // Before we start, check that the label is valid. - if err := labels.Validate(request.Label); err != nil { - return nil, err - } - // Request current server loop in terms and use these to calculate the // swap fee that we should subtract from the swap amount in the payment // request that we send to the server. diff --git a/loopout.go b/loopout.go index 120705d..b447a6d 100644 --- a/loopout.go +++ b/loopout.go @@ -13,7 +13,6 @@ import ( "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" "github.com/lightninglabs/lndclient" - "github.com/lightninglabs/loop/labels" "github.com/lightninglabs/loop/loopdb" "github.com/lightninglabs/loop/swap" "github.com/lightninglabs/loop/sweep" @@ -88,11 +87,6 @@ type loopOutInitResult struct { func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig, currentHeight int32, request *OutRequest) (*loopOutInitResult, error) { - // Before we start, check that the label is valid. - if err := labels.Validate(request.Label); err != nil { - return nil, err - } - // Generate random preimage. var swapPreimage [32]byte if _, err := rand.Read(swapPreimage[:]); err != nil {