|
| 1 | +package org.web3j.abi; |
| 2 | + |
| 3 | +import static org.web3j.abi.datatypes.Type.MAX_BIT_LENGTH; |
| 4 | +import static org.web3j.abi.datatypes.Type.MAX_BYTE_LENGTH; |
| 5 | + |
| 6 | +import java.math.BigInteger; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | + |
| 9 | +import org.bouncycastle.util.encoders.Hex; |
| 10 | +import org.web3j.abi.datatypes.IntType; |
| 11 | +import org.web3j.abi.datatypes.NumericType; |
| 12 | +import org.web3j.abi.datatypes.Type; |
| 13 | +import org.web3j.abi.datatypes.Ufixed; |
| 14 | +import org.web3j.abi.datatypes.Uint; |
| 15 | +import org.web3j.abi.datatypes.Utf8String; |
| 16 | +import org.web3j.utils.Numeric; |
| 17 | + |
| 18 | +/** |
| 19 | + * <p>Ethereum Contract Application Binary Interface (ABI) encoding for types. |
| 20 | + * Further details are available |
| 21 | + * <a href="https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI">here</a>. |
| 22 | + * </p> |
| 23 | + */ |
| 24 | +public class PlatOnTypeEncoder { |
| 25 | + |
| 26 | + private PlatOnTypeEncoder() { } |
| 27 | + |
| 28 | + @SuppressWarnings("unchecked") |
| 29 | + public static String encode(Type parameter) { |
| 30 | + if (parameter instanceof IntType) { |
| 31 | + return encodeInt(((IntType) parameter)); |
| 32 | + }else if (parameter instanceof Utf8String) { |
| 33 | + return encodeString((Utf8String) parameter); |
| 34 | + }else { |
| 35 | + throw new UnsupportedOperationException( |
| 36 | + "Type cannot be encoded: " + parameter.getClass()); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + private static String encodeInt(IntType intType) { |
| 41 | + byte[] rawValue = toByteArray(intType); |
| 42 | + byte paddingValue = getPaddingValue(intType); |
| 43 | + byte[] paddedRawValue = new byte[intType.getBitSize()/8]; |
| 44 | + if (paddingValue != 0) { |
| 45 | + for (int i = 0; i < paddedRawValue.length; i++) { |
| 46 | + paddedRawValue[i] = paddingValue; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + System.arraycopy( |
| 51 | + rawValue, 0, |
| 52 | + paddedRawValue, paddedRawValue.length - rawValue.length, |
| 53 | + rawValue.length); |
| 54 | + return Numeric.toHexStringNoPrefix(paddedRawValue); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + private static String encodeString(Utf8String string) { |
| 59 | + byte[] utfEncoded = string.getValue().getBytes(StandardCharsets.UTF_8); |
| 60 | + return Hex.toHexString(utfEncoded); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + private static byte getPaddingValue(NumericType numericType) { |
| 65 | + if (numericType.getValue().signum() == -1) { |
| 66 | + return (byte) 0xff; |
| 67 | + } else { |
| 68 | + return 0; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private static byte[] toByteArray(NumericType numericType) { |
| 73 | + BigInteger value = numericType.getValue(); |
| 74 | + if (numericType instanceof Ufixed || numericType instanceof Uint) { |
| 75 | + if (value.bitLength() == MAX_BIT_LENGTH) { |
| 76 | + // As BigInteger is signed, if we have a 256 bit value, the resultant byte array |
| 77 | + // will contain a sign byte in it's MSB, which we should ignore for this unsigned |
| 78 | + // integer type. |
| 79 | + byte[] byteArray = new byte[MAX_BYTE_LENGTH]; |
| 80 | + System.arraycopy(value.toByteArray(), 1, byteArray, 0, MAX_BYTE_LENGTH); |
| 81 | + return byteArray; |
| 82 | + } |
| 83 | + } |
| 84 | + return value.toByteArray(); |
| 85 | + } |
| 86 | +} |
0 commit comments