lnd.xprv/lnwire/typed_delivery_addr.go
Johan T. Halseth 6842c0ba8c
lnwire: convert the delivery addr in [Open+Accept]Channel to a TLV type in ExtraData
In this commit, we convert the delivery address in the open and accept
channel methods to be a TLV type. This works as an "empty" delivery
address is encoded using a two zero bytes (uint16 length zero), and a
tlv type of 0 is encoded in the same manner (byte for type, byte for
zero length). This change allows us to easily extend these messages in
the future, in a uniform manner.

When decoding the message we snip the bytes from the read TLV data.
Similarly, when encoding we concatenate the TLV record for the shutdown
script with the rest of the TLV data.
2021-02-24 17:40:08 +01:00

42 lines
1.2 KiB
Go

package lnwire
import (
"github.com/lightningnetwork/lnd/tlv"
)
const (
// DeliveryAddrType is the TLV record type for delivery addreses within
// the name space of the OpenChannel and AcceptChannel messages.
DeliveryAddrType = 0
// deliveryAddressMaxSize is the maximum expected size in bytes of a
// DeliveryAddress based on the types of scripts we know.
// Following are the known scripts and their sizes in bytes.
// - pay to witness script hash: 34
// - pay to pubkey hash: 25
// - pay to script hash: 22
// - pay to witness pubkey hash: 22.
deliveryAddressMaxSize = 34
)
// DeliveryAddress is used to communicate the address to which funds from a
// closed channel should be sent. The address can be a p2wsh, p2pkh, p2sh or
// p2wpkh.
type DeliveryAddress []byte
// NewRecord returns a TLV record that can be used to encode the delivery
// address within the ExtraData TLV stream. This was intorudced in order to
// allow the OpenChannel/AcceptChannel messages to properly be extended with
// TLV types.
func (d *DeliveryAddress) NewRecord() tlv.Record {
addrBytes := (*[]byte)(d)
return tlv.MakeDynamicRecord(
DeliveryAddrType, addrBytes,
func() uint64 {
return uint64(len(*addrBytes))
},
tlv.EVarBytes, tlv.DVarBytes,
)
}