cli: refactor code that opens wallet
Reduce code duplications, no functional changes.
This commit is contained in:
parent
213bfe6bbf
commit
b2f188f8f0
2 changed files with 40 additions and 38 deletions
|
@ -350,7 +350,7 @@ func importNEP17Token(ctx *cli.Context) error {
|
|||
}
|
||||
|
||||
func importNEPToken(ctx *cli.Context, standard string) error {
|
||||
wall, _, err := openWallet(ctx.String("wallet"), ctx.String("wallet-config"))
|
||||
wall, _, err := openWallet(ctx, true)
|
||||
if err != nil {
|
||||
return cli.NewExitError(err, 1)
|
||||
}
|
||||
|
@ -446,7 +446,7 @@ func removeNEP17Token(ctx *cli.Context) error {
|
|||
}
|
||||
|
||||
func removeNEPToken(ctx *cli.Context, standard string) error {
|
||||
wall, _, err := openWallet(ctx.String("wallet"), ctx.String("wallet-config"))
|
||||
wall, _, err := openWallet(ctx, true)
|
||||
if err != nil {
|
||||
return cli.NewExitError(err, 1)
|
||||
}
|
||||
|
|
|
@ -333,7 +333,7 @@ func claimGas(ctx *cli.Context) error {
|
|||
}
|
||||
|
||||
func changePassword(ctx *cli.Context) error {
|
||||
wall, _, err := openWallet(ctx.String("wallet"), "")
|
||||
wall, _, err := openWallet(ctx, false)
|
||||
if err != nil {
|
||||
return cli.NewExitError(err, 1)
|
||||
}
|
||||
|
@ -423,7 +423,7 @@ func convertWallet(ctx *cli.Context) error {
|
|||
}
|
||||
|
||||
func addAccount(ctx *cli.Context) error {
|
||||
wall, pass, err := openWallet(ctx.String("wallet"), ctx.String("wallet-config"))
|
||||
wall, pass, err := openWallet(ctx, true)
|
||||
if err != nil {
|
||||
return cli.NewExitError(err, 1)
|
||||
}
|
||||
|
@ -499,7 +499,7 @@ loop:
|
|||
}
|
||||
|
||||
func importMultisig(ctx *cli.Context) error {
|
||||
wall, _, err := openWallet(ctx.String("wallet"), ctx.String("wallet-config"))
|
||||
wall, _, err := openWallet(ctx, true)
|
||||
if err != nil {
|
||||
return cli.NewExitError(err, 1)
|
||||
}
|
||||
|
@ -541,7 +541,7 @@ func importMultisig(ctx *cli.Context) error {
|
|||
}
|
||||
|
||||
func importDeployed(ctx *cli.Context) error {
|
||||
wall, _, err := openWallet(ctx.String("wallet"), ctx.String("wallet-config"))
|
||||
wall, _, err := openWallet(ctx, true)
|
||||
if err != nil {
|
||||
return cli.NewExitError(err, 1)
|
||||
}
|
||||
|
@ -596,7 +596,7 @@ func importDeployed(ctx *cli.Context) error {
|
|||
}
|
||||
|
||||
func importWallet(ctx *cli.Context) error {
|
||||
wall, _, err := openWallet(ctx.String("wallet"), ctx.String("wallet-config"))
|
||||
wall, _, err := openWallet(ctx, true)
|
||||
if err != nil {
|
||||
return cli.NewExitError(err, 1)
|
||||
}
|
||||
|
@ -626,7 +626,7 @@ func importWallet(ctx *cli.Context) error {
|
|||
}
|
||||
|
||||
func removeAccount(ctx *cli.Context) error {
|
||||
wall, _, err := openWallet(ctx.String("wallet"), ctx.String("wallet-config"))
|
||||
wall, _, err := openWallet(ctx, true)
|
||||
if err != nil {
|
||||
return cli.NewExitError(err, 1)
|
||||
}
|
||||
|
@ -823,24 +823,13 @@ func createAccount(wall *wallet.Wallet, pass *string) error {
|
|||
return wall.CreateAccount(name, phrase)
|
||||
}
|
||||
|
||||
func openWallet(path string, configPath string) (*wallet.Wallet, *string, error) {
|
||||
if len(path) != 0 && len(configPath) != 0 {
|
||||
return nil, nil, errConflictingWalletFlags
|
||||
}
|
||||
if len(path) == 0 && len(configPath) == 0 {
|
||||
return nil, nil, errNoPath
|
||||
}
|
||||
if path == "-" {
|
||||
return nil, nil, errNoStdin
|
||||
}
|
||||
var pass *string
|
||||
if len(configPath) != 0 {
|
||||
cfg, err := ReadWalletConfig(configPath)
|
||||
func openWallet(ctx *cli.Context, canUseWalletConfig bool) (*wallet.Wallet, *string, error) {
|
||||
path, pass, err := getWalletPathAndPass(ctx, canUseWalletConfig)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
path = cfg.Path
|
||||
pass = &cfg.Password
|
||||
if path == "-" {
|
||||
return nil, nil, errNoStdin
|
||||
}
|
||||
w, err := wallet.NewWalletFromFile(path)
|
||||
if err != nil {
|
||||
|
@ -850,22 +839,10 @@ func openWallet(path string, configPath string) (*wallet.Wallet, *string, error)
|
|||
}
|
||||
|
||||
func readWallet(ctx *cli.Context) (*wallet.Wallet, *string, error) {
|
||||
path, configPath := ctx.String("wallet"), ctx.String("wallet-config")
|
||||
if len(path) != 0 && len(configPath) != 0 {
|
||||
return nil, nil, errConflictingWalletFlags
|
||||
}
|
||||
if len(path) == 0 && len(configPath) == 0 {
|
||||
return nil, nil, errNoPath
|
||||
}
|
||||
var pass *string
|
||||
if len(configPath) != 0 {
|
||||
cfg, err := ReadWalletConfig(configPath)
|
||||
path, pass, err := getWalletPathAndPass(ctx, true)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
path = cfg.Path
|
||||
pass = &cfg.Password
|
||||
}
|
||||
if path == "-" {
|
||||
w := &wallet.Wallet{}
|
||||
if err := json.NewDecoder(os.Stdin).Decode(w); err != nil {
|
||||
|
@ -880,6 +857,31 @@ func readWallet(ctx *cli.Context) (*wallet.Wallet, *string, error) {
|
|||
return w, pass, nil
|
||||
}
|
||||
|
||||
// getWalletPathAndPass retrieves wallet path from context or from wallet configuration file.
|
||||
// If wallet configuration file is specified, then account password is returned.
|
||||
func getWalletPathAndPass(ctx *cli.Context, canUseWalletConfig bool) (string, *string, error) {
|
||||
path, configPath := ctx.String("wallet"), ctx.String("wallet-config")
|
||||
if !canUseWalletConfig && len(configPath) != 0 {
|
||||
return "", nil, errors.New("can't use wallet configuration file for this command")
|
||||
}
|
||||
if len(path) != 0 && len(configPath) != 0 {
|
||||
return "", nil, errConflictingWalletFlags
|
||||
}
|
||||
if len(path) == 0 && len(configPath) == 0 {
|
||||
return "", nil, errNoPath
|
||||
}
|
||||
var pass *string
|
||||
if len(configPath) != 0 {
|
||||
cfg, err := ReadWalletConfig(configPath)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
path = cfg.Path
|
||||
pass = &cfg.Password
|
||||
}
|
||||
return path, pass, nil
|
||||
}
|
||||
|
||||
func ReadWalletConfig(configPath string) (*config.Wallet, error) {
|
||||
file, err := os.Open(configPath)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in a new issue