github.com/turingchain2020/turingchain@v1.1.21/system/dapp/commands/seed.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package commands
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/turingchain2020/turingchain/rpc/jsonclient"
    11  	rpctypes "github.com/turingchain2020/turingchain/rpc/types"
    12  	"github.com/turingchain2020/turingchain/types"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  // SeedCmd seed command
    17  func SeedCmd() *cobra.Command {
    18  	cmd := &cobra.Command{
    19  		Use:   "seed",
    20  		Short: "Seed management",
    21  		Args:  cobra.MinimumNArgs(1),
    22  	}
    23  
    24  	cmd.AddCommand(
    25  		GenSeedCmd(),
    26  		GetSeedCmd(),
    27  		SaveSeedCmd(),
    28  	)
    29  
    30  	return cmd
    31  }
    32  
    33  // GenSeedCmd generate seed
    34  func GenSeedCmd() *cobra.Command {
    35  	cmd := &cobra.Command{
    36  		Use:   "generate",
    37  		Short: "Generate seed",
    38  		Run:   genSeed,
    39  	}
    40  	addGenSeedFlags(cmd)
    41  	return cmd
    42  }
    43  
    44  func addGenSeedFlags(cmd *cobra.Command) {
    45  	cmd.Flags().Int32P("lang", "l", 0, "seed language(0:English, 1:简体中文)")
    46  	cmd.MarkFlagRequired("lang")
    47  }
    48  
    49  func genSeed(cmd *cobra.Command, args []string) {
    50  	rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
    51  	lang, _ := cmd.Flags().GetInt32("lang")
    52  	params := types.GenSeedLang{
    53  		Lang: lang,
    54  	}
    55  	var res types.ReplySeed
    56  	ctx := jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.GenSeed", params, &res)
    57  	_, err := ctx.RunResult()
    58  	if err != nil {
    59  		fmt.Println(err)
    60  		return
    61  	}
    62  	fmt.Println(res.Seed)
    63  }
    64  
    65  // GetSeedCmd get seed
    66  func GetSeedCmd() *cobra.Command {
    67  	cmd := &cobra.Command{
    68  		Use:   "get",
    69  		Short: "Get seed by password",
    70  		Run:   getSeed,
    71  	}
    72  	addGetSeedFlags(cmd)
    73  	return cmd
    74  }
    75  
    76  func addGetSeedFlags(cmd *cobra.Command) {
    77  	cmd.Flags().StringP("pwd", "p", "", "password used to fetch seed")
    78  	cmd.MarkFlagRequired("pwd")
    79  }
    80  
    81  func getSeed(cmd *cobra.Command, args []string) {
    82  	rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
    83  	pwd, _ := cmd.Flags().GetString("pwd")
    84  	params := types.GetSeedByPw{
    85  		Passwd: pwd,
    86  	}
    87  	var res types.ReplySeed
    88  	ctx := jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.GetSeed", params, &res)
    89  	ctx.Run()
    90  }
    91  
    92  // SaveSeedCmd save seed
    93  func SaveSeedCmd() *cobra.Command {
    94  	cmd := &cobra.Command{
    95  		Use:   "save",
    96  		Short: "Save seed and encrypt with passwd",
    97  		Run:   saveSeed,
    98  	}
    99  	addSaveSeedFlags(cmd)
   100  	return cmd
   101  }
   102  
   103  func addSaveSeedFlags(cmd *cobra.Command) {
   104  	cmd.Flags().StringP("seed", "s", "", "15 seed characters separated by space")
   105  	cmd.MarkFlagRequired("seed")
   106  
   107  	cmd.Flags().StringP("pwd", "p", "", "password used to encrypt seed, [8-30]letter and digit")
   108  	cmd.MarkFlagRequired("pwd")
   109  }
   110  
   111  func saveSeed(cmd *cobra.Command, args []string) {
   112  	rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
   113  	seed, _ := cmd.Flags().GetString("seed")
   114  	pwd, _ := cmd.Flags().GetString("pwd")
   115  	params := types.SaveSeedByPw{
   116  		Seed:   seed,
   117  		Passwd: pwd,
   118  	}
   119  	var res rpctypes.Reply
   120  	ctx := jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.SaveSeed", params, &res)
   121  	ctx.Run()
   122  	//钱包解锁60s
   123  	params1 := types.WalletUnLock{
   124  		Passwd:         pwd,
   125  		Timeout:        60,
   126  		WalletOrTicket: false,
   127  	}
   128  	var res1 rpctypes.Reply
   129  	jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.UnLock", params1, &res1)
   130  }