diff --git a/cmd/account.go b/cmd/account.go index 56d86d00..dc507d8e 100644 --- a/cmd/account.go +++ b/cmd/account.go @@ -46,24 +46,34 @@ 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 @@ -71,25 +81,25 @@ 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 @@ -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 @@ -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() { @@ -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") } diff --git a/cmd/account_test.go b/cmd/account_test.go new file mode 100644 index 00000000..417205f2 --- /dev/null +++ b/cmd/account_test.go @@ -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"}) +}