cli/wallet: decrypt account immediately

This commit is contained in:
Evgenii Stratonikov 2020-08-07 12:34:54 +03:00
parent 832ec5eaa0
commit 78eade24a3
4 changed files with 32 additions and 47 deletions

View file

@ -50,9 +50,9 @@ func signMultisig(ctx *cli.Context) error {
if err != nil { if err != nil {
return cli.NewExitError(fmt.Errorf("invalid address: %w", err), 1) return cli.NewExitError(fmt.Errorf("invalid address: %w", err), 1)
} }
acc := wall.GetAccount(sh) acc, err := getDecryptedAccount(wall, sh)
if acc == nil { if err != nil {
return cli.NewExitError(fmt.Errorf("can't find account for the address: %s", addr), 1) return cli.NewExitError(err, 1)
} }
tx, ok := c.Verifiable.(*transaction.Transaction) tx, ok := c.Verifiable.(*transaction.Transaction)
@ -60,13 +60,6 @@ func signMultisig(ctx *cli.Context) error {
return cli.NewExitError("verifiable item is not a transaction", 1) return cli.NewExitError("verifiable item is not a transaction", 1)
} }
printTxInfo(tx) printTxInfo(tx)
fmt.Println("Enter password to unlock wallet and sign the transaction")
pass, err := readPassword("Password > ")
if err != nil {
return cli.NewExitError(err, 1)
} else if err := acc.Decrypt(pass); err != nil {
return cli.NewExitError(fmt.Errorf("can't unlock an account: %w", err), 1)
}
priv := acc.PrivateKey() priv := acc.PrivateKey()
sign := priv.Sign(tx.GetSignedPart()) sign := priv.Sign(tx.GetSignedPart())

View file

