github.com/turingchain2020/turingchain@v1.1.21/system/dapp/commands/exec.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 "encoding/hex" 9 "fmt" 10 "math/rand" 11 "os" 12 "strings" 13 "time" 14 15 "github.com/turingchain2020/turingchain/common/address" 16 "github.com/turingchain2020/turingchain/types" 17 "github.com/spf13/cobra" 18 ) 19 20 // ExecCmd exec command 21 func ExecCmd() *cobra.Command { 22 cmd := &cobra.Command{ 23 Use: "exec", 24 Short: "Executor operation", 25 Args: cobra.MinimumNArgs(1), 26 } 27 28 cmd.AddCommand( 29 GetExecAddrCmd(), 30 UserDataCmd(), 31 ) 32 33 return cmd 34 } 35 36 // GetExecAddrCmd get address of an execer 37 func GetExecAddrCmd() *cobra.Command { 38 cmd := &cobra.Command{ 39 Use: "addr", 40 Short: "Get address of executor", 41 Run: getAddrByExec, 42 } 43 addGetAddrFlags(cmd) 44 return cmd 45 } 46 47 func addGetAddrFlags(cmd *cobra.Command) { 48 cmd.Flags().StringP("exec", "e", "", getExecuterNameString()) 49 cmd.MarkFlagRequired("exec") 50 } 51 52 func getAddrByExec(cmd *cobra.Command, args []string) { 53 execer, _ := cmd.Flags().GetString("exec") 54 if ok := types.IsAllowExecName([]byte(execer), []byte(execer)); !ok { 55 fmt.Println(types.ErrExecNameNotAllow.Error()) 56 return 57 } 58 addrResult := address.ExecAddress(execer) 59 result := addrResult 60 fmt.Println(result) 61 } 62 63 // UserDataCmd create user data 64 func UserDataCmd() *cobra.Command { 65 cmd := &cobra.Command{ 66 Use: "userdata", 67 Short: "Write data to user defined executor", 68 Run: addUserData, 69 } 70 addUserDataFlags(cmd) 71 return cmd 72 } 73 func addUserDataFlags(cmd *cobra.Command) { 74 cmd.Flags().StringP("exec", "e", "", `executor name must contain prefix "user"`) 75 cmd.MarkFlagRequired("exec") 76 cmd.Flags().StringP("topic", "t", "", `add #topic# before data, if empty add nothing`) 77 cmd.Flags().StringP("data", "d", "", `payload data that users want to write`) 78 cmd.MarkFlagRequired("data") 79 } 80 81 func addUserData(cmd *cobra.Command, args []string) { 82 title, _ := cmd.Flags().GetString("title") 83 cfg := types.GetCliSysParam(title) 84 execer, err := cmd.Flags().GetString("exec") 85 if err != nil { 86 fmt.Println(err) 87 return 88 } 89 if !strings.HasPrefix(execer, "user.") { 90 fmt.Println(`user defined executor should start with "user."`) 91 return 92 } 93 if len(execer) > 50 { 94 fmt.Println("executor name too long") 95 return 96 } 97 addrResult := address.ExecAddress(execer) 98 topic, _ := cmd.Flags().GetString("topic") 99 data, _ := cmd.Flags().GetString("data") 100 if topic != "" { 101 data = "#" + topic + "#" + data 102 } 103 if data == "" { 104 fmt.Println("write empty data") 105 return 106 } 107 tx := &types.Transaction{ 108 Execer: []byte(execer), 109 Payload: []byte(data), 110 To: addrResult, 111 } 112 tx.Fee, err = tx.GetRealFee(cfg.GetMinTxFeeRate()) 113 if err != nil { 114 fmt.Fprintln(os.Stderr, err) 115 return 116 } 117 random := rand.New(rand.NewSource(time.Now().UnixNano())) 118 tx.Nonce = random.Int63() 119 tx.ChainID = cfg.GetChainID() 120 //tx.Sign(int32(wallet.SignType), privKey) 121 txHex := types.Encode(tx) 122 fmt.Println(hex.EncodeToString(txHex)) 123 }