cosmos-sdk/x/auth/tx/encoder.go
SaReN b2348180b8
Migrate {x/auth, x/gov, x/staking} missing CLI queries to proto (#6994)
* Fix error code

* Fix decoder

* Fix typo

* Fix decode

* refactor

* Migrate SearchTxsResult to proto

* fix MarkEventsToIndex

* lint++

* Fix output

* Add QueryTxCmd cli test

* Add fmt

* Put txBuilder in types/tx

* Add GetAnyTx in TxBuilder

* Add new IsAnyTx

* Rename to IntoAny

* Fix bug

* fmt

Co-authored-by: Marie <marie.gauthier63@gmail.com>

* Fix ibc CLI to use proto

* Fix any MarshalJSON

* Fix test

* Make tx.Tx implement sdk.Tx

* Register sdk.Tx

* Fix lint

* Allow DefaultJSONTxEncoder to take tx.Tx

* refactor

* Rename variable

* remove fmt

Co-authored-by: Anil Kumar Kammari <anil@vitwit.com>
Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Aleksandr Bezobchuk <aleks.bezobchuk@gmail.com>
Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com>
Co-authored-by: Marie <marie.gauthier63@gmail.com>
2020-09-10 18:26:47 +00:00

48 lines
1.1 KiB
Go

package tx
import (
"fmt"
"github.com/gogo/protobuf/proto"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
)
// DefaultTxEncoder returns a default protobuf TxEncoder using the provided Marshaler
func DefaultTxEncoder() sdk.TxEncoder {
return func(tx sdk.Tx) ([]byte, error) {
txWrapper, ok := tx.(*wrapper)
if !ok {
return nil, fmt.Errorf("expected %T, got %T", &wrapper{}, tx)
}
raw := &txtypes.TxRaw{
BodyBytes: txWrapper.getBodyBytes(),
AuthInfoBytes: txWrapper.getAuthInfoBytes(),
Signatures: txWrapper.tx.Signatures,
}
return proto.Marshal(raw)
}
}
// DefaultJSONTxEncoder returns a default protobuf JSON TxEncoder using the provided Marshaler.
func DefaultJSONTxEncoder() sdk.TxEncoder {
return func(tx sdk.Tx) ([]byte, error) {
txWrapper, ok := tx.(*wrapper)
if ok {
return codec.ProtoMarshalJSON(txWrapper.tx)
}
protoTx, ok := tx.(*txtypes.Tx)
if ok {
return codec.ProtoMarshalJSON(protoTx)
}
return nil, fmt.Errorf("expected %T, got %T", &wrapper{}, tx)
}
}