@@ -44,6 +44,10 @@ type HopPayload struct {
4444 // HMAC is an HMAC computed over the entire per-hop payload that also
4545 // includes the higher-level (optional) associated data bytes.
4646 HMAC [HMACSize ]byte
47+
48+ // TLVPayloadGuaranteed is set to true if the payload is guaranteed to
49+ // be a TLVPayload. E.g. in the case of an onion message.
50+ TLVPayloadGuaranteed bool
4751}
4852
4953// NewTLVHopPayload creates a new TLV encoded HopPayload. The payload will be
@@ -87,13 +91,8 @@ func (hp *HopPayload) Encode(w io.Writer) error {
8791}
8892
8993// Decode unpacks an encoded HopPayload from the passed reader into the target
90- // HopPayload. The isMessage boolean should be set to true if we're parsing a
91- // payload that is known to be for an onion message.
92- func (hp * HopPayload ) Decode (r io.Reader , isMessage ... bool ) error {
93- // To preserve backwards compatibility, we'll default to isMessage being
94- // false if it is not provided.
95- isMsg := len (isMessage ) > 0 && isMessage [0 ]
96-
94+ // HopPayload.
95+ func (hp * HopPayload ) Decode (r io.Reader ) error {
9796 bufReader := bufio .NewReader (r )
9897
9998 // In order to properly parse the payload, we'll need to check the
@@ -104,12 +103,9 @@ func (hp *HopPayload) Decode(r io.Reader, isMessage ...bool) error {
104103 return err
105104 }
106105
107- // Per BOLT 7, onion messages MUST use the TLV format.
108- if isMsg {
109- return decodeTLVHopPayload (hp , bufReader )
110- }
111-
112- if isLegacyPayloadByte (peekByte [0 ]) {
106+ // If the HopPayload is guaranteed to be a TLV payload, we can skip the
107+ // check for the legacy payload byte.
108+ if ! hp .TLVPayloadGuaranteed && isLegacyPayloadByte (peekByte [0 ]) {
113109 return decodeLegacyHopPayload (hp , bufReader )
114110 }
115111
0 commit comments