Skip to content

Commit 55741a9

Browse files
committed
validate flags when saving hex key
1 parent fd716a4 commit 55741a9

1 file changed

Lines changed: 36 additions & 3 deletions

File tree

flow/keys/save/hex/hex.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package hex
2020

2121
import (
22+
"encoding/hex"
2223
"log"
2324

2425
"github.com/onflow/flow-go-sdk"
@@ -48,17 +49,49 @@ var Cmd = &cobra.Command{
4849
Run: func(cmd *cobra.Command, args []string) {
4950
projectConf := cli.LoadConfig()
5051

52+
if conf.Name == "" {
53+
cli.Exitf(1, "missing name")
54+
}
55+
5156
_, accountExists := projectConf.Accounts[conf.Name]
5257
if accountExists && !conf.Overwrite {
5358
cli.Exitf(1, "%s already exists in the config, and overwrite is false", conf.Name)
5459
}
5560

61+
// Parse address
62+
decodedAddress, err := hex.DecodeString(conf.Address)
63+
if err != nil {
64+
cli.Exitf(1, "invalid address: %s", err.Error())
65+
}
66+
address := flow.BytesToAddress(decodedAddress)
67+
68+
// Parse signature algorithm
69+
if conf.SigAlgo == "" {
70+
cli.Exitf(1, "missing signature algorithm")
71+
}
72+
73+
algorithm := crypto.StringToSignatureAlgorithm(conf.SigAlgo)
74+
if algorithm == crypto.UnknownSignatureAlgorithm {
75+
cli.Exitf(1, "invalid signature algorithm")
76+
}
77+
78+
// Parse hash algorithm
79+
80+
if conf.HashAlgo == "" {
81+
cli.Exitf(1, "missing hash algorithm")
82+
}
83+
84+
hashAlgorithm := crypto.StringToHashAlgorithm(conf.HashAlgo)
85+
if hashAlgorithm == crypto.UnknownHashAlgorithm {
86+
cli.Exitf(1, "invalid hash algorithm")
87+
}
88+
5689
// Populate account
5790
account := &cli.Account{
5891
KeyType: cli.KeyTypeHex,
59-
Address: flow.HexToAddress(conf.Address),
60-
SigAlgo: crypto.StringToSignatureAlgorithm(conf.SigAlgo),
61-
HashAlgo: crypto.StringToHashAlgorithm(conf.HashAlgo),
92+
Address: address,
93+
SigAlgo: algorithm,
94+
HashAlgo: hashAlgorithm,
6295
KeyIndex: conf.KeyIndex,
6396
KeyContext: map[string]string{"privateKey": conf.KeyHex},
6497
}

0 commit comments

Comments
 (0)