From 5397d775537a7e54ee566e8e1b58ec24d245bd8b Mon Sep 17 00:00:00 2001 From: Ian Norden Date: Wed, 21 Oct 2020 08:43:42 -0500 Subject: [PATCH] utils --- cmd/deriveAddress.go | 54 +++++++++++++++++++++++++++++++++++++++++++ cmd/deriveContract.go | 19 +++++++-------- cmd/keyGen.go | 5 +++- cmd/root.go | 3 +++ cmd/sendTxs.go | 4 ++++ 5 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 cmd/deriveAddress.go diff --git a/cmd/deriveAddress.go b/cmd/deriveAddress.go new file mode 100644 index 0000000..01202ce --- /dev/null +++ b/cmd/deriveAddress.go @@ -0,0 +1,54 @@ +// Copyright © 2020 Vulcanize, Inc +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import ( + "fmt" + + "github.com/sirupsen/logrus" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +// deriveAddressCmd represents the deriveAddress command +var deriveAddressCmd = &cobra.Command{ + Use: "deriveAddress", + Short: "Derive address from key pair", + Long: `Derive the account address from an pubkey/address`, + Run: func(cmd *cobra.Command, args []string) { + subCommand = cmd.CalledAs() + logWithCommand = *logrus.WithField("SubCommand", subCommand) + deriveAddress() + }, +} + +func deriveAddress() { + var addr common.Address + keyPath := viper.GetString("keyGen.path") + key, err := crypto.LoadECDSA(keyPath) + if err != nil { + logWithCommand.Fatal(err) + } + addr = crypto.PubkeyToAddress(key.PublicKey) + fmt.Println(addr.Hex()) +} + +func init() { + rootCmd.AddCommand(deriveAddressCmd) +} diff --git a/cmd/deriveContract.go b/cmd/deriveContract.go index c2814e4..ae3a382 100644 --- a/cmd/deriveContract.go +++ b/cmd/deriveContract.go @@ -18,24 +18,27 @@ package cmd import ( "fmt" + "github.com/sirupsen/logrus" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/spf13/cobra" "github.com/spf13/viper" ) -// keyGenCmd represents the keyGen command +// deriveContractCmd represents the deriveContract command var deriveContractCmd = &cobra.Command{ Use: "deriveContract", Short: "Derive contract address", Long: `Derive the contract address created from an pubkey/address and nonce`, Run: func(cmd *cobra.Command, args []string) { + subCommand = cmd.CalledAs() + logWithCommand = *logrus.WithField("SubCommand", subCommand) deriveContract() }, } func deriveContract() { - // and their .toml config bindings nonce := viper.GetUint64("keyGen.nonce") addrStr := viper.GetString("keyGen.address") var addr common.Address @@ -54,13 +57,11 @@ func deriveContract() { } func init() { - rootCmd.AddCommand(keyGenCmd) + rootCmd.AddCommand(deriveContractCmd) - keyGenCmd.PersistentFlags().Uint64("nonce", 0, "nonce to derive contract address from") - keyGenCmd.PersistentFlags().String("key-path", "", "path to public key to derive contract address from") - keyGenCmd.PersistentFlags().String("address", "", "address to derive contract address from") + deriveContractCmd.PersistentFlags().Uint64("nonce", 0, "nonce to derive contract address from") + deriveContractCmd.PersistentFlags().String("address", "", "address to derive contract address from") - viper.BindPFlag("keyGen.nonce", keyGenCmd.PersistentFlags().Lookup("nonce")) - viper.BindPFlag("keyGen.path", keyGenCmd.PersistentFlags().Lookup("key-path")) - viper.BindPFlag("keyGen.address", keyGenCmd.PersistentFlags().Lookup("address")) + viper.BindPFlag("keyGen.nonce", deriveContractCmd.PersistentFlags().Lookup("nonce")) + viper.BindPFlag("keyGen.address", deriveContractCmd.PersistentFlags().Lookup("address")) } diff --git a/cmd/keyGen.go b/cmd/keyGen.go index d0bbd14..c01bab8 100644 --- a/cmd/keyGen.go +++ b/cmd/keyGen.go @@ -17,6 +17,7 @@ package cmd import ( "github.com/ethereum/go-ethereum/crypto" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -28,6 +29,8 @@ var keyGenCmd = &cobra.Command{ Long: `Generates a new ethereum key pair for each file path provided These keys should only be used for testing purposes`, Run: func(cmd *cobra.Command, args []string) { + subCommand = cmd.CalledAs() + logWithCommand = *logrus.WithField("SubCommand", subCommand) keyGen() }, } @@ -49,6 +52,6 @@ func keyGen() { func init() { rootCmd.AddCommand(keyGenCmd) - keyGenCmd.PersistentFlags().StringArray("write-paths", nil, "file paths to write keys to; generate a key for each path provided") + keyGenCmd.PersistentFlags().StringSlice("write-paths", nil, "file paths to write keys to; generate a key for each path provided") viper.BindPFlag("keyGen.paths", keyGenCmd.PersistentFlags().Lookup("write-paths")) } diff --git a/cmd/root.go b/cmd/root.go index 802f764..9a0b1cf 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -28,6 +28,7 @@ import ( var ( cfgFile string + subCommand string logWithCommand log.Entry ) @@ -87,10 +88,12 @@ func init() { rootCmd.PersistentFlags().String("log-level", log.InfoLevel.String(), "Log level (trace, debug, info, warn, error, fatal, panic") rootCmd.PersistentFlags().String("log-file", "", "file path for logging") + rootCmd.PersistentFlags().String("key-path", "", "path to public key to derive contract address from") // and their .toml bindings viper.BindPFlag("log.file", rootCmd.PersistentFlags().Lookup("log-file")) viper.BindPFlag("log.level", rootCmd.PersistentFlags().Lookup("log-level")) + viper.BindPFlag("keyGen.path", rootCmd.PersistentFlags().Lookup("key-path")) } func initConfig() { diff --git a/cmd/sendTxs.go b/cmd/sendTxs.go index ebdec21..3f6b522 100644 --- a/cmd/sendTxs.go +++ b/cmd/sendTxs.go @@ -20,6 +20,8 @@ import ( "os/signal" "sync" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" "github.com/vulcanize/tx_spammer/pkg" ) @@ -32,6 +34,8 @@ var sendTxsCmd = &cobra.Command{ Generates txs from configuration and sends them to designated node according to set frequency and number Support standard, optimism L2, optimism L1 to L2, and EIP1559 transactions`, Run: func(cmd *cobra.Command, args []string) { + subCommand = cmd.CalledAs() + logWithCommand = *logrus.WithField("SubCommand", subCommand) sendTxs() }, } -- 2.45.2