Search in sources :

Example 1 with TypeReference

use of org.web3j.abi.TypeReference in project winery by eclipse.

the class ProvenanceSmartContractWrapper method getProvenance.

public CompletableFuture<List<ModelProvenanceElement>> getProvenance(final String identifier) {
    // eventName, indexed parameters, unindexed parameters
    final Event event = new Event("ResourceVersion", Arrays.asList(new TypeReference<Utf8String>() {
    }, new TypeReference<Address>() {
    }, new TypeReference<Bytes>() {
    }));
    EthFilter filter = new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, contract.getContractAddress()).addSingleTopic(EventEncoder.encode(event)).addOptionalTopics(Hash.sha3String(identifier)).addNullTopic();
    final CompletableFuture<List<ModelProvenanceElement>> result = new CompletableFuture<>();
    try {
        final int recordsCount = web3j.ethGetLogs(filter).send().getLogs().size();
        LOGGER.info(recordsCount + " provenance elements detected.");
        if (recordsCount > 0) {
            final List<ModelProvenanceElement> provenanceElements = new ArrayList<>();
            final Disposable subscription = ((Provenance) contract).resourceVersionEventFlowable(filter).subscribe(resourceVersionEventResponse -> {
                try {
                    final ModelProvenanceElement currentElement = generateProvenanceElement(resourceVersionEventResponse);
                    provenanceElements.add(currentElement);
                    if (provenanceElements.size() == recordsCount) {
                        result.complete(provenanceElements);
                    }
                } catch (EthereumException e) {
                    result.completeExceptionally(e);
                }
            });
            // unsubscribe the observable when the CompletableFuture completes (this frees threads)
            result.whenComplete((r, e) -> subscription.dispose());
        } else {
            // empty result
            result.complete(new ArrayList<>());
        }
    } catch (IOException e) {
        final String msg = "Failed detecting the number of provenance elements for the collaboration resource. Reason: " + e.getMessage();
        LOGGER.error(msg);
        result.completeExceptionally(new EthereumException(msg, e));
    }
    return result;
}
Also used : Disposable(io.reactivex.disposables.Disposable) EthFilter(org.web3j.protocol.core.methods.request.EthFilter) ModelProvenanceElement(org.eclipse.winery.accountability.model.ModelProvenanceElement) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Utf8String(org.web3j.abi.datatypes.Utf8String) CompletableFuture(java.util.concurrent.CompletableFuture) Event(org.web3j.abi.datatypes.Event) EthereumException(org.eclipse.winery.accountability.exceptions.EthereumException) ArrayList(java.util.ArrayList) List(java.util.List) TypeReference(org.web3j.abi.TypeReference)

Example 2 with TypeReference

use of org.web3j.abi.TypeReference in project winery by eclipse.

the class AuthorizationSmartContractWrapper method getAuthorizationTree.

/**
 * Retrieves the {@link AuthorizationInfo} from the blockchain.
 * If no authorization data can be retrieved, the completable future returns <code>null</code>.
 *
 * @param identifier The process identifier identifying the collaboration process.
 * @return A completable future containing the authorization information.
 */
public CompletableFuture<AuthorizationInfo> getAuthorizationTree(final String identifier) {
    // eventName, indexed parameters, unindexed parameters
    final Event event = new Event("Authorized", Arrays.asList(new TypeReference<Utf8String>() {
    }, new TypeReference<Address>() {
    }, new TypeReference<Address>() {
    }, new TypeReference<Utf8String>() {
    }));
    EthFilter filter = new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, contract.getContractAddress()).addSingleTopic(EventEncoder.encode(event)).addOptionalTopics(Hash.sha3String(identifier)).addNullTopic().addNullTopic();
    final CompletableFuture<AuthorizationInfo> result = new CompletableFuture<>();
    try {
        final int recordsCount = web3j.ethGetLogs(filter).send().getLogs().size();
        LOGGER.info(recordsCount + " authorization elements detected.");
        if (recordsCount > 0) {
            final List<AuthorizationElement> authorizationElements = new ArrayList<>();
            final Disposable subscription = ((Authorization) contract).authorizedEventFlowable(filter).subscribe(authorizedEventResponse -> {
                try {
                    final AuthorizationElement currentElement = generateAuthorizationElement(authorizedEventResponse);
                    authorizationElements.add(currentElement);
                    if (authorizationElements.size() == recordsCount) {
                        final AuthorizationTree tree = new AuthorizationTree(authorizationElements);
                        result.complete(tree);
                    }
                } catch (EthereumException e) {
                    result.completeExceptionally(e);
                }
            });
            // unsubscribe the observable when the CompletableFuture completes (this frees threads)
            result.whenComplete((r, e) -> subscription.dispose());
        } else {
            // empty result
            result.complete(null);
        }
    } catch (IOException e) {
        final String msg = "Failed detecting the number of authorization elements for the collaboration resource. Reason: " + e.getMessage();
        LOGGER.error(msg);
        result.completeExceptionally(new EthereumException(msg, e));
    }
    return result;
}
Also used : Disposable(io.reactivex.disposables.Disposable) EthFilter(org.web3j.protocol.core.methods.request.EthFilter) ArrayList(java.util.ArrayList) AuthorizationElement(org.eclipse.winery.accountability.model.authorization.AuthorizationElement) IOException(java.io.IOException) Utf8String(org.web3j.abi.datatypes.Utf8String) AuthorizationInfo(org.eclipse.winery.accountability.model.authorization.AuthorizationInfo) CompletableFuture(java.util.concurrent.CompletableFuture) AuthorizationTree(org.eclipse.winery.accountability.model.authorization.AuthorizationTree) Event(org.web3j.abi.datatypes.Event) EthereumException(org.eclipse.winery.accountability.exceptions.EthereumException) TypeReference(org.web3j.abi.TypeReference)

