diff --git a/modules/etc/commands/query.go b/modules/etc/commands/query.go new file mode 100644 index 000000000..4d0a3cfee --- /dev/null +++ b/modules/etc/commands/query.go @@ -0,0 +1,44 @@ +package commands + +import ( + "encoding/hex" + + "github.com/spf13/cobra" + "github.com/spf13/viper" + + cmn "github.com/tendermint/tmlibs/common" + + "github.com/tendermint/basecoin/client/commands" + "github.com/tendermint/basecoin/client/commands/query" + "github.com/tendermint/basecoin/modules/etc" + "github.com/tendermint/basecoin/stack" +) + +// EtcQueryCmd - command to query raw data +var EtcQueryCmd = &cobra.Command{ + Use: "etc [key]", + Short: "Get data stored under key in etc", + RunE: commands.RequireInit(etcQueryCmd), +} + +func etcQueryCmd(cmd *cobra.Command, args []string) error { + var res etc.Data + + arg, err := commands.GetOneArg(args, "key") + if err != nil { + return err + } + key, err := hex.DecodeString(cmn.StripHex(arg)) + if err != nil { + return err + } + + key = stack.PrefixedKey(etc.Name, key) + prove := !viper.GetBool(commands.FlagTrustNode) + height, err := query.GetParsed(key, &res, prove) + if err != nil { + return err + } + + return query.OutputProof(res, height) +} diff --git a/modules/etc/commands/tx.go b/modules/etc/commands/tx.go new file mode 100644 index 000000000..92a444fa9 --- /dev/null +++ b/modules/etc/commands/tx.go @@ -0,0 +1,62 @@ +package commands + +import ( + "github.com/spf13/cobra" + + "github.com/tendermint/basecoin/client/commands" + "github.com/tendermint/basecoin/client/commands/txs" + "github.com/tendermint/basecoin/modules/etc" +) + +// SetTxCmd is CLI command to set data +var SetTxCmd = &cobra.Command{ + Use: "set", + Short: "Sets a key value pair", + RunE: commands.RequireInit(setTxCmd), +} + +// RemoveTxCmd is CLI command to remove data +var RemoveTxCmd = &cobra.Command{ + Use: "Remove", + Short: "Removes a key value pair", + RunE: commands.RequireInit(removeTxCmd), +} + +//nolint +const ( + FlagKey = "key" + FlagValue = "value" +) + +func init() { + SetTxCmd.Flags().String(FlagKey, "", "Key to store data under (hex)") + SetTxCmd.Flags().String(FlagValue, "", "Data to store (hex)") + + RemoveTxCmd.Flags().String(FlagKey, "", "Key under which to remove data (hex)") +} + +// setTxCmd creates a SetTx, wraps, signs, and delivers it +func setTxCmd(cmd *cobra.Command, args []string) error { + key, err := commands.ParseHexFlag(FlagKey) + if err != nil { + return err + } + value, err := commands.ParseHexFlag(FlagValue) + if err != nil { + return err + } + + tx := etc.NewSetTx(key, value) + return txs.DoTx(tx) +} + +// removeTxCmd creates a RemoveTx, wraps, signs, and delivers it +func removeTxCmd(cmd *cobra.Command, args []string) error { + key, err := commands.ParseHexFlag(FlagKey) + if err != nil { + return err + } + + tx := etc.NewRemoveTx(key) + return txs.DoTx(tx) +} diff --git a/modules/etc/tx.go b/modules/etc/tx.go index 706fcf181..12921443d 100644 --- a/modules/etc/tx.go +++ b/modules/etc/tx.go @@ -26,6 +26,10 @@ type SetTx struct { Value data.Bytes `json:"value"` } +func NewSetTx(key, value []byte) basecoin.Tx { + return SetTx{Key: key, Value: value}.Wrap() +} + // Wrap - fulfills TxInner interface func (t SetTx) Wrap() basecoin.Tx { return basecoin.Tx{t} @@ -44,6 +48,10 @@ type RemoveTx struct { Key data.Bytes `json:"key"` } +func NewRemoveTx(key []byte) basecoin.Tx { + return RemoveTx{Key: key}.Wrap() +} + // Wrap - fulfills TxInner interface func (t RemoveTx) Wrap() basecoin.Tx { return basecoin.Tx{t}