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.
pull/295/head
carla 4 years ago
parent 15a400b411
commit 7b56804bbe
No known key found for this signature in database
GPG Key ID: 4CA7FE54A6213C91

@ -2,6 +2,7 @@ package labels
import ( import (
"errors" "errors"
"strings"
) )
const ( const (
@ -29,16 +30,10 @@ func Validate(label string) error {
return ErrLabelTooLong 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 // 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 // it has our reserved prefix in another case, we just need to be able
// to reserve a subset of labels with this prefix. // to reserve a subset of labels with this prefix.
if label[0:len(Reserved)] == Reserved { if strings.HasPrefix(label, Reserved) {
return ErrReservedPrefix return ErrReservedPrefix
} }

@ -11,6 +11,7 @@ import (
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/lightninglabs/lndclient" "github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop" "github.com/lightninglabs/loop"
"github.com/lightninglabs/loop/labels"
"github.com/lightninglabs/loop/liquidity" "github.com/lightninglabs/loop/liquidity"
"github.com/lightninglabs/loop/loopdb" "github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/looprpc" "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{ req := &loop.OutRequest{
Amount: btcutil.Amount(in.Amt), Amount: btcutil.Amount(in.Amt),
DestAddr: sweepAddr, DestAddr: sweepAddr,
@ -477,6 +483,11 @@ func (s *swapClientServer) LoopIn(ctx context.Context,
return nil, err return nil, err
} }
// Check that the label is valid.
if err := labels.Validate(in.Label); err != nil {
return nil, err
}
req := &loop.LoopInRequest{ req := &loop.LoopInRequest{
Amount: btcutil.Amount(in.Amt), Amount: btcutil.Amount(in.Amt),
MaxMinerFee: btcutil.Amount(in.MaxMinerFee), MaxMinerFee: btcutil.Amount(in.MaxMinerFee),

@ -125,8 +125,9 @@ func putLabel(bucket *bbolt.Bucket, label string) error {
return nil return nil
} }
if err := labels.Validate(label); err != nil { // Check that the label does not exceed our maximum length.
return err if len(label) > labels.MaxLength {
return labels.ErrLabelTooLong
} }
return bucket.Put(labelKey, []byte(label)) return bucket.Put(labelKey, []byte(label))

@ -14,7 +14,6 @@ import (
"github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/lightninglabs/lndclient" "github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop/labels"
"github.com/lightninglabs/loop/loopdb" "github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/swap" "github.com/lightninglabs/loop/swap"
"github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/chainntnfs"
@ -79,11 +78,6 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig,
currentHeight int32, request *LoopInRequest) (*loopInInitResult, currentHeight int32, request *LoopInRequest) (*loopInInitResult,
error) { 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 // 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 // swap fee that we should subtract from the swap amount in the payment
// request that we send to the server. // request that we send to the server.

@ -13,7 +13,6 @@ import (
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/lightninglabs/lndclient" "github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop/labels"
"github.com/lightninglabs/loop/loopdb" "github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/swap" "github.com/lightninglabs/loop/swap"
"github.com/lightninglabs/loop/sweep" "github.com/lightninglabs/loop/sweep"
@ -88,11 +87,6 @@ type loopOutInitResult struct {
func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig, func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
currentHeight int32, request *OutRequest) (*loopOutInitResult, error) { 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. // Generate random preimage.
var swapPreimage [32]byte var swapPreimage [32]byte
if _, err := rand.Read(swapPreimage[:]); err != nil { if _, err := rand.Read(swapPreimage[:]); err != nil {

Loading…
Cancel
Save