node_announcement.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package netann
  2. import (
  3. "image/color"
  4. "net"
  5. "time"
  6. "github.com/lightningnetwork/lnd/keychain"
  7. "github.com/lightningnetwork/lnd/lnwallet"
  8. "github.com/lightningnetwork/lnd/lnwire"
  9. )
  10. // NodeAnnModifier is a closure that makes in-place modifications to an
  11. // lnwire.NodeAnnouncement.
  12. type NodeAnnModifier func(*lnwire.NodeAnnouncement)
  13. // NodeAnnSetAlias is a functional option that sets the alias of the
  14. // given node announcement.
  15. func NodeAnnSetAlias(alias lnwire.NodeAlias) func(*lnwire.NodeAnnouncement) {
  16. return func(nodeAnn *lnwire.NodeAnnouncement) {
  17. nodeAnn.Alias = alias
  18. }
  19. }
  20. // NodeAnnSetAddrs is a functional option that allows updating the addresses of
  21. // the given node announcement.
  22. func NodeAnnSetAddrs(addrs []net.Addr) func(*lnwire.NodeAnnouncement) {
  23. return func(nodeAnn *lnwire.NodeAnnouncement) {
  24. nodeAnn.Addresses = addrs
  25. }
  26. }
  27. // NodeAnnSetColor is a functional option that sets the color of the
  28. // given node announcement.
  29. func NodeAnnSetColor(newColor color.RGBA) func(*lnwire.NodeAnnouncement) {
  30. return func(nodeAnn *lnwire.NodeAnnouncement) {
  31. nodeAnn.RGBColor = newColor
  32. }
  33. }
  34. // NodeAnnSetFeatures is a functional option that allows updating the features of
  35. // the given node announcement.
  36. func NodeAnnSetFeatures(features *lnwire.RawFeatureVector) func(*lnwire.NodeAnnouncement) {
  37. return func(nodeAnn *lnwire.NodeAnnouncement) {
  38. nodeAnn.Features = features
  39. }
  40. }
  41. // NodeAnnSetTimestamp is a functional option that sets the timestamp of the
  42. // announcement to the current time, or increments it if the timestamp is
  43. // already in the future.
  44. func NodeAnnSetTimestamp(nodeAnn *lnwire.NodeAnnouncement) {
  45. newTimestamp := uint32(time.Now().Unix())
  46. if newTimestamp <= nodeAnn.Timestamp {
  47. // Increment the prior value to ensure the timestamp
  48. // monotonically increases, otherwise the announcement won't
  49. // propagate.
  50. newTimestamp = nodeAnn.Timestamp + 1
  51. }
  52. nodeAnn.Timestamp = newTimestamp
  53. }
  54. // SignNodeAnnouncement signs the lnwire.NodeAnnouncement provided, which
  55. // should be the most recent, valid update, otherwise the timestamp may not
  56. // monotonically increase from the prior.
  57. func SignNodeAnnouncement(signer lnwallet.MessageSigner,
  58. keyLoc keychain.KeyLocator, nodeAnn *lnwire.NodeAnnouncement) error {
  59. // Create the DER-encoded ECDSA signature over the message digest.
  60. sig, err := SignAnnouncement(signer, keyLoc, nodeAnn)
  61. if err != nil {
  62. return err
  63. }
  64. // Parse the DER-encoded signature into a fixed-size 64-byte array.
  65. nodeAnn.Signature, err = lnwire.NewSigFromSignature(sig)
  66. return err
  67. }