diff --git a/flake.nix b/flake.nix index d900d0ab6..07d4c8609 100644 --- a/flake.nix +++ b/flake.nix @@ -29,7 +29,7 @@ ]; doCheck = false; # FIXME: This needs to be manually changed when updating modules. - vendorSha256 = "sha256-4xChSKAkwwrFp5/ZMnhtvsR4drVfw1cLE3YXwVHeW0A="; + vendorSha256 = "sha256-1m5byn1CVD8Awzbo9XY94FgitC+uetTfon/8bBh5DiE="; # Fix for 'nix run' trying to execute 'go-waku'. meta = { mainProgram = "waku"; }; }; diff --git a/waku/v2/protocol/pb/codec.go b/waku/v2/protocol/pb/codec.go new file mode 100644 index 000000000..dd0c143f0 --- /dev/null +++ b/waku/v2/protocol/pb/codec.go @@ -0,0 +1,29 @@ +package pb + +import ( + "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/proto" +) + +func (m *WakuMessage) MarshalJSON() ([]byte, error) { + return (protojson.MarshalOptions{}).Marshal(m) +} + +func Unmarshal(data []byte) (*WakuMessage, error) { + msg := &WakuMessage{} + err := proto.Unmarshal(data, msg) + if err != nil { + return nil, err + } + + err = msg.Validate() + if err != nil { + return nil, err + } + + return msg, nil +} + +func (m *WakuMessage) UnmarshalJSON(data []byte) error { + return (protojson.UnmarshalOptions{}).Unmarshal(data, m) +} diff --git a/waku/v2/protocol/pb/validation.go b/waku/v2/protocol/pb/validation.go index fb1e000bd..89bc788ad 100644 --- a/waku/v2/protocol/pb/validation.go +++ b/waku/v2/protocol/pb/validation.go @@ -2,8 +2,6 @@ package pb import ( "errors" - - "google.golang.org/protobuf/proto" ) const MaxMetaAttrLength = 64 @@ -29,19 +27,3 @@ func (msg *WakuMessage) Validate() error { return nil } - -func Unmarshal(data []byte) (*WakuMessage, error) { - msg := &WakuMessage{} - err := proto.Unmarshal(data, msg) - if err != nil { - return nil, err - } - - err = msg.Validate() - if err != nil { - return nil, err - } - - return msg, nil - -}