github.com/lino-network/lino@v0.6.11/client/misc.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  	"strings"
     7  	"time"
     8  
     9  	sdk "github.com/cosmos/cosmos-sdk/types"
    10  	"github.com/spf13/cobra"
    11  	amino "github.com/tendermint/go-amino"
    12  	"github.com/tendermint/tendermint/crypto/secp256k1"
    13  	"golang.org/x/crypto/ssh/terminal"
    14  
    15  	"github.com/lino-network/lino/client/encrypt"
    16  )
    17  
    18  func GetNowCmd(cdc *amino.Codec) *cobra.Command {
    19  	return &cobra.Command{
    20  		Use:   "now",
    21  		Short: "now",
    22  		Args:  cobra.NoArgs,
    23  		RunE: func(cmd *cobra.Command, args []string) error {
    24  			str, _ := cdc.MarshalJSON(time.Now())
    25  			fmt.Println(string(str))
    26  			return nil
    27  		},
    28  	}
    29  }
    30  
    31  func GetGenAddrCmd() *cobra.Command {
    32  	return &cobra.Command{
    33  		Use:   "gen-addr [keyfile]",
    34  		Short: "gen-addr [keyfile] prints a lino bech32 address. The private key will be printed if keyfile not specified",
    35  		Args:  cobra.MaximumNArgs(1),
    36  		RunE: func(cmd *cobra.Command, args []string) error {
    37  			priv := secp256k1.GenPrivKey()
    38  			pub := priv.PubKey()
    39  			addr := sdk.AccAddress(pub.Address())
    40  			fmt.Printf("addr: %s\n", addr)
    41  			if len(args) > 0 {
    42  				keyfile := args[0]
    43  				return encryptSave(keyfile, []byte(hex.EncodeToString(priv.Bytes())))
    44  			} else {
    45  				fmt.Printf("priv-key: %s\n", strings.ToUpper(hex.EncodeToString(priv.Bytes())))
    46  			}
    47  			return nil
    48  		},
    49  	}
    50  }
    51  
    52  func GetAddrOfCmd() *cobra.Command {
    53  	return &cobra.Command{
    54  		Use:   "addr-of <@keyfile>",
    55  		Short: "addr-of <@keyfile> prints the lino bech32 address, @ included, e.g. @foo.key",
    56  		Args:  cobra.ExactArgs(1),
    57  		RunE: func(cmd *cobra.Command, args []string) error {
    58  			file := args[0]
    59  			priv, err := ParsePrivKey(file)
    60  			if err != nil {
    61  				return err
    62  			}
    63  			addr := sdk.AccAddress(priv.PubKey().Address())
    64  			fmt.Printf("addr: %s\n", addr)
    65  			return nil
    66  		},
    67  	}
    68  }
    69  
    70  func GetPubKeyOfCmd() *cobra.Command {
    71  	return &cobra.Command{
    72  		Use:   "pubkey-of <@keyfile>",
    73  		Short: "pubkey-of <@keyfile> prints the hex-encoded string of pubkey, @ included",
    74  		Args:  cobra.ExactArgs(1),
    75  		RunE: func(cmd *cobra.Command, args []string) error {
    76  			file := args[0]
    77  			priv, err := ParsePrivKey(file)
    78  			if err != nil {
    79  				return err
    80  			}
    81  			fmt.Printf("pubkey: %s\n", hex.EncodeToString(priv.PubKey().Bytes()))
    82  			return nil
    83  		},
    84  	}
    85  }
    86  
    87  func GetEncryptPrivKey() *cobra.Command {
    88  	return &cobra.Command{
    89  		Use:   "encrypt-key <file>",
    90  		Short: "encrypt-key <file>",
    91  		Args:  cobra.ExactArgs(1),
    92  		RunE: func(cmd *cobra.Command, args []string) error {
    93  			fmt.Printf("Hex-encoded Private key: ")
    94  			privKey, err := terminal.ReadPassword(0)
    95  			if err != nil {
    96  				return err
    97  			}
    98  			fmt.Println("")
    99  
   100  			// validate key
   101  			_, err = ParsePrivKey(string(privKey))
   102  			if err != nil {
   103  				return fmt.Errorf("invalid privKey: %+v", err)
   104  			}
   105  
   106  			return encryptSave(args[0], privKey)
   107  		},
   108  	}
   109  }
   110  
   111  func encryptSave(filepath string, privKey []byte) error {
   112  	fmt.Printf("Password: ")
   113  	pw1, err := terminal.ReadPassword(0)
   114  	if err != nil {
   115  		return err
   116  	}
   117  	fmt.Println("")
   118  
   119  	fmt.Printf("Password again: ")
   120  	pw2, err := terminal.ReadPassword(0)
   121  	if err != nil {
   122  		return err
   123  	}
   124  	fmt.Printf("\n\n")
   125  
   126  	if string(pw1) != string(pw2) {
   127  		return fmt.Errorf("password mismatch")
   128  	}
   129  
   130  	err = encrypt.EncryptToFile(filepath, privKey, string(pw1))
   131  	if err != nil {
   132  		return err
   133  	}
   134  	fmt.Printf("encerypted key have been wrote to %s.\n", filepath)
   135  	return nil
   136  }