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

Commit

Permalink
first implementation of direct order
Browse files Browse the repository at this point in the history
  • Loading branch information
aaitor committed Apr 27, 2020
1 parent ecc75ba commit 1b23465
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 63 deletions.
14 changes: 12 additions & 2 deletions src/main/java/io/keyko/nevermind/api/AssetsAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public interface AssetsAPI {
* @return the input stream wit the binary content of the file
* @throws ConsumeServiceException ConsumeServiceException
*/
public InputStream consumeBinary(String serviceAgreementId, DID did, int serviceDefinitionId, Integer index) throws ConsumeServiceException;
InputStream consumeBinary(String serviceAgreementId, DID did, int serviceDefinitionId, Integer index) throws ConsumeServiceException;

/**
* Gets the input stream of one file of the asset
Expand Down Expand Up @@ -226,7 +226,7 @@ public interface AssetsAPI {
* @return the input stream wit the binary content of the specified range
* @throws ConsumeServiceException ConsumeServiceException
*/
public InputStream consumeBinary(String serviceAgreementId, DID did, int serviceDefinitionId, Integer index, Integer rangeStart, Integer rangeEnd, int threshold) throws ConsumeServiceException;
InputStream consumeBinary(String serviceAgreementId, DID did, int serviceDefinitionId, Integer index, Integer rangeStart, Integer rangeEnd, int threshold) throws ConsumeServiceException;


/**
Expand All @@ -239,6 +239,16 @@ public interface AssetsAPI {
*/
Flowable<OrderResult> order(DID did, int serviceDefinitionId) throws OrderException;

/**
* Purchases an Asset represented by a DID. It implies to initialize a Service Agreement between publisher and consumer
*
* @param did the did of the DDO
* @param serviceDefinitionId the service definition id
* @return OrderResult
* @throws OrderException OrderException
*/
OrderResult orderDirect(DID did, int serviceDefinitionId) throws OrderException, ServiceException, EscrowRewardException;

/**
* Executes a remote service associated with an asset and serviceAgreementId
* @param agreementId the agreement id
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/io/keyko/nevermind/api/impl/AssetsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ public InputStream consumeBinary(String serviceAgreementId, DID did, int service

@Override
public Flowable<OrderResult> order(DID did, int serviceDefinitionId) throws OrderException {
return nevermindManager.purchaseAsset(did, serviceDefinitionId);
return nevermindManager.purchaseAssetFlowable(did, serviceDefinitionId);
}

public OrderResult orderDirect(DID did, int serviceDefinitionId) throws OrderException, ServiceException, EscrowRewardException {
return nevermindManager.purchaseAssetDirect(did, serviceDefinitionId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package io.keyko.nevermind.core.sla.functions;

import io.keyko.common.helpers.CryptoHelper;
import io.keyko.common.helpers.EncodingHelper;
import io.keyko.common.helpers.EthereumHelper;
import io.keyko.nevermind.contracts.LockRewardCondition;
import io.keyko.nevermind.exceptions.LockRewardFulfillException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.web3j.crypto.Hash;
import org.web3j.crypto.Keys;
import org.web3j.protocol.core.methods.response.TransactionReceipt;

Expand Down
25 changes: 21 additions & 4 deletions src/main/java/io/keyko/nevermind/manager/BaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
public abstract class BaseManager {

protected static final Logger log = LogManager.getLogger(BaseManager.class);
private static final int MAX_SS_RETRIES = 5;
private static final long SS_DECRYPTION_SLEEP = 1000l;

private KeeperService keeperService;
private MetadataService metadataService;
Expand All @@ -61,6 +63,7 @@ public abstract class BaseManager {
protected ConditionStoreManager conditionStoreManager;
protected ComputeExecutionCondition computeExecutionCondition;
protected EscrowComputeExecutionTemplate escrowComputeExecutionTemplate;
protected Condition condition;
protected ContractAddresses contractAddresses = new ContractAddresses();
protected Config config = ConfigFactory.load();

Expand Down Expand Up @@ -128,14 +131,28 @@ protected DDO buildDDO(io.keyko.nevermind.models.service.types.MetadataService m
return this.buildDDO(metadataService, authorizationService, address, 0);
}

public List<AssetMetadata.File> getMetadataFiles(DDO ddo) throws IOException, EncryptionException {
public List<AssetMetadata.File> getMetadataFiles(DDO ddo) throws IOException, EncryptionException, InterruptedException {
return getMetadataFiles(ddo, MAX_SS_RETRIES);
}

public List<AssetMetadata.File> getMetadataFiles(DDO ddo, int retries) throws IOException, EncryptionException, InterruptedException {
int counter = 0;
AuthorizationService authorizationService = ddo.getAuthorizationService();
SecretStoreManager secretStoreManager = getSecretStoreInstance(authorizationService);

String jsonFiles = secretStoreManager.decryptDocument(ddo.getDid().getHash(), ddo.getMetadataService().attributes.encryptedFiles);
return DDO.fromJSON(new TypeReference<ArrayList<AssetMetadata.File>>() {
}, jsonFiles);
String jsonFiles= null;
while (counter < retries) {
try {
jsonFiles = secretStoreManager.decryptDocument(ddo.getDid().getHash(), ddo.getMetadataService().attributes.encryptedFiles);
return DDO.fromJSON(new TypeReference<ArrayList<AssetMetadata.File>>() {}, jsonFiles);
} catch (EncryptionException e) {
log.warn("Unable to decrypt [" + counter + "]");
counter++;
Thread.sleep(SS_DECRYPTION_SLEEP);
}
}
throw new EncryptionException("Unable to decrypt document after " + retries + " retries");

}

public boolean tokenApprove(OceanToken tokenContract, String spenderAddress, String price) throws TokenApproveException {
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/io/keyko/nevermind/manager/ConditionsManager.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package io.keyko.nevermind.manager;

import io.keyko.common.helpers.CryptoHelper;
import io.keyko.common.helpers.EncodingHelper;
import io.keyko.common.helpers.EthereumHelper;
import io.keyko.common.web3.KeeperService;
import io.keyko.nevermind.contracts.Condition;
import io.keyko.nevermind.contracts.LockRewardCondition;
import io.keyko.nevermind.exceptions.LockRewardFulfillException;
import io.keyko.nevermind.models.DDO;
import io.keyko.nevermind.models.DID;
import io.keyko.nevermind.models.service.Agreement;
Expand Down Expand Up @@ -36,6 +41,35 @@ public static ConditionsManager getInstance(KeeperService keeperService, Metadat
return new ConditionsManager(keeperService, metadataService);
}

public static byte[] generateId(final String serviceAgreementId,
final String conditionAddress,
final String... hashValues) throws LockRewardFulfillException {


try {
// String valueHash = EthereumHelper.encodeParameterValue("bytes32", serviceAgreementId) +
// EthereumHelper.encodeParameterValue("address", conditionAddress);
String valueHash = "";

for (String value: hashValues) {
valueHash = valueHash + EthereumHelper.encodeParameterValue("bytes32", value);
}
//byte[] encryptedHash = CryptoHelper.keccak256(
String encryptedHash = CryptoHelper.sha3256(
EthereumHelper.add0x(valueHash));

return CryptoHelper.keccak256(
EthereumHelper.add0x(
EthereumHelper.encodeParameterValue("bytes32", serviceAgreementId)
+ EthereumHelper.encodeParameterValue("address", conditionAddress)
+ encryptedHash));
} catch (Exception e) {
String msg = "Error generating conditionId " + serviceAgreementId;
log.error(msg + ": " + e.getMessage());
throw new LockRewardFulfillException(msg, e);
}
}

/**
* Lock reward for a service agreement.
*
Expand Down

0 comments on commit 1b23465

Please sign in to comment.