From ce596468e92cbdd76f0596ce8fe110dbde67313a Mon Sep 17 00:00:00 2001 From: sputn1ck Date: Thu, 19 May 2022 19:47:26 +0200 Subject: [PATCH] loopout: reject unsupported address formats This commit adds a check to reject unsupported address formats such as P2PK or P2TR addresses --- loopd/swapclient_server.go | 18 ++++++++++++++++++ loopd/swapclient_server_test.go | 11 +++++++++++ 2 files changed, 29 insertions(+) diff --git a/loopd/swapclient_server.go b/loopd/swapclient_server.go index 791506b..4f30f24 100644 --- a/loopd/swapclient_server.go +++ b/loopd/swapclient_server.go @@ -55,6 +55,12 @@ var ( errBalanceTooLow = errors.New( "channel balance too low for loop out amount", ) + + // errInvalidAddress is returned when the destination address is of + // an unsupported format such as P2PK or P2TR addresses. + errInvalidAddress = errors.New( + "invalid or unsupported address", + ) ) // swapClientServer implements the grpc service exposed by loopd. @@ -1154,6 +1160,18 @@ func validateLoopOutRequest(ctx context.Context, lnd lndclient.LightningClient, errIncorrectChain, chainParams.Name) } + // Check that the provided destination address is a supported + // address format. + switch sweepAddr.(type) { + case *btcutil.AddressWitnessScriptHash, + *btcutil.AddressWitnessPubKeyHash, + *btcutil.AddressScriptHash, + *btcutil.AddressPubKeyHash: + + default: + return 0, errInvalidAddress + } + // Check that the label is valid. if err := labels.Validate(req.Label); err != nil { return 0, err diff --git a/loopd/swapclient_server_test.go b/loopd/swapclient_server_test.go index 138237a..53846ba 100644 --- a/loopd/swapclient_server_test.go +++ b/loopd/swapclient_server_test.go @@ -26,6 +26,10 @@ var ( []byte{123}, &chaincfg.MainNetParams, ) + nodepubkeyAddr, _ = btcutil.DecodeAddress( + mock_lnd.NewMockLnd().NodePubkey, &chaincfg.MainNetParams, + ) + chanID1 = lnwire.NewShortChanIDFromInt(1) chanID2 = lnwire.NewShortChanIDFromInt(2) chanID3 = lnwire.NewShortChanIDFromInt(3) @@ -445,6 +449,13 @@ func TestValidateLoopOutRequest(t *testing.T) { err: errBalanceTooLow, expectedTarget: 0, }, + { + name: "node pubkey as dest addr", + chain: chaincfg.MainNetParams, + destAddr: nodepubkeyAddr, + err: errInvalidAddress, + expectedTarget: 0, + }, } for _, test := range tests {