Skip to content

Commit

Permalink
cmd: separated functions from cobra commands, some unittests. --passw…
Browse files Browse the repository at this point in the history
…ord now belongs to account command, not its subcommands
  • Loading branch information
randomshinichi committed Feb 19, 2019
1 parent f10e442 commit 6bd18f7
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 120 deletions.
234 changes: 114 additions & 120 deletions cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,50 +46,60 @@ var addressCmd = &cobra.Command{
Short: "Print the aeternity account address",
Long: ``,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// ask for th keystore password
p, err := utils.AskPassword("Enter the password to unlock the keystore: ")
if err != nil {
fmt.Println("Error reading the password: ", err)
os.Exit(1)
}
// load the account
account, err := aeternity.LoadAccountFromKeyStoreFile(args[0], p)
if err != nil {
fmt.Println("Error unlocking the keystore: ", err)
os.Exit(1)
}
aeternity.Pp("Account address", account.Address)
if printPrivateKey {
aeternity.Pp("Account private key", account.SigningKeyToHexString())
}
},
Run: addressFunc,
}

func getPassword() (p string) {
if len(password) != 0 {
return password
}
p, err := utils.AskPassword("Enter the password to unlock the keystore: ")
if err != nil {
fmt.Println("Error reading the password: ", err)
os.Exit(1)
}
return p
}

func addressFunc(cmd *cobra.Command, args []string) {
p := getPassword()

// load the account
account, err := aeternity.LoadAccountFromKeyStoreFile(args[0], p)
if err != nil {
fmt.Println("Error unlocking the keystore: ", err)
os.Exit(1)
}
aeternity.Pp("Account address", account.Address)
if printPrivateKey {
aeternity.Pp("Account private key", account.SigningKeyToHexString())
}
}

// createCmd implements the account generate subcommand
var createCmd = &cobra.Command{
Use: "create",
Short: "Create a new account",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
account, _ := aeternity.NewAccount()
// ask for password
p, err := utils.AskPassword("Enter a password for your keystore: ")
if err != nil {
fmt.Println("Error reading the password: ", err)
return
}
// check if a name was given
f, err := aeternity.StoreAccountToKeyStoreFile(account, p, accountFileName)
if err != nil {
fmt.Println("Error saving the keystore file: ", err)
return
}
aeternity.Pp(
"Wallet path", f,
"Account address", account.Address,
)
},
Args: cobra.ExactArgs(1),
Run: createFunc,
}

func createFunc(cmd *cobra.Command, args []string) {
account, _ := aeternity.NewAccount()
p := getPassword()
accountFileName = args[0]

// check if a name was given
f, err := aeternity.StoreAccountToKeyStoreFile(account, p, accountFileName)
if err != nil {
fmt.Println("Error saving the keystore file: ", err)
return
}
aeternity.Pp(
"Wallet path", f,
"Account address", account.Address,
)
}

// balanceCmd implements the account balance subcommand
Expand All @@ -98,27 +108,25 @@ var balanceCmd = &cobra.Command{
Short: "Get the balance of an account",
Long: ``,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
aeCli := NewAeCli()
// ask for th keystore password
p, err := utils.AskPassword("Enter the password to unlock the keystore: ")
if err != nil {
fmt.Println("Error reading the password: ", err)
os.Exit(1)
}
// load the account
account, err := aeternity.LoadAccountFromKeyStoreFile(args[0], p)
if err != nil {
fmt.Println("Error unlocking the keystore: ", err)
os.Exit(1)
}
a, err := aeCli.APIGetAccount(account.Address)
if err != nil {
fmt.Println("Error retrieving the account: ", err)
os.Exit(1)
}
aeternity.PrintObject("account", a)
},
Run: balanceFunc,
}

func balanceFunc(cmd *cobra.Command, args []string) {
aeCli := NewAeCli()
p := getPassword()

// load the account
account, err := aeternity.LoadAccountFromKeyStoreFile(args[0], p)
if err != nil {
fmt.Println("Error unlocking the keystore: ", err)
os.Exit(1)
}
a, err := aeCli.APIGetAccount(account.Address)
if err != nil {
fmt.Println("Error retrieving the account: ", err)
os.Exit(1)
}
aeternity.PrintObject("account", a)
}

// signCmd implements the account sign subcommand
Expand All @@ -127,68 +135,60 @@ var signCmd = &cobra.Command{
Short: "Sign the input (e.g. a transaction)",
Long: ``,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
// ask for the keystore password
p, err := utils.AskPassword("Enter the password to unlock the keystore: ")
if err != nil {
fmt.Println("Error reading the password: ", err)
os.Exit(1)
}
// load the account
account, err := aeternity.LoadAccountFromKeyStoreFile(args[0], p)
if err != nil {
fmt.Println("Error unlocking the keystore: ", err)
os.Exit(1)
}

txUnsignedBase64 := args[1]
txSignedBase64, txHash, signature, err := aeternity.SignEncodeTxStr(account, txUnsignedBase64)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

aeternity.Pp(
"Signing account address", account.Address,
"Signature", signature,
"Unsigned", txUnsignedBase64,
"Signed", txSignedBase64,
"Hash", txHash,
)

},
Run: signFunc,
}

