diff --git a/channeldb/migration32/route.go b/channeldb/migration32/route.go index a4d40a45c..a35338e50 100644 --- a/channeldb/migration32/route.go +++ b/channeldb/migration32/route.go @@ -29,6 +29,32 @@ const VertexSize = 33 // public key. type Vertex [VertexSize]byte +// Record returns a TLV record that can be used to encode/decode a Vertex +// to/from a TLV stream. +func (v *Vertex) Record() tlv.Record { + return tlv.MakeStaticRecord( + 0, v, VertexSize, encodeVertex, decodeVertex, + ) +} + +func encodeVertex(w io.Writer, val interface{}, _ *[8]byte) error { + if b, ok := val.(*Vertex); ok { + _, err := w.Write(b[:]) + return err + } + + return tlv.NewTypeForEncodingErr(val, "Vertex") +} + +func decodeVertex(r io.Reader, val interface{}, _ *[8]byte, l uint64) error { + if b, ok := val.(*Vertex); ok { + _, err := io.ReadFull(r, b[:]) + return err + } + + return tlv.NewTypeForDecodingErr(val, "Vertex", l, VertexSize) +} + // Route represents a path through the channel graph which runs over one or // more channels in succession. This struct carries all the information // required to craft the Sphinx onion packet, and send the payment along the diff --git a/routing/route/route.go b/routing/route/route.go index 9aa28759b..2d62af465 100644 --- a/routing/route/route.go +++ b/routing/route/route.go @@ -94,6 +94,32 @@ func (v Vertex) String() string { return fmt.Sprintf("%x", v[:]) } +// Record returns a TLV record that can be used to encode/decode a Vertex +// to/from a TLV stream. +func (v *Vertex) Record() tlv.Record { + return tlv.MakeStaticRecord( + 0, v, VertexSize, encodeVertex, decodeVertex, + ) +} + +func encodeVertex(w io.Writer, val interface{}, _ *[8]byte) error { + if b, ok := val.(*Vertex); ok { + _, err := w.Write(b[:]) + return err + } + + return tlv.NewTypeForEncodingErr(val, "Vertex") +} + +func decodeVertex(r io.Reader, val interface{}, _ *[8]byte, l uint64) error { + if b, ok := val.(*Vertex); ok { + _, err := io.ReadFull(r, b[:]) + return err + } + + return tlv.NewTypeForDecodingErr(val, "Vertex", l, VertexSize) +} + // Hop represents an intermediate or final node of the route. This naming // is in line with the definition given in BOLT #4: Onion Routing Protocol. // The struct houses the channel along which this hop can be reached and