Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,6 @@ func NewPolycliCommand() *cobra.Command {
dockerlogger.Cmd,
contract.Cmd,
)

return cmd
}
66 changes: 66 additions & 0 deletions cmd/signer/create/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package create

import (
_ "embed"
"encoding/hex"
"fmt"

"github.com/0xPolygon/polygon-cli/signer"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/crypto"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

//go:embed usage.md
var usage string

var CreateCmd = &cobra.Command{
Use: "create",
Short: "Create a new key.",
Long: usage,
Args: cobra.NoArgs,
PreRunE: signer.SanityCheck,
RunE: func(cmd *cobra.Command, args []string) error {
opts := signer.InputOpts
if opts.Keystore == "" && opts.KMS == "" {
log.Info().Msg("Generating new private hex key and writing to stdout")
pk, err := crypto.GenerateKey()
if err != nil {
return err
}
k := hex.EncodeToString(crypto.FromECDSA(pk))
fmt.Println(k)
return nil
}
if opts.Keystore != "" {
ks := keystore.NewKeyStore(opts.Keystore, keystore.StandardScryptN, keystore.StandardScryptP)
pk, err := crypto.GenerateKey()
if err != nil {
return err
}
password, err := signer.GetKeystorePassword()
if err != nil {
return err
}
acc, err := ks.ImportECDSA(pk, password)
if err != nil {
return err
}
log.Info().Str("address", acc.Address.String()).Msg("imported new account")
return nil
}
if opts.KMS == "GCP" {
gcpKMS := signer.GCPKMS{}
err := gcpKMS.CreateKeyRing(cmd.Context())
if err != nil {
return err
}
err = gcpKMS.CreateKey(cmd.Context())
if err != nil {
return err
}
}
return nil
},
}
File renamed without changes.
51 changes: 51 additions & 0 deletions cmd/signer/import/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package importcmd

import (
_ "embed"
"fmt"

"github.com/0xPolygon/polygon-cli/signer"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/crypto"
"github.com/spf13/cobra"
)

//go:embed usage.md
var usage string

var ImportCmd = &cobra.Command{
Use: "import",
Short: "Import a private key into the keyring / keystore.",
Long: usage,
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := signer.SanityCheck(cmd, args); err != nil {
return err
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
opts := signer.InputOpts
if opts.Keystore != "" {
ks := keystore.NewKeyStore(opts.Keystore, keystore.StandardScryptN, keystore.StandardScryptP)
pk, err := crypto.HexToECDSA(opts.PrivateKey)
if err != nil {
return err
}
pass, err := signer.GetKeystorePassword()
if err != nil {
return err
}
_, err = ks.ImportECDSA(pk, pass)
return err
}
if opts.KMS == "GCP" {
gcpKMS := signer.GCPKMS{}
if err := gcpKMS.CreateImportJob(cmd.Context()); err != nil {
return err
}
return gcpKMS.ImportKey(cmd.Context())
}
return fmt.Errorf("unable to import key")
},
}
File renamed without changes.
38 changes: 38 additions & 0 deletions cmd/signer/list/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package list

import (
_ "embed"
"fmt"

"github.com/0xPolygon/polygon-cli/signer"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

//go:embed usage.md
var usage string

var ListCmd = &cobra.Command{
Use: "list",
Short: "List the keys in the keyring / keystore.",
Long: usage,
Args: cobra.NoArgs,
PreRunE: signer.SanityCheck,
RunE: func(cmd *cobra.Command, args []string) error {
opts := signer.InputOpts
if opts.Keystore != "" {
ks := keystore.NewKeyStore(opts.Keystore, keystore.StandardScryptN, keystore.StandardScryptP)
accounts := ks.Accounts()
for idx, a := range accounts {
log.Info().Str("account", a.Address.String()).Int("index", idx).Msg("Account")
}
return nil
}
if opts.KMS == "GCP" {
gcpKMS := signer.GCPKMS{}
return gcpKMS.ListKeyRingKeys(cmd.Context())
}
return fmt.Errorf("unable to list accounts")
},
}
File renamed without changes.
88 changes: 88 additions & 0 deletions cmd/signer/sign/sign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package sign

import (
_ "embed"
"fmt"
"os"

"github.com/0xPolygon/polygon-cli/gethkeystore"
"github.com/0xPolygon/polygon-cli/signer"
accounts2 "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/crypto"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

//go:embed usage.md
var usage string

var SignCmd = &cobra.Command{
Use: "sign",
Short: "Sign tx data.",
Long: usage,
Args: cobra.NoArgs,
PreRunE: signer.SanityCheck,
RunE: func(cmd *cobra.Command, args []string) error {
opts := signer.InputOpts
if opts.Keystore == "" && opts.PrivateKey == "" && opts.KMS == "" {
return fmt.Errorf("no valid keystore was specified")
}

if opts.Keystore != "" {
ks := keystore.NewKeyStore(opts.Keystore, keystore.StandardScryptN, keystore.StandardScryptP)
accounts := ks.Accounts()
var accountToUnlock *accounts2.Account
for _, a := range accounts {
if a.Address.String() == opts.KeyID {
accountToUnlock = &a
break
}
}
if accountToUnlock == nil {
accountStrings := ""
for _, a := range accounts {
accountStrings += a.Address.String() + " "
}
return fmt.Errorf("account with address %s not found in list [%s]", opts.KeyID, accountStrings)
}
password, err := signer.GetKeystorePassword()
if err != nil {
return err
}

err = ks.Unlock(*accountToUnlock, password)
if err != nil {
return err
}

log.Info().Str("path", accountToUnlock.URL.Path).Msg("Unlocked account")
encryptedKey, err := os.ReadFile(accountToUnlock.URL.Path)
if err != nil {
return err
}
privKey, err := gethkeystore.DecryptKeystoreFile(encryptedKey, password)
if err != nil {
return err
}
return signer.Sign(privKey)
}

if opts.PrivateKey != "" {
pk, err := crypto.HexToECDSA(opts.PrivateKey)
if err != nil {
return err
}
return signer.Sign(pk)
}
if opts.KMS == "GCP" {
tx, err := signer.GetTxDataToSign()
if err != nil {
return err
}
gcpKMS := signer.GCPKMS{}
return gcpKMS.Sign(cmd.Context(), tx)
}
return fmt.Errorf("not implemented")
},
}
File renamed without changes.
Loading