1
// Copyright © 2017-2020 Trust Wallet.
2
//
3
// This file is part of Trust. The full Trust copyright notice, including
4
// terms governing use, modification, and redistribution, is contained in the
5
// file LICENSE at the root of the source code distribution tree.
6

7
#include "Serialization.h"
8
#include "../HexCoding.h"
9
#include <sstream>
10
#include <vector>
11
#include <algorithm>
12

13
using namespace TW;
14
using namespace TW::Tron;
15
using namespace std;
16

17
using json = nlohmann::json;
18

19 1
string typeName(const protocol::Transaction::Contract::ContractType type) {
20 1
    return protocol::Transaction::Contract::ContractType_Name(type);
21
}
22

23 1
string typeUrl(const protocol::Transaction::Contract::ContractType type) {
24 1
    std::ostringstream stringStream;
25 1
    stringStream << "type.googleapis.com/protocol." << typeName(type);
26 1
    return stringStream.str();
27
}
28

29 1
json valueJSON(const protocol::TransferContract& contract) {
30 1
    json valueJSON;
31 1
    valueJSON["owner_address"] = hex(contract.owner_address());
32 1
    valueJSON["to_address"] = hex(contract.to_address());
33 1
    valueJSON["amount"] = contract.amount();
34

35 1
    return valueJSON;
36
}
37

38 1
json valueJSON(const protocol::TransferAssetContract& contract) {
39 1
    json valueJSON;
40 1
    valueJSON["asset_name"] = hex(contract.asset_name());
41 1
    valueJSON["owner_address"] = hex(contract.owner_address());
42 1
    valueJSON["to_address"] = hex(contract.to_address());
43 1
    valueJSON["amount"] = contract.amount();
44

45 1
    return valueJSON;
46
}
47

48 1
json valueJSON(const protocol::VoteAssetContract& contract) {
49 1
    json valueJSON;
50
    
51 1
    vector<string> vote_address;
52 1
    for (const string& addr : contract.vote_address()) {
53 1
        vote_address.push_back(hex(addr));
54
    }
55

56 1
    valueJSON["owner_address"] = hex(contract.owner_address());
57 1
    valueJSON["vote_address"] = vote_address;
58 1
    valueJSON["support"] = contract.support();
59 1
    valueJSON["count"] = contract.count();
60
    
61 1
    return valueJSON;
62
}
63

64 1
json voteJSON(const protocol::VoteWitnessContract::Vote& vote) {
65 1
    json voteJSON;
66 1
    voteJSON["vote_address"] = hex(vote.vote_address());
67 1
    voteJSON["vote_count"] = vote.vote_count();
68

69 1
    return voteJSON;
70
}
71

72 1
json valueJSON(const protocol::VoteWitnessContract& contract) {
73 1
    json valueJSON;
74

75 1
    vector<json> votes;
76 1
    for (const protocol::VoteWitnessContract::Vote& vote : contract.votes()) {
77 1
        votes.push_back(voteJSON(vote));
78
    }
79

80 1
    valueJSON["owner_address"] = hex(contract.owner_address());
81 1
    valueJSON["votes"] = votes;
82 1
    valueJSON["support"] = contract.support();
83

84 1
    return valueJSON;
85
}
86

87 1
json valueJSON(const protocol::FreezeBalanceContract& contract) {
88 1
    json valueJSON;
89 1
    valueJSON["owner_address"] = hex(contract.owner_address());
90 1
    valueJSON["frozen_balance"] = contract.frozen_balance();
91 1
    valueJSON["frozen_duration"] = contract.frozen_duration();
92 1
    valueJSON["resource"] = protocol::ResourceCode_Name(contract.resource());
93 1
    valueJSON["receiver_address"] = hex(contract.receiver_address());
94

95 1
    return valueJSON;
96
}
97

98 1
json valueJSON(const protocol::UnfreezeBalanceContract& contract) {
99 1
    json valueJSON;
100 1
    valueJSON["owner_address"] = hex(contract.owner_address());
101 1
    valueJSON["resource"] = protocol::ResourceCode_Name(contract.resource());
102 1
    valueJSON["receiver_address"] = hex(contract.receiver_address());
103

104 1
    return valueJSON;
105
}
106

107 1
json valueJSON(const protocol::WithdrawBalanceContract& contract) {
108 1
    json valueJSON;
109 1
    valueJSON["owner_address"] = hex(contract.owner_address());
110

111 1
    return valueJSON;
112
}
113

114 1
json valueJSON(const protocol::UnfreezeAssetContract& contract) {
115 1
    json valueJSON;
116 1
    valueJSON["owner_address"] = hex(contract.owner_address());
117

118 1
    return valueJSON;
119
}
120

