@@ -303,20 +303,44 @@ public List<Object> decodeMethod(String ABI, String methodName, String output)
303
303
throws ABICodecException {
304
304
return decodeMethodAndGetOutputObject (ABI , methodName , output ).getLeft ();
305
305
}
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
+ }
306
318
307
- public List <Object > decodeMethodById (String ABI , String methodId , String output )
319
+ public Pair < List <Object >, List < ABIObject >> decodeTransactionInput (String ABI , String input )
308
320
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 {
309
328
ContractABIDefinition contractABIDefinition = abiDefinitionFactory .loadABI (ABI );
310
329
ABIDefinition abiDefinition = contractABIDefinition .getABIDefinitionByMethodId (methodId );
311
330
if (abiDefinition == null ) {
312
331
String errorMsg = " methodId " + methodId + " is invalid" ;
313
332
logger .error (errorMsg );
314
333
throw new ABICodecException (errorMsg );
315
334
}
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
+ }
317
341
ABICodecObject abiCodecObject = new ABICodecObject ();
318
342
try {
319
- return abiCodecObject .decodeJavaObject (outputABIObject , output );
343
+ return abiCodecObject .decodeJavaObjectAndOutputObject (outputABIObject , data );
320
344
} catch (Exception e ) {
321
345
logger .error (" exception in decodeMethodByIdToObject : {}" , e .getMessage ());
322
346
}
@@ -326,6 +350,11 @@ public List<Object> decodeMethodById(String ABI, String methodId, String output)
326
350
throw new ABICodecException (errorMsg );
327
351
}
328
352
353
+ public List <Object > decodeMethodById (String ABI , String methodId , String output )
354
+ throws ABICodecException {
355
+ return decodeDataByMethodId (ABI , methodId , output , true ).getLeft ();
356
+ }
357
+
329
358
public List <Object > decodeMethodByInterface (String ABI , String methodInterface , String output )
330
359
throws ABICodecException {
331
360
FunctionEncoder functionEncoder = new FunctionEncoder (cryptoSuite );
@@ -361,17 +390,27 @@ public List<String> decodeMethodToString(String ABI, String methodName, String o
361
390
362
391
public List <String > decodeMethodByIdToString (String ABI , String methodId , String output )
363
392
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 {
364
398
ContractABIDefinition contractABIDefinition = abiDefinitionFactory .loadABI (ABI );
365
399
ABIDefinition abiDefinition = contractABIDefinition .getABIDefinitionByMethodId (methodId );
366
400
if (abiDefinition == null ) {
367
401
String errorMsg = " methodId " + methodId + " is invalid" ;
368
402
logger .error (errorMsg );
369
403
throw new ABICodecException (errorMsg );
370
404
}
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
+ }
372
411
ABICodecJsonWrapper abiCodecJsonWrapper = new ABICodecJsonWrapper ();
373
412
try {
374
- return abiCodecJsonWrapper .decode (outputABIObject , output );
413
+ return abiCodecJsonWrapper .decode (outputABIObject , data );
375
414
} catch (UnsupportedOperationException e ) {
376
415
logger .error (" exception in decodeMethodByIdToString : {}" , e .getMessage ());
377
416
}
@@ -513,6 +552,13 @@ public List<String> decodeEventByInterfaceToString(
513
552
return decodeEventByTopicToString (ABI , methodId , log );
514
553
}
515
554
555
+ private String addHexPrefixToString (String s ) {
556
+ if (!s .startsWith ("0x" )) {
557
+ return "0x" + s ;
558
+ }
559
+ return s ;
560
+ }
561
+
516
562
private List <Object > mergeEventParamsAndTopics (
517
563
ABIDefinition abiDefinition , List <Object > params , List <String > topics ) {
518
564
List <Object > ret = new ArrayList <>();
0 commit comments