From 76cfcc242cd2001cc07104326a8f0a7a9a590256 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Wed, 28 Sep 2022 11:19:57 +0300 Subject: [PATCH] [#1820] neofs-adm: Add wallet-address flag in refill command Signed-off-by: Alex Vanin --- CHANGELOG.md | 1 + .../internal/modules/morph/generate.go | 73 +++++++++++-------- cmd/neofs-adm/internal/modules/morph/root.go | 3 + 3 files changed, 47 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6aa3001c5..f9a983b57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Changelog for NeoFS Node - Validate storage node configuration before node startup (#1805) - `neofs-node -check` command to check the configuration file (#1805) - `flush-cache` control service command to flush write-cache (#1806) +- `wallet-address` flag in `neofs-adm morph refill-gas` command (#1820) ### Changed diff --git a/cmd/neofs-adm/internal/modules/morph/generate.go b/cmd/neofs-adm/internal/modules/morph/generate.go index 26c0530d2..be9b8db49 100644 --- a/cmd/neofs-adm/internal/modules/morph/generate.go +++ b/cmd/neofs-adm/internal/modules/morph/generate.go @@ -8,10 +8,12 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" "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/encoding/fixedn" "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" + "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/opcode" "github.com/nspcc-dev/neo-go/pkg/wallet" @@ -133,44 +135,55 @@ func generateStorageCreds(cmd *cobra.Command, _ []string) error { return refillGas(cmd, storageGasConfigFlag, true) } -func refillGas(cmd *cobra.Command, gasFlag string, createWallet bool) error { +func refillGas(cmd *cobra.Command, gasFlag string, createWallet bool) (err error) { // storage wallet path is not part of the config - storageWalletPath, err := cmd.Flags().GetString(storageWalletFlag) - if err != nil { - return err - } - if storageWalletPath == "" { - return fmt.Errorf("missing wallet path (use '--%s ')", storageWalletFlag) - } + storageWalletPath, _ := cmd.Flags().GetString(storageWalletFlag) + // wallet address is not part of the config + walletAddress, _ := cmd.Flags().GetString(walletAddressFlag) - var w *wallet.Wallet + var gasReceiver util.Uint160 - if createWallet { - w, err = wallet.NewWallet(storageWalletPath) - } else { - w, err = wallet.NewWalletFromFile(storageWalletPath) - } - - if err != nil { - return fmt.Errorf("can't create wallet: %w", err) - } - - if createWallet { - var password string - - label, _ := cmd.Flags().GetString(storageWalletLabelFlag) - password, err := config.GetStoragePassword(viper.GetViper(), label) + if len(walletAddress) != 0 { + gasReceiver, err = address.StringToUint160(walletAddress) if err != nil { - return fmt.Errorf("can't fetch password: %w", err) + return fmt.Errorf("invalid wallet address %s: %w", walletAddress, err) + } + } else { + if storageWalletPath == "" { + return fmt.Errorf("missing wallet path (use '--%s ')", storageWalletFlag) } - if label == "" { - label = singleAccountName + var w *wallet.Wallet + + if createWallet { + w, err = wallet.NewWallet(storageWalletPath) + } else { + w, err = wallet.NewWalletFromFile(storageWalletPath) } - if err := w.CreateAccount(label, password); err != nil { - return fmt.Errorf("can't create account: %w", err) + if err != nil { + return fmt.Errorf("can't create wallet: %w", err) } + + if createWallet { + var password string + + label, _ := cmd.Flags().GetString(storageWalletLabelFlag) + password, err := config.GetStoragePassword(viper.GetViper(), label) + if err != nil { + return fmt.Errorf("can't fetch password: %w", err) + } + + if label == "" { + label = singleAccountName + } + + if err := w.CreateAccount(label, password); err != nil { + return fmt.Errorf("can't create account: %w", err) + } + } + + gasReceiver = w.Accounts[0].Contract.ScriptHash() } gasStr := viper.GetString(gasFlag) @@ -189,7 +202,7 @@ func refillGas(cmd *cobra.Command, gasFlag string, createWallet bool) error { bw := io.NewBufBinWriter() emit.AppCall(bw.BinWriter, gasHash, "transfer", callflag.All, - wCtx.CommitteeAcc.Contract.ScriptHash(), w.Accounts[0].Contract.ScriptHash(), int64(gasAmount), nil) + wCtx.CommitteeAcc.Contract.ScriptHash(), gasReceiver, int64(gasAmount), nil) emit.Opcodes(bw.BinWriter, opcode.ASSERT) if bw.Err != nil { return fmt.Errorf("BUG: invalid transfer arguments: %w", bw.Err) diff --git a/cmd/neofs-adm/internal/modules/morph/root.go b/cmd/neofs-adm/internal/modules/morph/root.go index 3a354123d..223e7b043 100644 --- a/cmd/neofs-adm/internal/modules/morph/root.go +++ b/cmd/neofs-adm/internal/modules/morph/root.go @@ -40,6 +40,7 @@ const ( notaryDepositTillFlag = "till" localDumpFlag = "local-dump" protoConfigPath = "protocol" + walletAddressFlag = "wallet-address" ) var ( @@ -296,7 +297,9 @@ func init() { refillGasCmd.Flags().String(alphabetWalletsFlag, "", "path to alphabet wallets dir") refillGasCmd.Flags().StringP(endpointFlag, "r", "", "N3 RPC node endpoint") refillGasCmd.Flags().String(storageWalletFlag, "", "path to storage node wallet") + refillGasCmd.Flags().String(walletAddressFlag, "", "address of wallet") refillGasCmd.Flags().String(refillGasAmountFlag, "", "additional amount of GAS to transfer") + refillGasCmd.MarkFlagsMutuallyExclusive(walletAddressFlag, storageWalletFlag) RootCmd.AddCommand(cmdSubnet)