Example 3 with TypeReference

use of org.web3j.abi.TypeReference in project jbpm-work-items by kiegroup.

the class QueryExistingContractWorkitemHandler method executeWorkItem.

public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
    try {
        String serviceURL = (String) workItem.getParameter("ServiceURL");
        String contractAddress = (String) workItem.getParameter("ContractAddress");
        String contractMethodName = (String) workItem.getParameter("ContractMethodName");
        String contractMethodOutputType = (String) workItem.getParameter("MethodOutputType");
        if (StringUtils.isNotEmpty(serviceURL) && StringUtils.isNotEmpty(contractAddress) && StringUtils.isNotEmpty(contractMethodName)) {
            Map<String, Object> results = new HashMap<String, Object>();
            if (web3j == null) {
                web3j = Web3j.build(new HttpService(serviceURL));
            }
            auth = new EthereumAuth(walletPassword, walletPath, classLoader);
            Credentials credentials = auth.getCredentials();
            List<TypeReference<?>> outputTypeList = null;
            if (contractMethodOutputType != null) {
                Class typeClazz = Class.forName(contractMethodOutputType);
                outputTypeList = Collections.singletonList(TypeReference.create(typeClazz));
            }
            Object queryReturnObj = EthereumUtils.queryExistingContract(credentials, web3j, contractAddress, contractMethodName, null, outputTypeList);
            results.put(RESULTS, queryReturnObj);
            workItemManager.completeWorkItem(workItem.getId(), results);
        } else {
            logger.error("Missing service url, valid address or method name to execute.");
            throw new IllegalArgumentException("Missing service url, valid address or method name to execute.");
        }
    } catch (Exception e) {
        logger.error("Error executing workitem: " + e.getMessage());
        handleException(e);
    }
}
Also used : HashMap(java.util.HashMap) HttpService(org.web3j.protocol.http.HttpService) TypeReference(org.web3j.abi.TypeReference) Credentials(org.web3j.crypto.Credentials)

Example 4 with TypeReference

use of org.web3j.abi.TypeReference in project jbpm-work-items by kiegroup.

the class EthereumUtils method getFunction.

public static Function getFunction(String queryName, List<Type> queryInputTypes, List<TypeReference<?>> queryOutputTypes) {
    List<Type> inputParameters;
    if (queryInputTypes != null) {
        inputParameters = queryInputTypes;
    } else {
        inputParameters = new ArrayList<>();
    }
    List<TypeReference<?>> outputTypes;
    if (queryOutputTypes != null) {
        outputTypes = queryOutputTypes;
    } else {
        outputTypes = new ArrayList<>();
    }
    Function function = new Function(queryName, inputParameters, outputTypes);
    return function;
}
Also used : Function(org.web3j.abi.datatypes.Function) Type(org.web3j.abi.datatypes.Type) TypeReference(org.web3j.abi.TypeReference)

Aggregations

TypeReference (org.web3j.abi.TypeReference)4 Disposable (io.reactivex.disposables.Disposable)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 EthereumException (org.eclipse.winery.accountability.exceptions.EthereumException)2 Event (org.web3j.abi.datatypes.Event)2 Utf8String (org.web3j.abi.datatypes.Utf8String)2 EthFilter (org.web3j.protocol.core.methods.request.EthFilter)2 HashMap (java.util.HashMap)1 List (java.util.List)1 ModelProvenanceElement (org.eclipse.winery.accountability.model.ModelProvenanceElement)1 AuthorizationElement (org.eclipse.winery.accountability.model.authorization.AuthorizationElement)1 AuthorizationInfo (org.eclipse.winery.accountability.model.authorization.AuthorizationInfo)1 AuthorizationTree (org.eclipse.winery.accountability.model.authorization.AuthorizationTree)1 Function (org.web3j.abi.datatypes.Function)1 Type (org.web3j.abi.datatypes.Type)1 Credentials (org.web3j.crypto.Credentials)1 HttpService (org.web3j.protocol.http.HttpService)1