Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: WakuMessage json encoding #880

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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"; };
};
Expand Down
29 changes: 29 additions & 0 deletions waku/v2/protocol/pb/codec.go
Original file line number Diff line number Diff line change
@@ -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)
chaitanyaprem marked this conversation as resolved.
Show resolved Hide resolved
}

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)
}
18 changes: 0 additions & 18 deletions waku/v2/protocol/pb/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package pb

import (
"errors"

"google.golang.org/protobuf/proto"
)

const MaxMetaAttrLength = 64
Expand All @@ -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

}