mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-01-19 05:45:21 +01:00
zpay32: improve readability of blinded path encoding
This commit is contained in:
parent
4f5dd20f7e
commit
188dd44b42
@ -13,15 +13,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// relayInfoSize is the number of bytes that the relay info of a blinded
|
|
||||||
// payment will occupy.
|
|
||||||
// base fee: 4 bytes
|
|
||||||
// prop fee: 4 bytes
|
|
||||||
// cltv delta: 2 bytes
|
|
||||||
// min htlc: 8 bytes
|
|
||||||
// max htlc: 8 bytes
|
|
||||||
relayInfoSize = 26
|
|
||||||
|
|
||||||
// maxNumHopsPerPath is the maximum number of blinded path hops that can
|
// maxNumHopsPerPath is the maximum number of blinded path hops that can
|
||||||
// be included in a single encoded blinded path. This is calculated
|
// be included in a single encoded blinded path. This is calculated
|
||||||
// based on the `data_length` limit of 638 bytes for any tagged field in
|
// based on the `data_length` limit of 638 bytes for any tagged field in
|
||||||
@ -32,6 +23,12 @@ const (
|
|||||||
maxNumHopsPerPath = 7
|
maxNumHopsPerPath = 7
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// byteOrder defines the endian-ness we use for encoding to and from
|
||||||
|
// buffers.
|
||||||
|
byteOrder = binary.BigEndian
|
||||||
|
)
|
||||||
|
|
||||||
// BlindedPaymentPath holds all the information a payer needs to know about a
|
// BlindedPaymentPath holds all the information a payer needs to know about a
|
||||||
// blinded path to a receiver of a payment.
|
// blinded path to a receiver of a payment.
|
||||||
type BlindedPaymentPath struct {
|
type BlindedPaymentPath struct {
|
||||||
@ -69,24 +66,30 @@ type BlindedPaymentPath struct {
|
|||||||
// DecodeBlindedPayment attempts to parse a BlindedPaymentPath from the passed
|
// DecodeBlindedPayment attempts to parse a BlindedPaymentPath from the passed
|
||||||
// reader.
|
// reader.
|
||||||
func DecodeBlindedPayment(r io.Reader) (*BlindedPaymentPath, error) {
|
func DecodeBlindedPayment(r io.Reader) (*BlindedPaymentPath, error) {
|
||||||
var relayInfo [relayInfoSize]byte
|
var payment BlindedPaymentPath
|
||||||
n, err := r.Read(relayInfo[:])
|
|
||||||
|
if err := binary.Read(r, byteOrder, &payment.FeeBaseMsat); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := binary.Read(r, byteOrder, &payment.FeeRate); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err := binary.Read(r, byteOrder, &payment.CltvExpiryDelta)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if n != relayInfoSize {
|
|
||||||
return nil, fmt.Errorf("unable to read %d relay info bytes "+
|
err = binary.Read(r, byteOrder, &payment.HTLCMinMsat)
|
||||||
"off of the given stream: %w", relayInfoSize, err)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var payment BlindedPaymentPath
|
err = binary.Read(r, byteOrder, &payment.HTLCMaxMsat)
|
||||||
|
if err != nil {
|
||||||
// Parse the relay info fields.
|
return nil, err
|
||||||
payment.FeeBaseMsat = binary.BigEndian.Uint32(relayInfo[:4])
|
}
|
||||||
payment.FeeRate = binary.BigEndian.Uint32(relayInfo[4:8])
|
|
||||||
payment.CltvExpiryDelta = binary.BigEndian.Uint16(relayInfo[8:10])
|
|
||||||
payment.HTLCMinMsat = binary.BigEndian.Uint64(relayInfo[10:18])
|
|
||||||
payment.HTLCMaxMsat = binary.BigEndian.Uint64(relayInfo[18:])
|
|
||||||
|
|
||||||
// Parse the feature bit vector.
|
// Parse the feature bit vector.
|
||||||
f := lnwire.EmptyFeatureVector()
|
f := lnwire.EmptyFeatureVector()
|
||||||
@ -146,24 +149,31 @@ func DecodeBlindedPayment(r io.Reader) (*BlindedPaymentPath, error) {
|
|||||||
// 5) Number of hops: 1 byte.
|
// 5) Number of hops: 1 byte.
|
||||||
// 6) Encoded BlindedHops.
|
// 6) Encoded BlindedHops.
|
||||||
func (p *BlindedPaymentPath) Encode(w io.Writer) error {
|
func (p *BlindedPaymentPath) Encode(w io.Writer) error {
|
||||||
var relayInfo [26]byte
|
if err := binary.Write(w, byteOrder, p.FeeBaseMsat); err != nil {
|
||||||
binary.BigEndian.PutUint32(relayInfo[:4], p.FeeBaseMsat)
|
|
||||||
binary.BigEndian.PutUint32(relayInfo[4:8], p.FeeRate)
|
|
||||||
binary.BigEndian.PutUint16(relayInfo[8:10], p.CltvExpiryDelta)
|
|
||||||
binary.BigEndian.PutUint64(relayInfo[10:18], p.HTLCMinMsat)
|
|
||||||
binary.BigEndian.PutUint64(relayInfo[18:], p.HTLCMaxMsat)
|
|
||||||
|
|
||||||
_, err := w.Write(relayInfo[:])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = p.Features.Encode(w)
|
if err := binary.Write(w, byteOrder, p.FeeRate); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = w.Write(p.FirstEphemeralBlindingPoint.SerializeCompressed())
|
if err := binary.Write(w, byteOrder, p.CltvExpiryDelta); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := binary.Write(w, byteOrder, p.HTLCMinMsat); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := binary.Write(w, byteOrder, p.HTLCMaxMsat); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := p.Features.Encode(w); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := w.Write(p.FirstEphemeralBlindingPoint.SerializeCompressed())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -174,14 +184,12 @@ func (p *BlindedPaymentPath) Encode(w io.Writer) error {
|
|||||||
"maximum of %d", numHops, maxNumHopsPerPath)
|
"maximum of %d", numHops, maxNumHopsPerPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = w.Write([]byte{byte(numHops)})
|
if _, err := w.Write([]byte{byte(numHops)}); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, hop := range p.Hops {
|
for _, hop := range p.Hops {
|
||||||
err = EncodeBlindedHop(w, hop)
|
if err := EncodeBlindedHop(w, hop); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user