@ -343,9 +343,9 @@ func multiTransferNEP5(ctx *cli.Context) error {
fromFlag := ctx.Generic("from").(*flags.Address) fromFlag := ctx.Generic("from").(*flags.Address)
from := fromFlag.Uint160() from := fromFlag.Uint160()
acc := wall.GetAccount(from) acc, err := getDecryptedAccount(wall, from)
if acc == nil { if err != nil {
return cli.NewExitError(fmt.Errorf("can't find account for the address: %s", fromFlag), 1) return cli.NewExitError(err, 1)
} }
gctx, cancel := options.GetTimeoutContext(ctx) gctx, cancel := options.GetTimeoutContext(ctx)
@ -406,9 +406,9 @@ func transferNEP5(ctx *cli.Context) error {
fromFlag := ctx.Generic("from").(*flags.Address) fromFlag := ctx.Generic("from").(*flags.Address)
from := fromFlag.Uint160() from := fromFlag.Uint160()
acc := wall.GetAccount(from) acc, err := getDecryptedAccount(wall, from)
if acc == nil { if err != nil {
return cli.NewExitError(fmt.Errorf("can't find account for the address: %s", fromFlag), 1) return cli.NewExitError(err, 1)
} }
gctx, cancel := options.GetTimeoutContext(ctx) gctx, cancel := options.GetTimeoutContext(ctx)
@ -445,12 +445,6 @@ func transferNEP5(ctx *cli.Context) error {
func signAndSendTransfer(ctx *cli.Context, c *client.Client, acc *wallet.Account, recepients []client.TransferTarget) error { func signAndSendTransfer(ctx *cli.Context, c *client.Client, acc *wallet.Account, recepients []client.TransferTarget) error {
gas := flags.Fixed8FromContext(ctx, "gas") gas := flags.Fixed8FromContext(ctx, "gas")
if pass, err := readPassword("Password > "); err != nil {
return cli.NewExitError(err, 1)
} else if err := acc.Decrypt(pass); err != nil {
return cli.NewExitError(err, 1)
}
tx, err := c.CreateNEP5MultiTransferTx(acc, int64(gas), recepients...) tx, err := c.CreateNEP5MultiTransferTx(acc, int64(gas), recepients...)
if err != nil { if err != nil {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)

View file

@ -6,10 +6,13 @@ import (
"github.com/nspcc-dev/neo-go/cli/flags" "github.com/nspcc-dev/neo-go/cli/flags"
"github.com/nspcc-dev/neo-go/cli/options" "github.com/nspcc-dev/neo-go/cli/options"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/rpc/client" "github.com/nspcc-dev/neo-go/pkg/rpc/client"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/emit" "github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode" "github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -80,14 +83,8 @@ func handleCandidate(ctx *cli.Context, method string) error {
addrFlag := ctx.Generic("address").(*flags.Address) addrFlag := ctx.Generic("address").(*flags.Address)
addr := addrFlag.Uint160() addr := addrFlag.Uint160()
acc := wall.GetAccount(addr) acc, err := getDecryptedAccount(wall, addr)
if acc == nil { if err != nil {
return cli.NewExitError(fmt.Errorf("can't find account for the address: %s", addrFlag), 1)
}
if pass, err := readPassword("Password > "); err != nil {
return cli.NewExitError(err, 1)
} else if err := acc.Decrypt(pass); err != nil {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }
@ -126,9 +123,9 @@ func handleVote(ctx *cli.Context) error {
addrFlag := ctx.Generic("address").(*flags.Address) addrFlag := ctx.Generic("address").(*flags.Address)
addr := addrFlag.Uint160() addr := addrFlag.Uint160()
acc := wall.GetAccount(addr) acc, err := getDecryptedAccount(wall, addr)
if acc == nil { if err != nil {
return cli.NewExitError(fmt.Errorf("can't find account for the address: %s", addrFlag), 1) return cli.NewExitError(err, 1)
} }
var pub *keys.PublicKey var pub *keys.PublicKey
@ -163,12 +160,6 @@ func handleVote(ctx *cli.Context) error {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }
if pass, err := readPassword("Password > "); err != nil {
return cli.NewExitError(err, 1)
} else if err := acc.Decrypt(pass); err != nil {
return cli.NewExitError(err, 1)
}
if err = acc.SignTx(tx); err != nil { if err = acc.SignTx(tx); err != nil {
return cli.NewExitError(fmt.Errorf("can't sign tx: %v", err), 1) return cli.NewExitError(fmt.Errorf("can't sign tx: %v", err), 1)
} }
@ -180,3 +171,17 @@ func handleVote(ctx *cli.Context) error {
fmt.Println(res.StringLE()) fmt.Println(res.StringLE())
return nil return nil
} }
func getDecryptedAccount(wall *wallet.Wallet, addr util.Uint160) (*wallet.Account, error) {
acc := wall.GetAccount(addr)
if acc == nil {
return nil, fmt.Errorf("can't find account for the address: %s", address.Uint160ToString(addr))
}
if pass, err := readPassword("Password > "); err != nil {
return nil, err
} else if err := acc.Decrypt(pass); err != nil {
return nil, err
}
return acc, nil
}

View file

@ -207,16 +207,9 @@ func claimGas(ctx *cli.Context) error {
return cli.NewExitError("address was not provided", 1) return cli.NewExitError("address was not provided", 1)
} }
scriptHash := addrFlag.Uint160() scriptHash := addrFlag.Uint160()
acc := wall.GetAccount(scriptHash) acc, err := getDecryptedAccount(wall, scriptHash)
if acc == nil {
return cli.NewExitError(fmt.Errorf("wallet contains no account for '%s'", addrFlag), 1)
}
pass, err := readPassword("Enter password > ")
if err != nil { if err != nil {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} else if err := acc.Decrypt(pass); err != nil {
return cli.NewExitError(err, 1)
} }
gctx, cancel := options.GetTimeoutContext(ctx) gctx, cancel := options.GetTimeoutContext(ctx)