Skip to content

Commit 94b5937

Browse files
committed
show contract address to tron-address like
1 parent 74e734d commit 94b5937

1 file changed

Lines changed: 32 additions & 2 deletions

File tree

‎src/main/java/org/tron/core/services/http/GetTransactionInfoByIdServlet.java‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.google.protobuf.ByteString;
44
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.List;
57
import java.util.stream.Collectors;
68
import javax.servlet.http.HttpServlet;
79
import javax.servlet.http.HttpServletRequest;
@@ -10,9 +12,11 @@
1012
import org.springframework.beans.factory.annotation.Autowired;
1113
import org.springframework.stereotype.Component;
1214
import org.tron.api.GrpcAPI.BytesMessage;
15+
import org.tron.common.runtime.utils.MUtil;
1316
import org.tron.common.utils.ByteArray;
1417
import org.tron.core.Wallet;
1518
import org.tron.protos.Protocol.TransactionInfo;
19+
import org.tron.protos.Protocol.TransactionInfo.Log;
1620

1721

1822
@Component
@@ -29,7 +33,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) {
2933
TransactionInfo reply = wallet
3034
.getTransactionInfoById(ByteString.copyFrom(ByteArray.fromHexString(input)));
3135
if (reply != null) {
32-
response.getWriter().println(JsonFormat.printToString(reply, visible));
36+
response.getWriter().println(convertLogAddressToTronAddress(reply, visible));
3337
} else {
3438
response.getWriter().println("{}");
3539
}
@@ -53,7 +57,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
5357
JsonFormat.merge(input, build, visible);
5458
TransactionInfo reply = wallet.getTransactionInfoById(build.getValue());
5559
if (reply != null) {
56-
response.getWriter().println(JsonFormat.printToString(reply, visible));
60+
response.getWriter().println(convertLogAddressToTronAddress(reply, visible));
5761
} else {
5862
response.getWriter().println("{}");
5963
}
@@ -66,4 +70,30 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
6670
}
6771
}
6872
}
73+
74+
private static String convertLogAddressToTronAddress(TransactionInfo transactionInfo,
75+
boolean visible) {
76+
if (visible) {
77+
List<Log> newLogList = new ArrayList<>();
78+
for (Log log : transactionInfo.getLogList()) {
79+
Log.Builder logBuilder = Log.newBuilder();
80+
logBuilder.setData(log.getData());
81+
logBuilder.addAllTopics(log.getTopicsList());
82+
83+
byte[] oldAddress = log.getAddress().toByteArray();
84+
if (oldAddress.length == 0 || oldAddress.length > 20) {
85+
logBuilder.setAddress(log.getAddress());
86+
} else {
87+
byte[] newAddress = new byte[20];
88+
89+
int start = 20 - oldAddress.length;
90+
System.arraycopy(oldAddress, 0, newAddress, start, oldAddress.length);
91+
logBuilder.setAddress(ByteString.copyFrom(MUtil.convertToTronAddress(newAddress)));
92+
}
93+
newLogList.add(logBuilder.build());
94+
}
95+
transactionInfo = transactionInfo.toBuilder().clearLog().addAllLog(newLogList).build();
96+
}
97+
return JsonFormat.printToString(transactionInfo, visible);
98+
}
6999
}

0 commit comments

Comments
 (0)