From 430d400a29ec3babad1fb6bf1f8a5bc1576a3e92 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 22 Jun 2023 16:11:27 +0000 Subject: [PATCH] chore: add sortJSON back & mark as deprecated (backport #16649) (#16657) Co-authored-by: Marko --- types/utils.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/types/utils.go b/types/utils.go index da243d756b..b912be36f4 100644 --- a/types/utils.go +++ b/types/utils.go @@ -2,6 +2,7 @@ package types import ( "encoding/binary" + "encoding/json" "fmt" "time" @@ -10,6 +11,36 @@ import ( "github.com/cosmos/cosmos-sdk/types/kv" ) +// SortedJSON takes any JSON and returns it sorted by keys. Also, all white-spaces +// are removed. +// This method can be used to canonicalize JSON to be returned by GetSignBytes, +// e.g. for the ledger integration. +// If the passed JSON isn't valid it will return an error. +// Deprecated: SortJSON was used for GetSignbytes, this is now automatic with amino signing +func SortJSON(toSortJSON []byte) ([]byte, error) { + var c interface{} + err := json.Unmarshal(toSortJSON, &c) + if err != nil { + return nil, err + } + js, err := json.Marshal(c) + if err != nil { + return nil, err + } + return js, nil +} + +// MustSortJSON is like SortJSON but panic if an error occurs, e.g., if +// the passed JSON isn't valid. +// Deprecated: SortJSON was used for GetSignbytes, this is now automatic with amino signing +func MustSortJSON(toSortJSON []byte) []byte { + js, err := SortJSON(toSortJSON) + if err != nil { + panic(err) + } + return js +} + // Uint64ToBigEndian - marshals uint64 to a bigendian byte slice so it can be sorted func Uint64ToBigEndian(i uint64) []byte { b := make([]byte, 8)