2020-08-04 14:46:12 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-10-12 13:57:37 +00:00
|
|
|
"crypto/ecdsa"
|
2021-05-26 16:30:04 +00:00
|
|
|
"crypto/tls"
|
2020-10-12 13:57:37 +00:00
|
|
|
"errors"
|
2020-08-04 14:46:12 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-06-24 12:31:50 +00:00
|
|
|
"path/filepath"
|
2020-12-24 11:25:36 +00:00
|
|
|
"strings"
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2020-10-12 13:57:37 +00:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2021-04-21 12:27:32 +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"
|
2021-07-22 10:56:02 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-04-21 12:27:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2022-01-20 15:34:53 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/acl"
|
2021-06-20 16:11:49 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/misc"
|
2020-10-12 13:59:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/client"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/token"
|
2020-08-04 14:46:12 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2020-10-12 15:14:17 +00:00
|
|
|
const (
|
|
|
|
envPrefix = "NEOFS_CLI"
|
|
|
|
)
|
2020-10-12 14:35:04 +00:00
|
|
|
|
2020-12-24 11:25:36 +00:00
|
|
|
var xHeaders []string
|
|
|
|
|
2020-10-12 13:57:37 +00:00
|
|
|
// Global scope flags.
|
|
|
|
var (
|
2020-10-12 15:14:17 +00:00
|
|
|
cfgFile string
|
2020-10-12 13:57:37 +00:00
|
|
|
)
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
const (
|
|
|
|
// Common CLI flag keys, shorthands, default
|
|
|
|
// values and their usage descriptions.
|
|
|
|
generateKey = "generate-key"
|
2021-12-10 12:30:03 +00:00
|
|
|
generateKeyShorthand = "g"
|
2021-09-27 15:36:14 +00:00
|
|
|
generateKeyDefault = false
|
|
|
|
generateKeyUsage = "generate new private key"
|
|
|
|
|
|
|
|
walletPath = "wallet"
|
|
|
|
walletPathShorthand = "w"
|
|
|
|
walletPathDefault = ""
|
2022-01-19 10:56:10 +00:00
|
|
|
walletPathUsage = "WIF (NEP-2) string or path to the wallet or binary key"
|
2021-09-27 15:36:14 +00:00
|
|
|
|
|
|
|
address = "address"
|
|
|
|
addressShorthand = ""
|
|
|
|
addressDefault = ""
|
|
|
|
addressUsage = "address of wallet account"
|
|
|
|
|
2022-01-26 07:50:32 +00:00
|
|
|
password = "password"
|
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
rpc = "rpc-endpoint"
|
|
|
|
rpcShorthand = "r"
|
|
|
|
rpcDefault = ""
|
|
|
|
rpcUsage = "remote node address (as 'multiaddr' or '<host>:<port>')"
|
|
|
|
|
|
|
|
verbose = "verbose"
|
|
|
|
verboseShorthand = "v"
|
|
|
|
verboseDefault = false
|
|
|
|
verboseUsage = "verbose output"
|
|
|
|
|
|
|
|
ttl = "ttl"
|
|
|
|
ttlShorthand = ""
|
|
|
|
ttlDefault = 2
|
|
|
|
ttlUsage = "TTL value in request meta header"
|
|
|
|
|
|
|
|
xHeadersKey = "xhdr"
|
|
|
|
xHeadersShorthand = "x"
|
|
|
|
xHeadersUsage = "Request X-Headers in form of Key=Value"
|
|
|
|
)
|
|
|
|
|
|
|
|
var xHeadersDefault []string
|
|
|
|
|
2020-08-04 14:46:12 +00:00
|
|
|
// rootCmd represents the base command when called without any subcommands
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "neofs-cli",
|
|
|
|
Short: "Command Line Tool to work with NeoFS",
|
|
|
|
Long: `NeoFS CLI provides all basic interactions with NeoFS and it's services.
|
|
|
|
|
|
|
|
It contains commands for interaction with NeoFS nodes using different versions
|
|
|
|
of neofs-api and some useful utilities for compiling ACL rules from JSON
|
|
|
|
notation, managing container access through protocol gates, querying network map
|
|
|
|
and much more!`,
|
2021-06-20 16:11:49 +00:00
|
|
|
Run: entryPoint,
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 13:57:37 +00:00
|
|
|
var (
|
2020-10-12 13:59:33 +00:00
|
|
|
errInvalidKey = errors.New("provided key is incorrect")
|
|
|
|
errInvalidEndpoint = errors.New("provided RPC endpoint is incorrect")
|
2020-10-12 14:35:04 +00:00
|
|
|
errCantGenerateKey = errors.New("can't generate new private key")
|
2021-04-21 12:27:32 +00:00
|
|
|
errInvalidAddress = errors.New("--address option must be specified and valid")
|
|
|
|
errInvalidPassword = errors.New("invalid password for the encrypted key")
|
2020-10-12 13:57:37 +00:00
|
|
|
)
|
|
|
|
|
2020-08-04 14:46:12 +00:00
|
|
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
|
|
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
|
|
|
func Execute() {
|
2021-07-06 12:27:54 +00:00
|
|
|
err := rootCmd.Execute()
|
|
|
|
exitOnErr(rootCmd, err)
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
|
2020-11-05 13:49:05 +00:00
|
|
|
// use stdout as default output for cmd.Print()
|
|
|
|
rootCmd.SetOut(os.Stdout)
|
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
// Cobra supports persistent flags, which, if defined here,
|
|
|
|
// will be global for your application.
|
2020-10-12 13:57:37 +00:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.config/neofs-cli/config.yaml)")
|
2022-01-28 18:03:44 +00:00
|
|
|
rootCmd.PersistentFlags().BoolP(verbose, verboseShorthand, verboseDefault, verboseUsage)
|
|
|
|
|
|
|
|
_ = viper.BindPFlag(verbose, rootCmd.PersistentFlags().Lookup(verbose))
|
2020-10-12 15:14:17 +00:00
|
|
|
|
2020-08-04 14:46:12 +00:00
|
|
|
// Cobra also supports local flags, which will only run
|
|
|
|
// when this action is called directly.
|
2021-06-20 16:11:49 +00:00
|
|
|
rootCmd.Flags().Bool("version", false, "Application version and NeoFS API compatibility")
|
2022-01-20 15:34:53 +00:00
|
|
|
|
|
|
|
rootCmd.AddCommand(acl.Cmd)
|
2021-06-20 16:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func entryPoint(cmd *cobra.Command, _ []string) {
|
|
|
|
printVersion, _ := cmd.Flags().GetBool("version")
|
|
|
|
if printVersion {
|
|
|
|
cmd.Printf(
|
|
|
|
"Version: %s \nBuild: %s \nDebug: %s\n",
|
|
|
|
misc.Version,
|
|
|
|
misc.Build,
|
|
|
|
misc.Debug,
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = cmd.Usage()
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// initConfig reads in config file and ENV variables if set.
|
|
|
|
func initConfig() {
|
|
|
|
if cfgFile != "" {
|
|
|
|
// Use config file from the flag.
|
|
|
|
viper.SetConfigFile(cfgFile)
|
|
|
|
} else {
|
|
|
|
// Find home directory.
|
|
|
|
home, err := homedir.Dir()
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(rootCmd, err)
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2021-06-25 00:23:10 +00:00
|
|
|
// Search config in `$HOME/.config/neofs-cli/` with name "config.yaml"
|
2021-06-24 12:31:50 +00:00
|
|
|
viper.AddConfigPath(filepath.Join(home, ".config", "neofs-cli"))
|
|
|
|
viper.SetConfigName("config")
|
|
|
|
viper.SetConfigType("yaml")
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 15:14:17 +00:00
|
|
|
viper.SetEnvPrefix(envPrefix)
|
2020-08-04 14:46:12 +00:00
|
|
|
viper.AutomaticEnv() // read in environment variables that match
|
|
|
|
|
|
|
|
// If a config file is found, read it in.
|
|
|
|
if err := viper.ReadInConfig(); err == nil {
|
2020-10-14 12:36:27 +00:00
|
|
|
printVerbose("Using config file: %s", viper.ConfigFileUsed())
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-12 13:57:37 +00:00
|
|
|
|
2021-04-21 12:27:32 +00:00
|
|
|
const nep2Base58Length = 58
|
|
|
|
|
2020-10-12 13:57:37 +00:00
|
|
|
// getKey returns private key that was provided in global arguments.
|
|
|
|
func getKey() (*ecdsa.PrivateKey, error) {
|
2021-09-27 15:36:14 +00:00
|
|
|
if viper.GetBool(generateKey) {
|
2021-05-31 08:55:38 +00:00
|
|
|
priv, err := keys.NewPrivateKey()
|
2020-10-12 14:35:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errCantGenerateKey
|
|
|
|
}
|
2021-05-31 08:55:38 +00:00
|
|
|
return &priv.PrivateKey, nil
|
2021-04-21 12:27:32 +00:00
|
|
|
}
|
2022-01-26 07:53:30 +00:00
|
|
|
return getKeyNoGenerate()
|
|
|
|
}
|
2021-04-21 12:27:32 +00:00
|
|
|
|
2022-01-26 07:53:30 +00:00
|
|
|
func getKeyNoGenerate() (*ecdsa.PrivateKey, error) {
|
2022-01-19 10:56:10 +00:00
|
|
|
// Ideally we want to touch file-system on the last step.
|
|
|
|
// However, asking for NEP-2 password seems to be confusing if we provide a wallet.
|
|
|
|
// Thus we try keys in the following order:
|
|
|
|
// 1. WIF
|
|
|
|
// 2. Raw binary key
|
|
|
|
// 3. Wallet file
|
|
|
|
// 4. NEP-2 encrypted WIF.
|
|
|
|
keyDesc := viper.GetString(walletPath)
|
|
|
|
priv, err := keys.NewPrivateKeyFromWIF(keyDesc)
|
|
|
|
if err == nil {
|
|
|
|
return &priv.PrivateKey, nil
|
2021-06-15 15:51:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 10:56:10 +00:00
|
|
|
p, err := getKeyFromFile(keyDesc)
|
|
|
|
if err == nil {
|
|
|
|
return p, nil
|
2021-04-21 12:27:32 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 10:56:10 +00:00
|
|
|
w, err := wallet.NewWalletFromFile(keyDesc)
|
|
|
|
if err == nil {
|
|
|
|
return getKeyFromWallet(w, viper.GetString(address))
|
2021-04-21 12:27:32 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 10:56:10 +00:00
|
|
|
if len(keyDesc) == nep2Base58Length {
|
|
|
|
return getKeyFromNEP2(keyDesc)
|
2021-06-15 15:56:05 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 10:56:10 +00:00
|
|
|
return nil, errInvalidKey
|
2021-04-21 12:27:32 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 07:50:32 +00:00
|
|
|
func getPassword() (string, error) {
|
2022-02-04 15:06:30 +00:00
|
|
|
// this check allows empty passwords
|
|
|
|
if viper.IsSet(password) {
|
|
|
|
return viper.GetString(password), nil
|
2022-01-26 07:50:32 +00:00
|
|
|
}
|
2022-02-04 15:06:30 +00:00
|
|
|
|
2022-01-26 07:50:32 +00:00
|
|
|
return input.ReadPassword("Enter password > ")
|
|
|
|
}
|
|
|
|
|
2021-06-15 15:51:33 +00:00
|
|
|
func getKeyFromFile(keyPath string) (*ecdsa.PrivateKey, error) {
|
2021-06-28 14:01:31 +00:00
|
|
|
data, err := os.ReadFile(keyPath)
|
2021-06-15 15:51:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: %v", errInvalidKey, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
priv, err := keys.NewPrivateKeyFromBytes(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: %v", errInvalidKey, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &priv.PrivateKey, nil
|
|
|
|
}
|
|
|
|
|
2021-04-21 12:27:32 +00:00
|
|
|
func getKeyFromNEP2(encryptedWif string) (*ecdsa.PrivateKey, error) {
|
2022-01-26 07:50:32 +00:00
|
|
|
pass, err := getPassword()
|
2021-04-21 12:27:32 +00:00
|
|
|
if err != nil {
|
|
|
|
printVerbose("Can't read password: %v", err)
|
|
|
|
return nil, errInvalidPassword
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:24:04 +00:00
|
|
|
k, err := keys.NEP2Decrypt(encryptedWif, pass, keys.NEP2ScryptParams())
|
2020-10-12 13:57:37 +00:00
|
|
|
if err != nil {
|
2021-04-21 12:27:32 +00:00
|
|
|
printVerbose("Invalid key or password: %v", err)
|
|
|
|
return nil, errInvalidPassword
|
2020-10-12 13:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 12:27:32 +00:00
|
|
|
return &k.PrivateKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getKeyFromWallet(w *wallet.Wallet, addrStr string) (*ecdsa.PrivateKey, error) {
|
2021-07-22 10:56:02 +00:00
|
|
|
var (
|
|
|
|
addr util.Uint160
|
|
|
|
err error
|
|
|
|
)
|
2021-04-21 12:27:32 +00:00
|
|
|
|
2021-07-22 10:56:02 +00:00
|
|
|
if addrStr == "" {
|
|
|
|
printVerbose("Using default wallet address")
|
|
|
|
addr = w.GetChangeAddress()
|
|
|
|
} else {
|
|
|
|
addr, err = flags.ParseAddress(addrStr)
|
|
|
|
if err != nil {
|
|
|
|
printVerbose("Can't parse address: %s", addrStr)
|
|
|
|
return nil, errInvalidAddress
|
|
|
|
}
|
2021-04-21 12:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
acc := w.GetAccount(addr)
|
|
|
|
if acc == nil {
|
|
|
|
printVerbose("Can't find wallet account for %s", addrStr)
|
|
|
|
return nil, errInvalidAddress
|
|
|
|
}
|
|
|
|
|
2022-01-26 07:50:32 +00:00
|
|
|
pass, err := getPassword()
|
2021-04-21 12:27:32 +00:00
|
|
|
if err != nil {
|
|
|
|
printVerbose("Can't read password: %v", err)
|
|
|
|
return nil, errInvalidPassword
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:24:04 +00:00
|
|
|
if err := acc.Decrypt(pass, keys.NEP2ScryptParams()); err != nil {
|
2021-04-21 12:27:32 +00:00
|
|
|
printVerbose("Can't decrypt account: %v", err)
|
|
|
|
return nil, errInvalidPassword
|
|
|
|
}
|
|
|
|
|
|
|
|
return &acc.PrivateKey().PrivateKey, nil
|
2020-10-12 13:57:37 +00:00
|
|
|
}
|
2020-10-12 13:59:33 +00:00
|
|
|
|
|
|
|
// getEndpointAddress returns network address structure that stores multiaddr
|
|
|
|
// inside, parsed from global arguments.
|
2021-10-15 11:24:02 +00:00
|
|
|
func getEndpointAddress(endpointFlag string) (addr network.Address, err error) {
|
|
|
|
endpoint := viper.GetString(endpointFlag)
|
2020-10-12 15:14:17 +00:00
|
|
|
|
2021-06-18 06:00:21 +00:00
|
|
|
err = addr.FromString(endpoint)
|
2020-10-12 13:59:33 +00:00
|
|
|
if err != nil {
|
2021-06-18 06:00:21 +00:00
|
|
|
err = errInvalidEndpoint
|
2020-10-12 13:59:33 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 06:00:21 +00:00
|
|
|
return
|
2020-10-12 13:59:33 +00:00
|
|
|
}
|
2020-10-12 14:19:57 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
type clientWithKey interface {
|
2022-01-13 15:01:50 +00:00
|
|
|
SetClient(*client.Client)
|
2021-10-28 14:48:46 +00:00
|
|
|
SetKey(*ecdsa.PrivateKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// reads private key from command args and call prepareAPIClientWithKey with it.
|
|
|
|
func prepareAPIClient(cmd *cobra.Command, dst ...clientWithKey) {
|
|
|
|
key, err := getKey()
|
|
|
|
exitOnErr(cmd, errf("get private key: %w", err))
|
|
|
|
|
|
|
|
prepareAPIClientWithKey(cmd, key, dst...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// creates NeoFS API client and writes it to target along with the private key.
|
|
|
|
func prepareAPIClientWithKey(cmd *cobra.Command, key *ecdsa.PrivateKey, dst ...clientWithKey) {
|
|
|
|
cli, err := getSDKClient(key)
|
|
|
|
exitOnErr(cmd, errf("create API client: %w", err))
|
|
|
|
|
|
|
|
for _, d := range dst {
|
|
|
|
d.SetClient(cli)
|
|
|
|
d.SetKey(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type bearerPrm interface {
|
|
|
|
SetBearerToken(prm *token.BearerToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareBearerPrm(cmd *cobra.Command, prm bearerPrm) {
|
|
|
|
btok, err := getBearerToken(cmd, bearerTokenFlag)
|
|
|
|
exitOnErr(cmd, errf("bearer token: %w", err))
|
|
|
|
|
|
|
|
prm.SetBearerToken(btok)
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:19:57 +00:00
|
|
|
// getSDKClient returns default neofs-api-go sdk client. Consider using
|
|
|
|
// opts... to provide TTL or other global configuration flags.
|
2022-01-13 15:01:50 +00:00
|
|
|
func getSDKClient(key *ecdsa.PrivateKey) (*client.Client, error) {
|
2021-10-15 11:24:02 +00:00
|
|
|
netAddr, err := getEndpointAddress(rpc)
|
2020-10-12 14:19:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-26 16:30:04 +00:00
|
|
|
options := []client.Option{
|
2021-06-16 11:12:03 +00:00
|
|
|
client.WithAddress(netAddr.HostAddr()),
|
2021-05-26 16:30:04 +00:00
|
|
|
client.WithDefaultPrivateKey(key),
|
2022-01-13 15:01:50 +00:00
|
|
|
client.WithNeoFSErrorParsing(),
|
2021-05-26 16:30:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if netAddr.TLSEnabled() {
|
|
|
|
options = append(options, client.WithTLSConfig(&tls.Config{}))
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := client.New(options...)
|
2021-09-01 16:33:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("coult not init api client:%w", err)
|
|
|
|
}
|
2021-05-26 16:30:04 +00:00
|
|
|
|
2021-04-21 12:27:32 +00:00
|
|
|
return c, err
|
2020-10-12 14:19:57 +00:00
|
|
|
}
|
2020-10-12 14:31:00 +00:00
|
|
|
|
2020-11-16 08:25:42 +00:00
|
|
|
func getTTL() uint32 {
|
2021-09-27 15:36:14 +00:00
|
|
|
ttl := viper.GetUint32(ttl)
|
2020-11-16 08:25:42 +00:00
|
|
|
printVerbose("TTL: %d", ttl)
|
|
|
|
|
|
|
|
return ttl
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:31:00 +00:00
|
|
|
// ownerFromString converts string with NEO3 wallet address to neofs owner ID.
|
|
|
|
func ownerFromString(s string) (*owner.ID, error) {
|
2020-10-15 08:45:00 +00:00
|
|
|
result := owner.NewID()
|
2020-10-12 14:31:00 +00:00
|
|
|
|
2020-10-15 08:45:00 +00:00
|
|
|
err := result.Parse(s)
|
|
|
|
if err != nil {
|
2020-10-12 14:31:00 +00:00
|
|
|
return nil, errors.New("can't decode owner ID wallet address")
|
|
|
|
}
|
|
|
|
|
2020-10-15 08:45:00 +00:00
|
|
|
return result, nil
|
2020-10-12 14:31:00 +00:00
|
|
|
}
|
2020-10-14 12:36:27 +00:00
|
|
|
|
|
|
|
func printVerbose(format string, a ...interface{}) {
|
2021-09-27 15:36:14 +00:00
|
|
|
if viper.GetBool(verbose) {
|
2020-10-14 12:36:27 +00:00
|
|
|
fmt.Printf(format+"\n", a...)
|
|
|
|
}
|
|
|
|
}
|
2020-12-24 11:25:36 +00:00
|
|
|
|
2021-11-10 07:08:33 +00:00
|
|
|
func parseXHeaders() []*session.XHeader {
|
|
|
|
xs := make([]*session.XHeader, 0, len(xHeaders))
|
2020-12-24 11:25:36 +00:00
|
|
|
|
|
|
|
for i := range xHeaders {
|
|
|
|
kv := strings.SplitN(xHeaders[i], "=", 2)
|
|
|
|
if len(kv) != 2 {
|
|
|
|
panic(fmt.Errorf("invalid X-Header format: %s", xHeaders[i]))
|
|
|
|
}
|
|
|
|
|
2021-11-10 07:08:33 +00:00
|
|
|
x := session.NewXHeader()
|
2020-12-24 11:25:36 +00:00
|
|
|
x.SetKey(kv[0])
|
|
|
|
x.SetValue(kv[1])
|
|
|
|
|
|
|
|
xs = append(xs, x)
|
|
|
|
}
|
|
|
|
|
|
|
|
return xs
|
|
|
|
}
|
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
// add common flags to the command:
|
|
|
|
// - key;
|
|
|
|
// - wallet;
|
|
|
|
// - WIF;
|
|
|
|
// - address;
|
|
|
|
// - RPC;
|
|
|
|
func initCommonFlags(cmd *cobra.Command) {
|
|
|
|
ff := cmd.Flags()
|
|
|
|
|
|
|
|
ff.BoolP(generateKey, generateKeyShorthand, generateKeyDefault, generateKeyUsage)
|
|
|
|
ff.StringP(walletPath, walletPathShorthand, walletPathDefault, walletPathUsage)
|
|
|
|
ff.StringP(address, addressShorthand, addressDefault, addressUsage)
|
|
|
|
ff.StringP(rpc, rpcShorthand, rpcDefault, rpcUsage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// bind common command flags to the viper
|
|
|
|
func bindCommonFlags(cmd *cobra.Command) {
|
|
|
|
ff := cmd.Flags()
|
|
|
|
|
|
|
|
_ = viper.BindPFlag(generateKey, ff.Lookup(generateKey))
|
|
|
|
_ = viper.BindPFlag(walletPath, ff.Lookup(walletPath))
|
|
|
|
_ = viper.BindPFlag(address, ff.Lookup(address))
|
|
|
|
_ = viper.BindPFlag(rpc, ff.Lookup(rpc))
|
|
|
|
}
|
2021-10-07 10:37:57 +00:00
|
|
|
|
|
|
|
func bindAPIFlags(cmd *cobra.Command) {
|
|
|
|
ff := cmd.Flags()
|
|
|
|
|
|
|
|
_ = viper.BindPFlag(ttl, ff.Lookup(ttl))
|
|
|
|
_ = viper.BindPFlag(xHeadersKey, ff.Lookup(xHeadersKey))
|
|
|
|
}
|