Merge pull request #5 from vulcanize/utils

keyGen utils
This commit is contained in:
Ian Norden 2020-10-21 08:59:38 -05:00 committed by GitHub
commit 58c258d7c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 10 deletions

54
cmd/deriveAddress.go Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
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)
}

View File

@ -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"))
}

View File

@ -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"))
}

View File

@ -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() {

View File

@ -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()
},
}