cosmos-sdk/client/context/helpers.go

348 lines
8.3 KiB
Go
Raw Normal View History

2018-04-18 12:39:32 -07:00
package context
2018-03-03 11:07:50 -08:00
import (
"fmt"
"github.com/tendermint/tendermint/libs/common"
2018-03-03 11:07:50 -08:00
"github.com/pkg/errors"
"github.com/cosmos/cosmos-sdk/wire"
2018-05-23 19:26:54 -07:00
"github.com/cosmos/cosmos-sdk/x/auth"
cmn "github.com/tendermint/tendermint/libs/common"
2018-03-03 11:07:50 -08:00
rpcclient "github.com/tendermint/tendermint/rpc/client"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Broadcast the transaction bytes to Tendermint
2018-03-30 05:57:53 -07:00
func (ctx CoreContext) BroadcastTx(tx []byte) (*ctypes.ResultBroadcastTxCommit, error) {
2018-03-03 11:07:50 -08:00
2018-03-30 05:57:53 -07:00
node, err := ctx.GetNode()
2018-03-03 11:07:50 -08:00
if err != nil {
return nil, err
}
res, err := node.BroadcastTxCommit(tx)
if err != nil {
return res, err
}
if res.CheckTx.Code != uint32(0) {
return res, errors.Errorf("checkTx failed: (%d) %s",
2018-03-03 11:07:50 -08:00
res.CheckTx.Code,
res.CheckTx.Log)
}
if res.DeliverTx.Code != uint32(0) {
return res, errors.Errorf("deliverTx failed: (%d) %s",
2018-03-03 11:07:50 -08:00
res.DeliverTx.Code,
res.DeliverTx.Log)
}
return res, err
}
// Broadcast the transaction bytes to Tendermint
func (ctx CoreContext) BroadcastTxAsync(tx []byte) (*ctypes.ResultBroadcastTx, error) {
node, err := ctx.GetNode()
if err != nil {
return nil, err
}
res, err := node.BroadcastTxAsync(tx)
if err != nil {
return res, err
}
return res, err
}
// Query information about the connected node
func (ctx CoreContext) Query(path string) (res []byte, err error) {
return ctx.query(path, nil)
}
// QueryStore from Tendermint with the provided key and storename
func (ctx CoreContext) QueryStore(key cmn.HexBytes, storeName string) (res []byte, err error) {
return ctx.queryStore(key, storeName, "key")
}
// Query from Tendermint with the provided storename and subspace
2018-05-09 15:24:51 -07:00
func (ctx CoreContext) QuerySubspace(cdc *wire.Codec, subspace []byte, storeName string) (res []sdk.KVPair, err error) {
resRaw, err := ctx.queryStore(subspace, storeName, "subspace")
if err != nil {
return res, err
}
cdc.MustUnmarshalBinary(resRaw, &res)
return
}
// Query from Tendermint with the provided storename and path
func (ctx CoreContext) query(path string, key common.HexBytes) (res []byte, err error) {
2018-03-30 05:57:53 -07:00
node, err := ctx.GetNode()
2018-03-03 11:07:50 -08:00
if err != nil {
return res, err
}
opts := rpcclient.ABCIQueryOptions{
2018-03-30 05:57:53 -07:00
Height: ctx.Height,
Trusted: ctx.TrustNode,
2018-03-03 11:07:50 -08:00
}
result, err := node.ABCIQueryWithOptions(path, key, opts)
if err != nil {
return res, err
}
resp := result.Response
if resp.Code != uint32(0) {
return res, errors.Errorf("query failed: (%d) %s", resp.Code, resp.Log)
2018-03-03 11:07:50 -08:00
}
return resp.Value, nil
}
// Query from Tendermint with the provided storename and path
func (ctx CoreContext) queryStore(key cmn.HexBytes, storeName, endPath string) (res []byte, err error) {
path := fmt.Sprintf("/store/%s/%s", storeName, endPath)
return ctx.query(path, key)
}
2018-03-03 11:07:50 -08:00
// Get the from address from the name flag
2018-07-06 00:06:53 -07:00
func (ctx CoreContext) GetFromAddress() (from sdk.AccAddress, err error) {
2018-03-03 11:07:50 -08:00
keybase, err := keys.GetKeyBase()
if err != nil {
return nil, err
}
2018-03-30 05:57:53 -07:00
name := ctx.FromAddressName
2018-03-03 11:41:43 -08:00
if name == "" {
2018-03-30 05:57:53 -07:00
return nil, errors.Errorf("must provide a from address name")
2018-03-03 11:41:43 -08:00
}
2018-03-03 11:07:50 -08:00
info, err := keybase.Get(name)
if err != nil {
return nil, errors.Errorf("no key for: %s", name)
2018-03-03 11:07:50 -08:00
}
2018-07-06 00:06:53 -07:00
return sdk.AccAddress(info.GetPubKey().Address()), nil
2018-03-03 11:07:50 -08:00
}
// sign and build the transaction from the msg
func (ctx CoreContext) SignAndBuild(name, passphrase string, msgs []sdk.Msg, cdc *wire.Codec) ([]byte, error) {
// build the Sign Messsage from the Standard Message
2018-03-30 05:57:53 -07:00
chainID := ctx.ChainID
if chainID == "" {
return nil, errors.Errorf("chain ID required but not specified")
}
accnum := ctx.AccountNumber
2018-03-30 05:57:53 -07:00
sequence := ctx.Sequence
memo := ctx.Memo
2018-06-29 08:50:32 -07:00
fee := sdk.Coin{}
if ctx.Fee != "" {
parsedFee, err := sdk.ParseCoin(ctx.Fee)
if err != nil {
return nil, err
}
fee = parsedFee
2018-06-29 06:37:14 -07:00
}
2018-05-23 19:26:54 -07:00
signMsg := auth.StdSignMsg{
ChainID: chainID,
AccountNumber: accnum,
Sequence: sequence,
Msgs: msgs,
Memo: memo,
2018-06-29 06:37:14 -07:00
Fee: auth.NewStdFee(ctx.Gas, fee), // TODO run simulate to estimate gas?
}
2018-03-03 11:07:50 -08:00
keybase, err := keys.GetKeyBase()
if err != nil {
return nil, err
}
// sign and build
2018-03-14 05:11:19 -07:00
bz := signMsg.Bytes()
2018-03-17 17:42:18 -07:00
2018-03-03 11:07:50 -08:00
sig, pubkey, err := keybase.Sign(name, passphrase, bz)
if err != nil {
return nil, err
}
2018-05-23 19:26:54 -07:00
sigs := []auth.StdSignature{{
PubKey: pubkey,
Signature: sig,
AccountNumber: accnum,
Sequence: sequence,
2018-03-03 11:07:50 -08:00
}}
// marshal bytes
tx := auth.NewStdTx(signMsg.Msgs, signMsg.Fee, sigs, memo)
2018-03-03 11:07:50 -08:00
return cdc.MarshalBinary(tx)
}
// sign and build the transaction from the msg
func (ctx CoreContext) ensureSignBuild(name string, msgs []sdk.Msg, cdc *wire.Codec) (tyBytes []byte, err error) {
err = EnsureAccountExists(ctx, name)
if err != nil {
return nil, err
}
ctx, err = EnsureAccountNumber(ctx)
if err != nil {
return nil, err
}
2018-04-18 21:49:24 -07:00
// default to next sequence number if none provided
ctx, err = EnsureSequence(ctx)
if err != nil {
return nil, err
}
var txBytes []byte
keybase, err := keys.GetKeyBase()
if err != nil {
return nil, err
}
info, err := keybase.Get(name)
if err != nil {
return nil, err
}
var passphrase string
// Only need a passphrase for locally-stored keys
if info.GetType() == "local" {
passphrase, err = ctx.GetPassphraseFromStdin(name)
if err != nil {
return nil, fmt.Errorf("Error fetching passphrase: %v", err)
}
}
txBytes, err = ctx.SignAndBuild(name, passphrase, msgs, cdc)
if err != nil {
return nil, fmt.Errorf("Error signing transaction: %v", err)
}
2018-03-03 11:07:50 -08:00
return txBytes, err
}
// sign and build the transaction from the msg
2018-07-05 22:19:50 -07:00
func (ctx CoreContext) EnsureSignBuildBroadcast(name string, msgs []sdk.Msg, cdc *wire.Codec) (err error) {
txBytes, err := ctx.ensureSignBuild(name, msgs, cdc)
2018-03-03 11:07:50 -08:00
if err != nil {
return err
2018-03-03 11:07:50 -08:00
}
2018-07-05 20:15:40 -07:00
if ctx.Async {
res, err := ctx.BroadcastTxAsync(txBytes)
2018-07-05 20:42:41 -07:00
if err != nil {
return err
}
if ctx.JSON {
type toJSON struct {
TxHash string
}
valueToJSON := toJSON{res.Hash.String()}
JSON, err := cdc.MarshalJSON(valueToJSON)
if err != nil {
return err
}
fmt.Println(string(JSON))
} else {
fmt.Println("Async tx sent. tx hash: ", res.Hash.String())
}
return nil
}
res, err := ctx.BroadcastTx(txBytes)
2018-07-05 20:42:41 -07:00
if err != nil {
return err
}
if ctx.JSON {
2018-07-05 22:19:50 -07:00
// Since JSON is intended for automated scripts, always include response in JSON mode
2018-07-05 20:42:41 -07:00
type toJSON struct {
Height int64
TxHash string
Response string
}
2018-07-05 22:19:50 -07:00
valueToJSON := toJSON{res.Height, res.Hash.String(), fmt.Sprintf("%+v", res.DeliverTx)}
2018-07-05 20:42:41 -07:00
JSON, err := cdc.MarshalJSON(valueToJSON)
if err != nil {
return err
}
fmt.Println(string(JSON))
return nil
}
2018-07-05 22:19:50 -07:00
if ctx.PrintResponse {
fmt.Printf("Committed at block %d. Hash: %s Response:%+v \n", res.Height, res.Hash.String(), res.DeliverTx)
} else {
fmt.Printf("Committed at block %d. Hash: %s \n", res.Height, res.Hash.String())
}
2018-07-05 20:42:41 -07:00
return nil
}
// get the next sequence for the account address
func (ctx CoreContext) GetAccountNumber(address []byte) (int64, error) {
if ctx.Decoder == nil {
return 0, errors.New("accountDecoder required but not provided")
}
res, err := ctx.QueryStore(auth.AddressStoreKey(address), ctx.AccountStore)
if err != nil {
return 0, err
}
if len(res) == 0 {
fmt.Printf("No account found. Returning 0.\n")
return 0, err
}
account, err := ctx.Decoder(res)
if err != nil {
panic(err)
}
return account.GetAccountNumber(), nil
}
// get the next sequence for the account address
2018-04-18 12:39:32 -07:00
func (ctx CoreContext) NextSequence(address []byte) (int64, error) {
if ctx.Decoder == nil {
return 0, errors.New("accountDecoder required but not provided")
}
res, err := ctx.QueryStore(auth.AddressStoreKey(address), ctx.AccountStore)
if err != nil {
return 0, err
}
2018-05-03 14:19:56 -07:00
if len(res) == 0 {
fmt.Printf("No account found, defaulting to sequence 0\n")
return 0, err
}
2018-04-18 12:39:32 -07:00
account, err := ctx.Decoder(res)
if err != nil {
panic(err)
}
return account.GetSequence(), nil
}
// get passphrase from std input
2018-03-30 05:57:53 -07:00
func (ctx CoreContext) GetPassphraseFromStdin(name string) (pass string, err error) {
buf := client.BufferStdin()
prompt := fmt.Sprintf("Password to sign with '%s':", name)
return client.GetPassword(prompt, buf)
}
2018-03-30 05:57:53 -07:00
// GetNode prepares a simple rpc.Client
func (ctx CoreContext) GetNode() (rpcclient.Client, error) {
if ctx.Client == nil {
return nil, errors.New("must define node URI")
2018-03-30 05:57:53 -07:00
}
return ctx.Client, nil
2018-03-30 05:57:53 -07:00
}