Browse Source

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 3 months ago
parent
commit
7b56804bbe
No known key found for this signature in database GPG Key ID: 4CA7FE54A6213C91
5 changed files with 16 additions and 21 deletions
  1. +2
    -7
      labels/labels.go
  2. +11
    -0
      loopd/swapclient_server.go
  3. +3
    -2
      loopdb/loopin.go
  4. +0
    -6
      loopin.go
  5. +0
    -6
      loopout.go

+ 2
- 7
labels/labels.go View File

@ -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
}

+ 11
- 0
loopd/swapclient_server.go View File

@ -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),

+ 3
- 2
loopdb/loopin.go View File

@ -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))

+ 0
- 6
loopin.go View File

@ -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.

+ 0
- 6
loopout.go View File

@ -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 {

Loading…
Cancel
Save