github.com/keybase/client/go@v0.0.0-20240520164431-4f512a4c85a3/client/cmd_wallet_import.go (about) 1 // Copyright 2018 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 4 package client 5 6 import ( 7 "errors" 8 "fmt" 9 10 "github.com/keybase/cli" 11 "github.com/keybase/client/go/libcmdline" 12 "github.com/keybase/client/go/libkb" 13 "github.com/keybase/client/go/protocol/stellar1" 14 "golang.org/x/net/context" 15 ) 16 17 type cmdWalletImport struct { 18 libkb.Contextified 19 makePrimary bool 20 } 21 22 func newCmdWalletImport(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command { 23 cmd := &cmdWalletImport{ 24 Contextified: libkb.NewContextified(g), 25 } 26 return cli.Command{ 27 Name: "import", 28 Description: "Import a Stellar account", 29 Usage: "Import Stellar account keys", 30 ArgumentHelp: "[--primary]", 31 Action: func(c *cli.Context) { 32 cl.ChooseCommand(cmd, "import", c) 33 }, 34 Flags: []cli.Flag{ 35 cli.BoolFlag{ 36 Name: "primary", 37 Usage: "make this your main public account", 38 }, 39 }, 40 } 41 } 42 43 func (c *cmdWalletImport) ParseArgv(ctx *cli.Context) (err error) { 44 if len(ctx.Args()) != 0 { 45 return errors.New("expected no arguments") 46 } 47 c.makePrimary = ctx.Bool("primary") 48 return nil 49 } 50 51 func (c *cmdWalletImport) Run() (err error) { 52 defer transformStellarCLIError(&err) 53 ctx := context.TODO() 54 secKey, accountID, err := c.promptSecretKey() 55 if err != nil { 56 return err 57 } 58 59 cli, err := GetWalletClient(c.G()) 60 if err != nil { 61 return err 62 } 63 64 own, err := cli.OwnAccountLocal(ctx, accountID) 65 if err != nil { 66 return err 67 } 68 if own { 69 return fmt.Errorf("account has already been imported: %v", accountID) 70 } 71 c.G().UI.GetTerminalUI().Printf(fmt.Sprintf("Ready to import account: %v\n", accountID)) 72 name, err := c.promptAccountName(ctx) 73 if err != nil { 74 return err 75 } 76 err = c.confirm(accountID) 77 if err != nil { 78 return err 79 } 80 err = cli.ImportSecretKeyLocal(ctx, stellar1.ImportSecretKeyLocalArg{ 81 SecretKey: secKey, 82 MakePrimary: c.makePrimary, 83 Name: name, 84 }) 85 if err != nil { 86 return err 87 } 88 dui := c.G().UI.GetDumbOutputUI() 89 dui.Printf("✓ Account imported\n") 90 return err 91 } 92 93 func (c *cmdWalletImport) GetUsage() libkb.Usage { 94 return libkb.Usage{ 95 Config: true, 96 API: true, 97 KbKeyring: true, 98 } 99 } 100 101 func (c *cmdWalletImport) promptSecretKey() (stellar1.SecretKey, stellar1.AccountID, error) { 102 secStr, err := c.G().UI.GetTerminalUI().PromptPassword(PromptDescriptorImportStellarSecretKey, "Enter a stellar secret key to import") 103 if err != nil { 104 return "", "", err 105 } 106 if len(secStr) == 0 { 107 return "", "", libkb.InputCanceledError{} 108 } 109 secKey, accountID, _, err := libkb.ParseStellarSecretKey(secStr) 110 return secKey, accountID, err 111 } 112 113 func (c *cmdWalletImport) promptAccountName(ctx context.Context) (string, error) { 114 return c.G().UI.GetTerminalUI().Prompt(PromptDescriptorImportStellarAccountName, "Enter a private name for this account: ") 115 } 116 117 func (c *cmdWalletImport) confirm(accountID stellar1.AccountID) error { 118 c.G().UI.GetTerminalUI().Printf("The stellar secret key will be encrypted, uploaded, and made available on all of your devices.\n") 119 doIt, err := c.G().UI.GetTerminalUI().PromptYesNo(PromptDescriptorConfirmStellarImport, "Ready to import?", libkb.PromptDefaultYes) 120 if err != nil { 121 return err 122 } 123 if !doIt { 124 return libkb.NewCanceledError("import canceled") 125 } 126 return nil 127 }