Commit 30fdff6f authored by Joshua Gutow's avatar Joshua Gutow

op-node/derivation: JSON Encoding for Frames

parent 1786f370
......@@ -24,10 +24,10 @@ const MaxFrameLen = 1_000_000
// is_last = bool
type Frame struct {
ID ChannelID
FrameNumber uint16
Data []byte
IsLast bool
ID ChannelID `json:"id"`
FrameNumber uint16 `json:"frame_number"`
Data []byte `json:"data"`
IsLast bool `'json:"is_last"`
}
// MarshalBinary writes the frame to `w`.
......
package derive
import (
"encoding/json"
"encoding/hex"
"errors"
"fmt"
)
......@@ -46,6 +46,18 @@ func (id ChannelID) TerminalString() string {
return fmt.Sprintf("%x..%x", id[:3], id[13:])
}
func (id ChannelID) MarshalJSON() ([]byte, error) {
return json.Marshal(id.String())
func (id ChannelID) MarshalText() ([]byte, error) {
return []byte(id.String()), nil
}
func (id *ChannelID) UnmarshalText(text []byte) error {
h, err := hex.DecodeString(string(text))
if err != nil {
return err
}
if len(h) != ChannelIDLength {
return errors.New("invalid length")
}
copy(id[:], h)
return nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment