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

     1  package commands
     2  
     3  import (
     4  	"encoding/hex"
     5  	"os"
     6  
     7  	"github.com/spf13/cobra"
     8  	jww "github.com/spf13/jwalterweatherman"
     9  
    10  	"github.com/bytom/bytom/crypto/ed25519/chainkd"
    11  	chainjson "github.com/bytom/bytom/encoding/json"
    12  	"github.com/bytom/bytom/util"
    13  )
    14  
    15  var createKeyCmd = &cobra.Command{
    16  	Use:   "create-key <alias> <password>",
    17  	Short: "Create a key",
    18  	Args:  cobra.ExactArgs(2),
    19  	Run: func(cmd *cobra.Command, args []string) {
    20  		var key = struct {
    21  			Alias    string `json:"alias"`
    22  			Password string `json:"password"`
    23  		}{Alias: args[0], Password: args[1]}
    24  
    25  		data, exitCode := util.ClientCall("/create-key", &key)
    26  		if exitCode != util.Success {
    27  			os.Exit(exitCode)
    28  		}
    29  
    30  		printJSON(data)
    31  	},
    32  }
    33  
    34  var deleteKeyCmd = &cobra.Command{
    35  	Use:   "delete-key <xpub> <password>",
    36  	Short: "Delete a key",
    37  	Args:  cobra.ExactArgs(2),
    38  	Run: func(cmd *cobra.Command, args []string) {
    39  		xpub := new(chainkd.XPub)
    40  		if err := xpub.UnmarshalText([]byte(args[0])); err != nil {
    41  			jww.ERROR.Println("delete-key:", err)
    42  			os.Exit(util.ErrLocalExe)
    43  		}
    44  
    45  		var key = struct {
    46  			Password string
    47  			XPub     chainkd.XPub `json:"xpub"`
    48  		}{XPub: *xpub, Password: args[1]}
    49  
    50  		if _, exitCode := util.ClientCall("/delete-key", &key); exitCode != util.Success {
    51  			os.Exit(exitCode)
    52  		}
    53  		jww.FEEDBACK.Println("Successfully delete key")
    54  	},
    55  }
    56  
    57  var listKeysCmd = &cobra.Command{
    58  	Use:   "list-keys",
    59  	Short: "List the existing keys",
    60  	Args:  cobra.NoArgs,
    61  	Run: func(cmd *cobra.Command, args []string) {
    62  		data, exitCode := util.ClientCall("/list-keys")
    63  		if exitCode != util.Success {
    64  			os.Exit(exitCode)
    65  		}
    66  
    67  		printJSONList(data)
    68  	},
    69  }
    70  
    71  var updateKeyAliasCmd = &cobra.Command{
    72  	Use:   "update-key-alias <xpub> <new-alias>",
    73  	Short: "Update key alias",
    74  	Args:  cobra.ExactArgs(2),
    75  	Run: func(cmd *cobra.Command, args []string) {
    76  		xpub := new(chainkd.XPub)
    77  		if err := xpub.UnmarshalText([]byte(args[0])); err != nil {
    78  			jww.ERROR.Println("update-key-alias xpub not valid:", err)
    79  			os.Exit(util.ErrLocalExe)
    80  		}
    81  
    82  		ins := struct {
    83  			XPub     chainkd.XPub `json:"xpub"`
    84  			NewAlias string       `json:"new_alias"`
    85  		}{XPub: *xpub, NewAlias: args[1]}
    86  
    87  		if _, exitCode := util.ClientCall("/update-key-alias", &ins); exitCode != util.Success {
    88  			os.Exit(exitCode)
    89  		}
    90  		jww.FEEDBACK.Println("Successfully update key alias")
    91  	},
    92  }
    93  
    94  var resetKeyPwdCmd = &cobra.Command{
    95  	Use:   "reset-key-password <xpub> <old-password> <new-password>",
    96  	Short: "Reset key password",
    97  	Args:  cobra.ExactArgs(3),
    98  	Run: func(cmd *cobra.Command, args []string) {
    99  		xpub := new(chainkd.XPub)
   100  		if err := xpub.UnmarshalText([]byte(args[0])); err != nil {
   101  			jww.ERROR.Println("reset-key-password args not valid:", err)
   102  			os.Exit(util.ErrLocalExe)
   103  		}
   104  
   105  		ins := struct {
   106  			XPub        chainkd.XPub `json:"xpub"`
   107  			OldPassword string       `json:"old_password"`
   108  			NewPassword string       `json:"new_password"`
   109  		}{XPub: *xpub, OldPassword: args[1], NewPassword: args[2]}
   110  
   111  		data, exitCode := util.ClientCall("/reset-key-password", &ins)
   112  		if exitCode != util.Success {
   113  			os.Exit(exitCode)
   114  		}
   115  
   116  		printJSON(data)
   117  	},
   118  }
   119  
   120  var checkKeyPwdCmd = &cobra.Command{
   121  	Use:   "check-key-password <xpub> <password>",
   122  	Short: "check key password",
   123  	Args:  cobra.ExactArgs(2),
   124  	Run: func(cmd *cobra.Command, args []string) {
   125  		xpub := new(chainkd.XPub)
   126  		if err := xpub.UnmarshalText([]byte(args[0])); err != nil {
   127  			jww.ERROR.Println("check-key-password args not valid:", err)
   128  			os.Exit(util.ErrLocalExe)
   129  		}
   130  
   131  		ins := struct {
   132  			XPub     chainkd.XPub `json:"xpub"`
   133  			Password string       `json:"password"`
   134  		}{XPub: *xpub, Password: args[1]}
   135  
   136  		data, exitCode := util.ClientCall("/check-key-password", &ins)
   137  		if exitCode != util.Success {
   138  			os.Exit(exitCode)
   139  		}
   140  
   141  		printJSON(data)
   142  	},
   143  }
   144  
   145  var signMsgCmd = &cobra.Command{
   146  	Use:   "sign-message <address> <message> <password>",
   147  	Short: "sign message to generate signature",
   148  	Args:  cobra.ExactArgs(3),
   149  	Run: func(cmd *cobra.Command, args []string) {
   150  		message, err := hex.DecodeString(args[1])
   151  		if err != nil {
   152  			jww.ERROR.Println("sign-message args not valid:", err)
   153  			os.Exit(util.ErrLocalExe)
   154  		}
   155  
   156  		var req = struct {
   157  			Address  string             `json:"address"`
   158  			Message  chainjson.HexBytes `json:"message"`
   159  			Password string             `json:"password"`
   160  		}{Address: args[0], Message: message, Password: args[2]}
   161  
   162  		data, exitCode := util.ClientCall("/sign-message", &req)
   163  		if exitCode != util.Success {
   164  			os.Exit(exitCode)
   165  		}
   166  		printJSON(data)
   167  	},
   168  }
   169  
   170  var verifyMsgCmd = &cobra.Command{
   171  	Use:   "verify-message <address> <xpub> <message> <signature>",
   172  	Short: "verify signature for specified message",
   173  	Args:  cobra.ExactArgs(4),
   174  	Run: func(cmd *cobra.Command, args []string) {
   175  		xpub := chainkd.XPub{}
   176  		if err := xpub.UnmarshalText([]byte(args[1])); err != nil {
   177  			jww.ERROR.Println(err)
   178  			os.Exit(util.ErrLocalExe)
   179  		}
   180  
   181  		message, err := hex.DecodeString(args[2])
   182  		if err != nil {
   183  			jww.ERROR.Println("sign-message args not valid:", err)
   184  			os.Exit(util.ErrLocalExe)
   185  		}
   186  
   187  		var req = struct {
   188  			Address     string             `json:"address"`
   189  			DerivedXPub chainkd.XPub       `json:"derived_xpub"`
   190  			Message     chainjson.HexBytes `json:"message"`
   191  			Signature   string             `json:"signature"`
   192  		}{Address: args[0], DerivedXPub: xpub, Message: message, Signature: args[3]}
   193  
   194  		data, exitCode := util.ClientCall("/verify-message", &req)
   195  		if exitCode != util.Success {
   196  			os.Exit(exitCode)
   197  		}
   198  		printJSON(data)
   199  	},
   200  }