-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathTxEncoder.java
More file actions
232 lines (214 loc) · 8.06 KB
/
Copy pathTxEncoder.java
File metadata and controls
232 lines (214 loc) · 8.06 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package com.baidu.xuper.api;
import com.baidu.xuper.crypto.Hash;
import com.baidu.xuper.pb.XchainOuterClass;
import com.baidu.xuper.pb.XchainOuterClass.*;
import com.google.gson.*;
import com.google.protobuf.ByteString;
import org.bouncycastle.util.encoders.Base64;
import java.lang.reflect.Type;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
class TxEncoder {
//private static int anInt;
private static final Gson gson = new GsonBuilder()
.serializeNulls()
.registerTypeHierarchyAdapter(ByteString.class, new PbByteStringAdapter())
.disableHtmlEscaping()
.create();
private StringBuilder buffer;
TxEncoder() {
buffer = new StringBuilder();
}
static byte[] makeTxDigest(XchainOuterClass.Transaction tx) {
TxEncoder enc = new TxEncoder();
return Hash.doubleSha256(enc.encodeTx(tx, false));
}
static byte[] makeTxID(XchainOuterClass.Transaction tx) {
TxEncoder enc = new TxEncoder();
return Hash.doubleSha256(enc.encodeTx(tx, true));
}
private void encode(Object obj) {
String s = gson.toJson(obj);
buffer.append(s);
buffer.append("\n");
}
private void encode(ByteString bs) {
if (bs == null){
String s = gson.toJson(null);
buffer.append(s);
buffer.append("\n");
return;
}
if (!bs.isEmpty()) {
encode(Base64.toBase64String(bs.toByteArray()));
}
}
byte[] encodeTx(XchainOuterClass.Transaction tx, boolean needSign) {
for (TxInput input : tx.getTxInputsList()) {
encode(input.getRefTxid());
encode(input.getRefOffset());
encode(input.getFromAddr());
encode(input.getAmount());
encode(input.getFrozenHeight());
}
Object[] txOutputs = new Object[tx.getTxOutputsCount()];
for (int i = 0; i < tx.getTxOutputsCount(); i++) {
txOutputs[i] = TxOutputBean.create(tx.getTxOutputs(i));
}
if (txOutputs.length > 0){
encode(txOutputs);
}else{
encode(null);
}
encode(tx.getDesc());
encode(tx.getNonce());
encode(tx.getTimestamp());
encode(tx.getVersion());
for (TxInputExt input : tx.getTxInputsExtList()) {
encode(input.getBucket());
encode(input.getKey());
encode(input.getRefTxid());
encode(input.getRefOffset());
}
for (TxOutputExt output : tx.getTxOutputsExtList()) {
encode(output.getBucket());
encode(output.getKey());
encode(output.getValue());
}
Object[] invokes = null;
if (tx.getContractRequestsCount() != 0) {
invokes = new Object[tx.getContractRequestsCount()];
for (int i = 0; i < tx.getContractRequestsCount(); i++) {
invokes[i] = InvokeRequestBean.create(tx.getContractRequests(i));
}
}
encode(invokes);
encode(tx.getInitiator());
if (tx.getAuthRequireCount() > 0) {
encode(tx.getAuthRequireList().toArray());
} else {
encode((Object) null);
}
if (needSign) {
Object[] sigs = null;
if (tx.getInitiatorSignsCount() != 0) {
sigs = new Object[tx.getInitiatorSignsCount()];
}
for (int i = 0; i < tx.getInitiatorSignsCount(); i++) {
sigs[i] = SignatureInfoBean.create(tx.getInitiatorSigns(i));
}
encode(sigs);
sigs = null;
if (tx.getAuthRequireSignsCount() != 0) {
sigs = new Object[tx.getAuthRequireSignsCount()];
}
for (int i = 0; i < tx.getAuthRequireSignsCount(); i++) {
sigs[i] = SignatureInfoBean.create(tx.getAuthRequireSigns(i));
}
encode(sigs);
if (tx.hasXuperSign()) {
encode(tx.getXuperSign());
}
}
encode(tx.getCoinbase());
encode(tx.getAutogen());
// try {
// anInt++;
// System.out.println("anInt:" + anInt);
// System.out.println("txID:" + Arrays.toString(tx.getTxid().toByteArray()));
// FileOutputStream f = new FileOutputStream("./tmp/tx" + anInt + ".pb");
// f.write(tx.toByteArray());
// f.close();
// f = new FileOutputStream("./tmp/tx" + anInt + "txt");
// f.write(buffer.toString().getBytes());
// f.close();
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
return buffer.toString().getBytes();
}
private static class PbByteStringAdapter implements JsonSerializer<ByteString> {
public JsonElement serialize(ByteString src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(Base64.toBase64String(src.toByteArray()));
}
}
private static class TxOutputBean {
static Object create(TxOutput pb) {
LinkedHashMap<String, Object> m = new LinkedHashMap<>();
if (!pb.getAmount().isEmpty()) {
m.put("amount", pb.getAmount());
}
if (!pb.getToAddr().isEmpty()) {
m.put("to_addr", pb.getToAddr());
}
if (pb.getFrozenHeight() != 0) {
m.put("frozen_height", pb.getFrozenHeight());
}
return m;
}
}
private static class InvokeRequestBean {
static Object create(InvokeRequest pb) {
LinkedHashMap<String, Object> m = new LinkedHashMap<>();
if (!pb.getMethodName().isEmpty()) {
m.put("module_name", pb.getModuleName());
}
if (!pb.getContractName().isEmpty()) {
m.put("contract_name", pb.getContractName());
}
if (!pb.getMethodName().isEmpty()) {
m.put("method_name", pb.getMethodName());
}
if (pb.getArgsCount() != 0) {
TreeMap<String, ByteString> margs = new TreeMap<>();
for (Map.Entry<String, ByteString> entry : pb.getArgsMap().entrySet()) {
/*
if (entry.getValue().isEmpty()) {
margs.put(entry.getKey(), null);
} else {
margs.put(entry.getKey(), entry.getValue());
}*/
//修复 参数值为 ""时,设置了为null,造成签名和实际传输的数据不同,验签失败
margs.put(entry.getKey(), entry.getValue());
}
m.put("args", margs);
}
if (pb.getResourceLimitsCount() != 0) {
Object[] resource_limits = new Object[pb.getResourceLimitsCount()];
for (int i = 0; i < pb.getResourceLimitsCount(); i++) {
resource_limits[i] = ResourceLimitBean.create(pb.getResourceLimits(i));
}
m.put("resource_limits", resource_limits);
}
if (!pb.getAmount().isEmpty()) {
m.put("amount", pb.getAmount());
}
return m;
}
}
private static class ResourceLimitBean {
static Object create(ResourceLimit pb) {
LinkedHashMap<String, Object> m = new LinkedHashMap<>();
if (pb.getType().getNumber() != 0) {
m.put("type", pb.getType().getNumber());
}
if (pb.getLimit() != 0) {
m.put("limit", pb.getLimit());
}
return m;
}
}
private static class SignatureInfoBean {
static Object create(SignatureInfo pb) {
LinkedHashMap<String, Object> m = new LinkedHashMap<>();
if (!pb.getPublicKey().isEmpty()) {
m.put("PublicKey", pb.getPublicKey());
}
if (!pb.getSign().isEmpty()) {
m.put("Sign", pb.getSign());
}
return m;
}
}
}