All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bcos.web3j.abi.FunctionEncoder Maven / Gradle / Ivy

There is a newer version: 2.6.6
Show newest version
package org.bcos.web3j.abi;

import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

import org.bcos.web3j.abi.datatypes.Function;
import org.bcos.web3j.abi.datatypes.StaticArray;
import org.bcos.web3j.abi.datatypes.Type;
import org.bcos.web3j.abi.datatypes.Uint;
import org.bcos.web3j.crypto.Hash;
import org.bcos.web3j.utils.Numeric;

/**
 * 

Ethereum Contract Application Binary Interface (ABI) encoding for functions. * Further details are available * here. *

*/ public class FunctionEncoder { private FunctionEncoder() { } public static String encode(Function function) { List parameters = function.getInputParameters(); String methodSignature = buildMethodSignature(function.getName(), parameters); String methodId = buildMethodId(methodSignature); StringBuilder result = new StringBuilder(); result.append(methodId); return encodeParameters(parameters, result); } public static String encodeConstructor(List parameters) { return encodeParameters(parameters, new StringBuilder()); } private static String encodeParameters(List parameters, StringBuilder result) { int dynamicDataOffset = getLength(parameters) * Type.MAX_BYTE_LENGTH; StringBuilder dynamicData = new StringBuilder(); for (Type parameter:parameters) { String encodedValue = TypeEncoder.encode(parameter); if (TypeEncoder.isDynamic(parameter)) { String encodedDataOffset = TypeEncoder.encodeNumeric( new Uint(BigInteger.valueOf(dynamicDataOffset))); result.append(encodedDataOffset); dynamicData.append(encodedValue); dynamicDataOffset += encodedValue.length() >> 1; } else { result.append(encodedValue); } } result.append(dynamicData); return result.toString(); } private static int getLength(List parameters) { int count = 0; for (Type type:parameters) { if (type instanceof StaticArray) { count += ((StaticArray) type).getValue().size(); } else { count++; } } return count; } static String buildMethodSignature(String methodName, List parameters) { StringBuilder result = new StringBuilder(); result.append(methodName); result.append("("); String params = parameters.stream() .map(Type::getTypeAsString) .collect(Collectors.joining(",")); result.append(params); result.append(")"); return result.toString(); } static String buildMethodId(String methodSignature) { byte[] input = methodSignature.getBytes(); byte[] hash = Hash.sha3(input); return Numeric.toHexString(hash).substring(0, 10); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy