Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/release-notes/release-notes-next.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@
keeps the deposits locked and sweeps the htlc timeout path once the htlc
times out.

* Setting a P2PKH or P2SH address or a raw public key as the autoloop
destination no longer crashes loopd. The address is now decoded for the
active network, and a raw public key is rejected.

* Autoloop, Instant Out, static address withdrawals and manual HTLC sweeps now
reject destination addresses of another network, as Loop Out already did. If
the saved autoloop destination belongs to another network, loopd removes it
at startup, and autoloop pays to wallet addresses.

#### Maintenance

#### Contributors (Alphabetical Order)
55 changes: 55 additions & 0 deletions instantout/instantout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/wire"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop/fsm"
"github.com/lightninglabs/loop/instantout/reservation"
"github.com/lightninglabs/loop/utils"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -269,3 +271,56 @@ func TestNewInstantOutRejectsNegativeMaxSwapFee(t *testing.T) {
_, err := manager.NewInstantOut(t.Context(), nil, "", -1)
require.ErrorContains(t, err, "maximum swap fee must not be negative")
}

// TestNewInstantOutRejectsUnsupportedDestAddr verifies that an instant out
// cannot pay to a destination type that loop out rejects.
func TestNewInstantOutRejectsUnsupportedDestAddr(t *testing.T) {
tests := []struct {
name string
addr string
}{
{
// The compressed secp256k1 generator point.
name: "raw public key",
addr: "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce" +
"28d959f2815b16f81798",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// A manager that only knows the network verifies that
// the destination is rejected before the swap starts.
manager := &Manager{
cfg: &Config{Network: &chaincfg.MainNetParams},
}

_, err := manager.NewInstantOut(
t.Context(), nil, test.addr, 0,
)
require.ErrorContains(
t, err, "unsupported destination address type",
)
require.Empty(t, manager.activeInstantOuts)
})
}
}

// TestNewInstantOutRejectsWrongNetworkDestAddr verifies that an instant out
// cannot pay to an address of another network.
func TestNewInstantOutRejectsWrongNetworkDestAddr(t *testing.T) {
addr, err := btcutil.NewAddressWitnessPubKeyHash(
make([]byte, 20), &chaincfg.TestNet3Params,
)
require.NoError(t, err)

// A manager that only knows the network verifies that the destination
// is rejected before the swap starts.
manager := &Manager{cfg: &Config{Network: &chaincfg.MainNetParams}}

_, err = manager.NewInstantOut(
t.Context(), nil, addr.EncodeAddress(), 0,
)
require.ErrorIs(t, err, utils.ErrWrongNetwork)
require.Empty(t, manager.activeInstantOuts)
}
12 changes: 12 additions & 0 deletions instantout/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/lightninglabs/loop/fsm"
"github.com/lightninglabs/loop/instantout/reservation"
"github.com/lightninglabs/loop/swapserverrpc"
"github.com/lightninglabs/loop/utils"
"github.com/lightningnetwork/lnd/lntypes"
)

Expand Down Expand Up @@ -154,6 +155,17 @@ func (m *Manager) NewInstantOut(ctx context.Context,
if err != nil {
return nil, err
}

err = utils.CheckAddrNetwork(sweepAddr, m.cfg.Network)
if err != nil {
return nil, err
}

// Reject the destination types that loop out rejects too.
if !utils.IsSupportedDestAddr(sweepAddr) {
return nil, fmt.Errorf("unsupported destination "+
"address type %T", sweepAddr)
}
}