func signFunc(cmd *cobra.Command, args []string) {
p := getPassword()

// load the account
account, err := aeternity.LoadAccountFromKeyStoreFile(args[0], p)
if err != nil {
fmt.Println("Error unlocking the keystore: ", err)
os.Exit(1)
}

txUnsignedBase64 := args[1]
txSignedBase64, txHash, signature, err := aeternity.SignEncodeTxStr(account, txUnsignedBase64)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

aeternity.Pp(
"Signing account address", account.Address,
"Signature", signature,
"Unsigned", txUnsignedBase64,
"Signed", txSignedBase64,
"Hash", txHash,
)
}

// saveCmd implements the account save subcommand
var saveCmd = &cobra.Command{
Use: "save ACCOUNT_HEX_STRING",
Use: "save ACCOUNT_KEYSTORE ACCOUNT_HEX_STRING",
Short: "Save an account from a hex string to a keystore file",
Long: ``,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
accountFileName := args[0]
account, err := aeternity.AccountFromHexString(args[1])
if err != nil {
fmt.Println("Error parsing the private key hex string:", err)
return
}

if len(password) == 0 {
var err error
password, err = utils.AskPassword("Enter a password for your keystore: ")
if err != nil {
fmt.Println("Error reading the password: ", err)
os.Exit(1)
}
}

f, err := aeternity.StoreAccountToKeyStoreFile(account, password, accountFileName)
if err != nil {
fmt.Println("Error saving the keystore file: ", err)
return
}
aeternity.Pp("Keystore path ", f)
},
Run: saveFunc,
}

func saveFunc(cmd *cobra.Command, args []string) {
accountFileName := args[0]
account, err := aeternity.AccountFromHexString(args[1])
if err != nil {
fmt.Println("Error parsing the private key hex string:", err)
return
}

p := getPassword()

f, err := aeternity.StoreAccountToKeyStoreFile(account, p, accountFileName)
if err != nil {
fmt.Println("Error saving the keystore file: ", err)
return
}
aeternity.Pp("Keystore path ", f)
}

func init() {
Expand All @@ -198,13 +198,7 @@ func init() {
accountCmd.AddCommand(saveCmd)
accountCmd.AddCommand(balanceCmd)
accountCmd.AddCommand(signCmd)

// account sign flags
signCmd.Flags().StringVar(&password, "password", "", "Read account password from stdin [WARN: this method is not secure]")
// account create flags
createCmd.Flags().StringVar(&accountFileName, "name", "", "Override the default name of a wallet")
// account save flags
saveCmd.Flags().StringVar(&password, "password", "", "Read account password from stdin [WARN: this method is not secure]")
accountCmd.PersistentFlags().StringVar(&password, "password", "", "Read account password from stdin [WARN: this method is not secure]")
// account address flags
addressCmd.Flags().BoolVar(&printPrivateKey, "private-key", false, "Print the private key as hex string")
}
66 changes: 66 additions & 0 deletions cmd/account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

import (
"io/ioutil"
"os"
"testing"

"github.com/spf13/cobra"
)

func TestCreate(t *testing.T) {
password = "password"
emptyCmd := cobra.Command{}

dir, err := ioutil.TempDir("", "aecli")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
os.Chdir(dir)

createFunc(&emptyCmd, []string{"test.json"})
}

func TestAddress(t *testing.T) {
password = "password"
emptyCmd := cobra.Command{}

dir, err := ioutil.TempDir("", "aecli")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
os.Chdir(dir)

createFunc(&emptyCmd, []string{"test.json"})
addressFunc(&emptyCmd, []string{"test.json"})
}

func TestSave(t *testing.T) {
password = "password"
emptyCmd := cobra.Command{}
privateKey := "025528252ec5db7d77cd57e14ae7819b9205c84abe5eef8353f88330467048f458019537ef2e809fefe1f2513cda8c8aacc74fb30f8c1f8b32d99a16b7f539b8"
dir, err := ioutil.TempDir("", "aecli")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
os.Chdir(dir)

saveFunc(&emptyCmd, []string{"test.json", privateKey})
}

func TestBalance(t *testing.T) {
password = "password"
emptyCmd := cobra.Command{}
dir, err := ioutil.TempDir("", "aecli")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
os.Chdir(dir)

createFunc(&emptyCmd, []string{"test.json"})
balanceFunc(&emptyCmd, []string{"test.json"})
}

0 comments on commit 6bd18f7

Please sign in to comment.