From 303d441d4d7cd5b256070f18c81ac4bd3513df81 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Mon, 30 Mar 2020 16:56:12 -0700 Subject: [PATCH] rpcserver: ensure OpenChannel+OpenChannelSync parse NodePubkey --- rpcserver.go | 52 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/rpcserver.go b/rpcserver.go index 01faa0d33..f9753bbbe 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -1666,13 +1666,10 @@ func (r *rpcServer) OpenChannel(in *lnrpc.OpenChannelRequest, return err } - var ( - nodePubKey *btcec.PublicKey - nodePubKeyBytes []byte - ) - // TODO(roasbeef): also return channel ID? + var nodePubKey *btcec.PublicKey + // Ensure that the NodePubKey is set before attempting to use it if len(in.NodePubkey) == 0 { return fmt.Errorf("NodePubKey is not set") @@ -1691,8 +1688,6 @@ func (r *rpcServer) OpenChannel(in *lnrpc.OpenChannelRequest, return fmt.Errorf("cannot open channel to self") } - nodePubKeyBytes = nodePubKey.SerializeCompressed() - // Based on the passed fee related parameters, we'll determine an // appropriate fee rate for the funding transaction. satPerKw := chainfee.SatPerKVByte(in.SatPerByte * 1000).FeePerKWeight() @@ -1780,7 +1775,7 @@ out: select { case err := <-errChan: rpcsLog.Errorf("unable to open channel to NodeKey(%x): %v", - nodePubKeyBytes, err) + nodePubKey.SerializeCompressed(), err) return err case fundingUpdate := <-updateChan: rpcsLog.Tracef("[openchannel] sending update: %v", @@ -1812,7 +1807,7 @@ out: } rpcsLog.Tracef("[openchannel] success NodeKey(%x), ChannelPoint(%v)", - nodePubKeyBytes, outpoint) + nodePubKey.SerializeCompressed(), outpoint) return nil } @@ -1845,16 +1840,31 @@ func (r *rpcServer) OpenChannelSync(ctx context.Context, "wallet is fully synced") } - // Decode the provided target node's public key, parsing it into a pub - // key object. For all sync call, byte slices are expected to be - // encoded as hex strings. - keyBytes, err := hex.DecodeString(in.NodePubkeyString) - if err != nil { - return nil, err - } - nodepubKey, err := btcec.ParsePubKey(keyBytes, btcec.S256()) - if err != nil { - return nil, err + var nodePubKey *btcec.PublicKey + + // Parse the remote pubkey the NodePubkey field of the request. If it's + // not present, we'll fallback to the deprecated version that parses the + // key from a hex string. + if len(in.NodePubkey) > 0 { + // Parse the raw bytes of the node key into a pubkey object so we + // can easily manipulate it. + nodePubKey, err = btcec.ParsePubKey(in.NodePubkey, btcec.S256()) + if err != nil { + return nil, err + } + } else { + // Decode the provided target node's public key, parsing it into + // a pub key object. For all sync call, byte slices are expected + // to be encoded as hex strings. + keyBytes, err := hex.DecodeString(in.NodePubkeyString) + if err != nil { + return nil, err + } + + nodePubKey, err = btcec.ParsePubKey(keyBytes, btcec.S256()) + if err != nil { + return nil, err + } } localFundingAmt := btcutil.Amount(in.LocalFundingAmount) @@ -1916,7 +1926,7 @@ func (r *rpcServer) OpenChannelSync(ctx context.Context, } req := &openChanReq{ - targetPubkey: nodepubKey, + targetPubkey: nodePubKey, chainHash: *activeNetParams.GenesisHash, localFundingAmt: localFundingAmt, pushAmt: lnwire.NewMSatFromSatoshis(remoteInitialBalance), @@ -1933,7 +1943,7 @@ func (r *rpcServer) OpenChannelSync(ctx context.Context, // If an error occurs them immediately return the error to the client. case err := <-errChan: rpcsLog.Errorf("unable to open channel to NodeKey(%x): %v", - nodepubKey, err) + nodePubKey.SerializeCompressed(), err) return nil, err // Otherwise, wait for the first channel update. The first update sent