Automatically determine chainID, throw error if not provided (closes #810)
This commit is contained in:
parent
e588ebfa9a
commit
0be655b122
@ -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),
|
||||
|
||||
@ -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,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user