@@ -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 <>();
0 commit comments