diff --git a/client/context/viper.go b/client/context/viper.go index 750a37c616..4264776df0 100644 --- a/client/context/viper.go +++ b/client/context/viper.go @@ -1,9 +1,13 @@ package context import ( + "encoding/json" "github.com/spf13/viper" + "io/ioutil" + tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" rpcclient "github.com/tendermint/tendermint/rpc/client" + tmtypes "github.com/tendermint/tendermint/types" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/core" @@ -15,8 +19,24 @@ func NewCoreContextFromViper() core.CoreContext { if nodeURI != "" { rpc = rpcclient.NewHTTP(nodeURI, "/websocket") } + chainID := viper.GetString(client.FlagChainID) + // if chain ID is not specified manually, read chain ID from genesis file if present + if chainID == "" { + cfg, err := tcmd.ParseConfig() + if err == nil { + genesisFile := cfg.GenesisFile() + bz, err := ioutil.ReadFile(genesisFile) + if err == nil { + var doc tmtypes.GenesisDoc + err = json.Unmarshal(bz, &doc) + if err == nil { + chainID = doc.ChainID + } + } + } + } return core.CoreContext{ - ChainID: viper.GetString(client.FlagChainID), + ChainID: chainID, Height: viper.GetInt64(client.FlagHeight), TrustNode: viper.GetBool(client.FlagTrustNode), FromAddressName: viper.GetString(client.FlagName), diff --git a/client/core/core.go b/client/core/core.go index a5c7b340c7..a39472c040 100644 --- a/client/core/core.go +++ b/client/core/core.go @@ -91,6 +91,9 @@ func (ctx CoreContext) SignAndBuild(name, passphrase string, msg sdk.Msg, cdc *w // build the Sign Messsage from the Standard Message chainID := ctx.ChainID + if chainID == "" { + return nil, errors.Errorf("Chain ID required but not specified") + } sequence := ctx.Sequence signMsg := sdk.StdSignMsg{ ChainID: chainID,