-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdecoder.go
58 lines (45 loc) · 1.41 KB
/
decoder.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
package cosmosutils
import (
"encoding/base64"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
)
type Decoder struct {
interfacesRegistry codectypes.InterfaceRegistry
}
// RegisterInterfaces register decoding interface to the decoder by using the provided interface
// registry.
func (decoder *Decoder) RegisterInterfaces(registry func(registry codectypes.InterfaceRegistry)) *Decoder {
registry(decoder.interfacesRegistry)
return decoder
}
// Decode decodes the base64-encoded transaction bytes to Tx
func (decoder *Decoder) DecodeBase64(base64Tx string) (Tx, error) {
txBytes, err := base64.StdEncoding.DecodeString(base64Tx)
if err != nil {
return Tx{}, err
}
return decoder.Decode(txBytes)
}
// Decode decodes the transaction bytes to Tx
func (decoder *Decoder) Decode(txBytes []byte) (Tx, error) {
marshaler := codec.NewProtoCodec(decoder.interfacesRegistry)
tx, err := authtx.DefaultTxDecoder(marshaler)(txBytes)
if err != nil {
return Tx{}, err
}
return Tx{
tx,
marshaler,
}, nil
}
// NewDecoder creates a new decoder
func NewDecoder() *Decoder {
interfaceRegistry := codectypes.NewInterfaceRegistry()
return &Decoder{
interfaceRegistry,
}
}
// DefaultDecoder is a decoder with all Cosmos builtin modules interfaces registered
var DefaultDecoder = NewDecoder().RegisterInterfaces(RegisterDefaultInterfaces)