github.com/eris-ltd/erisdb@v0.25.0/txs/amino_codec.go (about)

     1  package txs
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hyperledger/burrow/txs/payload"
     7  	amino "github.com/tendermint/go-amino"
     8  )
     9  
    10  type aminoCodec struct {
    11  	*amino.Codec
    12  }
    13  
    14  func NewAminoCodec() *aminoCodec {
    15  	cdc := amino.NewCodec()
    16  	cdc.RegisterInterface((*payload.Payload)(nil), nil)
    17  	registerTx(cdc, &payload.SendTx{})
    18  	registerTx(cdc, &payload.CallTx{})
    19  	registerTx(cdc, &payload.BondTx{})
    20  	registerTx(cdc, &payload.UnbondTx{})
    21  	registerTx(cdc, &payload.PermsTx{})
    22  	registerTx(cdc, &payload.NameTx{})
    23  	registerTx(cdc, &payload.GovTx{})
    24  	registerTx(cdc, &payload.ProposalTx{})
    25  	return &aminoCodec{cdc}
    26  }
    27  
    28  func (gwc *aminoCodec) EncodeTx(env *Envelope) ([]byte, error) {
    29  	return gwc.MarshalBinaryBare(env)
    30  }
    31  
    32  func (gwc *aminoCodec) DecodeTx(txBytes []byte) (*Envelope, error) {
    33  	env := new(Envelope)
    34  	err := gwc.UnmarshalBinaryBare(txBytes, env)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	return env, nil
    39  }
    40  
    41  func registerTx(cdc *amino.Codec, tx payload.Payload) {
    42  	cdc.RegisterConcrete(tx, fmt.Sprintf("burrow/txs/payload/%v", tx.Type()), nil)
    43  }