2017-03-28 21:08:14 +02:00
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
2018-07-31 09:17:17 +02:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2017-03-27 19:25:44 +02:00
|
|
|
"github.com/go-errors/errors"
|
2017-03-28 21:08:14 +02:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2017-03-27 19:25:44 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2017-03-28 21:08:14 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
)
|
|
|
|
|
2018-04-17 04:09:11 +02:00
|
|
|
// CreateChanAnnouncement is a helper function which creates all channel
|
2017-04-01 14:33:17 +02:00
|
|
|
// announcements given the necessary channel related database items. This
|
2018-02-07 04:11:11 +01: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 14:33:17 +02:00
|
|
|
// peer's initial routing table upon connect.
|
2018-04-17 04:09:11 +02:00
|
|
|
func CreateChanAnnouncement(chanProof *channeldb.ChannelAuthProof,
|
2017-03-28 21:08:14 +02:00
|
|
|
chanInfo *channeldb.ChannelEdgeInfo,
|
2017-04-01 14:33:17 +02:00
|
|
|
e1, e2 *channeldb.ChannelEdgePolicy) (*lnwire.ChannelAnnouncement,
|
2018-01-31 05:23:14 +01:00
|
|
|
*lnwire.ChannelUpdate, *lnwire.ChannelUpdate, error) {
|
2017-04-01 14:33:17 +02: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 21:08:14 +02:00
|
|
|
chanID := lnwire.NewShortChanIDFromInt(chanInfo.ChannelID)
|
|
|
|
chanAnn := &lnwire.ChannelAnnouncement{
|
2018-09-01 05:13:14 +02: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 05:23:14 +01: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 21:08:14 +02:00
|
|
|
}
|
|
|
|
|
2017-04-01 14:33:17 +02: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 21:08:14 +02:00
|
|
|
|
2017-04-01 14:33:17 +02:00
|
|
|
// Since it's up to a node's policy as to whether they advertise the
|
2017-12-01 07:32:53 +01:00
|
|
|
// edge in a direction, we don't create an advertisement if the edge is
|
|
|
|
// nil.
|
2017-04-20 01:20:46 +02:00
|
|
|
var edge1Ann, edge2Ann *lnwire.ChannelUpdate
|
2017-03-28 21:08:14 +02:00
|
|
|
if e1 != nil {
|
2017-04-20 01:20:46 +02:00
|
|
|
edge1Ann = &lnwire.ChannelUpdate{
|
2017-08-22 08:16:48 +02:00
|
|
|
ChainHash: chanInfo.ChainHash,
|
2017-06-16 22:48:07 +02:00
|
|
|
ShortChannelID: chanID,
|
|
|
|
Timestamp: uint32(e1.LastUpdate.Unix()),
|
2017-12-01 07:32:53 +01:00
|
|
|
Flags: e1.Flags,
|
2017-06-16 22:48:07 +02:00
|
|
|
TimeLockDelta: e1.TimeLockDelta,
|
2017-08-22 08:16:48 +02:00
|
|
|
HtlcMinimumMsat: e1.MinHTLC,
|
2017-06-16 22:48:07 +02:00
|
|
|
BaseFee: uint32(e1.FeeBaseMSat),
|
|
|
|
FeeRate: uint32(e1.FeeProportionalMillionths),
|
2018-09-01 05:13:14 +02:00
|
|
|
ExtraOpaqueData: e1.ExtraOpaqueData,
|
2017-03-28 21:08:14 +02:00
|
|
|
}
|
2018-01-31 05:23:14 +01:00
|
|
|
edge1Ann.Signature, err = lnwire.NewSigFromRawSignature(e1.SigBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2017-03-28 21:08:14 +02:00
|
|
|
}
|
|
|
|
if e2 != nil {
|
2017-04-20 01:20:46 +02:00
|
|
|
edge2Ann = &lnwire.ChannelUpdate{
|
2017-08-22 08:16:48 +02:00
|
|
|
ChainHash: chanInfo.ChainHash,
|
2017-06-16 22:48:07 +02:00
|
|
|
ShortChannelID: chanID,
|
|
|
|
Timestamp: uint32(e2.LastUpdate.Unix()),
|
2017-12-01 07:32:53 +01:00
|
|
|
Flags: e2.Flags,
|
2017-06-16 22:48:07 +02:00
|
|
|
TimeLockDelta: e2.TimeLockDelta,
|
2017-08-22 08:16:48 +02:00
|
|
|
HtlcMinimumMsat: e2.MinHTLC,
|
2017-06-16 22:48:07 +02:00
|
|
|
BaseFee: uint32(e2.FeeBaseMSat),
|
|
|
|
FeeRate: uint32(e2.FeeProportionalMillionths),
|
2018-09-01 05:13:14 +02:00
|
|
|
ExtraOpaqueData: e2.ExtraOpaqueData,
|
2017-03-28 21:08:14 +02:00
|
|
|
}
|
2018-01-31 05:23:14 +01:00
|
|
|
edge2Ann.Signature, err = lnwire.NewSigFromRawSignature(e2.SigBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2017-03-28 21:08:14 +02:00
|
|
|
}
|
|
|
|
|
2018-01-31 05:23:14 +01:00
|
|
|
return chanAnn, edge1Ann, edge2Ann, nil
|
2017-03-28 21:08:14 +02:00
|
|
|
}
|
2017-03-27 19:25:44 +02:00
|
|
|
|
2017-04-01 14:33:17 +02:00
|
|
|
// copyPubKey performs a copy of the target public key, setting a fresh curve
|
|
|
|
// parameter during the process.
|
2017-03-27 19:25:44 +02:00
|
|
|
func copyPubKey(pub *btcec.PublicKey) *btcec.PublicKey {
|
|
|
|
return &btcec.PublicKey{
|
|
|
|
Curve: btcec.S256(),
|
|
|
|
X: pub.X,
|
|
|
|
Y: pub.Y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 14:33:17 +02:00
|
|
|
// SignAnnouncement is a helper function which is used to sign any outgoing
|
|
|
|
// channel node node announcement messages.
|
2017-04-14 20:08:07 +02:00
|
|
|
func SignAnnouncement(signer lnwallet.MessageSigner, pubKey *btcec.PublicKey,
|
2017-03-27 19:25:44 +02:00
|
|
|
msg lnwire.Message) (*btcec.Signature, error) {
|
2017-04-01 14:33:17 +02:00
|
|
|
|
|
|
|
var (
|
|
|
|
data []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2017-03-27 19:25:44 +02:00
|
|
|
switch m := msg.(type) {
|
|
|
|
case *lnwire.ChannelAnnouncement:
|
|
|
|
data, err = m.DataToSign()
|
2017-04-20 01:20:46 +02:00
|
|
|
case *lnwire.ChannelUpdate:
|
2017-03-27 19:25:44 +02: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 14:33:17 +02:00
|
|
|
return nil, errors.Errorf("unable to get data to sign: %v", err)
|
2017-03-27 19:25:44 +02:00
|
|
|
}
|
|
|
|
|
2017-04-14 20:08:07 +02:00
|
|
|
return signer.SignMessage(pubKey, data)
|
2017-03-27 19:25:44 +02:00
|
|
|
}
|