discovery: publicly export announcement validation related functions
This commit is contained in:
parent
7bd2cd0a16
commit
3802cb90df
@ -1,6 +1,8 @@
|
|||||||
package discovery
|
package discovery
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
"github.com/go-errors/errors"
|
"github.com/go-errors/errors"
|
||||||
"github.com/lightningnetwork/lnd/lnwire"
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
@ -8,10 +10,10 @@ import (
|
|||||||
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
||||||
)
|
)
|
||||||
|
|
||||||
// validateChannelAnn validates the channel announcement message and checks
|
// ValidateChannelAnn validates the channel announcement message and checks
|
||||||
// that node signatures covers the announcement message, and that the bitcoin
|
// that node signatures covers the announcement message, and that the bitcoin
|
||||||
// signatures covers the node keys.
|
// signatures covers the node keys.
|
||||||
func (d *AuthenticatedGossiper) validateChannelAnn(a *lnwire.ChannelAnnouncement) error {
|
func ValidateChannelAnn(a *lnwire.ChannelAnnouncement) error {
|
||||||
// First, we'll compute the digest (h) which is to be signed by each of
|
// First, we'll compute the digest (h) which is to be signed by each of
|
||||||
// the keys included within the node announcement message. This hash
|
// the keys included within the node announcement message. This hash
|
||||||
// digest includes all the keys, so the (up to 4 signatures) will
|
// digest includes all the keys, so the (up to 4 signatures) will
|
||||||
@ -48,10 +50,10 @@ func (d *AuthenticatedGossiper) validateChannelAnn(a *lnwire.ChannelAnnouncement
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// validateNodeAnn validates the node announcement by ensuring that the
|
// ValidateNodeAnn validates the node announcement by ensuring that the
|
||||||
// attached signature is needed a signature of the node announcement under the
|
// attached signature is needed a signature of the node announcement under the
|
||||||
// specified node public key.
|
// specified node public key.
|
||||||
func (d *AuthenticatedGossiper) validateNodeAnn(a *lnwire.NodeAnnouncement) error {
|
func ValidateNodeAnn(a *lnwire.NodeAnnouncement) error {
|
||||||
// Reconstruct the data of announcement which should be covered by the
|
// Reconstruct the data of announcement which should be covered by the
|
||||||
// signature so we can verify the signature shortly below
|
// signature so we can verify the signature shortly below
|
||||||
data, err := a.DataToSign()
|
data, err := a.DataToSign()
|
||||||
@ -63,16 +65,23 @@ func (d *AuthenticatedGossiper) validateNodeAnn(a *lnwire.NodeAnnouncement) erro
|
|||||||
// return an error so this node announcement can be rejected.
|
// return an error so this node announcement can be rejected.
|
||||||
dataHash := chainhash.DoubleHashB(data)
|
dataHash := chainhash.DoubleHashB(data)
|
||||||
if !a.Signature.Verify(dataHash, copyPubKey(a.NodeID)) {
|
if !a.Signature.Verify(dataHash, copyPubKey(a.NodeID)) {
|
||||||
return errors.New("signature on node announcement is invalid")
|
var msgBuf bytes.Buffer
|
||||||
|
if _, err := lnwire.WriteMessage(&msgBuf, a, 0); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.Errorf("signature on NodeAnnouncement(%x) is "+
|
||||||
|
"invalid: %x", a.NodeID.SerializeCompressed(),
|
||||||
|
msgBuf.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// validateChannelUpdateAnn validates the channel update announcement by
|
// ValidateChannelUpdateAnn validates the channel update announcement by
|
||||||
// checking that the included signature covers he announcement and has been
|
// checking that the included signature covers he announcement and has been
|
||||||
// signed by the node's private key.
|
// signed by the node's private key.
|
||||||
func (d *AuthenticatedGossiper) validateChannelUpdateAnn(pubKey *btcec.PublicKey,
|
func ValidateChannelUpdateAnn(pubKey *btcec.PublicKey,
|
||||||
a *lnwire.ChannelUpdate) error {
|
a *lnwire.ChannelUpdate) error {
|
||||||
|
|
||||||
data, err := a.DataToSign()
|
data, err := a.DataToSign()
|
||||||
|
@ -861,7 +861,7 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(nMsg *networkMsg) []l
|
|||||||
// updating previously advertised information.
|
// updating previously advertised information.
|
||||||
case *lnwire.NodeAnnouncement:
|
case *lnwire.NodeAnnouncement:
|
||||||
if nMsg.isRemote {
|
if nMsg.isRemote {
|
||||||
if err := d.validateNodeAnn(msg); err != nil {
|
if err := ValidateNodeAnn(msg); err != nil {
|
||||||
err := errors.Errorf("unable to validate "+
|
err := errors.Errorf("unable to validate "+
|
||||||
"node announcement: %v", err)
|
"node announcement: %v", err)
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
@ -941,7 +941,7 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(nMsg *networkMsg) []l
|
|||||||
// formed.
|
// formed.
|
||||||
var proof *channeldb.ChannelAuthProof
|
var proof *channeldb.ChannelAuthProof
|
||||||
if nMsg.isRemote {
|
if nMsg.isRemote {
|
||||||
if err := d.validateChannelAnn(msg); err != nil {
|
if err := ValidateChannelAnn(msg); err != nil {
|
||||||
err := errors.Errorf("unable to validate "+
|
err := errors.Errorf("unable to validate "+
|
||||||
"announcement: %v", err)
|
"announcement: %v", err)
|
||||||
|
|
||||||
@ -1072,7 +1072,7 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(nMsg *networkMsg) []l
|
|||||||
// Validate the channel announcement with the expected public
|
// Validate the channel announcement with the expected public
|
||||||
// key, In the case of an invalid channel , we'll return an
|
// key, In the case of an invalid channel , we'll return an
|
||||||
// error to the caller and exit early.
|
// error to the caller and exit early.
|
||||||
if err := d.validateChannelUpdateAnn(pubKey, msg); err != nil {
|
if err := ValidateChannelUpdateAnn(pubKey, msg); err != nil {
|
||||||
rErr := errors.Errorf("unable to validate channel "+
|
rErr := errors.Errorf("unable to validate channel "+
|
||||||
"update announcement for short_chan_id=%v: %v",
|
"update announcement for short_chan_id=%v: %v",
|
||||||
spew.Sdump(msg.ShortChannelID), err)
|
spew.Sdump(msg.ShortChannelID), err)
|
||||||
@ -1268,7 +1268,7 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(nMsg *networkMsg) []l
|
|||||||
|
|
||||||
// With all the necessary components assembled validate the
|
// With all the necessary components assembled validate the
|
||||||
// full channel announcement proof.
|
// full channel announcement proof.
|
||||||
if err := d.validateChannelAnn(chanAnn); err != nil {
|
if err := ValidateChannelAnn(chanAnn); err != nil {
|
||||||
err := errors.Errorf("channel announcement proof "+
|
err := errors.Errorf("channel announcement proof "+
|
||||||
"for short_chan_id=%v isn't valid: %v",
|
"for short_chan_id=%v isn't valid: %v",
|
||||||
shortChanID, err)
|
shortChanID, err)
|
||||||
@ -1377,7 +1377,7 @@ func (d *AuthenticatedGossiper) updateChannel(info *channeldb.ChannelEdgeInfo,
|
|||||||
|
|
||||||
// To ensure that our signature is valid, we'll verify it ourself
|
// To ensure that our signature is valid, we'll verify it ourself
|
||||||
// before committing it to the slice returned.
|
// before committing it to the slice returned.
|
||||||
err = d.validateChannelUpdateAnn(d.selfKey, chanUpdate)
|
err = ValidateChannelUpdateAnn(d.selfKey, chanUpdate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("generated invalid channel "+
|
return nil, nil, fmt.Errorf("generated invalid channel "+
|
||||||
"update sig: %v", err)
|
"update sig: %v", err)
|
||||||
|
Loading…
Reference in New Issue
Block a user