From c46457fb5b92075cbeead327731db7548c39c1ca Mon Sep 17 00:00:00 2001 From: ccdle12 Date: Wed, 10 Apr 2019 10:45:55 +0800 Subject: [PATCH] rpcserver+lnd_test: adding check in SendCoins to prevent txs sent to pubkeys --- lnd_test.go | 32 +++++++++++++++++++++++++++++++- rpcserver.go | 9 +++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/lnd_test.go b/lnd_test.go index eed2bc56..0fe7a83d 100644 --- a/lnd_test.go +++ b/lnd_test.go @@ -13044,6 +13044,36 @@ func testSweepAllCoins(net *lntest.NetworkHarness, t *harnessTest) { t.Fatalf("unable to send coins to eve: %v", err) } + // Ensure that we can't send coins to our own Pubkey. + info, err := ainz.GetInfo(ctxt, &lnrpc.GetInfoRequest{}) + if err != nil { + t.Fatalf("unable to get node info: %v", err) + } + + sweepReq := &lnrpc.SendCoinsRequest{ + Addr: info.IdentityPubkey, + SendAll: true, + } + _, err = ainz.SendCoins(ctxt, sweepReq) + if err == nil { + t.Fatalf("expected SendCoins to users own pubkey to fail") + } + + // Ensure that we can't send coins to another users Pubkey. + info, err = net.Alice.GetInfo(ctxt, &lnrpc.GetInfoRequest{}) + if err != nil { + t.Fatalf("unable to get node info: %v", err) + } + + sweepReq = &lnrpc.SendCoinsRequest{ + Addr: info.IdentityPubkey, + SendAll: true, + } + _, err = ainz.SendCoins(ctxt, sweepReq) + if err == nil { + t.Fatalf("expected SendCoins to Alices pubkey to fail") + } + // With the two coins above mined, we'll now instruct ainz to sweep all // the coins to an external address not under its control. // We will first attempt to send the coins to addresses that are not @@ -13053,7 +13083,7 @@ func testSweepAllCoins(net *lntest.NetworkHarness, t *harnessTest) { // Send coins to a testnet3 address. ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) - sweepReq := &lnrpc.SendCoinsRequest{ + sweepReq = &lnrpc.SendCoinsRequest{ Addr: "tb1qfc8fusa98jx8uvnhzavxccqlzvg749tvjw82tg", SendAll: true, } diff --git a/rpcserver.go b/rpcserver.go index cba4ea83..1c8e2fb7 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -913,6 +913,15 @@ func (r *rpcServer) SendCoins(ctx context.Context, activeNetParams.Params.Name) } + // If the destination address parses to a valid pubkey, we assume the user + // accidently tried to send funds to a bare pubkey address. This check is + // here to prevent unintended transfers. + decodedAddr, _ := hex.DecodeString(in.Addr) + _, err = btcec.ParsePubKey(decodedAddr, btcec.S256()) + if err == nil { + return nil, fmt.Errorf("cannot send coins to pubkeys") + } + var txid *chainhash.Hash wallet := r.server.cc.wallet