Skip to content
This repository has been archived by the owner on Feb 8, 2022. It is now read-only.

Commit

Permalink
Implementing Provenance API released in contracts v0.5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
aaitor committed Nov 26, 2020
1 parent 078ffe1 commit accb317
Show file tree
Hide file tree
Showing 21 changed files with 1,118 additions and 26 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.keyko.nevermined</groupId>
<artifactId>api</artifactId>
<version>0.3.6</version>
<version>0.3.7</version>
<packaging>jar</packaging>
<name>Nevermined Data Platform Java SDK</name>
<url>https://github.com/nevermined-io/sdk-java</url>
Expand All @@ -26,7 +26,7 @@
<web3j.version>4.5.18</web3j.version>
<common-utils.version>0.3.2</common-utils.version>

<nevermined.contracts.version>0.5.1</nevermined.contracts.version>
<nevermined.contracts.version>0.5.2</nevermined.contracts.version>
<secretstore.version>0.1.0</secretstore.version>
<eddsa.version>0.3.0</eddsa.version>

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/keyko/nevermined/api/AssetsAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,6 @@ List<ComputeLogs> getComputeLogs(String serviceAgreementId, String executionId,
* @throws ServiceException ServiceException
*/
ComputeStatus getComputeStatus(String serviceAgreementId, String executionId, String consumerAddress,
ProviderConfig providerConfig) throws ServiceException;
ProviderConfig providerConfig) throws ServiceException;

}
15 changes: 15 additions & 0 deletions src/main/java/io/keyko/nevermined/api/NeverminedAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class NeverminedAPI {
private SecretStoreManager secretStoreManager;
private NeverminedManager neverminedManager;
private AssetsManager assetsManager;
private ProvenanceManager provenanceManager;
private AccountsManager accountsManager;
private AgreementsManager agreementsManager;
private ConditionsManager conditionsManager;
Expand All @@ -65,6 +66,7 @@ public class NeverminedAPI {
private ConditionsAPI conditionsAPI;
private TokensAPI tokensAPI;
private AssetsAPI assetsAPI;
private ProvenanceAPI provenanceAPI;
private SecretStoreAPI secretStoreAPI;
private TemplatesAPI templatesAPI;

Expand Down Expand Up @@ -234,12 +236,16 @@ public static NeverminedAPI getInstance(Properties properties) throws Initializa
.setMainAccount(neverminedAPI.mainAccount)
.setDidRegistryContract(neverminedAPI.didRegistryContract);

neverminedAPI.provenanceManager = initializationHelper.getProvenanceManager(neverminedAPI.keeperService);
neverminedAPI.provenanceManager.setDidRegistryContract(neverminedAPI.didRegistryContract);

neverminedAPI.accountsAPI = new AccountsImpl(neverminedAPI.accountsManager);
neverminedAPI.agreementsAPI = new AgreementsImpl(neverminedAPI.agreementsManager, neverminedAPI.neverminedManager);
neverminedAPI.conditionsAPI = new ConditionsImpl(neverminedAPI.conditionsManager);
neverminedAPI.tokensAPI = new TokensImpl(neverminedAPI.accountsManager);
neverminedAPI.secretStoreAPI = new SecretStoreImpl(neverminedAPI.secretStoreManager);
neverminedAPI.assetsAPI = new AssetsImpl(neverminedAPI.neverminedManager, neverminedAPI.assetsManager, neverminedAPI.agreementsManager);
neverminedAPI.provenanceAPI = new ProvenanceImpl(neverminedAPI.neverminedManager, neverminedAPI.provenanceManager);
neverminedAPI.templatesAPI = new TemplatesImpl(neverminedAPI.templatesManager);

return neverminedAPI;
Expand Down Expand Up @@ -317,6 +323,15 @@ public AssetsAPI getAssetsAPI() {
return this.assetsAPI;
}

/**
* Gets the ProvenanceAPI
*
* @return an instance of an Implementation class of ProvenanceAPI
*/
public ProvenanceAPI getProvenanceAPI() {
return this.provenanceAPI;
}

/**
* Gets the SecretStoreAPI
*
Expand Down
132 changes: 132 additions & 0 deletions src/main/java/io/keyko/nevermined/api/ProvenanceAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package io.keyko.nevermined.api;

import io.keyko.nevermined.exceptions.ProvenanceException;
import io.keyko.nevermined.models.DID;
import io.keyko.nevermined.models.contracts.ProvenanceEntry;
import io.keyko.nevermined.models.contracts.ProvenanceEvent;

import java.util.List;

/**
* Exposes the Public API related with Provenance
*/
public interface ProvenanceAPI {

/**
* Implements the W3C PROV Usage action
*
* @param provenanceId Provenance ID
* @param did Identifier of the entity created
* @param agentId Agent Identifier
* @param activityId Identifier of the activity creating the new entity
* @param signature Signature (optional) provided by the agent involved
* @param attributes Attributes associated with the action
* @return true if the provenance event was registered correctly
* @throws ProvenanceException Error registering the event
*/
boolean used(String provenanceId, DID did, String agentId, String activityId, String signature, String attributes) throws ProvenanceException;

/**
* Implements the W3C PROV Derivation action
*
* @param provenanceId Provenance ID
* @param newEntityDid Identifier of the new entity derived
* @param usedEntityDid Identifier of the entity used to derive the new entity
* @param agentId Agent Identifier
* @param activityId Identifier of the activity creating the new entity
* @param attributes Attributes associated with the action
* @return true if the provenance event was registered correctly
* @throws ProvenanceException Error registering the event
*/
boolean wasDerivedFrom(String provenanceId, DID newEntityDid, DID usedEntityDid, String agentId, String activityId, String attributes) throws ProvenanceException;

/**
* Implements the W3C PROV Association action
*
* @param provenanceId Provenance ID
* @param did Identifier of the entity created
* @param agentId Agent Identifier
* @param activityId Identifier of the activity creating the new entity
* @param attributes Attributes associated with the action
* @return true if the provenance event was registered correctly
* @throws ProvenanceException Error registering the event
*/
boolean wasAssociatedWith(String provenanceId, DID did, String agentId, String activityId, String attributes) throws ProvenanceException;

/**
* Implements the W3C PROV Delegation action
*
* @param provenanceId Provenance ID
* @param did Identifier of the entity created
* @param delegateAgentId Delegate Agent Identifier
* @param responsibleAgentId Responsible Agent Identifier
* @param activityId Identifier of the activity creating the new entity
* @param signature Signature provided by the delegated agent
* @param attributes Attributes associated with the action
* @return true if the provenance event was registered correctly
* @throws ProvenanceException Error registering the event
*/
boolean actedOnBehalf(String provenanceId, DID did, String delegateAgentId, String responsibleAgentId, String activityId, String signature, String attributes) throws ProvenanceException;

/**
* Fetch from the on-chain Provenance registry the information about one provenance event, given a provenance id
*
* @param provenanceId unique identifier of the provenance entry
* @return ProvenanceEntry object with all the information stored on-chain
* @throws ProvenanceException Error fetching the data
*/
ProvenanceEntry getProvenanceEntry(String provenanceId) throws ProvenanceException;

/**
* Indicates if an address is a provenance delegate for a given DID
* @param did Identifier of the asset
* @param delegate address of the delegate
* @return true if the address is a provenance delegate
* @throws ProvenanceException Error fetching the data
*/
boolean isProvenanceDelegate(DID did, String delegate) throws ProvenanceException;

/**
* Adds an address as delegate for a given DID
* @param did Identifier of the asset
* @param delegate address of the delegate
* @return true if the address was added as a delegate for the DID given
* @throws ProvenanceException Error fetching the data
*/
boolean addDIDProvenanceDelegate(DID did, String delegate) throws ProvenanceException;

/**
* Remove an address as delegate for a given DID
* @param did Identifier of the asset
* @param delegate address of the delegate
* @return true if the address was removed as a delegate for the DID given
* @throws ProvenanceException Error fetching the data
*/
boolean removeDIDProvenanceDelegate(DID did, String delegate) throws ProvenanceException;

/**
* Adds an address as delegate for a given DID
* @param provenanceId unique identifier of the provenance entry
* @return String with the address owning the provenance entry
* @throws ProvenanceException Error fetching the data
*/
String getProvenanceOwner(String provenanceId) throws ProvenanceException;

/**
* Search for ProvenanceAttributeRegistered events related with a specific DID
*
* @param did Identifier of the entity we are looking provenance events
* @return List of ProvenanceEvents found
* @throws ProvenanceException DDOException
*/
List<ProvenanceEvent> getDIDProvenanceEvents(DID did) throws ProvenanceException;

/**
* Search for provenance methods (used, wasGeneratedBy, etc.) given a DID
*
* @param did Identifier of the entity we are looking provenance events
* @return List of ProvenanceEvents found
* @throws ProvenanceException DDOException
*/
List<ProvenanceEvent> getProvenanceMethodEvents(ProvenanceEntry.ProvenanceMethod method, DID did) throws ProvenanceException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ public AssetsManager getAssetsManager(KeeperService keeperService, MetadataApiSe
return AssetsManager.getInstance(keeperService, metadataApiService);
}

/**
* Initialize an instance of ProvenanceManager
*
* @param keeperService the KeeperService
* @return an initialized ProvenanceManager object
* @throws IOException IOException
* @throws CipherException CipherException
*/
public ProvenanceManager getProvenanceManager(KeeperService keeperService) {
return ProvenanceManager.getInstance(keeperService);
}

/**
* Loads the NeverminedToken contract from Keeper
*
Expand Down
90 changes: 90 additions & 0 deletions src/main/java/io/keyko/nevermined/api/impl/ProvenanceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package io.keyko.nevermined.api.impl;

import io.keyko.nevermined.api.ProvenanceAPI;
import io.keyko.nevermined.exceptions.ProvenanceException;
import io.keyko.nevermined.manager.NeverminedManager;
import io.keyko.nevermined.manager.ProvenanceManager;
import io.keyko.nevermined.models.DID;
import io.keyko.nevermined.models.contracts.ProvenanceEntry;
import io.keyko.nevermined.models.contracts.ProvenanceEvent;

import java.util.List;

/**
* Implementation of AssetsAPI
*/
public class ProvenanceImpl implements ProvenanceAPI {

private NeverminedManager neverminedManager;
private ProvenanceManager provenanceManager;


/**
* Constructor
*
* @param neverminedManager the neverminedManager
* @param provenanceManager the provenanceManager
*/
public ProvenanceImpl(NeverminedManager neverminedManager, ProvenanceManager provenanceManager) {

this.neverminedManager = neverminedManager;
this.provenanceManager = provenanceManager;
}

@Override
public boolean used(String provenanceId, DID did, String agentId, String activityId, String signature, String attributes) throws ProvenanceException {
return provenanceManager.used(provenanceId, did, agentId, activityId, signature, attributes);
}

@Override
public boolean wasDerivedFrom(String provenanceId, DID newEntityDid, DID usedEntityDid, String agentId, String activityId, String attributes) throws ProvenanceException {
return provenanceManager.wasDerivedFrom(provenanceId, newEntityDid, usedEntityDid, agentId, activityId, attributes);
}

@Override
public boolean wasAssociatedWith(String provenanceId, DID did, String agentId, String activityId, String attributes) throws ProvenanceException {
return provenanceManager.wasAssociatedWith(provenanceId, did, agentId, activityId, attributes);
}

@Override
public boolean actedOnBehalf(String provenanceId, DID did, String delegateAgentId, String responsibleAgentId, String activityId, String signature, String attributes) throws ProvenanceException {
return provenanceManager.actedOnBehalf(provenanceId, did, delegateAgentId, responsibleAgentId, activityId, signature, attributes);
}

@Override
public ProvenanceEntry getProvenanceEntry(String provenanceId) throws ProvenanceException {
return provenanceManager.getProvenanceEntry(provenanceId);
}

@Override
public boolean isProvenanceDelegate(DID did, String delegate) throws ProvenanceException {
return provenanceManager.isProvenanceDelegate(did, delegate);
}

@Override
public boolean addDIDProvenanceDelegate(DID did, String delegate) throws ProvenanceException {
return provenanceManager.addDIDProvenanceDelegate(did, delegate);
}

@Override
public boolean removeDIDProvenanceDelegate(DID did, String delegate) throws ProvenanceException {
return provenanceManager.removeDIDProvenanceDelegate(did, delegate);
}

@Override
public String getProvenanceOwner(String provenanceId) throws ProvenanceException {
return provenanceManager.getProvenanceOwner(provenanceId);
}

@Override
public List<ProvenanceEvent> getDIDProvenanceEvents(DID did) throws ProvenanceException {
return provenanceManager.getDIDProvenanceEvents(did);
}

@Override
public List<ProvenanceEvent> getProvenanceMethodEvents(ProvenanceEntry.ProvenanceMethod method, DID did) throws ProvenanceException {
return provenanceManager.getProvenanceMethodEvents(method, did);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.keyko.nevermined.exceptions;

/**
* Business Exception related with Provenance issues
*/
public class ProvenanceException extends NeverminedException {

public ProvenanceException(String message, Throwable e) {
super(message, e);
}

public ProvenanceException(String message) {
super(message);
}
}
12 changes: 9 additions & 3 deletions src/main/java/io/keyko/nevermined/manager/BaseManager.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package io.keyko.nevermined.manager;

import com.fasterxml.jackson.core.type.TypeReference;
import io.keyko.secretstore.core.EvmDto;
import io.keyko.secretstore.core.SecretStoreDto;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import io.keyko.common.helpers.EncodingHelper;
import io.keyko.common.helpers.EthereumHelper;
import io.keyko.common.helpers.UrlHelper;
import io.keyko.common.web3.KeeperService;
import io.keyko.nevermined.contracts.*;
Expand All @@ -17,16 +16,18 @@
import io.keyko.nevermined.models.asset.AssetMetadata;
import io.keyko.nevermined.models.service.types.AuthorizationService;
import io.keyko.nevermined.models.service.types.MetadataService;
import io.keyko.secretstore.core.EvmDto;
import io.keyko.secretstore.core.SecretStoreDto;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.web3j.abi.EventEncoder;
import org.web3j.abi.FunctionReturnDecoder;
import org.web3j.abi.datatypes.Event;
import org.web3j.abi.datatypes.Type;
import org.web3j.crypto.CipherException;
import org.web3j.crypto.Hash;
import org.web3j.crypto.Keys;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.RemoteFunctionCall;
import org.web3j.protocol.core.methods.request.EthFilter;
import org.web3j.protocol.core.methods.response.EthLog;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
Expand Down Expand Up @@ -561,6 +562,11 @@ public byte[] getTemplateIdByName(String contractName) {

}

public String generateSignature(String message) throws IOException, CipherException {
return EncodingHelper.signatureToString(
EthereumHelper.signMessage(message, getKeeperService().getCredentials()));
}

@Override
public String toString() {
return "BaseManager{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1009,10 +1009,6 @@ private String buildDestinationPath(String basePath, DID did, int fileIndex, Ass
return destinationPath;
}

public String generateSignature(String message) throws IOException, CipherException {
return EncodingHelper.signatureToString(
EthereumHelper.signMessage(message, getKeeperService().getCredentials()));
}

/**
* Downloads a single file of an Asset previously ordered through a Service Agreement
Expand Down

0 comments on commit accb317

Please sign in to comment.