Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

goal: added "wallet rename" #6161

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/goal/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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': "
Expand Down
48 changes: 48 additions & 0 deletions cmd/goal/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
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")
Expand Down Expand Up @@ -200,6 +201,53 @@
},
}

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()

Check warning on line 209 in cmd/goal/wallet.go

View check run for this annotation

Codecov / codecov/patch

cmd/goal/wallet.go#L208-L209

Added lines #L208 - L209 were not covered by tests

client := ensureKmdClient(dataDir)

Check warning on line 211 in cmd/goal/wallet.go

View check run for this annotation

Codecov / codecov/patch

cmd/goal/wallet.go#L211

Added line #L211 was not covered by tests

walletName := []byte(args[0])
newWalletName := []byte(args[1])

Check warning on line 214 in cmd/goal/wallet.go

View check run for this annotation

Codecov / codecov/patch

cmd/goal/wallet.go#L213-L214

Added lines #L213 - L214 were not covered by tests

wid, duplicate, err := client.FindWalletIDByName(walletName)

Check warning on line 216 in cmd/goal/wallet.go

View check run for this annotation

Codecov / codecov/patch

cmd/goal/wallet.go#L216

Added line #L216 was not covered by tests

if wid == nil {
reportErrorf(errorCouldntFindWallet, string(walletName))

Check warning on line 219 in cmd/goal/wallet.go

View check run for this annotation

Codecov / codecov/patch

cmd/goal/wallet.go#L218-L219

Added lines #L218 - L219 were not covered by tests
}

if err != nil {
reportErrorf(errorCouldntRenameWallet, err)

Check warning on line 223 in cmd/goal/wallet.go

View check run for this annotation

Codecov / codecov/patch

cmd/goal/wallet.go#L222-L223

Added lines #L222 - L223 were not covered by tests
}
Comment on lines +218 to +224
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should check err first, so you get the more detailed error message.


if duplicate {
reportErrorf(errorCouldntRenameWallet, "Multiple wallets by the same name are not supported")

Check warning on line 227 in cmd/goal/wallet.go

View check run for this annotation

Codecov / codecov/patch

cmd/goal/wallet.go#L226-L227

Added lines #L226 - L227 were not covered by tests
}

if bytes.Equal(walletName, newWalletName) {
reportErrorf(errorCouldntRenameWallet, "new name is identical to current name")

Check warning on line 231 in cmd/goal/wallet.go

View check run for this annotation

Codecov / codecov/patch

cmd/goal/wallet.go#L230-L231

Added lines #L230 - L231 were not covered by tests
}
Comment on lines +230 to +232
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like you might as well do this test before interacting with kmd at all.


walletPassword := []byte{}

Check warning on line 234 in cmd/goal/wallet.go

View check run for this annotation

Codecov / codecov/patch

cmd/goal/wallet.go#L234

Added line #L234 was not covered by tests

// if wallet is encrypted, fetch the password
if !client.WalletIsUnencrypted(wid) {
fmt.Printf(infoPasswordPrompt, walletName)
walletPassword = ensurePassword()

Check warning on line 239 in cmd/goal/wallet.go

View check run for this annotation

Codecov / codecov/patch

cmd/goal/wallet.go#L237-L239

Added lines #L237 - L239 were not covered by tests
}

err = client.RenameWallet(wid, newWalletName, walletPassword)
if err != nil {
reportErrorf(errorCouldntRenameWallet, err)

Check warning on line 244 in cmd/goal/wallet.go

View check run for this annotation

Codecov / codecov/patch

cmd/goal/wallet.go#L242-L244

Added lines #L242 - L244 were not covered by tests
}

reportInfof(infoRenamedWallet, walletName, newWalletName)

Check warning on line 247 in cmd/goal/wallet.go

View check run for this annotation

Codecov / codecov/patch

cmd/goal/wallet.go#L247

Added line #L247 was not covered by tests
},
}

func printWallets(dataDir string, wallets []kmdapi.APIV1Wallet) {
accountList := makeAccountsList(dataDir)
defaultWalletID := string(accountList.getDefaultWalletID())
Expand Down
11 changes: 11 additions & 0 deletions daemon/kmd/client/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
18 changes: 18 additions & 0 deletions libgoal/wallets.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@
return []byte(resp.Wallet.ID), nil
}

// RenameWallet renames a kmd wallet
func (c *Client) RenameWallet(wid []byte, name []byte, password []byte) error {

Check warning on line 49 in libgoal/wallets.go

View check run for this annotation

Codecov / codecov/patch

libgoal/wallets.go#L49

Added line #L49 was not covered by tests
// Pull the list of all wallets from kmd
kmd, err := c.ensureKmdClient()
if err != nil {
return err

Check warning on line 53 in libgoal/wallets.go

View check run for this annotation

Codecov / codecov/patch

libgoal/wallets.go#L51-L53

Added lines #L51 - L53 were not covered by tests
}

// Rename the wallet
_, err = kmd.RenameWallet(wid, name, password)

Check warning on line 57 in libgoal/wallets.go

View check run for this annotation

Codecov / codecov/patch

libgoal/wallets.go#L57

Added line #L57 was not covered by tests

if err != nil {
return err

Check warning on line 60 in libgoal/wallets.go

View check run for this annotation

Codecov / codecov/patch

libgoal/wallets.go#L59-L60

Added lines #L59 - L60 were not covered by tests
}

return nil

Check warning on line 63 in libgoal/wallets.go

View check run for this annotation

Codecov / codecov/patch

libgoal/wallets.go#L63

Added line #L63 was not covered by tests
}

// 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()
Expand Down
Loading