diff --git a/cmd/goal/messages.go b/cmd/goal/messages.go index 1bc78ce30a..b3cf284881 100644 --- a/cmd/goal/messages.go +++ b/cmd/goal/messages.go @@ -178,16 +178,19 @@ const ( infoPrintedBackupPhrase = "Your backup phrase is printed below.\nKeep this information safe -- never share it with anyone!" infoBackupPhrase = "\n%s" infoNoWallets = "No wallets found. You can create a wallet with `goal wallet new`" + infoRenamedWallet = "Renamed wallet '%s' to '%s'" errorCouldntCreateWallet = "Couldn't create wallet: %s" errorCouldntInitializeWallet = "Couldn't initialize wallet: %s" errorCouldntExportMDK = "Couldn't export master derivation key: %s" errorCouldntMakeMnemonic = "Couldn't make mnemonic: %s" errorCouldntListWallets = "Couldn't list wallets: %s" + errorCouldntFindWallet = "Couldn't find wallet: %s" errorPasswordConfirmation = "Password confirmation did not match" errorBadMnemonic = "Problem with mnemonic: %s" errorBadRecoveredKey = "Recovered invalid key" errorFailedToReadResponse = "Couldn't read response: %s" errorFailedToReadPassword = "Couldn't read password: %s" + errorCouldntRenameWallet = "Couldn't rename wallet: %s" // Commands infoPasswordPrompt = "Please enter the password for wallet '%s': " diff --git a/cmd/goal/wallet.go b/cmd/goal/wallet.go index e95aea62a6..a0a06e6cf4 100644 --- a/cmd/goal/wallet.go +++ b/cmd/goal/wallet.go @@ -39,6 +39,7 @@ var ( func init() { walletCmd.AddCommand(newWalletCmd) walletCmd.AddCommand(listWalletsCmd) + walletCmd.AddCommand(renameWalletCmd) // Default wallet to use when -w not specified walletCmd.Flags().StringVarP(&defaultWalletName, "default", "f", "", "Set the wallet with this name to be the default wallet") @@ -200,6 +201,53 @@ var listWalletsCmd = &cobra.Command{ }, } +var renameWalletCmd = &cobra.Command{ + Use: "rename [wallet name] [new wallet name]", + Short: "Rename wallet", + Args: cobra.ExactArgs(2), + Run: func(cmd *cobra.Command, args []string) { + dataDir := datadir.EnsureSingleDataDir() + + client := ensureKmdClient(dataDir) + + walletName := []byte(args[0]) + newWalletName := []byte(args[1]) + + wid, duplicate, err := client.FindWalletIDByName(walletName) + + if wid == nil { + reportErrorf(errorCouldntFindWallet, string(walletName)) + } + + if err != nil { + reportErrorf(errorCouldntRenameWallet, err) + } + + if duplicate { + reportErrorf(errorCouldntRenameWallet, "Multiple wallets by the same name are not supported") + } + + if bytes.Equal(walletName, newWalletName) { + reportErrorf(errorCouldntRenameWallet, "new name is identical to current name") + } + + walletPassword := []byte{} + + // if wallet is encrypted, fetch the password + if !client.WalletIsUnencrypted(wid) { + fmt.Printf(infoPasswordPrompt, walletName) + walletPassword = ensurePassword() + } + + err = client.RenameWallet(wid, newWalletName, walletPassword) + if err != nil { + reportErrorf(errorCouldntRenameWallet, err) + } + + reportInfof(infoRenamedWallet, walletName, newWalletName) + }, +} + func printWallets(dataDir string, wallets []kmdapi.APIV1Wallet) { accountList := makeAccountsList(dataDir) defaultWalletID := string(accountList.getDefaultWalletID()) diff --git a/daemon/kmd/client/wrappers.go b/daemon/kmd/client/wrappers.go index a7b5613aff..2167276c08 100644 --- a/daemon/kmd/client/wrappers.go +++ b/daemon/kmd/client/wrappers.go @@ -59,6 +59,17 @@ func (kcl KMDClient) CreateWallet(walletName []byte, walletDriverName string, wa return } +// RenameWallet wraps kmdapi.APIV1POSTWalletRenameRequest +func (kcl KMDClient) RenameWallet(walletID []byte, newWalletName []byte, walletPassword []byte) (resp kmdapi.APIV1POSTWalletRenameResponse, err error) { + req := kmdapi.APIV1POSTWalletRenameRequest{ + WalletID: string(walletID), + NewWalletName: string(newWalletName), + WalletPassword: string(walletPassword), + } + err = kcl.DoV1Request(req, &resp) + return +} + // InitWallet wraps kmdapi.APIV1POSTWalletInitRequest func (kcl KMDClient) InitWallet(walletID []byte, walletPassword []byte) (resp kmdapi.APIV1POSTWalletInitResponse, err error) { req := kmdapi.APIV1POSTWalletInitRequest{ diff --git a/libgoal/wallets.go b/libgoal/wallets.go index e175d62f77..c0db621bda 100644 --- a/libgoal/wallets.go +++ b/libgoal/wallets.go @@ -45,6 +45,24 @@ func (c *Client) CreateWallet(name []byte, password []byte, mdk crypto.MasterDer return []byte(resp.Wallet.ID), nil } +// RenameWallet renames a kmd wallet +func (c *Client) RenameWallet(wid []byte, name []byte, password []byte) error { + // Pull the list of all wallets from kmd + kmd, err := c.ensureKmdClient() + if err != nil { + return err + } + + // Rename the wallet + _, err = kmd.RenameWallet(wid, name, password) + + if err != nil { + return err + } + + return nil +} + // GetWalletHandleToken inits the wallet with the given id, returning a wallet handle token func (c *Client) GetWalletHandleToken(wid, pw []byte) ([]byte, error) { kmd, err := c.ensureKmdClient()