code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/passphrase_update.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package cmd 17 18 import ( 19 "context" 20 "errors" 21 "fmt" 22 "io" 23 24 "code.vegaprotocol.io/vega/cmd/vegawallet/commands/cli" 25 "code.vegaprotocol.io/vega/cmd/vegawallet/commands/flags" 26 "code.vegaprotocol.io/vega/cmd/vegawallet/commands/printer" 27 "code.vegaprotocol.io/vega/wallet/api" 28 "code.vegaprotocol.io/vega/wallet/wallets" 29 30 "github.com/spf13/cobra" 31 ) 32 33 var ( 34 updatePassphraseLong = cli.LongDesc(` 35 Update the passphrase of the specified wallet. 36 `) 37 38 updatePassphraseExample = cli.Examples(` 39 # Update the passphrase of the specified wallet 40 {{.Software}} passphrase update --wallet WALLET 41 `) 42 43 newWalletPassphraseOptions = flags.PassphraseOptions{ 44 Name: "new", 45 } 46 ) 47 48 type UpdatePassphraseHandler func(api.AdminUpdatePassphraseParams, string) error 49 50 func NewCmdUpdatePassphrase(w io.Writer, rf *RootFlags) *cobra.Command { 51 h := func(params api.AdminUpdatePassphraseParams, passphrase string) error { 52 ctx := context.Background() 53 54 walletStore, err := wallets.InitialiseStore(rf.Home, false) 55 if err != nil { 56 return fmt.Errorf("couldn't initialise wallets store: %w", err) 57 } 58 defer walletStore.Close() 59 60 if _, errDetails := api.NewAdminUnlockWallet(walletStore).Handle(ctx, api.AdminUnlockWalletParams{ 61 Wallet: params.Wallet, 62 Passphrase: passphrase, 63 }); errDetails != nil { 64 return errors.New(errDetails.Data) 65 } 66 67 if _, errDetails := api.NewAdminUpdatePassphrase(walletStore).Handle(ctx, params); errDetails != nil { 68 return errors.New(errDetails.Data) 69 } 70 return nil 71 } 72 73 return BuildCmdUpdatePassphrase(w, h, rf) 74 } 75 76 func BuildCmdUpdatePassphrase(w io.Writer, handler UpdatePassphraseHandler, rf *RootFlags) *cobra.Command { 77 f := &UpdatePassphraseFlags{} 78 79 cmd := &cobra.Command{ 80 Use: "update", 81 Short: "Update the passphrase of the specified wallet", 82 Long: updatePassphraseLong, 83 Example: updatePassphraseExample, 84 RunE: func(_ *cobra.Command, _ []string) error { 85 params, pass, err := f.Validate() 86 if err != nil { 87 return err 88 } 89 90 if err := handler(params, pass); err != nil { 91 return err 92 } 93 94 switch rf.Output { 95 case flags.InteractiveOutput: 96 PrintUpdatePassphraseResponse(w) 97 case flags.JSONOutput: 98 return nil 99 } 100 101 return nil 102 }, 103 } 104 105 cmd.Flags().StringVarP(&f.Wallet, 106 "wallet", "w", 107 "", 108 "Wallet to rename", 109 ) 110 cmd.Flags().StringVarP(&f.PassphraseFile, 111 "passphrase-file", "p", 112 "", 113 "Path to the file containing the current wallet's passphrase", 114 ) 115 cmd.Flags().StringVar(&f.NewPassphraseFile, 116 "new-passphrase-file", 117 "", 118 "Path to the file containing the new wallet's passphrase", 119 ) 120 121 autoCompleteWallet(cmd, rf.Home, "wallet") 122 123 return cmd 124 } 125 126 type UpdatePassphraseFlags struct { 127 Wallet string 128 PassphraseFile string 129 NewPassphraseFile string 130 } 131 132 func (f *UpdatePassphraseFlags) Validate() (api.AdminUpdatePassphraseParams, string, error) { 133 if len(f.Wallet) == 0 { 134 return api.AdminUpdatePassphraseParams{}, "", flags.MustBeSpecifiedError("wallet") 135 } 136 137 passphrase, err := flags.GetPassphrase(f.PassphraseFile) 138 if err != nil { 139 return api.AdminUpdatePassphraseParams{}, "", err 140 } 141 142 newPassphrase, err := flags.GetConfirmedPassphraseWithContext(newWalletPassphraseOptions, f.NewPassphraseFile) 143 if err != nil { 144 return api.AdminUpdatePassphraseParams{}, "", err 145 } 146 147 return api.AdminUpdatePassphraseParams{ 148 Wallet: f.Wallet, 149 NewPassphrase: newPassphrase, 150 }, passphrase, nil 151 } 152 153 func PrintUpdatePassphraseResponse(w io.Writer) { 154 p := printer.NewInteractivePrinter(w) 155 156 str := p.String() 157 defer p.Print(str) 158 159 str.CheckMark().SuccessText("The wallet's passphrase has been updated.").NextLine() 160 }