Skip to content

Commit 5399e60

Browse files
committed
loopd: verify that dest addr is for correct chain
This commit adds verification to the loop out request to ensure that the formatting of the specified destination address matches the network that lnd is running on.
1 parent 5c34dd1 commit 5399e60

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

loopd/swapclient_server.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
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"
@@ -35,6 +36,11 @@ const (
3536
)
3637

3738
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+
3844
// errConfTargetTooLow is returned when the chosen confirmation target
3945
// is below the allowed minimum.
4046
errConfTargetTooLow = errors.New("confirmation target too low")
@@ -82,8 +88,9 @@ func (s *swapClientServer) LoopOut(ctx context.Context,
8288
}
8389
}
8490

85-
sweepConfTarget, err := validateLoopOutRequest(in.SweepConfTarget,
86-
in.Label)
91+
sweepConfTarget, err := validateLoopOutRequest(
92+
s.lnd.ChainParams, in.SweepConfTarget, sweepAddr, in.Label,
93+
)
8794
if err != nil {
8895
return nil, err
8996
}
@@ -922,9 +929,17 @@ func validateLoopInRequest(htlcConfTarget int32, external bool) (int32, error) {
922929
return validateConfTarget(htlcConfTarget, loop.DefaultHtlcConfTarget)
923930
}
924931

925-
// validateLoopOutRequest validates the confirmation target and label of the
926-
// loop out request.
927-
func validateLoopOutRequest(confTarget int32, label string) (int32, error) {
932+
// validateLoopOutRequest validates the confirmation target, destination
933+
// address and label of the loop out request.
934+
func validateLoopOutRequest(chainParams *chaincfg.Params, confTarget int32,
935+
sweepAddr btcutil.Address, label string) (int32, error) {
936+
// Check that the provided destination address has the correct format
937+
// for the active network.
938+
if !sweepAddr.IsForNet(chainParams) {
939+
return 0, fmt.Errorf("%w: Current active network is %s",
940+
errIncorrectChain, chainParams.Name)
941+
}
942+
928943
// Check that the label is valid.
929944
if err := labels.Validate(label); err != nil {
930945
return 0, err

loopd/swapclient_server_test.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,27 +163,71 @@ func TestValidateLoopInRequest(t *testing.T) {
163163
func TestValidateLoopOutRequest(t *testing.T) {
164164
tests := []struct {
165165
name string
166+
chain chaincfg.Params
166167
confTarget int32
168+
destAddr btcutil.Address
167169
label string
168170
err error
169171
expectedTarget int32
170172
}{
173+
{
174+
name: "mainnet address with mainnet backend",
175+
chain: chaincfg.MainNetParams,
176+
destAddr: mainnetAddr,
177+
label: "label ok",
178+
confTarget: 2,
179+
err: nil,
180+
expectedTarget: 2,
181+
},
182+
{
183+
name: "mainnet address with testnet backend",
184+
chain: chaincfg.TestNet3Params,
185+
destAddr: mainnetAddr,
186+
label: "label ok",
187+
confTarget: 2,
188+
err: errIncorrectChain,
189+
expectedTarget: 0,
190+
},
191+
{
192+
name: "testnet address with testnet backend",
193+
chain: chaincfg.TestNet3Params,
194+
destAddr: testnetAddr,
195+
label: "label ok",
196+
confTarget: 2,
197+
err: nil,
198+
expectedTarget: 2,
199+
},
200+
{
201+
name: "testnet address with mainnet backend",
202+
chain: chaincfg.MainNetParams,
203+
destAddr: testnetAddr,
204+
label: "label ok",
205+
confTarget: 2,
206+
err: errIncorrectChain,
207+
expectedTarget: 0,
208+
},
171209
{
172210
name: "invalid label",
211+
chain: chaincfg.MainNetParams,
212+
destAddr: mainnetAddr,
173213
label: labels.Reserved,
174214
confTarget: 2,
175215
err: labels.ErrReservedPrefix,
176216
expectedTarget: 0,
177217
},
178218
{
179219
name: "invalid conf target",
220+
chain: chaincfg.MainNetParams,
221+
destAddr: mainnetAddr,
180222
label: "label ok",
181223
confTarget: 1,
182224
err: errConfTargetTooLow,
183225
expectedTarget: 0,
184226
},
185227
{
186228
name: "default conf target",
229+
chain: chaincfg.MainNetParams,
230+
destAddr: mainnetAddr,
187231
label: "label ok",
188232
confTarget: 0,
189233
err: nil,
@@ -198,7 +242,8 @@ func TestValidateLoopOutRequest(t *testing.T) {
198242
t.Parallel()
199243

200244
conf, err := validateLoopOutRequest(
201-
test.confTarget, test.label,
245+
&test.chain, test.confTarget, test.destAddr,
246+
test.label,
202247
)
203248
require.True(t, errors.Is(err, test.err))
204249
require.Equal(t, test.expectedTarget, conf)

0 commit comments

Comments
 (0)