|
| 1 | +package com.peersafe.abi; |
| 2 | + |
| 3 | +import java.math.BigInteger; |
| 4 | +import java.util.List; |
| 5 | +import java.util.stream.Collectors; |
| 6 | + |
| 7 | +import com.peersafe.abi.datatypes.Function; |
| 8 | +import com.peersafe.abi.datatypes.StaticArray; |
| 9 | +import com.peersafe.abi.datatypes.Type; |
| 10 | +import com.peersafe.abi.datatypes.Uint; |
| 11 | +import org.web3j.crypto.Hash; |
| 12 | +import org.web3j.utils.Numeric; |
| 13 | + |
| 14 | +/** |
| 15 | + * <p>Ethereum Contract Application Binary Interface (ABI) encoding for functions. |
| 16 | + * Further details are available |
| 17 | + * <a href="https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI">here</a>. |
| 18 | + * </p> |
| 19 | + */ |
| 20 | +public class FunctionEncoder { |
| 21 | + |
| 22 | + private FunctionEncoder() { } |
| 23 | + |
| 24 | + public static String encode(Function function) { |
| 25 | + List<Type> parameters = function.getInputParameters(); |
| 26 | + |
| 27 | + String methodSignature = buildMethodSignature(function.getName(), parameters); |
| 28 | + String methodId = buildMethodId(methodSignature); |
| 29 | + |
| 30 | + StringBuilder result = new StringBuilder(); |
| 31 | + result.append(methodId); |
| 32 | + |
| 33 | + return encodeParameters(parameters, result); |
| 34 | + } |
| 35 | + |
| 36 | + public static String encodeConstructor(List<Type> parameters) { |
| 37 | + return encodeParameters(parameters, new StringBuilder()); |
| 38 | + } |
| 39 | + |
| 40 | + private static String encodeParameters(List<Type> parameters, StringBuilder result) { |
| 41 | + int dynamicDataOffset = getLength(parameters) * Type.MAX_BYTE_LENGTH; |
| 42 | + StringBuilder dynamicData = new StringBuilder(); |
| 43 | + |
| 44 | + for (Type parameter:parameters) { |
| 45 | + String encodedValue = TypeEncoder.encode(parameter); |
| 46 | + |
| 47 | + if (TypeEncoder.isDynamic(parameter)) { |
| 48 | + String encodedDataOffset = TypeEncoder.encodeNumeric( |
| 49 | + new Uint(BigInteger.valueOf(dynamicDataOffset))); |
| 50 | + result.append(encodedDataOffset); |
| 51 | + dynamicData.append(encodedValue); |
| 52 | + dynamicDataOffset += encodedValue.length() >> 1; |
| 53 | + } else { |
| 54 | + result.append(encodedValue); |
| 55 | + } |
| 56 | + } |
| 57 | + result.append(dynamicData); |
| 58 | + |
| 59 | + return result.toString(); |
| 60 | + } |
| 61 | + |
| 62 | + private static int getLength(List<Type> parameters) { |
| 63 | + int count = 0; |
| 64 | + for (Type type:parameters) { |
| 65 | + if (type instanceof StaticArray) { |
| 66 | + count += ((StaticArray) type).getValue().size(); |
| 67 | + } else { |
| 68 | + count++; |
| 69 | + } |
| 70 | + } |
| 71 | + return count; |
| 72 | + } |
| 73 | + |
| 74 | + static String buildMethodSignature(String methodName, List<Type> parameters) { |
| 75 | + StringBuilder result = new StringBuilder(); |
| 76 | + result.append(methodName); |
| 77 | + result.append("("); |
| 78 | + String params = parameters.stream() |
| 79 | + .map(Type::getTypeAsString) |
| 80 | + .collect(Collectors.joining(",")); |
| 81 | + result.append(params); |
| 82 | + result.append(")"); |
| 83 | + return result.toString(); |
| 84 | + } |
| 85 | + |
| 86 | + static String buildMethodId(String methodSignature) { |
| 87 | + byte[] input = methodSignature.getBytes(); |
| 88 | + byte[] hash = Hash.sha3(input); |
| 89 | + return Numeric.toHexString(hash).substring(0, 10); |
| 90 | + } |
| 91 | +} |
0 commit comments