Change-Id: I1b73e561a8daad67d0a8ffc0d293cbdd09aaab6b Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
70 lines
2 KiB
Go
70 lines
2 KiB
Go
package zombie
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"fmt"
|
|
"os"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-node/config"
|
|
nodeconfig "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-node/config/node"
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
|
"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"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func getPrivateKey(cmd *cobra.Command, appCfg *config.Config) *ecdsa.PrivateKey {
|
|
keyDesc := viper.GetString(walletFlag)
|
|
if keyDesc == "" {
|
|
return &nodeconfig.Key(appCfg).PrivateKey
|
|
}
|
|
data, err := os.ReadFile(keyDesc)
|
|
commonCmd.ExitOnErr(cmd, "open wallet file: %w", err)
|
|
|
|
priv, err := keys.NewPrivateKeyFromBytes(data)
|
|
if err != nil {
|
|
w, err := wallet.NewWalletFromFile(keyDesc)
|
|
commonCmd.ExitOnErr(cmd, "provided key is incorrect, only wallet or binary key supported: %w", err)
|
|
return fromWallet(cmd, w, viper.GetString(addressFlag))
|
|
}
|
|
return &priv.PrivateKey
|
|
}
|
|
|
|
func fromWallet(cmd *cobra.Command, w *wallet.Wallet, addrStr string) *ecdsa.PrivateKey {
|
|
var (
|
|
addr util.Uint160
|
|
err error
|
|
)
|
|
|
|
if addrStr == "" {
|
|
addr = w.GetChangeAddress()
|
|
} else {
|
|
addr, err = flags.ParseAddress(addrStr)
|
|
commonCmd.ExitOnErr(cmd, "--address option must be specified and valid: %w", err)
|
|
}
|
|
|
|
acc := w.GetAccount(addr)
|
|
if acc == nil {
|
|
commonCmd.ExitOnErr(cmd, "--address option must be specified and valid: %w", fmt.Errorf("can't find wallet account for %s", addrStr))
|
|
}
|
|
|
|
pass, err := getPassword()
|
|
commonCmd.ExitOnErr(cmd, "invalid password for the encrypted key: %w", err)
|
|
|
|
commonCmd.ExitOnErr(cmd, "can't decrypt account: %w", acc.Decrypt(pass, keys.NEP2ScryptParams()))
|
|
|
|
return &acc.PrivateKey().PrivateKey
|
|
}
|
|
|
|
func getPassword() (string, error) {
|
|
// this check allows empty passwords
|
|
if viper.IsSet("password") {
|
|
return viper.GetString("password"), nil
|
|
}
|
|
|
|
return input.ReadPassword("Enter password > ")
|
|
}
|