Skip to content

Commit 056e75c

Browse files
authored
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
2 parents 1049a26 + 8a7c490 commit 056e75c

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ FEATURES
2020
* [cli] [\#2569](https://github.com/cosmos/cosmos-sdk/pull/2569) Add commands to query validator unbondings and redelegations
2121
* [cli] [\#2524](https://github.com/cosmos/cosmos-sdk/issues/2524) Add support offline mode to `gaiacli tx sign`. Lookups are not performed if the flag `--offline` is on.
2222
* [cli] [\#2558](https://github.com/cosmos/cosmos-sdk/issues/2558) Rename --print-sigs to --validate-signatures. It now performs a complete set of sanity checks and reports to the user. Also added --print-signature-only to print the signature only, not the whole transaction.
23+
* [cli] [\#2704](https://github.com/cosmos/cosmos-sdk/pull/2704) New add-genesis-account convenience command to populate genesis.json with genesis accounts.
2324

2425
* SDK
2526
* [\#1336](https://github.com/cosmos/cosmos-sdk/issues/1336) Mechanism for SDK Users to configure their own Bech32 prefixes instead of using the default cosmos prefixes.

cmd/gaia/cmd/gaiad/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func main() {
4242
rootCmd.AddCommand(gaiaInit.CollectGenTxsCmd(ctx, cdc))
4343
rootCmd.AddCommand(gaiaInit.TestnetFilesCmd(ctx, cdc, server.AppInit{}))
4444
rootCmd.AddCommand(gaiaInit.GenTxCmd(ctx, cdc))
45+
rootCmd.AddCommand(gaiaInit.AddGenesisAccountCmd(ctx, cdc))
4546

4647
server.AddCommands(ctx, cdc, rootCmd, appInit,
4748
newApp, exportAppStateAndTMValidators)

cmd/gaia/init/genesis_accts.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package init
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/cosmos/cosmos-sdk/cmd/gaia/app"
7+
"github.com/cosmos/cosmos-sdk/codec"
8+
"github.com/cosmos/cosmos-sdk/server"
9+
sdk "github.com/cosmos/cosmos-sdk/types"
10+
"github.com/cosmos/cosmos-sdk/x/auth"
11+
"github.com/spf13/cobra"
12+
"github.com/spf13/viper"
13+
"github.com/tendermint/tendermint/libs/cli"
14+
"github.com/tendermint/tendermint/libs/common"
15+
)
16+
17+
// AddGenesisAccountCmd returns add-genesis-account cobra Command
18+
func AddGenesisAccountCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command {
19+
cmd := &cobra.Command{
20+
Use: "add-genesis-account [address] [coin][,[coin]]",
21+
Short: "Add genesis account to genesis.json",
22+
Args: cobra.ExactArgs(2),
23+
RunE: func(_ *cobra.Command, args []string) error {
24+
config := ctx.Config
25+
config.SetRoot(viper.GetString(cli.HomeFlag))
26+
27+
addr, err := sdk.AccAddressFromBech32(args[0])
28+
if err != nil {
29+
return err
30+
}
31+
coins, err := sdk.ParseCoins(args[1])
32+
if err != nil {
33+
return err
34+
}
35+
coins.Sort()
36+
37+
genFile := config.GenesisFile()
38+
if !common.FileExists(genFile) {
39+
return fmt.Errorf("%s does not exist, run `gaiad init` first", genFile)
40+
}
41+
genDoc, err := loadGenesisDoc(cdc, genFile)
42+
if err != nil {
43+
return err
44+
}
45+
46+
var appState app.GenesisState
47+
if err = cdc.UnmarshalJSON(genDoc.AppState, &appState); err != nil {
48+
return err
49+
}
50+
51+
acc := auth.NewBaseAccountWithAddress(addr)
52+
acc.Coins = coins
53+
appState.Accounts = append(appState.Accounts, app.NewGenesisAccount(&acc))
54+
55+
appStateJSON, err := cdc.MarshalJSON(appState)
56+
if err != nil {
57+
return err
58+
}
59+
60+
return ExportGenesisFile(genFile, genDoc.ChainID, nil, appStateJSON)
61+
},
62+
}
63+
64+
cmd.Flags().String(cli.HomeFlag, app.DefaultNodeHome, "node's home directory")
65+
return cmd
66+
}

0 commit comments

Comments
 (0)