-
Notifications
You must be signed in to change notification settings - Fork 2
/
codec.go
65 lines (52 loc) · 1.36 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package codec
import (
"bytes"
"encoding/json"
"fmt"
"github.com/tendermint/go-amino"
cryptoamino "github.com/hdac-io/tendermint/crypto/encoding/amino"
tmtypes "github.com/hdac-io/tendermint/types"
)
// amino codec to marshal/unmarshal
type Codec = amino.Codec
func New() *Codec {
return amino.NewCodec()
}
// Register the go-crypto to the codec
func RegisterCrypto(cdc *Codec) {
cryptoamino.RegisterAmino(cdc)
}
// RegisterEvidences registers Tendermint evidence types with the provided codec.
func RegisterEvidences(cdc *Codec) {
tmtypes.RegisterEvidences(cdc)
}
// attempt to make some pretty json
func MarshalJSONIndent(cdc *Codec, obj interface{}) ([]byte, error) {
bz, err := cdc.MarshalJSON(obj)
if err != nil {
return nil, err
}
var out bytes.Buffer
err = json.Indent(&out, bz, "", " ")
if err != nil {
return nil, err
}
return out.Bytes(), nil
}
// MustMarshalJSONIndent executes MarshalJSONIndent except it panics upon failure.
func MustMarshalJSONIndent(cdc *Codec, obj interface{}) []byte {
bz, err := MarshalJSONIndent(cdc, obj)
if err != nil {
panic(fmt.Sprintf("failed to marshal JSON: %s", err))
}
return bz
}
//__________________________________________________________________
// generic sealed codec to be used throughout sdk
var Cdc *Codec
func init() {
cdc := New()
RegisterCrypto(cdc)
RegisterEvidences(cdc)
Cdc = cdc.Seal()
}