49 lines
880 B
Go
49 lines
880 B
Go
package utils
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"cosmossdk.io/core/transaction"
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
)
|
|
|
|
var _ transaction.Codec[transaction.Tx] = &GenericTxDecoder[transaction.Tx]{}
|
|
|
|
type GenericTxDecoder[T transaction.Tx] struct {
|
|
TxConfig client.TxConfig
|
|
}
|
|
|
|
// Decode implements transaction.Codec.
|
|
func (t GenericTxDecoder[T]) Decode(bz []byte) (T, error) {
|
|
var out T
|
|
tx, err := t.TxConfig.TxDecoder()(bz)
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
|
|
var ok bool
|
|
out, ok = tx.(T)
|
|
if !ok {
|
|
return out, errors.New("unexpected Tx type")
|
|
}
|
|
|
|
return out, nil
|
|
}
|
|
|
|
// DecodeJSON implements transaction.Codec.
|
|
func (t GenericTxDecoder[T]) DecodeJSON(bz []byte) (T, error) {
|
|
var out T
|
|
tx, err := t.TxConfig.TxJSONDecoder()(bz)
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
|
|
var ok bool
|
|
out, ok = tx.(T)
|
|
if !ok {
|
|
return out, errors.New("unexpected Tx type")
|
|
}
|
|
|
|
return out, nil
|
|
}
|