lnd/bolt12/decode.go
bitromortac bc2089588c bolt12: add Offer message struct with TLV codec
The Offer struct models a long-lived, reusable BOLT 12 payment template.
It defines TLV fields as optional records and exposes Encode/DecodeOffer
for round-trip serialization. The struct implements
lnwire.PureTLVMessage; AllRecords filters the decoded TypeMap through
bolt12InUnsignedRange to derive any signed-range extras the encoder must
re-emit, keeping offer_id and the Merkle root stable across encoders
that understand a wider set of even/odd extensions.
2026-06-03 12:11:29 +02:00

29 lines
669 B
Go

package bolt12
import (
"bytes"
"fmt"
"github.com/lightningnetwork/lnd/tlv"
)
// decodeStream runs a single typed-stream pass over data and returns the
// canonical TypeMap. Records may be passed in any order; NewStream requires
// them sorted, so SortRecords runs first.
func decodeStream(data []byte, records ...tlv.Record) (tlv.TypeMap, error) {
tlv.SortRecords(records)
stream, err := tlv.NewStream(records...)
if err != nil {
return nil, fmt.Errorf("create stream: %w", err)
}
typeMap, err := stream.DecodeWithParsedTypesP2P(
bytes.NewReader(data),
)
if err != nil {
return nil, fmt.Errorf("decode stream: %w", err)
}
return typeMap, nil
}