88 "sync"
99 "time"
1010
11+ "github.com/btcsuite/btcd/chaincfg"
1112 "github.com/btcsuite/btcutil"
1213 "github.com/lightninglabs/lndclient"
1314 "github.com/lightninglabs/loop"
@@ -34,6 +35,17 @@ const (
3435 minConfTarget = 2
3536)
3637
38+ var (
39+ // errIncorrectChain is returned when the format of the
40+ // destination address provided does not match the active chain.
41+ errIncorrectChain = errors .New ("invalid address format for the " +
42+ "active chain" )
43+
44+ // errConfTargetTooLow is returned when the chosen confirmation target
45+ // is below the allowed minimum.
46+ errConfTargetTooLow = errors .New ("confirmation target too low" )
47+ )
48+
3749// swapClientServer implements the grpc service exposed by loopd.
3850type swapClientServer struct {
3951 network lndclient.Network
@@ -58,13 +70,6 @@ func (s *swapClientServer) LoopOut(ctx context.Context,
5870
5971 log .Infof ("Loop out request received" )
6072
61- sweepConfTarget , err := validateConfTarget (
62- in .SweepConfTarget , loop .DefaultSweepConfTarget ,
63- )
64- if err != nil {
65- return nil , err
66- }
67-
6873 var sweepAddr btcutil.Address
6974 if in .Dest == "" {
7075 // Generate sweep address if none specified.
@@ -83,8 +88,10 @@ func (s *swapClientServer) LoopOut(ctx context.Context,
8388 }
8489 }
8590
86- // Check that the label is valid.
87- if err := labels .Validate (in .Label ); err != nil {
91+ sweepConfTarget , err := validateLoopOutRequest (
92+ s .lnd .ChainParams , in .SweepConfTarget , sweepAddr , in .Label ,
93+ )
94+ if err != nil {
8895 return nil , err
8996 }
9097
@@ -943,8 +950,9 @@ func validateConfTarget(target, defaultTarget int32) (int32, error) {
943950
944951 // Ensure the target respects our minimum threshold.
945952 case target < minConfTarget :
946- return 0 , fmt .Errorf ("a confirmation target of at least %v " +
947- "must be provided" , minConfTarget )
953+ return 0 , fmt .Errorf ("%w: A confirmation target of at " +
954+ "least %v must be provided" , errConfTargetTooLow ,
955+ minConfTarget )
948956
949957 default :
950958 return target , nil
@@ -969,3 +977,22 @@ func validateLoopInRequest(htlcConfTarget int32, external bool) (int32, error) {
969977
970978 return validateConfTarget (htlcConfTarget , loop .DefaultHtlcConfTarget )
971979}
980+
981+ // validateLoopOutRequest validates the confirmation target, destination
982+ // address and label of the loop out request.
983+ func validateLoopOutRequest (chainParams * chaincfg.Params , confTarget int32 ,
984+ sweepAddr btcutil.Address , label string ) (int32 , error ) {
985+ // Check that the provided destination address has the correct format
986+ // for the active network.
987+ if ! sweepAddr .IsForNet (chainParams ) {
988+ return 0 , fmt .Errorf ("%w: Current active network is %s" ,
989+ errIncorrectChain , chainParams .Name )
990+ }
991+
992+ // Check that the label is valid.
993+ if err := labels .Validate (label ); err != nil {
994+ return 0 , err
995+ }
996+
997+ return validateConfTarget (confTarget , loop .DefaultSweepConfTarget )
998+ }
0 commit comments