2020-03-18 00:23:54 +01:00
|
|
|
package netann
|
|
|
|
|
|
|
|
import (
|
2020-06-02 02:06:08 +02:00
|
|
|
"bytes"
|
2024-09-11 12:15:44 +02:00
|
|
|
"errors"
|
2020-06-02 02:06:08 +02:00
|
|
|
|
2024-09-11 12:15:44 +02:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2023-11-08 10:18:45 +01:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb/models"
|
2020-03-18 00:23:54 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreateChanAnnouncement is a helper function which creates all channel
|
|
|
|
// announcements given the necessary channel related database items. This
|
|
|
|
// function is used to transform out database structs into the corresponding wire
|
|
|
|
// structs for announcing new channels to other peers, or simply syncing up a
|
|
|
|
// peer's initial routing table upon connect.
|
2023-11-08 10:18:45 +01:00
|
|
|
func CreateChanAnnouncement(chanProof *models.ChannelAuthProof,
|
|
|
|
chanInfo *models.ChannelEdgeInfo,
|
2024-08-21 08:37:50 +02:00
|
|
|
e1, e2 *models.ChannelEdgePolicy) (*lnwire.ChannelAnnouncement1,
|
2024-08-21 08:39:37 +02:00
|
|
|
*lnwire.ChannelUpdate1, *lnwire.ChannelUpdate1, error) {
|
2020-03-18 00:23:54 +01:00
|
|
|
|
|
|
|
// First, using the parameters of the channel, along with the channel
|
|
|
|
// authentication chanProof, we'll create re-create the original
|
|
|
|
// authenticated channel announcement.
|
|
|
|
chanID := lnwire.NewShortChanIDFromInt(chanInfo.ChannelID)
|
2024-08-21 08:37:50 +02:00
|
|
|
chanAnn := &lnwire.ChannelAnnouncement1{
|
2020-03-18 00:23:54 +01:00
|
|
|
ShortChannelID: chanID,
|
|
|
|
NodeID1: chanInfo.NodeKey1Bytes,
|
|
|
|
NodeID2: chanInfo.NodeKey2Bytes,
|
|
|
|
ChainHash: chanInfo.ChainHash,
|
|
|
|
BitcoinKey1: chanInfo.BitcoinKey1Bytes,
|
|
|
|
BitcoinKey2: chanInfo.BitcoinKey2Bytes,
|
|
|
|
Features: lnwire.NewRawFeatureVector(),
|
|
|
|
ExtraOpaqueData: chanInfo.ExtraOpaqueData,
|
|
|
|
}
|
|
|
|
|
2020-06-02 02:06:08 +02:00
|
|
|
err := chanAnn.Features.Decode(bytes.NewReader(chanInfo.Features))
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2023-01-17 04:33:21 +01:00
|
|
|
chanAnn.BitcoinSig1, err = lnwire.NewSigFromECDSARawSignature(
|
2020-03-18 00:23:54 +01:00
|
|
|
chanProof.BitcoinSig1Bytes,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2023-01-17 04:33:21 +01:00
|
|
|
chanAnn.BitcoinSig2, err = lnwire.NewSigFromECDSARawSignature(
|
2020-03-18 00:23:54 +01:00
|
|
|
chanProof.BitcoinSig2Bytes,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2023-01-17 04:33:21 +01:00
|
|
|
chanAnn.NodeSig1, err = lnwire.NewSigFromECDSARawSignature(
|
2020-03-18 00:23:54 +01:00
|
|
|
chanProof.NodeSig1Bytes,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2023-01-17 04:33:21 +01:00
|
|
|
chanAnn.NodeSig2, err = lnwire.NewSigFromECDSARawSignature(
|
2020-03-18 00:23:54 +01:00
|
|
|
chanProof.NodeSig2Bytes,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll unconditionally queue the channel's existence chanProof as it
|
|
|
|
// will need to be processed before either of the channel update
|
|
|
|
// networkMsgs.
|
|
|
|
|
|
|
|
// Since it's up to a node's policy as to whether they advertise the
|
|
|
|
// edge in a direction, we don't create an advertisement if the edge is
|
|
|
|
// nil.
|
2024-08-21 08:39:37 +02:00
|
|
|
var edge1Ann, edge2Ann *lnwire.ChannelUpdate1
|
2020-03-18 00:23:54 +01:00
|
|
|
if e1 != nil {
|
|
|
|
edge1Ann, err = ChannelUpdateFromEdge(chanInfo, e1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if e2 != nil {
|
|
|
|
edge2Ann, err = ChannelUpdateFromEdge(chanInfo, e2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return chanAnn, edge1Ann, edge2Ann, nil
|
|
|
|
}
|
2024-09-11 12:15:44 +02:00
|
|
|
|
|
|
|
// ValidateChannelAnn validates the channel announcement message and checks
|
|
|
|
// that node signatures covers the announcement message, and that the bitcoin
|
|
|
|
// signatures covers the node keys.
|
|
|
|
func ValidateChannelAnn(a *lnwire.ChannelAnnouncement1) error {
|
|
|
|
// 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
|
|
|
|
// digest includes all the keys, so the (up to 4 signatures) will
|
|
|
|
// attest to the validity of each of the keys.
|
|
|
|
data, err := a.DataToSign()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dataHash := chainhash.DoubleHashB(data)
|
|
|
|
|
|
|
|
// First we'll verify that the passed bitcoin key signature is indeed a
|
|
|
|
// signature over the computed hash digest.
|
|
|
|
bitcoinSig1, err := a.BitcoinSig1.ToSignature()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
bitcoinKey1, err := btcec.ParsePubKey(a.BitcoinKey1[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !bitcoinSig1.Verify(dataHash, bitcoinKey1) {
|
|
|
|
return errors.New("can't verify first bitcoin signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If that checks out, then we'll verify that the second bitcoin
|
|
|
|
// signature is a valid signature of the bitcoin public key over hash
|
|
|
|
// digest as well.
|
|
|
|
bitcoinSig2, err := a.BitcoinSig2.ToSignature()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
bitcoinKey2, err := btcec.ParsePubKey(a.BitcoinKey2[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !bitcoinSig2.Verify(dataHash, bitcoinKey2) {
|
|
|
|
return errors.New("can't verify second bitcoin signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both node signatures attached should indeed be a valid signature
|
|
|
|
// over the selected digest of the channel announcement signature.
|
|
|
|
nodeSig1, err := a.NodeSig1.ToSignature()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nodeKey1, err := btcec.ParsePubKey(a.NodeID1[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !nodeSig1.Verify(dataHash, nodeKey1) {
|
|
|
|
return errors.New("can't verify data in first node signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeSig2, err := a.NodeSig2.ToSignature()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nodeKey2, err := btcec.ParsePubKey(a.NodeID2[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !nodeSig2.Verify(dataHash, nodeKey2) {
|
|
|
|
return errors.New("can't verify data in second node signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|