2017-03-28 22:08:14 +03:00
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
2018-07-31 10:17:17 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2017-03-27 20:25:44 +03:00
|
|
|
"github.com/go-errors/errors"
|
2017-03-28 22:08:14 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2017-03-27 20:25:44 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2017-03-28 22:08:14 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
)
|
|
|
|
|
2018-04-17 05:09:11 +03:00
|
|
|
// CreateChanAnnouncement is a helper function which creates all channel
|
2017-04-01 15:33:17 +03:00
|
|
|
// announcements given the necessary channel related database items. This
|
2018-02-07 06:11:11 +03:00
|
|
|
// 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
|
2017-04-01 15:33:17 +03:00
|
|
|
// peer's initial routing table upon connect.
|
2018-04-17 05:09:11 +03:00
|
|
|
func CreateChanAnnouncement(chanProof *channeldb.ChannelAuthProof,
|
2017-03-28 22:08:14 +03:00
|
|
|
chanInfo *channeldb.ChannelEdgeInfo,
|
2017-04-01 15:33:17 +03:00
|
|
|
e1, e2 *channeldb.ChannelEdgePolicy) (*lnwire.ChannelAnnouncement,
|
2018-01-31 07:23:14 +03:00
|
|
|
*lnwire.ChannelUpdate, *lnwire.ChannelUpdate, error) {
|
2017-04-01 15:33:17 +03:00
|
|
|
|
|
|
|
// First, using the parameters of the channel, along with the channel
|
|
|
|
// authentication chanProof, we'll create re-create the original
|
|
|
|
// authenticated channel announcement.
|
2017-03-28 22:08:14 +03:00
|
|
|
chanID := lnwire.NewShortChanIDFromInt(chanInfo.ChannelID)
|
|
|
|
chanAnn := &lnwire.ChannelAnnouncement{
|
2018-09-01 06:13:14 +03:00
|
|
|
ShortChannelID: chanID,
|
|
|
|
NodeID1: chanInfo.NodeKey1Bytes,
|
|
|
|
NodeID2: chanInfo.NodeKey2Bytes,
|
|
|
|
ChainHash: chanInfo.ChainHash,
|
|
|
|
BitcoinKey1: chanInfo.BitcoinKey1Bytes,
|
|
|
|
BitcoinKey2: chanInfo.BitcoinKey2Bytes,
|
|
|
|
Features: lnwire.NewRawFeatureVector(),
|
|
|
|
ExtraOpaqueData: chanInfo.ExtraOpaqueData,
|
2018-01-31 07:23:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
chanAnn.BitcoinSig1, err = lnwire.NewSigFromRawSignature(
|
|
|
|
chanProof.BitcoinSig1Bytes,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
chanAnn.BitcoinSig2, err = lnwire.NewSigFromRawSignature(
|
|
|
|
chanProof.BitcoinSig2Bytes,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
chanAnn.NodeSig1, err = lnwire.NewSigFromRawSignature(
|
|
|
|
chanProof.NodeSig1Bytes,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
chanAnn.NodeSig2, err = lnwire.NewSigFromRawSignature(
|
|
|
|
chanProof.NodeSig2Bytes,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
2017-03-28 22:08:14 +03:00
|
|
|
}
|
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// We'll unconditionally queue the channel's existence chanProof as it
|
|
|
|
// will need to be processed before either of the channel update
|
|
|
|
// networkMsgs.
|
2017-03-28 22:08:14 +03:00
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// Since it's up to a node's policy as to whether they advertise the
|
2017-12-01 09:32:53 +03:00
|
|
|
// edge in a direction, we don't create an advertisement if the edge is
|
|
|
|
// nil.
|
2017-04-20 02:20:46 +03:00
|
|
|
var edge1Ann, edge2Ann *lnwire.ChannelUpdate
|
2017-03-28 22:08:14 +03:00
|
|
|
if e1 != nil {
|
2017-04-20 02:20:46 +03:00
|
|
|
edge1Ann = &lnwire.ChannelUpdate{
|
2017-08-22 09:16:48 +03:00
|
|
|
ChainHash: chanInfo.ChainHash,
|
2017-06-16 23:48:07 +03:00
|
|
|
ShortChannelID: chanID,
|
|
|
|
Timestamp: uint32(e1.LastUpdate.Unix()),
|
2019-01-12 20:59:43 +03:00
|
|
|
MessageFlags: e1.MessageFlags,
|
|
|
|
ChannelFlags: e1.ChannelFlags,
|
2017-06-16 23:48:07 +03:00
|
|
|
TimeLockDelta: e1.TimeLockDelta,
|
2017-08-22 09:16:48 +03:00
|
|
|
HtlcMinimumMsat: e1.MinHTLC,
|
2019-01-12 20:59:45 +03:00
|
|
|
HtlcMaximumMsat: e1.MaxHTLC,
|
2017-06-16 23:48:07 +03:00
|
|
|
BaseFee: uint32(e1.FeeBaseMSat),
|
|
|
|
FeeRate: uint32(e1.FeeProportionalMillionths),
|
2018-09-01 06:13:14 +03:00
|
|
|
ExtraOpaqueData: e1.ExtraOpaqueData,
|
2017-03-28 22:08:14 +03:00
|
|
|
}
|
2018-01-31 07:23:14 +03:00
|
|
|
edge1Ann.Signature, err = lnwire.NewSigFromRawSignature(e1.SigBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2017-03-28 22:08:14 +03:00
|
|
|
}
|
|
|
|
if e2 != nil {
|
2017-04-20 02:20:46 +03:00
|
|
|
edge2Ann = &lnwire.ChannelUpdate{
|
2017-08-22 09:16:48 +03:00
|
|
|
ChainHash: chanInfo.ChainHash,
|
2017-06-16 23:48:07 +03:00
|
|
|
ShortChannelID: chanID,
|
|
|
|
Timestamp: uint32(e2.LastUpdate.Unix()),
|
2019-01-12 20:59:43 +03:00
|
|
|
MessageFlags: e2.MessageFlags,
|
|
|
|
ChannelFlags: e2.ChannelFlags,
|
2017-06-16 23:48:07 +03:00
|
|
|
TimeLockDelta: e2.TimeLockDelta,
|
2017-08-22 09:16:48 +03:00
|
|
|
HtlcMinimumMsat: e2.MinHTLC,
|
2019-01-12 20:59:45 +03:00
|
|
|
HtlcMaximumMsat: e2.MaxHTLC,
|
2017-06-16 23:48:07 +03:00
|
|
|
BaseFee: uint32(e2.FeeBaseMSat),
|
|
|
|
FeeRate: uint32(e2.FeeProportionalMillionths),
|
2018-09-01 06:13:14 +03:00
|
|
|
ExtraOpaqueData: e2.ExtraOpaqueData,
|
2017-03-28 22:08:14 +03:00
|
|
|
}
|
2018-01-31 07:23:14 +03:00
|
|
|
edge2Ann.Signature, err = lnwire.NewSigFromRawSignature(e2.SigBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2017-03-28 22:08:14 +03:00
|
|
|
}
|
|
|
|
|
2018-01-31 07:23:14 +03:00
|
|
|
return chanAnn, edge1Ann, edge2Ann, nil
|
2017-03-28 22:08:14 +03:00
|
|
|
}
|
2017-03-27 20:25:44 +03:00
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// copyPubKey performs a copy of the target public key, setting a fresh curve
|
|
|
|
// parameter during the process.
|
2017-03-27 20:25:44 +03:00
|
|
|
func copyPubKey(pub *btcec.PublicKey) *btcec.PublicKey {
|
|
|
|
return &btcec.PublicKey{
|
|
|
|
Curve: btcec.S256(),
|
|
|
|
X: pub.X,
|
|
|
|
Y: pub.Y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// SignAnnouncement is a helper function which is used to sign any outgoing
|
|
|
|
// channel node node announcement messages.
|
2017-04-14 21:08:07 +03:00
|
|
|
func SignAnnouncement(signer lnwallet.MessageSigner, pubKey *btcec.PublicKey,
|
2017-03-27 20:25:44 +03:00
|
|
|
msg lnwire.Message) (*btcec.Signature, error) {
|
2017-04-01 15:33:17 +03:00
|
|
|
|
|
|
|
var (
|
|
|
|
data []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2017-03-27 20:25:44 +03:00
|
|
|
switch m := msg.(type) {
|
|
|
|
case *lnwire.ChannelAnnouncement:
|
|
|
|
data, err = m.DataToSign()
|
2017-04-20 02:20:46 +03:00
|
|
|
case *lnwire.ChannelUpdate:
|
2017-03-27 20:25:44 +03:00
|
|
|
data, err = m.DataToSign()
|
|
|
|
case *lnwire.NodeAnnouncement:
|
|
|
|
data, err = m.DataToSign()
|
|
|
|
default:
|
|
|
|
return nil, errors.New("can't sign message " +
|
|
|
|
"of this format")
|
|
|
|
}
|
|
|
|
if err != nil {
|
2017-04-01 15:33:17 +03:00
|
|
|
return nil, errors.Errorf("unable to get data to sign: %v", err)
|
2017-03-27 20:25:44 +03:00
|
|
|
}
|
|
|
|
|
2017-04-14 21:08:07 +03:00
|
|
|
return signer.SignMessage(pubKey, data)
|
2017-03-27 20:25:44 +03:00
|
|
|
}
|
2019-04-10 03:44:45 +03:00
|
|
|
|
|
|
|
// remotePubFromChanInfo returns the public key of the remote peer given a
|
|
|
|
// ChannelEdgeInfo that describe a channel we have with them.
|
|
|
|
func remotePubFromChanInfo(chanInfo *channeldb.ChannelEdgeInfo,
|
|
|
|
chanFlags lnwire.ChanUpdateChanFlags) [33]byte {
|
|
|
|
|
|
|
|
var remotePubKey [33]byte
|
|
|
|
switch {
|
|
|
|
case chanFlags&lnwire.ChanUpdateDirection == 0:
|
|
|
|
remotePubKey = chanInfo.NodeKey2Bytes
|
|
|
|
case chanFlags&lnwire.ChanUpdateDirection == 1:
|
|
|
|
remotePubKey = chanInfo.NodeKey1Bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
return remotePubKey
|
|
|
|
}
|