2018-04-26 19:59:30 -07:00
|
|
|
package types
|
|
|
|
|
2020-01-24 07:32:00 -08:00
|
|
|
import (
|
2020-03-13 06:57:15 -07:00
|
|
|
jsonc "github.com/gibson042/canonicaljson-go"
|
|
|
|
|
2020-01-24 07:32:00 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
)
|
2018-04-26 19:59:30 -07:00
|
|
|
|
|
|
|
// Register the sdk message type
|
2018-09-13 11:17:32 -07:00
|
|
|
func RegisterCodec(cdc *codec.Codec) {
|
2018-04-26 19:59:30 -07:00
|
|
|
cdc.RegisterInterface((*Msg)(nil), nil)
|
2018-06-11 13:09:29 -07:00
|
|
|
cdc.RegisterInterface((*Tx)(nil), nil)
|
2018-04-26 19:59:30 -07:00
|
|
|
}
|
2020-03-13 06:57:15 -07:00
|
|
|
|
2020-03-13 06:58:53 -07:00
|
|
|
// 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.
|
2020-03-26 09:46:10 -07:00
|
|
|
func CanonicalSignBytes(msg codec.ProtoMarshaler) ([]byte, error) {
|
|
|
|
// first, encode via canonical Proto3 JSON
|
|
|
|
bz, err := codec.ProtoMarshalJSON(msg)
|
|
|
|
if err != nil {
|
2020-03-13 06:57:15 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
genericJSON := make(map[string]interface{})
|
|
|
|
|
|
|
|
// decode canonical proto encoding into a generic map
|
2020-03-26 09:46:10 -07:00
|
|
|
if err := jsonc.Unmarshal(bz, &genericJSON); err != nil {
|
2020-03-13 06:57:15 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally, return the canonical JSON encoding via JSON Canonical Form
|
|
|
|
return jsonc.Marshal(genericJSON)
|
|
|
|
}
|