2024-06-16 19:30:01 -04:00
|
|
|
package graph
|
2017-03-27 20:25:44 +03:00
|
|
|
|
|
|
|
import (
|
2017-12-01 19:24:33 -08:00
|
|
|
"bytes"
|
|
|
|
|
2022-02-23 14:48:00 +01:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
2018-07-31 00:17:17 -07:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2017-03-27 20:25:44 +03:00
|
|
|
"github.com/go-errors/errors"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
)
|
|
|
|
|
2017-12-01 19:24:33 -08:00
|
|
|
// ValidateNodeAnn validates the node announcement by ensuring that the
|
2017-04-01 14:33:17 +02:00
|
|
|
// attached signature is needed a signature of the node announcement under the
|
|
|
|
// specified node public key.
|
2017-12-01 19:24:33 -08:00
|
|
|
func ValidateNodeAnn(a *lnwire.NodeAnnouncement) error {
|
2017-04-01 14:33:17 +02:00
|
|
|
// Reconstruct the data of announcement which should be covered by the
|
|
|
|
// signature so we can verify the signature shortly below
|
2017-03-27 20:25:44 +03:00
|
|
|
data, err := a.DataToSign()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-30 20:23:14 -08:00
|
|
|
nodeSig, err := a.Signature.ToSignature()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-23 14:48:00 +01:00
|
|
|
nodeKey, err := btcec.ParsePubKey(a.NodeID[:])
|
2018-01-30 20:23:14 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-01 14:33:17 +02:00
|
|
|
// Finally ensure that the passed signature is valid, if not we'll
|
|
|
|
// return an error so this node announcement can be rejected.
|
2017-03-27 20:25:44 +03:00
|
|
|
dataHash := chainhash.DoubleHashB(data)
|
2018-01-30 20:23:14 -08:00
|
|
|
if !nodeSig.Verify(dataHash, nodeKey) {
|
2017-12-01 19:24:33 -08:00
|
|
|
var msgBuf bytes.Buffer
|
|
|
|
if _, err := lnwire.WriteMessage(&msgBuf, a, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Errorf("signature on NodeAnnouncement(%x) is "+
|
2018-01-30 20:23:14 -08:00
|
|
|
"invalid: %x", nodeKey.SerializeCompressed(),
|
2017-12-01 19:24:33 -08:00
|
|
|
msgBuf.Bytes())
|
2017-03-27 20:25:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|