sort *all* GetSignBytes:
- call MustSortJSON before return JSON bytes to guarantee alphabetic ordering - moved SortJSON and MustSortJSON to types package to avoid cyclic package dep
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package types
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
// 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.
|
||||
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.
|
||||
func MustSortJSON(toSortJSON []byte) []byte {
|
||||
js, err := SortJSON(toSortJSON)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return js
|
||||
}
|
||||
Reference in New Issue
Block a user