typed_lease_expiry.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package lnwire
  2. import (
  3. "io"
  4. "github.com/lightningnetwork/lnd/tlv"
  5. )
  6. const (
  7. // LeaseExpiryType is the type of the experimental record used to
  8. // communicate the expiration of a channel lease throughout the channel
  9. // funding process.
  10. //
  11. // TODO: Decide on actual TLV type. Custom records start at 2^16.
  12. LeaseExpiryRecordType tlv.Type = 1 << 16
  13. )
  14. // LeaseExpiry represents the absolute expiration height of a channel lease. All
  15. // outputs that pay directly to the channel initiator are locked until this
  16. // height is reached.
  17. type LeaseExpiry uint32
  18. // Record returns a TLV record that can be used to encode/decode the LeaseExpiry
  19. // type from a given TLV stream.
  20. func (l *LeaseExpiry) Record() tlv.Record {
  21. return tlv.MakeStaticRecord(
  22. LeaseExpiryRecordType, l, 4, leaseExpiryEncoder, leaseExpiryDecoder,
  23. )
  24. }
  25. // leaseExpiryEncoder is a custom TLV encoder for the LeaseExpiry record.
  26. func leaseExpiryEncoder(w io.Writer, val interface{}, buf *[8]byte) error {
  27. if v, ok := val.(*LeaseExpiry); ok {
  28. return tlv.EUint32T(w, uint32(*v), buf)
  29. }
  30. return tlv.NewTypeForEncodingErr(val, "lnwire.LeaseExpiry")
  31. }
  32. // leaseExpiryDecoder is a custom TLV decoder for the LeaseExpiry record.
  33. func leaseExpiryDecoder(r io.Reader, val interface{}, buf *[8]byte, l uint64) error {
  34. if v, ok := val.(*LeaseExpiry); ok {
  35. var leaseExpiry uint32
  36. if err := tlv.DUint32(r, &leaseExpiry, buf, l); err != nil {
  37. return err
  38. }
  39. *v = LeaseExpiry(leaseExpiry)
  40. return nil
  41. }
  42. return tlv.NewTypeForEncodingErr(val, "lnwire.LeaseExpiry")
  43. }