Skip to content

Commit

Permalink
add account keygen to init
Browse files Browse the repository at this point in the history
  • Loading branch information
gartnera committed Jun 4, 2024
1 parent 726bd77 commit 7418e0f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 10 deletions.
13 changes: 9 additions & 4 deletions cmd/zetae2e/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func NewInitCmd() *cobra.Command {
var InitCmd = &cobra.Command{
Use: "init",
Short: "initialize config file for e2e tests",
Run: initConfig,
RunE: initConfig,
}

InitCmd.Flags().StringVar(&initConf.RPCs.EVM, "ethURL", "http://eth:8545", "--ethURL http://eth:8545")
Expand All @@ -33,9 +33,14 @@ func NewInitCmd() *cobra.Command {
return InitCmd
}

func initConfig(_ *cobra.Command, _ []string) {
err := config.WriteConfig(configFile, initConf)
func initConfig(_ *cobra.Command, _ []string) error {
err := initConf.GenerateKeys()
if err != nil {
fmt.Printf("error writing config file: %s", err.Error())
return fmt.Errorf("generating keys: %w", err)
}
err = config.WriteConfig(configFile, initConf)
if err != nil {
return fmt.Errorf("writing config: %w", err)
}
return nil
}
66 changes: 64 additions & 2 deletions e2e/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"os"

"github.com/btcsuite/btcd/chaincfg"
"github.com/cosmos/cosmos-sdk/types/bech32"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"gopkg.in/yaml.v2"
)
Expand All @@ -24,8 +26,9 @@ type Config struct {

// Account contains configuration for an account
type Account struct {
RawEVMAddress string `yaml:"evm_address"`
RawPrivateKey string `yaml:"private_key"`
RawBech32Address string `yaml:"bech32_address"`
RawEVMAddress string `yaml:"evm_address"`
RawPrivateKey string `yaml:"private_key"`
}

// Accounts are the required accounts to run any e2e test
Expand Down Expand Up @@ -198,6 +201,44 @@ func (c Config) Validate() error {
return nil

Check warning on line 201 in e2e/config/config.go

View check run for this annotation

Codecov / codecov/patch

e2e/config/config.go#L201

Added line #L201 was not covered by tests
}

// GenerateKeys generates new key pairs for all accounts
func (c *Config) GenerateKeys() error {
var err error
c.Accounts.Deployer, err = generateAccount()
if err != nil {
return err

Check warning on line 209 in e2e/config/config.go

View check run for this annotation

Codecov / codecov/patch

e2e/config/config.go#L209

Added line #L209 was not covered by tests
}
c.AdditionalAccounts.UserERC20, err = generateAccount()
if err != nil {
return err

Check warning on line 213 in e2e/config/config.go

View check run for this annotation

Codecov / codecov/patch

e2e/config/config.go#L213

Added line #L213 was not covered by tests
}
c.AdditionalAccounts.UserZetaTest, err = generateAccount()
if err != nil {
return err

Check warning on line 217 in e2e/config/config.go

View check run for this annotation

Codecov / codecov/patch

e2e/config/config.go#L217

Added line #L217 was not covered by tests
}
c.AdditionalAccounts.UserZEVMMPTest, err = generateAccount()
if err != nil {
return err

Check warning on line 221 in e2e/config/config.go

View check run for this annotation

Codecov / codecov/patch

e2e/config/config.go#L221

Added line #L221 was not covered by tests
}
c.AdditionalAccounts.UserBitcoin, err = generateAccount()
if err != nil {
return err

Check warning on line 225 in e2e/config/config.go

View check run for this annotation

Codecov / codecov/patch

e2e/config/config.go#L225

Added line #L225 was not covered by tests
}
c.AdditionalAccounts.UserEther, err = generateAccount()
if err != nil {
return err

Check warning on line 229 in e2e/config/config.go

View check run for this annotation

Codecov / codecov/patch

e2e/config/config.go#L229

Added line #L229 was not covered by tests
}
c.AdditionalAccounts.UserMisc, err = generateAccount()
if err != nil {
return err

Check warning on line 233 in e2e/config/config.go

View check run for this annotation

Codecov / codecov/patch

e2e/config/config.go#L233

Added line #L233 was not covered by tests
}
c.AdditionalAccounts.UserAdmin, err = generateAccount()
if err != nil {
return err

Check warning on line 237 in e2e/config/config.go

View check run for this annotation

Codecov / codecov/patch

e2e/config/config.go#L237

Added line #L237 was not covered by tests
}
return nil
}

func (a Account) EVMAddress() ethcommon.Address {
return ethcommon.HexToAddress(a.RawEVMAddress)

Check warning on line 243 in e2e/config/config.go

View check run for this annotation

Codecov / codecov/patch

e2e/config/config.go#L242-L243

Added lines #L242 - L243 were not covered by tests
}
Expand Down Expand Up @@ -247,3 +288,24 @@ func (bnt BitcoinNetworkType) GetParams() (chaincfg.Params, error) {
return chaincfg.Params{}, fmt.Errorf("invalid bitcoin params %s", bnt)
}
}

func generateAccount() (Account, error) {
privateKey, err := crypto.GenerateKey()
if err != nil {
return Account{}, fmt.Errorf("generating private key: %w", err)

Check warning on line 295 in e2e/config/config.go

View check run for this annotation

Codecov / codecov/patch

e2e/config/config.go#L295

Added line #L295 was not covered by tests
}
// encode private key and strip 0x prefix
encodedPrivateKey := hexutil.Encode(crypto.FromECDSA(privateKey))[2:]

evmAddress := crypto.PubkeyToAddress(privateKey.PublicKey)
bech32Address, err := bech32.ConvertAndEncode("zeta", evmAddress.Bytes())
if err != nil {
return Account{}, fmt.Errorf("bech32 convert and encode: %w", err)

Check warning on line 303 in e2e/config/config.go

View check run for this annotation

Codecov / codecov/patch

e2e/config/config.go#L303

Added line #L303 was not covered by tests
}

return Account{
RawEVMAddress: evmAddress.String(),
RawPrivateKey: encodedPrivateKey,
RawBech32Address: bech32Address,
}, nil
}
17 changes: 13 additions & 4 deletions e2e/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ import (
// TestAdditionalAccountsSlicesSynced ensures than if any new accounts are added the
// AccountsSlice() function is updated
func TestConfigAdditionalAccountsSliceSynced(t *testing.T) {
accountsType := reflect.TypeOf(AdditionalAccounts{})
numberOfAccounts := accountsType.NumField()
additionalAccountsType := reflect.TypeOf(AdditionalAccounts{})
numberOfAdditionalAccounts := additionalAccountsType.NumField()

require.Greater(t, numberOfAccounts, 0)
require.Greater(t, numberOfAdditionalAccounts, 0)

require.Len(t, Config{}.AdditionalAccounts.AsSlice(), numberOfAccounts)
conf := Config{}
err := conf.GenerateKeys()
require.NoError(t, err)

additionalAccountsSlice := conf.AdditionalAccounts.AsSlice()
require.Len(t, additionalAccountsSlice, numberOfAdditionalAccounts)

for _, account := range additionalAccountsSlice {
require.NoError(t, account.Validate())
}
}

func TestConfigInvalidAccount(t *testing.T) {
Expand Down

0 comments on commit 7418e0f

Please sign in to comment.