-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2704 from cosmos/alessio/utility-to-add-accts-to-…
…genesis R4R: Add small utility to add account to genesis.json after gaiad init
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package init | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/cmd/gaia/app" | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
"github.com/cosmos/cosmos-sdk/server" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/auth" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"github.com/tendermint/tendermint/libs/cli" | ||
"github.com/tendermint/tendermint/libs/common" | ||
) | ||
|
||
// AddGenesisAccountCmd returns add-genesis-account cobra Command | ||
func AddGenesisAccountCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "add-genesis-account [address] [coin][,[coin]]", | ||
Short: "Add genesis account to genesis.json", | ||
Args: cobra.ExactArgs(2), | ||
RunE: func(_ *cobra.Command, args []string) error { | ||
config := ctx.Config | ||
config.SetRoot(viper.GetString(cli.HomeFlag)) | ||
|
||
addr, err := sdk.AccAddressFromBech32(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
coins, err := sdk.ParseCoins(args[1]) | ||
if err != nil { | ||
return err | ||
} | ||
coins.Sort() | ||
|
||
genFile := config.GenesisFile() | ||
if !common.FileExists(genFile) { | ||
return fmt.Errorf("%s does not exist, run `gaiad init` first", genFile) | ||
} | ||
genDoc, err := loadGenesisDoc(cdc, genFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var appState app.GenesisState | ||
if err = cdc.UnmarshalJSON(genDoc.AppState, &appState); err != nil { | ||
return err | ||
} | ||
|
||
acc := auth.NewBaseAccountWithAddress(addr) | ||
acc.Coins = coins | ||
appState.Accounts = append(appState.Accounts, app.NewGenesisAccount(&acc)) | ||
|
||
appStateJSON, err := cdc.MarshalJSON(appState) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return ExportGenesisFile(genFile, genDoc.ChainID, nil, appStateJSON) | ||
}, | ||
} | ||
|
||
cmd.Flags().String(cli.HomeFlag, app.DefaultNodeHome, "node's home directory") | ||
return cmd | ||
} |