2019-08-30 10:40:47 +02:00
|
|
|
package record
|
|
|
|
|
|
|
|
import (
|
2022-10-26 16:57:37 +02:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
2019-08-30 10:40:47 +02:00
|
|
|
"github.com/lightningnetwork/lnd/tlv"
|
|
|
|
)
|
|
|
|
|
2019-08-30 10:41:34 +02:00
|
|
|
const (
|
2022-01-13 17:29:43 +01:00
|
|
|
// AmtOnionType is the type used in the onion to reference the amount to
|
2019-08-30 10:41:34 +02:00
|
|
|
// send to the next hop.
|
|
|
|
AmtOnionType tlv.Type = 2
|
|
|
|
|
2022-01-13 17:29:43 +01:00
|
|
|
// LockTimeTLV is the type used in the onion to reference the CLTV
|
2019-08-30 10:41:34 +02:00
|
|
|
// value that should be used for the next hop's HTLC.
|
|
|
|
LockTimeOnionType tlv.Type = 4
|
|
|
|
|
|
|
|
// NextHopOnionType is the type used in the onion to reference the ID
|
|
|
|
// of the next hop.
|
|
|
|
NextHopOnionType tlv.Type = 6
|
2021-10-04 09:33:12 +02:00
|
|
|
|
2022-10-26 16:57:37 +02:00
|
|
|
// EncryptedDataOnionType is the type used to include encrypted data
|
|
|
|
// provided by the receiver in the onion for use in blinded paths.
|
|
|
|
EncryptedDataOnionType tlv.Type = 10
|
|
|
|
|
|
|
|
// BlindingPointOnionType is the type used to include receiver provided
|
|
|
|
// ephemeral keys in the onion that are used in blinded paths.
|
|
|
|
BlindingPointOnionType tlv.Type = 12
|
|
|
|
|
2021-10-04 09:33:12 +02:00
|
|
|
// MetadataOnionType is the type used in the onion for the payment
|
|
|
|
// metadata.
|
|
|
|
MetadataOnionType tlv.Type = 16
|
2022-10-26 16:57:37 +02:00
|
|
|
|
|
|
|
// TotalAmtMsatBlindedType is the type used in the onion for the total
|
|
|
|
// amount field that is included in the final hop for blinded payments.
|
|
|
|
TotalAmtMsatBlindedType tlv.Type = 18
|
2019-08-30 10:41:34 +02:00
|
|
|
)
|
|
|
|
|
2019-08-30 10:40:47 +02:00
|
|
|
// NewAmtToFwdRecord creates a tlv.Record that encodes the amount_to_forward
|
|
|
|
// (type 2) for an onion payload.
|
|
|
|
func NewAmtToFwdRecord(amt *uint64) tlv.Record {
|
|
|
|
return tlv.MakeDynamicRecord(
|
2019-08-30 10:41:34 +02:00
|
|
|
AmtOnionType, amt, func() uint64 {
|
2019-08-30 10:40:47 +02:00
|
|
|
return tlv.SizeTUint64(*amt)
|
|
|
|
},
|
|
|
|
tlv.ETUint64, tlv.DTUint64,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLockTimeRecord creates a tlv.Record that encodes the outgoing_cltv_value
|
|
|
|
// (type 4) for an onion payload.
|
|
|
|
func NewLockTimeRecord(lockTime *uint32) tlv.Record {
|
|
|
|
return tlv.MakeDynamicRecord(
|
2019-08-30 10:41:34 +02:00
|
|
|
LockTimeOnionType, lockTime, func() uint64 {
|
2019-08-30 10:40:47 +02:00
|
|
|
return tlv.SizeTUint32(*lockTime)
|
|
|
|
},
|
|
|
|
tlv.ETUint32, tlv.DTUint32,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNextHopIDRecord creates a tlv.Record that encodes the short_channel_id
|
|
|
|
// (type 6) for an onion payload.
|
|
|
|
func NewNextHopIDRecord(cid *uint64) tlv.Record {
|
2019-08-30 10:41:34 +02:00
|
|
|
return tlv.MakePrimitiveRecord(NextHopOnionType, cid)
|
2019-08-30 10:40:47 +02:00
|
|
|
}
|
2021-10-04 09:33:12 +02:00
|
|
|
|
2022-10-26 16:57:37 +02:00
|
|
|
// NewEncryptedDataRecord creates a tlv.Record that encodes the encrypted_data
|
|
|
|
// (type 10) record for an onion payload.
|
|
|
|
func NewEncryptedDataRecord(data *[]byte) tlv.Record {
|
|
|
|
return tlv.MakePrimitiveRecord(EncryptedDataOnionType, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBlindingPointRecord creates a tlv.Record that encodes the blinding_point
|
|
|
|
// (type 12) record for an onion payload.
|
|
|
|
func NewBlindingPointRecord(point **btcec.PublicKey) tlv.Record {
|
|
|
|
return tlv.MakePrimitiveRecord(BlindingPointOnionType, point)
|
|
|
|
}
|
|
|
|
|
2021-10-04 09:33:12 +02:00
|
|
|
// NewMetadataRecord creates a tlv.Record that encodes the metadata (type 10)
|
|
|
|
// for an onion payload.
|
|
|
|
func NewMetadataRecord(metadata *[]byte) tlv.Record {
|
|
|
|
return tlv.MakeDynamicRecord(
|
|
|
|
MetadataOnionType, metadata,
|
|
|
|
func() uint64 {
|
|
|
|
return uint64(len(*metadata))
|
|
|
|
},
|
|
|
|
tlv.EVarBytes, tlv.DVarBytes,
|
|
|
|
)
|
|
|
|
}
|
2022-10-26 16:57:37 +02:00
|
|
|
|
|
|
|
// NewTotalAmtMsatBlinded creates a tlv.Record that encodes the
|
|
|
|
// total_amount_msat for the final an onion payload within a blinded route.
|
|
|
|
func NewTotalAmtMsatBlinded(amt *uint64) tlv.Record {
|
|
|
|
return tlv.MakeDynamicRecord(
|
|
|
|
TotalAmtMsatBlindedType, amt, func() uint64 {
|
|
|
|
return tlv.SizeTUint64(*amt)
|
|
|
|
},
|
|
|
|
tlv.ETUint64, tlv.DTUint64,
|
|
|
|
)
|
|
|
|
}
|