2020-04-01 18:49:21 +00:00
|
|
|
package main
|
2019-11-04 20:45:02 +00:00
|
|
|
|
|
|
|
import (
|
2019-12-13 19:50:19 +00:00
|
|
|
"bufio"
|
2021-04-17 10:00:07 +00:00
|
|
|
"encoding/json"
|
2019-11-04 20:45:02 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2021-04-18 17:23:26 +00:00
|
|
|
ethcrypto "github.com/ethereum/go-ethereum/crypto"
|
2019-11-04 20:45:02 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2020-04-22 19:26:01 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2020-08-23 21:41:54 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2019-11-04 20:45:02 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/server"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-04-17 10:00:07 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
|
|
authvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
|
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
2019-11-04 20:45:02 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/genutil"
|
2021-04-17 10:00:07 +00:00
|
|
|
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
|
2019-11-04 20:45:02 +00:00
|
|
|
|
2021-04-18 17:23:26 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2020-10-06 18:57:55 +00:00
|
|
|
"github.com/cosmos/ethermint/crypto/hd"
|
2021-04-18 17:23:26 +00:00
|
|
|
chain "github.com/cosmos/ethermint/types"
|
2019-11-04 20:45:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
flagVestingStart = "vesting-start-time"
|
|
|
|
flagVestingEnd = "vesting-end-time"
|
|
|
|
flagVestingAmt = "vesting-amount"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AddGenesisAccountCmd returns add-genesis-account cobra Command.
|
2021-04-17 10:00:07 +00:00
|
|
|
func AddGenesisAccountCmd(defaultNodeHome string) *cobra.Command {
|
2019-11-04 20:45:02 +00:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "add-genesis-account [address_or_key_name] [coin][,[coin]]",
|
|
|
|
Short: "Add a genesis account to genesis.json",
|
|
|
|
Long: `Add a genesis account to genesis.json. The provided account must specify
|
|
|
|
the account address or key name and a list of initial coins. If a key name is given,
|
2020-06-08 18:33:48 +00:00
|
|
|
the address will be looked up in the local Keybase. The list of initial tokens must
|
2019-11-04 20:45:02 +00:00
|
|
|
contain valid denominations. Accounts may optionally be supplied with vesting parameters.
|
|
|
|
`,
|
|
|
|
Args: cobra.ExactArgs(2),
|
2019-12-13 19:50:19 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2021-04-17 10:00:07 +00:00
|
|
|
clientCtx := client.GetClientContextFromCmd(cmd)
|
|
|
|
depCdc := clientCtx.JSONMarshaler
|
|
|
|
cdc := depCdc.(codec.Marshaler)
|
|
|
|
|
|
|
|
serverCtx := server.GetServerContextFromCmd(cmd)
|
|
|
|
config := serverCtx.Config
|
|
|
|
|
|
|
|
config.SetRoot(clientCtx.HomeDir)
|
2019-11-04 20:45:02 +00:00
|
|
|
|
|
|
|
addr, err := sdk.AccAddressFromBech32(args[0])
|
|
|
|
if err != nil {
|
2021-04-17 10:00:07 +00:00
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
2021-04-18 17:23:26 +00:00
|
|
|
keyringBackend, err := cmd.Flags().GetString(flags.FlagKeyringBackend)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// attempt to lookup address from Keybase if no address was provided
|
|
|
|
kb, err := keyring.New(
|
2020-04-22 19:26:01 +00:00
|
|
|
sdk.KeyringServiceName(),
|
2021-04-17 10:00:07 +00:00
|
|
|
keyringBackend,
|
|
|
|
clientCtx.HomeDir,
|
2020-04-22 19:26:01 +00:00
|
|
|
inBuf,
|
2021-04-17 10:00:07 +00:00
|
|
|
hd.EthSecp256k1Option(),
|
2020-04-22 19:26:01 +00:00
|
|
|
)
|
2019-11-04 20:45:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-18 17:23:26 +00:00
|
|
|
info, err := kb.Key(args[0])
|
2019-11-04 20:45:02 +00:00
|
|
|
if err != nil {
|
2021-04-18 17:23:26 +00:00
|
|
|
return fmt.Errorf("failed to get address from Keybase: %w", err)
|
2019-11-04 20:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addr = info.GetAddress()
|
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
coins, err := sdk.ParseCoinsNormalized(args[1])
|
2019-11-04 20:45:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse coins: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-04-18 17:23:26 +00:00
|
|
|
vestingStart, err := cmd.Flags().GetInt64(flagVestingStart)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
vestingEnd, err := cmd.Flags().GetInt64(flagVestingEnd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
vestingAmtStr, err := cmd.Flags().GetString(flagVestingAmt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
vestingAmt, err := sdk.ParseCoinsNormalized(vestingAmtStr)
|
2019-11-04 20:45:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse vesting amount: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create concrete account type based on input parameters
|
2021-04-17 10:00:07 +00:00
|
|
|
var genAccount authtypes.GenesisAccount
|
|
|
|
|
|
|
|
balances := banktypes.Balance{Address: addr.String(), Coins: coins.Sort()}
|
|
|
|
baseAccount := authtypes.NewBaseAccount(addr, nil, 0, 0)
|
2019-11-04 20:45:02 +00:00
|
|
|
|
|
|
|
if !vestingAmt.IsZero() {
|
2021-04-17 10:00:07 +00:00
|
|
|
baseVestingAccount := authvesting.NewBaseVestingAccount(baseAccount, vestingAmt.Sort(), vestingEnd)
|
2020-04-22 19:26:01 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
if (balances.Coins.IsZero() && !baseVestingAccount.OriginalVesting.IsZero()) ||
|
|
|
|
baseVestingAccount.OriginalVesting.IsAnyGT(balances.Coins) {
|
2020-04-22 19:26:01 +00:00
|
|
|
return errors.New("vesting amount cannot be greater than total amount")
|
2019-11-04 20:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case vestingStart != 0 && vestingEnd != 0:
|
|
|
|
genAccount = authvesting.NewContinuousVestingAccountRaw(baseVestingAccount, vestingStart)
|
|
|
|
|
|
|
|
case vestingEnd != 0:
|
|
|
|
genAccount = authvesting.NewDelayedVestingAccountRaw(baseVestingAccount)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return errors.New("invalid vesting parameters; must supply start and end time or end time")
|
|
|
|
}
|
|
|
|
} else {
|
2021-04-18 17:23:26 +00:00
|
|
|
genAccount = &chain.EthAccount{
|
2020-04-01 18:49:21 +00:00
|
|
|
BaseAccount: baseAccount,
|
|
|
|
CodeHash: ethcrypto.Keccak256(nil),
|
|
|
|
}
|
2019-11-04 20:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := genAccount.Validate(); err != nil {
|
|
|
|
return fmt.Errorf("failed to validate new genesis account: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
genFile := config.GenesisFile()
|
2021-04-17 10:00:07 +00:00
|
|
|
appState, genDoc, err := genutiltypes.GenesisStateFromGenFile(genFile)
|
2019-11-04 20:45:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal genesis state: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
authGenState := authtypes.GetGenesisStateFromAppState(cdc, appState)
|
2019-11-04 20:45:02 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
accs, err := authtypes.UnpackAccounts(authGenState.Accounts)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get accounts from any: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if accs.Contains(addr) {
|
2019-11-04 20:45:02 +00:00
|
|
|
return fmt.Errorf("cannot add account at existing address %s", addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the new account to the set of genesis accounts and sanitize the
|
|
|
|
// accounts afterwards.
|
2021-04-17 10:00:07 +00:00
|
|
|
accs = append(accs, genAccount)
|
|
|
|
accs = authtypes.SanitizeGenesisAccounts(accs)
|
|
|
|
|
|
|
|
genAccs, err := authtypes.PackAccounts(accs)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to convert accounts into any's: %w", err)
|
|
|
|
}
|
|
|
|
authGenState.Accounts = genAccs
|
2019-11-04 20:45:02 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
authGenStateBz, err := cdc.MarshalJSON(&authGenState)
|
2019-11-04 20:45:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to marshal auth genesis state: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
appState[authtypes.ModuleName] = authGenStateBz
|
|
|
|
|
|
|
|
bankGenState := banktypes.GetGenesisStateFromAppState(depCdc, appState)
|
|
|
|
bankGenState.Balances = append(bankGenState.Balances, balances)
|
|
|
|
bankGenState.Balances = banktypes.SanitizeGenesisBalances(bankGenState.Balances)
|
|
|
|
bankGenState.Supply = bankGenState.Supply.Add(balances.Coins...)
|
|
|
|
|
|
|
|
bankGenStateBz, err := cdc.MarshalJSON(bankGenState)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to marshal bank genesis state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
appState[banktypes.ModuleName] = bankGenStateBz
|
2019-11-04 20:45:02 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
appStateJSON, err := json.Marshal(appState)
|
2019-11-04 20:45:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to marshal application genesis state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
genDoc.AppState = appStateJSON
|
|
|
|
return genutil.ExportGenesisFile(genDoc, genFile)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
|
2021-04-18 17:23:26 +00:00
|
|
|
cmd.Flags().String(flags.FlagKeyringBackend, keyring.BackendFile, "Select keyring's backend (os|file|kwallet|pass|test)")
|
2019-11-04 20:45:02 +00:00
|
|
|
cmd.Flags().String(flagVestingAmt, "", "amount of coins for vesting accounts")
|
2021-04-17 10:00:07 +00:00
|
|
|
cmd.Flags().Int64(flagVestingStart, 0, "schedule start time (unix epoch) for vesting accounts")
|
|
|
|
cmd.Flags().Int64(flagVestingEnd, 0, "schedule end time (unix epoch) for vesting accounts")
|
|
|
|
flags.AddQueryFlagsToCmd(cmd)
|
2019-11-04 20:45:02 +00:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|