github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/cmd/bytomcli/commands/account.go (about)

     1  package commands
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  
     7  	"github.com/spf13/cobra"
     8  	jww "github.com/spf13/jwalterweatherman"
     9  
    10  	"github.com/bytom/bytom/crypto/ed25519/chainkd"
    11  	"github.com/bytom/bytom/util"
    12  )
    13  
    14  func init() {
    15  	createAccountCmd.PersistentFlags().IntVarP(&accountQuorum, "quorom", "q", 1, "quorum must be greater than 0 and less than or equal to the number of signers")
    16  	createAccountCmd.PersistentFlags().StringVarP(&accountToken, "access", "a", "", "access token")
    17  
    18  	updateAccountAliasCmd.PersistentFlags().StringVar(&accountID, "id", "", "account ID")
    19  	updateAccountAliasCmd.PersistentFlags().StringVar(&accountAlias, "alias", "", "account alias")
    20  
    21  	listAccountsCmd.PersistentFlags().StringVar(&accountID, "id", "", "account ID")
    22  	listAccountsCmd.PersistentFlags().StringVar(&accountAlias, "alias", "", "account alias")
    23  
    24  	deleteAccountCmd.PersistentFlags().StringVar(&accountID, "id", "", "account ID")
    25  	deleteAccountCmd.PersistentFlags().StringVar(&accountAlias, "alias", "", "account alias")
    26  
    27  	listAddressesCmd.PersistentFlags().StringVar(&accountID, "id", "", "account ID")
    28  	listAddressesCmd.PersistentFlags().StringVar(&accountAlias, "alias", "", "account alias")
    29  
    30  	listBalancesCmd.PersistentFlags().StringVar(&accountID, "id", "", "account ID")
    31  	listBalancesCmd.PersistentFlags().StringVar(&accountAlias, "alias", "", "account alias")
    32  
    33  	listUnspentOutputsCmd.PersistentFlags().StringVar(&accountID, "account_id", "", "account ID")
    34  	listUnspentOutputsCmd.PersistentFlags().StringVar(&accountAlias, "account_alias", "", "account alias")
    35  	listUnspentOutputsCmd.PersistentFlags().StringVar(&outputID, "id", "", "ID of unspent output")
    36  	listUnspentOutputsCmd.PersistentFlags().BoolVar(&unconfirmed, "unconfirmed", false, "list unconfirmed unspent outputs")
    37  	listUnspentOutputsCmd.PersistentFlags().BoolVar(&smartContract, "contract", false, "list smart contract unspent outputs")
    38  	listUnspentOutputsCmd.PersistentFlags().IntVar(&from, "from", 0, "the starting position of a page")
    39  	listUnspentOutputsCmd.PersistentFlags().IntVar(&count, "count", 0, "the longest count per page")
    40  }
    41  
    42  var (
    43  	accountID     = ""
    44  	accountAlias  = ""
    45  	accountQuorum = 1
    46  	accountToken  = ""
    47  	outputID      = ""
    48  	smartContract = false
    49  	from          = 0
    50  	count         = 0
    51  )
    52  
    53  var createAccountCmd = &cobra.Command{
    54  	Use:   "create-account <alias> <xpub(s)>",
    55  	Short: "Create an account",
    56  	Args:  cobra.MinimumNArgs(2),
    57  	Run: func(cmd *cobra.Command, args []string) {
    58  		ins := accountIns{}
    59  
    60  		for _, x := range args[1:] {
    61  			xpub := chainkd.XPub{}
    62  			if err := xpub.UnmarshalText([]byte(x)); err != nil {
    63  				jww.ERROR.Println(err)
    64  				os.Exit(util.ErrLocalExe)
    65  			}
    66  			ins.RootXPubs = append(ins.RootXPubs, xpub)
    67  		}
    68  
    69  		ins.Quorum = accountQuorum
    70  		ins.Alias = args[0]
    71  		ins.AccessToken = accountToken
    72  
    73  		data, exitCode := util.ClientCall("/create-account", &ins)
    74  		if exitCode != util.Success {
    75  			os.Exit(exitCode)
    76  		}
    77  
    78  		printJSON(data)
    79  	},
    80  }
    81  
    82  var updateAccountAliasCmd = &cobra.Command{
    83  	Use:   "update-account-alias <newAlias>",
    84  	Short: "update account alias",
    85  	Args:  cobra.ExactArgs(1),
    86  	Run: func(cmd *cobra.Command, args []string) {
    87  		var ins = struct {
    88  			AccountID    string `json:"account_id"`
    89  			AccountAlias string `json:"account_alias"`
    90  			NewAlias     string `json:"new_alias"`
    91  		}{AccountID: accountID, AccountAlias: accountAlias, NewAlias: args[0]}
    92  
    93  		_, exitCode := util.ClientCall("/update-account-alias", &ins)
    94  		if exitCode != util.Success {
    95  			os.Exit(exitCode)
    96  		}
    97  
    98  		jww.FEEDBACK.Println("Successfully update account alias")
    99  	},
   100  }
   101  
   102  var listAccountsCmd = &cobra.Command{
   103  	Use:   "list-accounts",
   104  	Short: "List the existing accounts",
   105  	Args:  cobra.NoArgs,
   106  	Run: func(cmd *cobra.Command, args []string) {
   107  		filter := struct {
   108  			ID    string `json:"id"`
   109  			Alias string `json:"alias"`
   110  		}{ID: accountID, Alias: accountAlias}
   111  
   112  		data, exitCode := util.ClientCall("/list-accounts", &filter)
   113  		if exitCode != util.Success {
   114  			os.Exit(exitCode)
   115  		}
   116  
   117  		printJSONList(data)
   118  	},
   119  }
   120  
   121  var deleteAccountCmd = &cobra.Command{
   122  	Use:   "delete-account <[accountAlias]|[accountID]>",
   123  	Short: "Delete the existing account",
   124  	Args:  cobra.NoArgs,
   125  	Run: func(cmd *cobra.Command, args []string) {
   126  		var ins = struct {
   127  			AccountID    string `json:"account_id"`
   128  			AccountAlias string `json:"account_alias"`
   129  		}{AccountID: accountID, AccountAlias: accountAlias}
   130  
   131  		if _, exitCode := util.ClientCall("/delete-account", &ins); exitCode != util.Success {
   132  			os.Exit(exitCode)
   133  		}
   134  
   135  		jww.FEEDBACK.Println("Successfully delete account")
   136  	},
   137  }
   138  
   139  var createAccountReceiverCmd = &cobra.Command{
   140  	Use:   "create-account-receiver <accountAlias> [accountID]",
   141  	Short: "Create an account receiver",
   142  	Args:  cobra.RangeArgs(1, 2),
   143  	Run: func(cmd *cobra.Command, args []string) {
   144  		var ins = struct {
   145  			AccountID    string `json:"account_id"`
   146  			AccountAlias string `json:"account_alias"`
   147  		}{AccountAlias: args[0]}
   148  
   149  		if len(args) == 2 {
   150  			ins.AccountID = args[1]
   151  		}
   152  
   153  		data, exitCode := util.ClientCall("/create-account-receiver", &ins)
   154  		if exitCode != util.Success {
   155  			os.Exit(exitCode)
   156  		}
   157  
   158  		printJSON(data)
   159  	},
   160  }
   161  
   162  var listAddressesCmd = &cobra.Command{
   163  	Use:   "list-addresses",
   164  	Short: "List the account addresses",
   165  	Args:  cobra.NoArgs,
   166  	Run: func(cmd *cobra.Command, args []string) {
   167  		var ins = struct {
   168  			AccountID    string `json:"account_id"`
   169  			AccountAlias string `json:"account_alias"`
   170  		}{AccountID: accountID, AccountAlias: accountAlias}
   171  
   172  		data, exitCode := util.ClientCall("/list-addresses", &ins)
   173  		if exitCode != util.Success {
   174  			os.Exit(exitCode)
   175  		}
   176  
   177  		printJSONList(data)
   178  	},
   179  }
   180  
   181  var validateAddressCmd = &cobra.Command{
   182  	Use:   "validate-address <address>",
   183  	Short: "validate the account addresses",
   184  	Args:  cobra.ExactArgs(1),
   185  	Run: func(cmd *cobra.Command, args []string) {
   186  		var ins = struct {
   187  			Address string `json:"address"`
   188  		}{Address: args[0]}
   189  
   190  		data, exitCode := util.ClientCall("/validate-address", &ins)
   191  		if exitCode != util.Success {
   192  			os.Exit(exitCode)
   193  		}
   194  
   195  		printJSON(data)
   196  	},
   197  }
   198  
   199  var listPubKeysCmd = &cobra.Command{
   200  	Use:   "list-pubkeys <accountInfo> [publicKey]",
   201  	Short: "list the account pubkeys",
   202  	Args:  cobra.RangeArgs(1, 2),
   203  	Run: func(cmd *cobra.Command, args []string) {
   204  		var ins = struct {
   205  			AccountID    string `json:"account_id"`
   206  			AccountAlias string `json:"account_alias"`
   207  			PublicKey    string `json:"public_key"`
   208  		}{}
   209  
   210  		if len(args[0]) == 13 && strings.HasPrefix(args[0], "0") {
   211  			ins.AccountID = args[0]
   212  		} else {
   213  			ins.AccountAlias = args[0]
   214  		}
   215  
   216  		if len(args) == 2 {
   217  			ins.PublicKey = args[1]
   218  		}
   219  
   220  		data, exitCode := util.ClientCall("/list-pubkeys", &ins)
   221  		if exitCode != util.Success {
   222  			os.Exit(exitCode)
   223  		}
   224  
   225  		printJSON(data)
   226  	},
   227  }
   228  
   229  var listBalancesCmd = &cobra.Command{
   230  	Use:   "list-balances",
   231  	Short: "List the accounts balances",
   232  	Args:  cobra.NoArgs,
   233  	Run: func(cmd *cobra.Command, args []string) {
   234  		var filter = struct {
   235  			AccountID    string `json:"account_id"`
   236  			AccountAlias string `json:"account_alias"`
   237  		}{AccountID: accountID, AccountAlias: accountAlias}
   238  
   239  		data, exitCode := util.ClientCall("/list-balances", &filter)
   240  		if exitCode != util.Success {
   241  			os.Exit(exitCode)
   242  		}
   243  
   244  		printJSONList(data)
   245  	},
   246  }
   247  
   248  var listUnspentOutputsCmd = &cobra.Command{
   249  	Use:   "list-unspent-outputs",
   250  	Short: "List the accounts unspent outputs",
   251  	Args:  cobra.NoArgs,
   252  	Run: func(cmd *cobra.Command, args []string) {
   253  		filter := struct {
   254  			AccountID     string `json:"account_id"`
   255  			AccountAlias  string `json:"account_alias"`
   256  			ID            string `json:"id"`
   257  			Unconfirmed   bool   `json:"unconfirmed"`
   258  			SmartContract bool   `json:"smart_contract"`
   259  			From          uint   `json:"from"`
   260  			Count         uint   `json:"count"`
   261  		}{
   262  			AccountID:     accountID,
   263  			AccountAlias:  accountAlias,
   264  			ID:            outputID,
   265  			Unconfirmed:   unconfirmed,
   266  			SmartContract: smartContract,
   267  			From:          uint(from),
   268  			Count:         uint(count),
   269  		}
   270  
   271  		data, exitCode := util.ClientCall("/list-unspent-outputs", &filter)
   272  		if exitCode != util.Success {
   273  			os.Exit(exitCode)
   274  		}
   275  
   276  		printJSONList(data)
   277  	},
   278  }