github.com/decred/dcrlnd@v0.7.6/lnwire/channel_announcement.go (about) 1 package lnwire 2 3 import ( 4 "bytes" 5 "io" 6 7 "github.com/decred/dcrd/chaincfg/chainhash" 8 ) 9 10 // ChannelAnnouncement message is used to announce the existence of a channel 11 // between two peers in the overlay, which is propagated by the discovery 12 // service over broadcast handler. 13 type ChannelAnnouncement struct { 14 // This signatures are used by nodes in order to create cross 15 // references between node's channel and node. Requiring both nodes 16 // to sign indicates they are both willing to route other payments via 17 // this node. 18 NodeSig1 Sig 19 NodeSig2 Sig 20 21 // This signatures are used by nodes in order to create cross 22 // references between node's channel and node. Requiring the decred 23 // signatures proves they control the channel. 24 DecredSig1 Sig 25 DecredSig2 Sig 26 27 // Features is the feature vector that encodes the features supported 28 // by the target node. This field can be used to signal the type of the 29 // channel, or modifications to the fields that would normally follow 30 // this vector. 31 Features *RawFeatureVector 32 33 // ChainHash denotes the target chain that this channel was opened 34 // within. This value should be the genesis hash of the target chain. 35 ChainHash chainhash.Hash 36 37 // ShortChannelID is the unique description of the funding transaction, 38 // or where exactly it's located within the target blockchain. 39 ShortChannelID ShortChannelID 40 41 // The public keys of the two nodes who are operating the channel, such 42 // that is NodeID1 the numerically-lesser than NodeID2 (ascending 43 // numerical order). 44 NodeID1 [33]byte 45 NodeID2 [33]byte 46 47 // Public keys which corresponds to the keys which was declared in 48 // multisig funding transaction output. 49 DecredKey1 [33]byte 50 DecredKey2 [33]byte 51 52 // ExtraOpaqueData is the set of data that was appended to this 53 // message, some of which we may not actually know how to iterate or 54 // parse. By holding onto this data, we ensure that we're able to 55 // properly validate the set of signatures that cover these new fields, 56 // and ensure we're able to make upgrades to the network in a forwards 57 // compatible manner. 58 ExtraOpaqueData ExtraOpaqueData 59 } 60 61 // A compile time check to ensure ChannelAnnouncement implements the 62 // lnwire.Message interface. 63 var _ Message = (*ChannelAnnouncement)(nil) 64 65 // Decode deserializes a serialized ChannelAnnouncement stored in the passed 66 // io.Reader observing the specified protocol version. 67 // 68 // This is part of the lnwire.Message interface. 69 func (a *ChannelAnnouncement) Decode(r io.Reader, pver uint32) error { 70 return ReadElements(r, 71 &a.NodeSig1, 72 &a.NodeSig2, 73 &a.DecredSig1, 74 &a.DecredSig2, 75 &a.Features, 76 a.ChainHash[:], 77 &a.ShortChannelID, 78 &a.NodeID1, 79 &a.NodeID2, 80 &a.DecredKey1, 81 &a.DecredKey2, 82 &a.ExtraOpaqueData, 83 ) 84 } 85 86 // Encode serializes the target ChannelAnnouncement into the passed io.Writer 87 // observing the protocol version specified. 88 // 89 // This is part of the lnwire.Message interface. 90 func (a *ChannelAnnouncement) Encode(w *bytes.Buffer, pver uint32) error { 91 if err := WriteSig(w, a.NodeSig1); err != nil { 92 return err 93 } 94 95 if err := WriteSig(w, a.NodeSig2); err != nil { 96 return err 97 } 98 99 if err := WriteSig(w, a.DecredSig1); err != nil { 100 return err 101 } 102 103 if err := WriteSig(w, a.DecredSig2); err != nil { 104 return err 105 } 106 107 if err := WriteRawFeatureVector(w, a.Features); err != nil { 108 return err 109 } 110 111 if err := WriteBytes(w, a.ChainHash[:]); err != nil { 112 return err 113 } 114 115 if err := WriteShortChannelID(w, a.ShortChannelID); err != nil { 116 return err 117 } 118 119 if err := WriteBytes(w, a.NodeID1[:]); err != nil { 120 return err 121 } 122 123 if err := WriteBytes(w, a.NodeID2[:]); err != nil { 124 return err 125 } 126 127 if err := WriteBytes(w, a.DecredKey1[:]); err != nil { 128 return err 129 } 130 131 if err := WriteBytes(w, a.DecredKey2[:]); err != nil { 132 return err 133 } 134 135 return WriteBytes(w, a.ExtraOpaqueData) 136 } 137 138 // MsgType returns the integer uniquely identifying this message type on the 139 // wire. 140 // 141 // This is part of the lnwire.Message interface. 142 func (a *ChannelAnnouncement) MsgType() MessageType { 143 return MsgChannelAnnouncement 144 } 145 146 // DataToSign is used to retrieve part of the announcement message which should 147 // be signed. 148 func (a *ChannelAnnouncement) DataToSign() ([]byte, error) { 149 // We should not include the signatures itself. 150 b := make([]byte, 0, MaxMsgBody) 151 buf := bytes.NewBuffer(b) 152 153 if err := WriteRawFeatureVector(buf, a.Features); err != nil { 154 return nil, err 155 } 156 157 if err := WriteBytes(buf, a.ChainHash[:]); err != nil { 158 return nil, err 159 } 160 161 if err := WriteShortChannelID(buf, a.ShortChannelID); err != nil { 162 return nil, err 163 } 164 165 if err := WriteBytes(buf, a.NodeID1[:]); err != nil { 166 return nil, err 167 } 168 169 if err := WriteBytes(buf, a.NodeID2[:]); err != nil { 170 return nil, err 171 } 172 173 if err := WriteBytes(buf, a.DecredKey1[:]); err != nil { 174 return nil, err 175 } 176 177 if err := WriteBytes(buf, a.DecredKey2[:]); err != nil { 178 return nil, err 179 } 180 181 if err := WriteBytes(buf, a.ExtraOpaqueData); err != nil { 182 return nil, err 183 } 184 185 return buf.Bytes(), nil 186 }