github.com/decred/dcrlnd@v0.7.6/netann/node_announcement.go (about)

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