m.Lock()
Expand Down
48 changes: 38 additions & 10 deletions liquidity/liquidity.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"github.com/lightninglabs/loop/loopdb"
clientrpc "github.com/lightninglabs/loop/looprpc"
"github.com/lightninglabs/loop/swap"
"github.com/lightninglabs/loop/utils"
"github.com/lightninglabs/taproot-assets/rfqmsg"
"github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/funding"
Expand Down Expand Up @@ -164,6 +165,11 @@ var (
// a extended public key account is set.
ErrAmbiguousDestAddr = errors.New("ambiguous destination address")

// ErrUnsupportedDestAddr is returned when the destination address has
// a type that loop out does not pay to.
ErrUnsupportedDestAddr = errors.New("unsupported destination address " +
"type")

// ErrAccountAndAddrType indicates if an account is set but the
// account address type is not or vice versa.
ErrAccountAndAddrType = errors.New("account and address type have " +
Expand Down Expand Up @@ -304,19 +310,12 @@ func (m *Manager) Run(ctx context.Context) error {
m.cfg.AutoloopTicker.Resume()
defer m.cfg.AutoloopTicker.Stop()

// Before we start the main loop, load the params from db.
req, err := m.loadParams(ctx)
// Before we start the main loop, restore the params from db.
err := m.restoreParams(ctx)
if err != nil {
return err
}

// Set the params if there's one.
if req != nil {
if err := m.SetParameters(ctx, req); err != nil {
return err
}
}

for {
select {
case <-m.cfg.AutoloopTicker.Ticks():
Expand Down Expand Up @@ -377,7 +376,7 @@ func (m *Manager) GetParameters() Parameters {
func (m *Manager) SetParameters(ctx context.Context,
req *clientrpc.LiquidityParameters) error {

params, err := RpcToParameters(req)
params, err := RpcToParameters(req, m.cfg.Lnd.ChainParams)
if err != nil {
return err
}
Expand Down Expand Up @@ -453,6 +452,35 @@ func (m *Manager) saveParams(ctx context.Context, req proto.Message) error {
return nil
}

// restoreParams sets the parameters saved in the db, if there are any.
func (m *Manager) restoreParams(ctx context.Context) error {
req, err := m.loadParams(ctx)
if err != nil {
return err
}

// There is nothing to restore if no params were saved.
if req == nil {
return nil
}

err = m.SetParameters(ctx, req)
if !errors.Is(err, utils.ErrWrongNetwork) {
return err
}

// Earlier versions saved the autoloop destination address without
// checking its network. Remove such an address, so that autoloop pays
// to wallet addresses, instead of failing to start: the params can't
// be changed while loopd is down.
log.Warnf("Removing the saved autoloop destination address %v: %v",
req.AutoloopDestAddress, err)

req.AutoloopDestAddress = ""

return m.SetParameters(ctx, req)
}

// loadParams unmarshals a serialized RPC request from db and returns the RPC
// request.
func (m *Manager) loadParams(ctx context.Context) (
Expand Down
42 changes: 42 additions & 0 deletions liquidity/liquidity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop"
"github.com/lightninglabs/loop/labels"
Expand Down Expand Up @@ -441,6 +442,47 @@ func TestPersistParams(t *testing.T) {
)
}

// TestRestoreParamsWrongNetworkDestAddr verifies that loopd removes a saved
// autoloop destination address of another network at startup, instead of
// failing to start.
func TestRestoreParamsWrongNetworkDestAddr(t *testing.T) {
ctx := t.Context()

var paramsBytes []byte
cfg, _ := newTestConfig()
cfg.FetchLiquidityParams = func(context.Context) ([]byte, error) {
return paramsBytes, nil
}
cfg.PutLiquidityParams = func(_ context.Context, data []byte) error {
paramsBytes = data

return nil
}
manager := NewManager(cfg)

// The mock lnd runs on testnet3. Earlier versions could save a mainnet
// destination address there.
destAddr, err := btcutil.NewAddressWitnessPubKeyHash(
make([]byte, 20), &chaincfg.MainNetParams,
)
require.NoError(t, err)

req, err := ParametersToRpc(defaultParameters)
require.NoError(t, err)
req.AutoloopDestAddress = destAddr.EncodeAddress()
req.AutoMaxInFlight = 3
require.NoError(t, manager.saveParams(ctx, req))

// The other params are restored without the destination address.
require.NoError(t, manager.restoreParams(ctx))
require.Nil(t, manager.GetParameters().DestAddr)
require.Equal(t, 3, manager.GetParameters().MaxAutoInFlight)

req, err = manager.loadParams(ctx)
require.NoError(t, err)
require.Empty(t, req.AutoloopDestAddress)
}

// TestStaticAddressAutoloopRequiresExperimental verifies that static-address
// loop-in sources are rejected unless the daemon enabled the experimental
// static autoloop path.
Expand Down
22 changes: 18 additions & 4 deletions liquidity/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"time"

"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/lightninglabs/lndclient"
clientrpc "github.com/lightninglabs/loop/looprpc"
"github.com/lightninglabs/loop/swap"
"github.com/lightninglabs/loop/utils"
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/lnwire"
Expand Down Expand Up @@ -281,6 +283,12 @@ func (p Parameters) validate(minConfs int32, openChans []lndclient.ChannelInfo,
return ErrAmbiguousDestAddr
}

// Autoloop must not sweep to a destination type that loop out
// rejects.
if p.DestAddr != nil && !utils.IsSupportedDestAddr(p.DestAddr) {
return fmt.Errorf("%w: %T", ErrUnsupportedDestAddr, p.DestAddr)
}

// If an account is specified the respective address type must be
// specified as well, or both must be unset.
if len(p.Account) == 0 !=
Expand Down Expand Up @@ -462,9 +470,10 @@ func loopInSourceToRPC(source LoopInSource) (clientrpc.LoopInSource, error) {
}

// RpcToParameters takes a `LiquidityParameters` and creates a `Parameters`
// from it.
func RpcToParameters(req *clientrpc.LiquidityParameters) (*Parameters,
error) {
// from it. It decodes the autoloop destination address for the network with
// the given chain parameters.
func RpcToParameters(req *clientrpc.LiquidityParameters,
chainParams *chaincfg.Params) (*Parameters, error) {

feeLimit, err := rpcToFee(req)
if err != nil {
Expand All @@ -477,11 +486,16 @@ func RpcToParameters(req *clientrpc.LiquidityParameters) (*Parameters,
destaddr = nil
} else {
destaddr, err = btcutil.DecodeAddress(
req.AutoloopDestAddress, nil,
req.AutoloopDestAddress, chainParams,
)
if err != nil {
return nil, err
}

err = utils.CheckAddrNetwork(destaddr, chainParams)
if err != nil {
return nil, err
}
}
}

Expand Down
Loading
Loading