cosmos-sdk/examples/kvstore/tx.go

60 lines
1.1 KiB
Go
Raw Normal View History

package main
import (
"bytes"
sdk "github.com/cosmos/cosmos-sdk/types"
2018-05-23 22:09:01 -07:00
"github.com/cosmos/cosmos-sdk/x/auth"
)
2018-01-26 06:22:56 -08:00
// An sdk.Tx which is its own sdk.Msg.
2018-02-19 14:17:06 -08:00
type kvstoreTx struct {
key []byte
value []byte
bytes []byte
}
2018-02-19 14:17:06 -08:00
func (tx kvstoreTx) Type() string {
return "kvstore"
}
2018-02-19 14:17:06 -08:00
func (tx kvstoreTx) GetMsg() sdk.Msg {
2018-01-26 06:22:56 -08:00
return tx
}
2018-02-19 14:17:06 -08:00
func (tx kvstoreTx) GetSignBytes() []byte {
return tx.bytes
}
// Should the app be calling this? Or only handlers?
2018-02-19 14:17:06 -08:00
func (tx kvstoreTx) ValidateBasic() sdk.Error {
return nil
}
2018-03-01 23:49:07 -08:00
func (tx kvstoreTx) GetSigners() []sdk.Address {
return nil
}
2018-05-23 22:09:01 -07:00
func (tx kvstoreTx) GetSignatures() []auth.StdSignature {
return nil
}
// takes raw transaction bytes and decodes them into an sdk.Tx. An sdk.Tx has
// all the signatures and can be used to authenticate.
2018-01-26 04:19:33 -08:00
func decodeTx(txBytes []byte) (sdk.Tx, sdk.Error) {
var tx sdk.Tx
split := bytes.Split(txBytes, []byte("="))
if len(split) == 1 {
k := split[0]
2018-02-19 14:17:06 -08:00
tx = kvstoreTx{k, k, txBytes}
} else if len(split) == 2 {
k, v := split[0], split[1]
2018-02-19 14:17:06 -08:00
tx = kvstoreTx{k, v, txBytes}
} else {
return nil, sdk.ErrTxDecode("too many =")
}
return tx, nil
}