2022-03-28 10:23:45 +00:00
|
|
|
package key
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
|
|
|
|
2022-12-27 09:36:30 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/common"
|
2022-03-28 10:23:45 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/flags"
|
|
|
|
"github.com/nspcc-dev/neo-go/cli/input"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2022-12-27 09:36:30 +00:00
|
|
|
"github.com/spf13/cobra"
|
2022-03-28 10:23:45 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Key-related errors.
|
|
|
|
var (
|
2022-09-16 06:48:12 +00:00
|
|
|
ErrFs = errors.New("unable to read file from given path")
|
|
|
|
ErrInvalidKey = errors.New("provided key is incorrect, only wallet or binary key supported")
|
2022-03-28 10:23:45 +00:00
|
|
|
ErrInvalidAddress = errors.New("--address option must be specified and valid")
|
|
|
|
ErrInvalidPassword = errors.New("invalid password for the encrypted key")
|
|
|
|
)
|
|
|
|
|
|
|
|
// FromWallet returns private key of the wallet account.
|
2022-12-27 09:36:30 +00:00
|
|
|
func FromWallet(cmd *cobra.Command, w *wallet.Wallet, addrStr string) (*ecdsa.PrivateKey, error) {
|
2022-03-28 10:23:45 +00:00
|
|
|
var (
|
|
|
|
addr util.Uint160
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if addrStr == "" {
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "Using default wallet address")
|
2022-03-28 10:23:45 +00:00
|
|
|
addr = w.GetChangeAddress()
|
|
|
|
} else {
|
|
|
|
addr, err = flags.ParseAddress(addrStr)
|
|
|
|
if err != nil {
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "Can't parse address: %s", addrStr)
|
2022-03-28 10:23:45 +00:00
|
|
|
return nil, ErrInvalidAddress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := w.GetAccount(addr)
|
|
|
|
if acc == nil {
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "Can't find wallet account for %s", addrStr)
|
2022-03-28 10:23:45 +00:00
|
|
|
return nil, ErrInvalidAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
pass, err := getPassword()
|
|
|
|
if err != nil {
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "Can't read password: %v", err)
|
2022-03-28 10:23:45 +00:00
|
|
|
return nil, ErrInvalidPassword
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := acc.Decrypt(pass, keys.NEP2ScryptParams()); err != nil {
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "Can't decrypt account: %v", err)
|
2022-03-28 10:23:45 +00:00
|
|
|
return nil, ErrInvalidPassword
|
|
|
|
}
|
|
|
|
|
|
|
|
return &acc.PrivateKey().PrivateKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPassword() (string, error) {
|
|
|
|
// this check allows empty passwords
|
|
|
|
if viper.IsSet("password") {
|
|
|
|
return viper.GetString("password"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return input.ReadPassword("Enter password > ")
|
|
|
|
}
|