121 1
json valueJSON(const protocol::TriggerSmartContract& contract) {
122 1
    json valueJSON;
123 1
    valueJSON["owner_address"] = hex(contract.owner_address());
124 1
    valueJSON["contract_address"] = hex(contract.contract_address());
125 1
    valueJSON["data"] = hex(contract.data());
126 1
    if (contract.call_value() > 0) {
127 0
        valueJSON["call_value"] = contract.call_value();
128
    }
129 1
    if (contract.call_token_value() > 0) {
130 1
        valueJSON["call_token_value"] = contract.call_token_value();
131
    }
132 1
    if (contract.token_id() > 0) {
133 1
        valueJSON["token_id"] = contract.token_id();
134
    }
135

136 1
    return valueJSON;
137
}
138

139 1
json parameterJSON(const google::protobuf::Any &parameter, const protocol::Transaction::Contract::ContractType type) {
140 1
    json paramJSON;
141 1
    paramJSON["type_url"] = typeUrl(type);
142

143 1
    switch (type) {
144
        case protocol::Transaction::Contract::TransferContract: {
145 1
            protocol::TransferContract contract;
146 1
            parameter.UnpackTo(&contract);
147 1
            paramJSON["value"] = valueJSON(contract);
148
            break;
149
        }
150
        case protocol::Transaction::Contract::TransferAssetContract: {
151 1
            protocol::TransferAssetContract contract;
152 1
            parameter.UnpackTo(&contract);
153 1
            paramJSON["value"] = valueJSON(contract);
154
            break;
155
        }
156
        case protocol::Transaction::Contract::VoteAssetContract: {
157 1
            protocol::VoteAssetContract contract;
158 1
            parameter.UnpackTo(&contract);
159 1
            paramJSON["value"] = valueJSON(contract);
160
            break;
161
        }
162
        case protocol::Transaction::Contract::VoteWitnessContract: {
163 1
            protocol::VoteWitnessContract contract;
164 1
            parameter.UnpackTo(&contract);
165 1
            paramJSON["value"] = valueJSON(contract);
166
            break;
167
        }
168
        case protocol::Transaction::Contract::FreezeBalanceContract: {
169 1
            protocol::FreezeBalanceContract contract;
170 1
            parameter.UnpackTo(&contract);
171 1
            paramJSON["value"] = valueJSON(contract);
172
            break;
173
        }
174
        case protocol::Transaction::Contract::UnfreezeBalanceContract: {
175 1
            protocol::UnfreezeBalanceContract contract;
176 1
            parameter.UnpackTo(&contract);
177 1
            paramJSON["value"] = valueJSON(contract);
178
            break;
179
        }
180
        case protocol::Transaction::Contract::WithdrawBalanceContract: {
181 1
            protocol::WithdrawBalanceContract contract;
182 1
            parameter.UnpackTo(&contract);
183 1
            paramJSON["value"] = valueJSON(contract);
184
            break;
185
        }
186
        case protocol::Transaction::Contract::UnfreezeAssetContract: {
187 1
            protocol::UnfreezeAssetContract contract;
188 1
            parameter.UnpackTo(&contract);
189 1
            paramJSON["value"] = valueJSON(contract);
190
            break;
191
        }
192
        case protocol::Transaction::Contract::TriggerSmartContract: {
193 1
            protocol::TriggerSmartContract contract;
194 1
            parameter.UnpackTo(&contract);
195 1
            paramJSON["value"] = valueJSON(contract);
196
            break;
197
        }
198
        case protocol::Transaction::Contract::AccountCreateContract:
199
        default:
200 0
            break;
201
    }
202

203 1
    return paramJSON;
204
}
205

206 1
json contractJSON(const protocol::Transaction::Contract &contract) {
207 1
    json contractJSON;
208 1
    contractJSON["type"] = typeName(contract.type());
209 1
    contractJSON["parameter"] = parameterJSON(contract.parameter(), contract.type());
210 1
    return contractJSON;
211
}
212

213 1
json raw_dataJSON(const protocol::Transaction::raw &raw) {
214 1
    json raw_dataJSON;
215

216 1
    raw_dataJSON["ref_block_bytes"] = hex(raw.ref_block_bytes());
217 1
    raw_dataJSON["ref_block_hash"] = hex(raw.ref_block_hash());
218 1
    if (raw.ref_block_num() > 0) {
219 0
        raw_dataJSON["ref_block_num"] = raw.ref_block_num();
220
    }
221 1
    if (raw.fee_limit() > 0) {
222 0
        raw_dataJSON["fee_limit"] = raw.fee_limit();
223
    }
224 1
    raw_dataJSON["timestamp"] = raw.timestamp();
225 1
    raw_dataJSON["expiration"] = raw.expiration();
226 1
    raw_dataJSON["contract"] = json::array({ contractJSON(raw.contract(0)) });
227

228 1
    return raw_dataJSON;
229
}
230

231 1
json TW::Tron::transactionJSON(const protocol::Transaction& transaction, const TW::Data& txID, const TW::Data& signature) {
232 1
    json transactionJSON;
233 1
    transactionJSON["raw_data"] = raw_dataJSON(transaction.raw_data());
234 1
    transactionJSON["txID"] = hex(txID);
235 1
    transactionJSON["signature"] = json::array({ hex(signature) });
236

237 1
    return transactionJSON;
238
}

Read our documentation on viewing source code .

Loading