Skip to content

Commit

Permalink
Merge pull request #47 from maticnetwork/jhilliard/eth-wallet-parse
Browse files Browse the repository at this point in the history
Extract keys from go-ethereum formatted file
  • Loading branch information
praetoriansentry authored Mar 4, 2023
2 parents 917d898 + ea6ec6d commit b40ec0b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ go run main.go abi < ../zkevm-node/etherman/smartcontracts/abi/polygonzkevm.abi

rawData, err := getInputData(cmd, args)
if err != nil {
return nil
return err
}
buf := bytes.NewReader(rawData)
abi, err := gethabi.JSON(buf)
Expand Down
97 changes: 97 additions & 0 deletions cmd/parseethwallet/parseethwallet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package parseethwallet

import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"golang.org/x/crypto/sha3"
"io"
"os"
"strings"

"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/spf13/cobra"
)

var (
inputFileName *string
inputPassword *string
)

type plainKeyJSON struct {
Address string `json:"address"`
Crypto keystore.CryptoJSON `json:"crypto"`
}
type outKey struct {
Address string
PublicKey string
PrivateKey string
}

var ParseETHWalletCmd = &cobra.Command{
Use: "parseethwallet --file UTC--2023-03-03T17-26-43.371893268Z--1652e7b47af367372a7a6d7d6fe5037702860c6d",
Short: "A simple tool to extract the private key from an eth wallet",
Long: `
`,
RunE: func(cmd *cobra.Command, args []string) error {
// it would be nice to have a generic reader

rawData, err := getInputData(cmd, args)
if err != nil {
return err
}
k := new(plainKeyJSON)
err = json.Unmarshal(rawData, &k)
if err != nil {
return err
}
d, err := keystore.DecryptDataV3(k.Crypto, *inputPassword)
if err != nil {
return err
}
ok := toOutputKey(d)
outData, err := json.Marshal(ok)
if err != nil {
return err
}
fmt.Println(string(outData))
return nil
},
Args: func(cmd *cobra.Command, args []string) error {
return nil
},
}

func init() {
flagSet := ParseETHWalletCmd.PersistentFlags()
inputFileName = flagSet.String("file", "", "Provide a file with the key information ")
inputPassword = flagSet.String("password", "", "An optional password use to unlock the key")
}

func getInputData(cmd *cobra.Command, args []string) ([]byte, error) {
if inputFileName != nil && *inputFileName != "" {
return os.ReadFile(*inputFileName)
}

if len(args) > 1 {
concat := strings.Join(args[1:], " ")
return []byte(concat), nil
}

return io.ReadAll(os.Stdin)
}

func toOutputKey(key []byte) outKey {
ok := outKey{}
ok.PrivateKey = hex.EncodeToString(key)
curve := secp256k1.S256()
x1, y1 := curve.ScalarBaseMult(key)
concat := append(x1.Bytes(), y1.Bytes()...)
h := sha3.NewLegacyKeccak256()
h.Write(concat)
b := h.Sum(nil)
ok.Address = fmt.Sprintf("0x%s", hex.EncodeToString(b[len(b)-20:]))
ok.PublicKey = hex.EncodeToString(concat)
return ok
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cmd
import (
"fmt"
"github.com/maticnetwork/polygon-cli/cmd/fork"
"github.com/maticnetwork/polygon-cli/cmd/parseethwallet"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -85,6 +86,7 @@ func init() {
rootCmd.AddCommand(version.VersionCmd)
rootCmd.AddCommand(wallet.WalletCmd)
rootCmd.AddCommand(fork.ForkCmd)
rootCmd.AddCommand(parseethwallet.ParseETHWalletCmd)
}

// initConfig reads in config file and ENV variables if set.
Expand Down

0 comments on commit b40ec0b

Please sign in to comment.