rpc: disallow connections to self, channel open attempt to self

Fixes #193.
This commit is contained in:
Olaoluwa Osuntokun 2017-04-21 13:32:17 -07:00
parent 4fd285236e
commit d188d97fce
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2

@ -200,11 +200,16 @@ func (r *rpcServer) ConnectPeer(ctx context.Context,
if err != nil { if err != nil {
return nil, err return nil, err
} }
pubkey, err := btcec.ParsePubKey(pubkeyHex, btcec.S256()) pubKey, err := btcec.ParsePubKey(pubkeyHex, btcec.S256())
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Connections to ourselves are disallowed for obvious reasons.
if pubKey.IsEqual(r.server.identityPriv.PubKey()) {
return nil, fmt.Errorf("cannot make connection to self")
}
// If the address doesn't already have a port, we'll assume the current // If the address doesn't already have a port, we'll assume the current
// default port. // default port.
var addr string var addr string
@ -221,7 +226,7 @@ func (r *rpcServer) ConnectPeer(ctx context.Context,
} }
peerAddr := &lnwire.NetAddress{ peerAddr := &lnwire.NetAddress{
IdentityKey: pubkey, IdentityKey: pubKey,
Address: host, Address: host,
ChainNet: activeNetParams.Net, ChainNet: activeNetParams.Net,
} }
@ -269,8 +274,8 @@ func (r *rpcServer) OpenChannel(in *lnrpc.OpenChannelRequest,
} }
var ( var (
nodepubKey *btcec.PublicKey nodePubKey *btcec.PublicKey
nodepubKeyBytes []byte nodePubKeyBytes []byte
err error err error
) )
@ -280,18 +285,25 @@ func (r *rpcServer) OpenChannel(in *lnrpc.OpenChannelRequest,
// object so we can easily manipulate it. If this isn't set, then we // object so we can easily manipulate it. If this isn't set, then we
// expected the TargetPeerId to be set accordingly. // expected the TargetPeerId to be set accordingly.
if len(in.NodePubkey) != 0 { if len(in.NodePubkey) != 0 {
nodepubKey, err = btcec.ParsePubKey(in.NodePubkey, btcec.S256()) nodePubKey, err = btcec.ParsePubKey(in.NodePubkey, btcec.S256())
if err != nil { if err != nil {
return err return err
} }
nodepubKeyBytes = nodepubKey.SerializeCompressed()
// Making a channel to ourselves wouldn't be of any use, so we
// explicitly disallow them.
if nodePubKey.IsEqual(r.server.identityPriv.PubKey()) {
return fmt.Errorf("cannot open channel to self")
}
nodePubKeyBytes = nodePubKey.SerializeCompressed()
} }
// Instruct the server to trigger the necessary events to attempt to // Instruct the server to trigger the necessary events to attempt to
// open a new channel. A stream is returned in place, this stream will // open a new channel. A stream is returned in place, this stream will
// be used to consume updates of the state of the pending channel. // be used to consume updates of the state of the pending channel.
updateChan, errChan := r.server.OpenChannel(in.TargetPeerId, updateChan, errChan := r.server.OpenChannel(in.TargetPeerId,
nodepubKey, localFundingAmt, remoteInitialBalance, in.NumConfs) nodePubKey, localFundingAmt, remoteInitialBalance, in.NumConfs)
var outpoint wire.OutPoint var outpoint wire.OutPoint
out: out:
@ -300,7 +312,7 @@ out:
case err := <-errChan: case err := <-errChan:
rpcsLog.Errorf("unable to open channel to "+ rpcsLog.Errorf("unable to open channel to "+
"identityPub(%x) nor peerID(%v): %v", "identityPub(%x) nor peerID(%v): %v",
nodepubKeyBytes, in.TargetPeerId, err) nodePubKeyBytes, in.TargetPeerId, err)
return err return err
case fundingUpdate := <-updateChan: case fundingUpdate := <-updateChan:
rpcsLog.Tracef("[openchannel] sending update: %v", rpcsLog.Tracef("[openchannel] sending update: %v",