Skip to content

Commit

Permalink
Automatically determine chainID, throw error if not provided (closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
cwgoes committed Apr 10, 2018
1 parent e588ebf commit 0be655b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
22 changes: 21 additions & 1 deletion client/context/viper.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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),
Expand Down
3 changes: 3 additions & 0 deletions client/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 0be655b

Please sign in to comment.