[#1024] adm: Change private key to wallet in subnet flags

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-12-08 14:06:35 +03:00 committed by LeL
parent 90fd883e32
commit 15ff98c229

View file

@ -5,9 +5,12 @@ import (
"errors" "errors"
"fmt" "fmt"
"math" "math"
"os"
"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/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/nspcc-dev/neofs-node/cmd/neofs-adm/internal/modules/morph/internal" "github.com/nspcc-dev/neofs-node/cmd/neofs-adm/internal/modules/morph/internal"
"github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/client"
morphsubnet "github.com/nspcc-dev/neofs-node/pkg/morph/client/subnet" morphsubnet "github.com/nspcc-dev/neofs-node/pkg/morph/client/subnet"
@ -23,8 +26,10 @@ import (
const ( const (
// Neo RPC endpoint // Neo RPC endpoint
flagSubnetEndpoint = endpointFlag flagSubnetEndpoint = endpointFlag
// filepath to private key // filepath to wallet
flagSubnetKey = "key" flagSubnetWallet = "wallet"
// address in the wallet, optional
flagSubnetAddress = "address"
) )
func viperBindFlags(cmd *cobra.Command, flags ...string) { func viperBindFlags(cmd *cobra.Command, flags ...string) {
@ -40,7 +45,8 @@ var cmdSubnet = &cobra.Command{
PreRun: func(cmd *cobra.Command, _ []string) { PreRun: func(cmd *cobra.Command, _ []string) {
viperBindFlags(cmd, viperBindFlags(cmd,
flagSubnetEndpoint, flagSubnetEndpoint,
flagSubnetKey, flagSubnetWallet,
flagSubnetAddress,
) )
}, },
} }
@ -53,26 +59,56 @@ const (
flagSubnetGroup = "group" flagSubnetGroup = "group"
) )
// reads private key from the filepath configured in flagSubnetKey flag. // reads wallet from the filepath configured in flagSubnetWallet flag,
// looks for address specified in flagSubnetAddress flag (uses default
// address if flag is empty) and decrypts private key.
func readSubnetKey(key *keys.PrivateKey) error { func readSubnetKey(key *keys.PrivateKey) error {
// read key from file // read wallet from file
keyPath := viper.GetString(flagSubnetKey)
if keyPath == "" { walletPath := viper.GetString(flagSubnetWallet)
return errors.New("missing path to private key") if walletPath == "" {
return errors.New("missing path to wallet")
} }
data, err := os.ReadFile(keyPath) w, err := wallet.NewWalletFromFile(walletPath)
if err != nil { if err != nil {
return fmt.Errorf("read private key file: %w", err) return fmt.Errorf("read wallet from file: %w", err)
} }
// decode key // read account from the wallet
k, err := keys.NewPrivateKeyFromBytes(data)
var (
addr util.Uint160
addrStr = viper.GetString(flagSubnetAddress)
)
if addrStr == "" {
addr = w.GetChangeAddress()
} else {
addr, err = flags.ParseAddress(addrStr)
if err != nil {
return fmt.Errorf("read wallet address: %w", err)
}
}
acc := w.GetAccount(addr)
if acc == nil {
return fmt.Errorf("address %s not found in %s", addrStr, walletPath)
}
// read password
pass, err := input.ReadPassword("Enter password > ")
if err != nil { if err != nil {
return fmt.Errorf("decode private key: %w", err) return fmt.Errorf("read password: %w", err)
} }
*key = *k // decrypt with just read password
err = acc.Decrypt(pass, keys.NEP2ScryptParams())
if err != nil {
return fmt.Errorf("decrypt wallet: %w", err)
}
*key = *acc.PrivateKey()
return nil return nil
} }
@ -850,8 +886,9 @@ func init() {
cmdSubnetFlags := cmdSubnet.PersistentFlags() cmdSubnetFlags := cmdSubnet.PersistentFlags()
cmdSubnetFlags.StringP(flagSubnetEndpoint, "r", "", "N3 RPC node endpoint") cmdSubnetFlags.StringP(flagSubnetEndpoint, "r", "", "N3 RPC node endpoint")
_ = cmdSubnet.MarkFlagRequired(flagSubnetEndpoint) _ = cmdSubnet.MarkFlagRequired(flagSubnetEndpoint)
cmdSubnetFlags.StringP(flagSubnetKey, "k", "", "Path to file with private key") cmdSubnetFlags.StringP(flagSubnetWallet, "w", "", "Path to file with wallet")
_ = cmdSubnet.MarkFlagRequired(flagSubnetKey) _ = cmdSubnet.MarkFlagRequired(flagSubnetWallet)
cmdSubnetFlags.StringP(flagSubnetAddress, "a", "", "Address in the wallet, optional")
// add all subnet commands to corresponding command section // add all subnet commands to corresponding command section
addCommandInheritPreRun(cmdSubnet, addCommandInheritPreRun(cmdSubnet,