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