Skip to content

Commit 54757b9

Browse files
authored
add decodeTransactionInput to ABICodec (#360)
1 parent d97c291 commit 54757b9

File tree

5 files changed

+90
-7
lines changed

5 files changed

+90
-7
lines changed

.ci/ci_check.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
set -e
4-
tag="v2.8.0"
4+
tag="v2.7.2"
55
LOG_INFO() {
66
local content=${1}
77
echo -e "\033[32m ${content}\033[0m"

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ ext {
3737
// integrationTest.mustRunAfter test
3838
allprojects {
3939
group = 'org.fisco-bcos.java-sdk'
40-
version = '2.8.0-GMT0018'
40+
version = '2.8.1-SNAPSHOT'
4141
apply plugin: 'maven'
4242
apply plugin: 'maven-publish'
4343
apply plugin: 'idea'

sdk-abi/src/main/java/org/fisco/bcos/sdk/abi/ABICodec.java

+51-5
Original file line numberDiff line numberDiff line change
@@ -303,20 +303,44 @@ public List<Object> decodeMethod(String ABI, String methodName, String output)
303303
throws ABICodecException {
304304
return decodeMethodAndGetOutputObject(ABI, methodName, output).getLeft();
305305
}
306+
/**
307+
* decode the input string into json
308+
*
309+
* @param input the transaction input
310+
* @return the decoded json string of the input
311+
*/
312+
public List<String> decodeTransactionInputToString(String ABI, String input)
313+
throws ABICodecException {
314+
String inputWithPrefix = addHexPrefixToString(input);
315+
String methodId = inputWithPrefix.substring(0, 10);
316+
return decodeMethodByIdToString(ABI, methodId, input.substring(10), false);
317+
}
306318

307-
public List<Object> decodeMethodById(String ABI, String methodId, String output)
319+
public Pair<List<Object>, List<ABIObject>> decodeTransactionInput(String ABI, String input)
308320
throws ABICodecException {
321+
String inputWithPrefix = addHexPrefixToString(input);
322+
String methodId = inputWithPrefix.substring(0, 10);
323+
return decodeDataByMethodId(ABI, methodId, input.substring(10), false);
324+
}
325+
326+
public Pair<List<Object>, List<ABIObject>> decodeDataByMethodId(
327+
String ABI, String methodId, String data, boolean isOutput) throws ABICodecException {
309328
ContractABIDefinition contractABIDefinition = abiDefinitionFactory.loadABI(ABI);
310329
ABIDefinition abiDefinition = contractABIDefinition.getABIDefinitionByMethodId(methodId);
311330
if (abiDefinition == null) {
312331
String errorMsg = " methodId " + methodId + " is invalid";
313332
logger.error(errorMsg);
314333
throw new ABICodecException(errorMsg);
315334
}
316-
ABIObject outputABIObject = abiObjectFactory.createOutputObject(abiDefinition);
335+
ABIObject outputABIObject = null;
336+
if (isOutput) {
337+
outputABIObject = abiObjectFactory.createOutputObject(abiDefinition);
338+
} else {
339+
outputABIObject = abiObjectFactory.createInputObject(abiDefinition);
340+
}
317341
ABICodecObject abiCodecObject = new ABICodecObject();
318342
try {
319-
return abiCodecObject.decodeJavaObject(outputABIObject, output);
343+
return abiCodecObject.decodeJavaObjectAndOutputObject(outputABIObject, data);
320344
} catch (Exception e) {
321345
logger.error(" exception in decodeMethodByIdToObject : {}", e.getMessage());
322346
}
@@ -326,6 +350,11 @@ public List<Object> decodeMethodById(String ABI, String methodId, String output)
326350
throw new ABICodecException(errorMsg);
327351
}
328352

353+
public List<Object> decodeMethodById(String ABI, String methodId, String output)
354+
throws ABICodecException {
355+
return decodeDataByMethodId(ABI, methodId, output, true).getLeft();
356+
}
357+
329358
public List<Object> decodeMethodByInterface(String ABI, String methodInterface, String output)
330359
throws ABICodecException {
331360
FunctionEncoder functionEncoder = new FunctionEncoder(cryptoSuite);
@@ -361,17 +390,27 @@ public List<String> decodeMethodToString(String ABI, String methodName, String o
361390

362391
public List<String> decodeMethodByIdToString(String ABI, String methodId, String output)
363392
throws ABICodecException {
393+
return decodeMethodByIdToString(ABI, methodId, output, true);
394+
}
395+
396+
public List<String> decodeMethodByIdToString(
397+
String ABI, String methodId, String data, boolean isOutput) throws ABICodecException {
364398
ContractABIDefinition contractABIDefinition = abiDefinitionFactory.loadABI(ABI);
365399
ABIDefinition abiDefinition = contractABIDefinition.getABIDefinitionByMethodId(methodId);
366400
if (abiDefinition == null) {
367401
String errorMsg = " methodId " + methodId + " is invalid";
368402
logger.error(errorMsg);
369403
throw new ABICodecException(errorMsg);
370404
}
371-
ABIObject outputABIObject = abiObjectFactory.createOutputObject(abiDefinition);
405+
ABIObject outputABIObject = null;
406+
if (isOutput) {
407+
outputABIObject = abiObjectFactory.createOutputObject(abiDefinition);
408+
} else {
409+
outputABIObject = abiObjectFactory.createInputObject(abiDefinition);
410+
}
372411
ABICodecJsonWrapper abiCodecJsonWrapper = new ABICodecJsonWrapper();
373412
try {
374-
return abiCodecJsonWrapper.decode(outputABIObject, output);
413+
return abiCodecJsonWrapper.decode(outputABIObject, data);
375414
} catch (UnsupportedOperationException e) {
376415
logger.error(" exception in decodeMethodByIdToString : {}", e.getMessage());
377416
}
@@ -513,6 +552,13 @@ public List<String> decodeEventByInterfaceToString(
513552
return decodeEventByTopicToString(ABI, methodId, log);
514553
}
515554

555+
private String addHexPrefixToString(String s) {
556+
if (!s.startsWith("0x")) {
557+
return "0x" + s;
558+
}
559+
return s;
560+
}
561+
516562
private List<Object> mergeEventParamsAndTopics(
517563
ABIDefinition abiDefinition, List<Object> params, List<String> topics) {
518564
List<Object> ret = new ArrayList<>();

sdk-transaction/src/main/java/org/fisco/bcos/sdk/transaction/codec/decode/TransactionDecoderService.java

+9
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ public TransactionResponse decodeReceiptWithValues(
7272
String abi, String functionName, TransactionReceipt transactionReceipt)
7373
throws IOException, ABICodecException, TransactionException {
7474
TransactionResponse response = decodeReceiptWithoutValues(abi, transactionReceipt);
75+
// parse the input
76+
if (transactionReceipt.getInput() != null) {
77+
Pair<List<Object>, List<ABIObject>> inputObject =
78+
abiCodec.decodeTransactionInput(abi, transactionReceipt.getInput());
79+
String inputValues = JsonUtils.toJson(inputObject.getLeft());
80+
response.setInputData(inputValues);
81+
response.setInputObject(inputObject.getLeft());
82+
response.setInputABIObject(inputObject.getRight());
83+
}
7584
// only successful tx has return values.
7685
if (transactionReceipt.getStatus().equals("0x0")) {
7786
Pair<List<Object>, List<ABIObject>> returnObject =

sdk-transaction/src/main/java/org/fisco/bcos/sdk/transaction/model/dto/TransactionResponse.java

+28
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public class TransactionResponse extends CommonResponse {
3737
private List<Object> returnObject;
3838
private List<ABIObject> returnABIObject;
3939

40+
private String inputData;
41+
private List<Object> inputObject;
42+
private List<ABIObject> inputABIObject;
43+
4044
public TransactionResponse() {
4145
super();
4246
}
@@ -134,4 +138,28 @@ public List<ABIObject> getReturnABIObject() {
134138
public void setReturnABIObject(List<ABIObject> returnABIObject) {
135139
this.returnABIObject = returnABIObject;
136140
}
141+
142+
public List<Object> getInputObject() {
143+
return inputObject;
144+
}
145+
146+
public void setInputObject(List<Object> inputObject) {
147+
this.inputObject = inputObject;
148+
}
149+
150+
public List<ABIObject> getInputABIObject() {
151+
return inputABIObject;
152+
}
153+
154+
public void setInputABIObject(List<ABIObject> inputABIObject) {
155+
this.inputABIObject = inputABIObject;
156+
}
157+
158+
public String getInputData() {
159+
return inputData;
160+
}
161+
162+
public void setInputData(String inputData) {
163+
this.inputData = inputData;
164+
}
137165
}

0 commit comments

Comments
 (0)