github.com/turingchain2020/turingchain@v1.1.21/rpc/types/code.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 types 6 7 import ( 8 "bytes" 9 "encoding/json" 10 "strconv" 11 "strings" 12 13 "github.com/turingchain2020/turingchain/common" 14 "github.com/turingchain2020/turingchain/types" 15 16 _ "github.com/turingchain2020/turingchain/system/dapp/coins/types" //load system plugin 17 ) 18 19 // DecodeLog decode log 20 func DecodeLog(execer []byte, rlog *ReceiptData) (*ReceiptDataResult, error) { 21 var rTy string 22 switch rlog.Ty { 23 case 0: 24 rTy = "ExecErr" 25 case 1: 26 rTy = "ExecPack" 27 case 2: 28 rTy = "ExecOk" 29 default: 30 rTy = "Unknown" 31 } 32 rd := &ReceiptDataResult{Ty: rlog.Ty, TyName: rTy} 33 for _, l := range rlog.Logs { 34 var lTy string 35 var logIns json.RawMessage 36 lLog, err := common.FromHex(l.Log) 37 if err != nil { 38 return nil, err 39 } 40 logType := types.LoadLog(execer, int64(l.Ty)) 41 if logType == nil { 42 lTy = "unkownType" 43 logIns = nil 44 } else { 45 logIns, _ = logType.JSON(lLog) 46 lTy = logType.Name() 47 } 48 rd.Logs = append(rd.Logs, &ReceiptLogResult{Ty: l.Ty, TyName: lTy, Log: logIns, RawLog: l.Log}) 49 } 50 return rd, nil 51 } 52 53 // ConvertWalletTxDetailToJSON conver the wallet tx detail to json 54 func ConvertWalletTxDetailToJSON(in *types.WalletTxDetails, out *WalletTxDetails) error { 55 if in == nil || out == nil { 56 return types.ErrInvalidParam 57 } 58 for _, tx := range in.TxDetails { 59 var recp ReceiptData 60 logs := tx.GetReceipt().GetLogs() 61 recp.Ty = tx.GetReceipt().GetTy() 62 for _, lg := range logs { 63 recp.Logs = append(recp.Logs, 64 &ReceiptLog{Ty: lg.Ty, Log: common.ToHex(lg.GetLog())}) 65 } 66 rd, err := DecodeLog(tx.Tx.Execer, &recp) 67 if err != nil { 68 continue 69 } 70 tran, err := DecodeTx(tx.GetTx()) 71 if err != nil { 72 continue 73 } 74 if tx.Tx.IsWithdraw() { 75 //swap from and to 76 tx.Fromaddr, tx.Tx.To = tx.Tx.To, tx.Fromaddr 77 tran.To = tx.Tx.GetRealToAddr() 78 } 79 out.TxDetails = append(out.TxDetails, &WalletTxDetail{ 80 Tx: tran, 81 Receipt: rd, 82 Height: tx.GetHeight(), 83 Index: tx.GetIndex(), 84 BlockTime: tx.GetBlocktime(), 85 Amount: tx.GetAmount(), 86 FromAddr: tx.GetFromaddr(), 87 TxHash: common.ToHex(tx.GetTxhash()), 88 ActionName: tx.GetActionName(), 89 }) 90 } 91 return nil 92 } 93 94 // DecodeTx docode transaction 95 func DecodeTx(tx *types.Transaction) (*Transaction, error) { 96 if tx == nil { 97 return nil, types.ErrEmpty 98 } 99 execStr := string(tx.Execer) 100 var pl types.Message 101 plType := types.LoadExecutorType(execStr) 102 var err error 103 if plType != nil { 104 pl, err = plType.DecodePayload(tx) 105 if err != nil { 106 pl = nil 107 } 108 } 109 if strings.HasSuffix(string(tx.Execer), "user.write") { 110 pl = decodeUserWrite(tx.GetPayload()) 111 } 112 var pljson json.RawMessage 113 if pl != nil { 114 pljson, _ = types.PBToJSONUTF8(pl) 115 } 116 result := &Transaction{ 117 Execer: string(tx.Execer), 118 Payload: pljson, 119 RawPayload: common.ToHex(tx.GetPayload()), 120 Signature: &Signature{ 121 Ty: tx.GetSignature().GetTy(), 122 Pubkey: common.ToHex(tx.GetSignature().GetPubkey()), 123 Signature: common.ToHex(tx.GetSignature().GetSignature()), 124 }, 125 Fee: tx.Fee, 126 Expire: tx.Expire, 127 Nonce: tx.Nonce, 128 To: tx.GetRealToAddr(), 129 From: tx.From(), 130 GroupCount: tx.GroupCount, 131 Header: common.ToHex(tx.Header), 132 Next: common.ToHex(tx.Next), 133 Hash: common.ToHex(tx.Hash()), 134 ChainID: tx.ChainID, 135 } 136 feeResult := strconv.FormatFloat(float64(tx.Fee)/float64(types.Coin), 'f', 4, 64) 137 result.FeeFmt = feeResult 138 return result, nil 139 } 140 141 func decodeUserWrite(payload []byte) *types.UserWrite { 142 var article types.UserWrite 143 if len(payload) != 0 { 144 if payload[0] == '#' { 145 data := bytes.SplitN(payload[1:], []byte("#"), 2) 146 if len(data) == 2 { 147 article.Topic = string(data[0]) 148 article.Content = string(data[1]) 149 return &article 150 } 151 } 152 } 153 article.Topic = "" 154 article.Content = string(payload) 155 return &article 156 }