forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
codec.go
35 lines (28 loc) · 1001 Bytes
/
codec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package types
import (
jsonc "github.com/gibson042/canonicaljson-go"
"github.com/cosmos/cosmos-sdk/codec"
)
// Register the sdk message type
func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterInterface((*Msg)(nil), nil)
cdc.RegisterInterface((*Tx)(nil), nil)
}
// CanonicalSignBytes returns a canonical JSON encoding of a Proto message that
// can be signed over. The JSON encoding ensures all field names adhere to their
// Proto definition, default values are omitted, and follows the JSON Canonical
// Form.
func CanonicalSignBytes(msg codec.ProtoMarshaler) ([]byte, error) {
// first, encode via canonical Proto3 JSON
bz, err := codec.ProtoMarshalJSON(msg)
if err != nil {
return nil, err
}
genericJSON := make(map[string]interface{})
// decode canonical proto encoding into a generic map
if err := jsonc.Unmarshal(bz, &genericJSON); err != nil {
return nil, err
}
// finally, return the canonical JSON encoding via JSON Canonical Form
return jsonc.Marshal(genericJSON)
}