org.bcos.web3j.abi.EventEncoder Maven / Gradle / Ivy
package org.bcos.web3j.abi;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.bcos.web3j.abi.datatypes.Event;
import org.bcos.web3j.abi.datatypes.Type;
import org.bcos.web3j.crypto.Hash;
import org.bcos.web3j.utils.Numeric;
/**
* Ethereum filter encoding.
* Further limited details are available
* here.
*
*/
public class EventEncoder {
private EventEncoder() { }
public static String encode(Event function) {
List> indexedParameters = function.getIndexedParameters();
List> nonIndexedParameters = function.getNonIndexedParameters();
String methodSignature = buildMethodSignature(function.getName(),
indexedParameters, nonIndexedParameters);
return buildEventSignature(methodSignature);
}
static String buildMethodSignature(
String methodName, List> indexParameters,
List> nonIndexedParameters) {
List> parameters = new ArrayList<>(indexParameters);
parameters.addAll(nonIndexedParameters);
StringBuilder result = new StringBuilder();
result.append(methodName);
result.append("(");
String params = parameters.stream()
.map(p -> Utils.getTypeName(p))
.collect(Collectors.joining(","));
result.append(params);
result.append(")");
return result.toString();
}
static String buildEventSignature(String methodSignature) {
byte[] input = methodSignature.getBytes();
byte[] hash = Hash.sha3(input);
return Numeric.toHexString(hash);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy