Skip to content

Commit 8abc054

Browse files
authored
chore: refactor signer command into subcommands (#827)
* chore: refactor signer command into subcommands * chore: move signer to shared package
1 parent a786c6a commit 8abc054

File tree

11 files changed

+846
-838
lines changed

11 files changed

+846
-838
lines changed

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,6 @@ func NewPolycliCommand() *cobra.Command {
162162
dockerlogger.Cmd,
163163
contract.Cmd,
164164
)
165+
165166
return cmd
166167
}

cmd/signer/create/create.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package create
2+
3+
import (
4+
_ "embed"
5+
"encoding/hex"
6+
"fmt"
7+
8+
"github.com/0xPolygon/polygon-cli/signer"
9+
"github.com/ethereum/go-ethereum/accounts/keystore"
10+
"github.com/ethereum/go-ethereum/crypto"
11+
"github.com/rs/zerolog/log"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
//go:embed usage.md
16+
var usage string
17+
18+
var CreateCmd = &cobra.Command{
19+
Use: "create",
20+
Short: "Create a new key.",
21+
Long: usage,
22+
Args: cobra.NoArgs,
23+
PreRunE: signer.SanityCheck,
24+
RunE: func(cmd *cobra.Command, args []string) error {
25+
opts := signer.InputOpts
26+
if opts.Keystore == "" && opts.KMS == "" {
27+
log.Info().Msg("Generating new private hex key and writing to stdout")
28+
pk, err := crypto.GenerateKey()
29+
if err != nil {
30+
return err
31+
}
32+
k := hex.EncodeToString(crypto.FromECDSA(pk))
33+
fmt.Println(k)
34+
return nil
35+
}
36+
if opts.Keystore != "" {
37+
ks := keystore.NewKeyStore(opts.Keystore, keystore.StandardScryptN, keystore.StandardScryptP)
38+
pk, err := crypto.GenerateKey()
39+
if err != nil {
40+
return err
41+
}
42+
password, err := signer.GetKeystorePassword()
43+
if err != nil {
44+
return err
45+
}
46+
acc, err := ks.ImportECDSA(pk, password)
47+
if err != nil {
48+
return err
49+
}
50+
log.Info().Str("address", acc.Address.String()).Msg("imported new account")
51+
return nil
52+
}
53+
if opts.KMS == "GCP" {
54+
gcpKMS := signer.GCPKMS{}
55+
err := gcpKMS.CreateKeyRing(cmd.Context())
56+
if err != nil {
57+
return err
58+
}
59+
err = gcpKMS.CreateKey(cmd.Context())
60+
if err != nil {
61+
return err
62+
}
63+
}
64+
return nil
65+
},
66+
}
File renamed without changes.

cmd/signer/import/import.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package importcmd
2+
3+
import (
4+
_ "embed"
5+
"fmt"
6+
7+
"github.com/0xPolygon/polygon-cli/signer"
8+
"github.com/ethereum/go-ethereum/accounts/keystore"
9+
"github.com/ethereum/go-ethereum/crypto"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
//go:embed usage.md
14+
var usage string
15+
16+
var ImportCmd = &cobra.Command{
17+
Use: "import",
18+
Short: "Import a private key into the keyring / keystore.",
19+
Long: usage,
20+
Args: cobra.NoArgs,
21+
PreRunE: func(cmd *cobra.Command, args []string) error {
22+
if err := signer.SanityCheck(cmd, args); err != nil {
23+
return err
24+
}
25+
return nil
26+
},
27+
RunE: func(cmd *cobra.Command, args []string) error {
28+
opts := signer.InputOpts
29+
if opts.Keystore != "" {
30+
ks := keystore.NewKeyStore(opts.Keystore, keystore.StandardScryptN, keystore.StandardScryptP)
31+
pk, err := crypto.HexToECDSA(opts.PrivateKey)
32+
if err != nil {
33+
return err
34+
}
35+
pass, err := signer.GetKeystorePassword()
36+
if err != nil {
37+
return err
38+
}
39+
_, err = ks.ImportECDSA(pk, pass)
40+
return err
41+
}
42+
if opts.KMS == "GCP" {
43+
gcpKMS := signer.GCPKMS{}
44+
if err := gcpKMS.CreateImportJob(cmd.Context()); err != nil {
45+
return err
46+
}
47+
return gcpKMS.ImportKey(cmd.Context())
48+
}
49+
return fmt.Errorf("unable to import key")
50+
},
51+
}
File renamed without changes.

cmd/signer/list/list.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package list
2+
3+
import (
4+
_ "embed"
5+
"fmt"
6+
7+
"github.com/0xPolygon/polygon-cli/signer"
8+
"github.com/ethereum/go-ethereum/accounts/keystore"
9+
"github.com/rs/zerolog/log"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
//go:embed usage.md
14+
var usage string
15+
16+
var ListCmd = &cobra.Command{
17+
Use: "list",
18+
Short: "List the keys in the keyring / keystore.",
19+
Long: usage,
20+
Args: cobra.NoArgs,
21+
PreRunE: signer.SanityCheck,
22+
RunE: func(cmd *cobra.Command, args []string) error {
23+
opts := signer.InputOpts
24+
if opts.Keystore != "" {
25+
ks := keystore.NewKeyStore(opts.Keystore, keystore.StandardScryptN, keystore.StandardScryptP)
26+
accounts := ks.Accounts()
27+
for idx, a := range accounts {
28+
log.Info().Str("account", a.Address.String()).Int("index", idx).Msg("Account")
29+
}
30+
return nil
31+
}
32+
if opts.KMS == "GCP" {
33+
gcpKMS := signer.GCPKMS{}
34+
return gcpKMS.ListKeyRingKeys(cmd.Context())
35+
}
36+
return fmt.Errorf("unable to list accounts")
37+
},
38+
}
File renamed without changes.

cmd/signer/sign/sign.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package sign
2+
3+
import (
4+
_ "embed"
5+
"fmt"
6+
"os"
7+
8+
"github.com/0xPolygon/polygon-cli/gethkeystore"
9+
"github.com/0xPolygon/polygon-cli/signer"
10+
accounts2 "github.com/ethereum/go-ethereum/accounts"
11+
"github.com/ethereum/go-ethereum/accounts/keystore"
12+
"github.com/ethereum/go-ethereum/crypto"
13+
"github.com/rs/zerolog/log"
14+
"github.com/spf13/cobra"
15+
)
16+
17+
//go:embed usage.md
18+
var usage string
19+
20+
var SignCmd = &cobra.Command{
21+
Use: "sign",
22+
Short: "Sign tx data.",
23+
Long: usage,
24+
Args: cobra.NoArgs,
25+
PreRunE: signer.SanityCheck,
26+
RunE: func(cmd *cobra.Command, args []string) error {
27+
opts := signer.InputOpts
28+
if opts.Keystore == "" && opts.PrivateKey == "" && opts.KMS == "" {
29+
return fmt.Errorf("no valid keystore was specified")
30+
}
31+
32+
if opts.Keystore != "" {
33+
ks := keystore.NewKeyStore(opts.Keystore, keystore.StandardScryptN, keystore.StandardScryptP)
34+
accounts := ks.Accounts()
35+
var accountToUnlock *accounts2.Account
36+
for _, a := range accounts {
37+
if a.Address.String() == opts.KeyID {
38+
accountToUnlock = &a
39+
break
40+
}
41+
}
42+
if accountToUnlock == nil {
43+
accountStrings := ""
44+
for _, a := range accounts {
45+
accountStrings += a.Address.String() + " "
46+
}
47+
return fmt.Errorf("account with address %s not found in list [%s]", opts.KeyID, accountStrings)
48+
}
49+
password, err := signer.GetKeystorePassword()
50+
if err != nil {
51+
return err
52+
}
53+
54+
err = ks.Unlock(*accountToUnlock, password)
55+
if err != nil {
56+
return err
57+
}
58+
59+
log.Info().Str("path", accountToUnlock.URL.Path).Msg("Unlocked account")
60+
encryptedKey, err := os.ReadFile(accountToUnlock.URL.Path)
61+
if err != nil {
62+
return err
63+
}
64+
privKey, err := gethkeystore.DecryptKeystoreFile(encryptedKey, password)
65+
if err != nil {
66+
return err
67+
}
68+
return signer.Sign(privKey)
69+
}
70+
71+
if opts.PrivateKey != "" {
72+
pk, err := crypto.HexToECDSA(opts.PrivateKey)
73+
if err != nil {
74+
return err
75+
}
76+
return signer.Sign(pk)
77+
}
78+
if opts.KMS == "GCP" {
79+
tx, err := signer.GetTxDataToSign()
80+
if err != nil {
81+
return err
82+
}
83+
gcpKMS := signer.GCPKMS{}
84+
return gcpKMS.Sign(cmd.Context(), tx)
85+
}
86+
return fmt.Errorf("not implemented")
87+
},
88+
}
File renamed without changes.

0 commit comments

Comments
 (0)