cosmos-sdk/docs/core/examples/app2.go

232 lines
6.1 KiB
Go
Raw Normal View History

package app
import (
2018-06-28 16:06:10 -07:00
"bytes"
2018-06-26 19:09:54 -07:00
"encoding/json"
"fmt"
"github.com/tendermint/tendermint/crypto"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
bapp "github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
2018-06-26 19:09:54 -07:00
"github.com/cosmos/cosmos-sdk/x/auth"
)
const (
app2Name = "App2"
)
2018-06-26 19:09:54 -07:00
var (
issuer = crypto.GenPrivKeyEd25519().PubKey().Address()
)
func NewCodec() *wire.Codec {
2018-06-26 19:09:54 -07:00
cdc := wire.NewCodec()
cdc.RegisterInterface((*sdk.Msg)(nil), nil)
cdc.RegisterConcrete(MsgSend{}, "example/MsgSend", nil)
cdc.RegisterConcrete(MsgIssue{}, "example/MsgIssue", nil)
return cdc
}
func NewApp2(logger log.Logger, db dbm.DB) *bapp.BaseApp {
cdc := NewCodec()
// Create the base application object.
app := bapp.NewBaseApp(app2Name, cdc, logger, db)
// Create a key for accessing the account store.
keyAccount := sdk.NewKVStoreKey("acc")
2018-06-28 16:41:40 -07:00
// Create a key for accessing the issue store.
keyIssue := sdk.NewKVStoreKey("issue")
2018-06-26 19:09:54 -07:00
// set antehandler function
app.SetAnteHandler(antehandler)
// Register message routes.
// Note the handler gets access to the account store.
app.Router().
AddRoute("send", handleMsgSend(keyAccount)).
2018-06-28 16:41:40 -07:00
AddRoute("issue", handleMsgIssue(keyAccount, keyIssue))
// Mount stores and load the latest state.
2018-06-28 16:41:40 -07:00
app.MountStoresIAVL(keyAccount, keyIssue)
err := app.LoadLatestVersion(keyAccount)
if err != nil {
cmn.Exit(err.Error())
}
return app
}
//------------------------------------------------------------------
// Msgs
2018-06-28 16:41:40 -07:00
// MsgIssue to allow a registered issuer
// to issue new coins.
2018-06-26 19:09:54 -07:00
type MsgIssue struct {
2018-06-28 16:06:10 -07:00
Issuer sdk.Address
Receiver sdk.Address
2018-06-28 16:06:10 -07:00
Coin sdk.Coin
2018-06-26 19:09:54 -07:00
}
2018-06-27 15:05:04 -07:00
// Implements Msg.
func (msg MsgIssue) Type() string { return "issue" }
2018-06-26 19:09:54 -07:00
2018-06-27 15:05:04 -07:00
// Implements Msg. Ensures addresses are valid and Coin is positive
2018-06-26 19:09:54 -07:00
func (msg MsgIssue) ValidateBasic() sdk.Error {
if len(msg.Issuer) == 0 {
return sdk.ErrInvalidAddress("Issuer address cannot be empty")
}
2018-06-26 19:09:54 -07:00
if len(msg.Receiver) == 0 {
return sdk.ErrInvalidAddress("Receiver address cannot be empty")
2018-06-26 19:09:54 -07:00
}
// Cannot issue zero or negative coins
if !msg.Coin.IsPositive() {
return sdk.ErrInvalidCoins("Cannot issue 0 or negative coin amounts")
}
2018-06-26 19:09:54 -07:00
return nil
}
2018-06-27 15:05:04 -07:00
// Implements Msg. Get canonical sign bytes for MsgIssue
2018-06-26 19:09:54 -07:00
func (msg MsgIssue) GetSignBytes() []byte {
bz, err := json.Marshal(msg)
if err != nil {
panic(err)
}
return bz
}
// Implements Msg. Return the signer.
func (msg MsgIssue) GetSigners() []sdk.Address {
return []sdk.Address{msg.Issuer}
}
// Returns the sdk.Tags for the message
func (msg MsgIssue) Tags() sdk.Tags {
return sdk.NewTags("issuer", []byte(msg.Issuer.String())).
AppendTag("receiver", []byte(msg.Receiver.String()))
}
//------------------------------------------------------------------
// Handler for the message
2018-06-28 17:08:38 -07:00
// Handle MsgIssue.
2018-06-28 16:41:40 -07:00
func handleMsgIssue(keyIssue *sdk.KVStoreKey, keyAcc *sdk.KVStoreKey) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
issueMsg, ok := msg.(MsgIssue)
if !ok {
2018-06-28 16:41:40 -07:00
return sdk.NewError(2, 1, "MsgIssue is malformed").Result()
}
2018-06-27 14:50:59 -07:00
// Retrieve stores
2018-06-28 16:41:40 -07:00
issueStore := ctx.KVStore(keyIssue)
accStore := ctx.KVStore(keyAcc)
2018-06-28 16:41:40 -07:00
// Handle updating coin info
if res := handleIssuer(issueStore, issueMsg.Issuer, issueMsg.Coin); !res.IsOK() {
return res
2018-06-26 19:09:54 -07:00
}
// Issue coins to receiver using previously defined handleTo function
if res := handleTo(accStore, issueMsg.Receiver, []sdk.Coin{issueMsg.Coin}); !res.IsOK() {
return res
2018-06-26 19:09:54 -07:00
}
return sdk.Result{
2018-06-27 14:50:59 -07:00
// Return result with Issue msg tags
Tags: issueMsg.Tags(),
}
}
}
2018-06-26 19:09:54 -07:00
2018-06-28 16:41:40 -07:00
func handleIssuer(store sdk.KVStore, issuer sdk.Address, coin sdk.Coin) sdk.Result {
// the issuer address is stored directly under the coin denomination
denom := []byte(coin.Denom)
2018-06-28 17:08:38 -07:00
infoBytes := store.Get(denom)
if infoBytes == nil {
2018-06-28 16:41:40 -07:00
return sdk.ErrInvalidCoins(fmt.Sprintf("Unknown coin type %s", coin.Denom)).Result()
}
2018-06-28 17:08:38 -07:00
var coinInfo coinInfo
err := json.Unmarshal(infoBytes, &coinInfo)
if err != nil {
return sdk.ErrInternal("Error when deserializing coinInfo").Result()
}
2018-06-27 14:50:59 -07:00
// Msg Issuer is not authorized to issue these coins
2018-06-28 17:08:38 -07:00
if !bytes.Equal(coinInfo.Issuer, issuer) {
return sdk.ErrUnauthorized(fmt.Sprintf("Msg Issuer cannot issue tokens: %s", coin.Denom)).Result()
}
2018-06-26 19:09:54 -07:00
return sdk.Result{}
2018-06-26 19:09:54 -07:00
}
2018-06-28 17:08:38 -07:00
// coinInfo stores meta data about a coin
type coinInfo struct {
Issuer sdk.Address `json:"issuer"`
}
//------------------------------------------------------------------
// Tx
// Simple tx to wrap the Msg.
type app2Tx struct {
sdk.Msg
2018-06-26 19:09:54 -07:00
Signatures []auth.StdSignature
}
// This tx only has one Msg.
func (tx app2Tx) GetMsgs() []sdk.Msg {
return []sdk.Msg{tx.Msg}
}
2018-06-26 19:09:54 -07:00
func (tx app2Tx) GetSignatures() []auth.StdSignature {
return tx.Signatures
}
//------------------------------------------------------------------
2018-06-28 16:41:40 -07:00
// Simple anteHandler that ensures msg signers have signed.
// Provides no replay protection.
2018-06-26 19:09:54 -07:00
func antehandler(ctx sdk.Context, tx sdk.Tx) (_ sdk.Context, _ sdk.Result, abort bool) {
appTx, ok := tx.(app2Tx)
if !ok {
// set abort boolean to true so that we don't continue to process failed tx
return ctx, sdk.ErrTxDecode("Tx must be of format app2Tx").Result(), true
}
// expect only one msg in app2Tx
msg := tx.GetMsgs()[0]
signerAddrs := msg.GetSigners()
if len(signerAddrs) != len(appTx.GetSignatures()) {
return ctx, sdk.ErrUnauthorized("Number of signatures do not match required amount").Result(), true
}
2018-06-28 16:41:40 -07:00
signBytes := msg.GetSignBytes()
2018-06-26 19:09:54 -07:00
for i, addr := range signerAddrs {
sig := appTx.GetSignatures()[i]
// check that submitted pubkey belongs to required address
2018-06-28 16:06:10 -07:00
if !bytes.Equal(sig.PubKey.Address(), addr) {
2018-06-26 19:09:54 -07:00
return ctx, sdk.ErrUnauthorized("Provided Pubkey does not match required address").Result(), true
}
// check that signature is over expected signBytes
if !sig.PubKey.VerifyBytes(signBytes, sig.Signature) {
return ctx, sdk.ErrUnauthorized("Signature verification failed").Result(), true
}
}
// authentication passed, app to continue processing by sending msg to handler
return ctx, sdk.Result{}, false
}