@@ -58,7 +58,8 @@ public String getCanonicalName() {
58
58
public static SolidityType getType (String typeName ) {
59
59
if (typeName .contains ("[" )) return ArrayType .getType (typeName );
60
60
if ("bool" .equals (typeName )) return new BoolType ();
61
- if (typeName .startsWith ("int" ) || typeName .startsWith ("uint" )) return new IntType (typeName );
61
+ if (typeName .startsWith ("int" )) return new IntType (typeName );
62
+ if (typeName .startsWith ("uint" )) return new UnsignedIntType (typeName );
62
63
if ("address" .equals (typeName )) return new AddressType ();
63
64
if ("string" .equals (typeName )) return new StringType ();
64
65
if ("bytes" .equals (typeName )) return new BytesType ();
@@ -356,23 +357,14 @@ public Object decode(byte[] encoded, int offset) {
356
357
return ByteUtil .bigIntegerToBytes (bi , 20 );
357
358
}
358
359
}
359
-
360
- public static class IntType extends SolidityType {
361
- public IntType (String name ) {
360
+
361
+ public static abstract class NumericType extends SolidityType {
362
+ public NumericType (String name ) {
362
363
super (name );
363
364
}
364
365
365
- @ Override
366
- public String getCanonicalName () {
367
- if (getName ().equals ("int" )) return "int256" ;
368
- if (getName ().equals ("uint" )) return "uint256" ;
369
- return super .getCanonicalName ();
370
- }
371
-
372
- @ Override
373
- public byte [] encode (Object value ) {
366
+ BigInteger encodeInternal (Object value ) {
374
367
BigInteger bigInt ;
375
-
376
368
if (value instanceof String ) {
377
369
String s = ((String )value ).toLowerCase ().trim ();
378
370
int radix = 10 ;
@@ -393,14 +385,20 @@ public byte[] encode(Object value) {
393
385
} else {
394
386
throw new RuntimeException ("Invalid value for type '" + this + "': " + value + " (" + value .getClass () + ")" );
395
387
}
396
- return encodeInt (bigInt );
388
+ return bigInt ;
389
+ }
390
+ }
391
+
392
+ public static class IntType extends NumericType {
393
+ public IntType (String name ) {
394
+ super (name );
397
395
}
398
396
399
397
@ Override
400
- public Object decode (byte [] encoded , int offset ) {
401
- return decodeInt (encoded , offset );
398
+ public String getCanonicalName () {
399
+ if (getName ().equals ("int" )) return "int256" ;
400
+ return super .getCanonicalName ();
402
401
}
403
-
404
402
public static BigInteger decodeInt (byte [] encoded , int offset ) {
405
403
return new BigInteger (Arrays .copyOfRange (encoded , offset , offset + 32 ));
406
404
}
@@ -410,6 +408,48 @@ public static byte[] encodeInt(int i) {
410
408
public static byte [] encodeInt (BigInteger bigInt ) {
411
409
return ByteUtil .bigIntegerToBytesSigned (bigInt , 32 );
412
410
}
411
+ @ Override
412
+ public Object decode (byte [] encoded , int offset ) {
413
+ return decodeInt (encoded , offset );
414
+ }
415
+ @ Override
416
+ public byte [] encode (Object value ) {
417
+ BigInteger bigInt = encodeInternal (value );
418
+ return encodeInt (bigInt );
419
+ }
420
+ }
421
+
422
+ public static class UnsignedIntType extends NumericType {
423
+ public UnsignedIntType (String name ) {
424
+ super (name );
425
+ }
426
+
427
+ @ Override
428
+ public String getCanonicalName () {
429
+ if (getName ().equals ("uint" )) return "uint256" ;
430
+ return super .getCanonicalName ();
431
+ }
432
+ public static BigInteger decodeInt (byte [] encoded , int offset ) {
433
+ return new BigInteger (1 , Arrays .copyOfRange (encoded , offset , offset + 32 ));
434
+ }
435
+ public static byte [] encodeInt (int i ) {
436
+ return encodeInt (new BigInteger ("" + i ));
437
+ }
438
+ public static byte [] encodeInt (BigInteger bigInt ) {
439
+ if (bigInt .signum () == -1 ) {
440
+ throw new RuntimeException ("Wrong value for uint type: " + bigInt );
441
+ }
442
+ return ByteUtil .bigIntegerToBytes (bigInt , 32 );
443
+ }
444
+ @ Override
445
+ public byte [] encode (Object value ) {
446
+ BigInteger bigInt = encodeInternal (value );
447
+ return encodeInt (bigInt );
448
+ }
449
+ @ Override
450
+ public Object decode (byte [] encoded , int offset ) {
451
+ return decodeInt (encoded , offset );
452
+ }
413
453
}
414
454
415
455
public static class BoolType extends IntType {
0 commit comments