diff --git a/watchtower/wtdb/session_info.go b/watchtower/wtdb/session_info.go index 1c7b7f0f..f4acf764 100644 --- a/watchtower/wtdb/session_info.go +++ b/watchtower/wtdb/session_info.go @@ -2,6 +2,7 @@ package wtdb import ( "errors" + "io" "github.com/lightningnetwork/lnd/watchtower/wtpolicy" ) @@ -59,6 +60,28 @@ type SessionInfo struct { // TODO(conner): store client metrics, DOS score, etc } +// Encode serializes the session info to the given io.Writer. +func (s *SessionInfo) Encode(w io.Writer) error { + return WriteElements(w, + s.ID, + s.Policy, + s.LastApplied, + s.ClientLastApplied, + s.RewardAddress, + ) +} + +// Decode deserializes the session infor from the given io.Reader. +func (s *SessionInfo) Decode(r io.Reader) error { + return ReadElements(r, + &s.ID, + &s.Policy, + &s.LastApplied, + &s.ClientLastApplied, + &s.RewardAddress, + ) +} + // AcceptUpdateSequence validates that a state update's sequence number and last // applied are valid given our past history with the client. These checks ensure // that clients are properly in sync and following the update protocol properly. diff --git a/watchtower/wtdb/session_state_update.go b/watchtower/wtdb/session_state_update.go index 7711b10f..de75f6ae 100644 --- a/watchtower/wtdb/session_state_update.go +++ b/watchtower/wtdb/session_state_update.go @@ -1,5 +1,7 @@ package wtdb +import "io" + // SessionStateUpdate holds a state update sent by a client along with its // SessionID. type SessionStateUpdate struct { @@ -21,3 +23,25 @@ type SessionStateUpdate struct { // hint is braodcast. EncryptedBlob []byte } + +// Encode serializes the state update into the provided io.Writer. +func (u *SessionStateUpdate) Encode(w io.Writer) error { + return WriteElements(w, + u.ID, + u.SeqNum, + u.LastApplied, + u.Hint, + u.EncryptedBlob, + ) +} + +// Decode deserializes the target state update from the provided io.Reader. +func (u *SessionStateUpdate) Decode(r io.Reader) error { + return ReadElements(r, + &u.ID, + &u.SeqNum, + &u.LastApplied, + &u.Hint, + &u.EncryptedBlob, + ) +}