Skip to content
🦑 Java client library for Ocean Protocol
Branch: develop
Clone or download
Latest commit 486f370 Mar 22, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Added GitHub templates Dec 21, 2018
profiles
src fixing codacy issues Mar 22, 2019
.bumpversion.cfg Bumpversion 0.3.5 -> 0.3.6 Feb 25, 2019
.gitignore Ignore extra Eclipse files Nov 1, 2018
.maven.xml
.travis.yml changing jacoco path Mar 16, 2019
CHANGELOG.md
CODEOWNERS
LICENSE adding year Mar 12, 2019
MANIFEST.in
README.md adding codacy badge Mar 16, 2019
bumpversion.sh
library.json
pom.xml Implementing ocean.template API, implements #74 Mar 16, 2019

README.md

banner

Squid-java

🦑 Java client library for Ocean Protocol

🐳 Ocean client Library (Java) oceanprotocol.com

Codacy Badge Build Status

Table of Contents


Features

This library enables to integrate the Ocean Protocol capabilities from JVM clients.

Currently squid-java implements the last version of the squid-spec (v0.2).

## Using the API

You can configure the library using TypeSafe Config or a Java Properties Object

In case you want to use TypeSafe Config you would need an application.conf file with this shape:

keeper.url="http://localhost:8545"
keeper.gasLimit=4712388
keeper.gasPrice=100000000000

aquarius.url="http://localhost:5000"

secretstore.url="http://localhost:12001"

# Contracts addresses
contract.SignCondition.address="0xEEE56e2a630DD29F9A628d618E58bb173911F393"
contract.HashLockCondition.address="0x85cCa2B01adddCA8Df221e6027EE0D7716224202"
contract.LockRewardCondition.address="0x3a3926f3f88F1eE05164404f93FDb3887cbE8e35"
contract.AccessSecretStoreCondition.address="0x19513460bc16254c74AE806683E906478A42B543"
contract.EscrowReward.address="0x8F006DbB3727d18f032C5618595ecDD2EDE13b61"
contract.EscrowAccessSecretStoreTemplate.address="0xD306b5edCDC7819E1EB80B43De6548931706A3f4"
contract.OceanToken.address="0x726baA2f854A3BEC2378a707AeB38c9d933Ebad6"
contract.Dispenser.address="0xF152cF3c67dFD41a317eAe8fAc0e1e8E98724A13"
contract.DIDRegistry.address="0xc354ba9AD5dF1023C2640b14A09E61a500F21546"
contract.ConditionStoreManager.address="0x336EFb3c9E56F713dFdA4CDB3Dd0882F3226b6eE"
contract.TemplateStoreManager.address="0xfeA10BBb093d7fcb1EDf575Aa7e28d37b9DcFcE9"
contract.AgreementStoreManager.address="0x645439117eB378a6d35148452E287a038666Ed67"

consume.basePath = "/tmp"

## Main account
account.main.address="0xaabbcc"
account.main.password="pass"
account.main.credentialsFile="/accounts/parity/aabbcc.json.testaccount"

And you can instantiate the API with the following lines:

 Config config = ConfigFactory.load();
 OceanAPI oceanAPI = OceanAPI.getInstance(config);

Remember that TypeSafe Config allows you to overwrite the values using Environment Variables or arguments passed to the JVM.

If you want to use Java's Properties, you just need to create a Properties Object with the same properties of the application.conf. You can read this Properties from a properties file, or define the values of these properties in your code

    // Default values for KEEPER_URL, KEEPER_GAS_LIMIT, KEEPER_GAS_PRICE, AQUARIUS_URL, SECRETSTORE_URL, CONSUME_BASE_PATH
    Properties properties = new Properties();
    properties.put(OceanConfig.MAIN_ACCOUNT_ADDRESS, "0xaabbcc");
    properties.put(OceanConfig.MAIN_ACCOUNT_PASSWORD,"pass");
    properties.put(OceanConfig.MAIN_ACCOUNT_CREDENTIALS_FILE,"/accounts/parity/aabbcc.json.testaccount");
    properties.put(OceanConfig.DID_REGISTRY_ADDRESS, "0x01daE123504DDf108E0C65a42190516E5c5dfc07");
    properties.put(OceanConfig.SERVICE_EXECUTION_AGREEMENT_ADDRESS, "0x21668cE2116Dbc48AC116F31678CfaaeF911F7aA");
    properties.put(OceanConfig.PAYMENT_CONDITIONS_ADDRESS, "0x38A531cc85A58adCb01D6a249E33c27CE277a2D1");
    properties.put(OceanConfig.ACCESS_CONDITIONS_ADDRESS, "0x605FAF898Fc7c2Aa847Ba0D558b5251c0F128Fd7");
    properties.put(OceanConfig.TOKEN_ADDRESS, "0xe749e2f8482810b11b838ae8c5eb69e54d610411");
    properties.put(OceanConfig.OCEAN_MARKET_ADDRESS, "0xf9e633cbeeb2a474d3fe22261046c99e805beec4");

    OceanAPI oceanAPIFromProperties = OceanAPI.getInstance(properties);

Once you have initialized the API you can call the methods through their correspondent API class. For instance:

 Balance balance = oceanAPI.getAccountsAPI().balance(oceanAPI.getMainAccount());

 String filesJson = metadataBase.toJson(metadataBase.base.files);
 String did = DID.builder().getHash();
 String encryptedDocument = oceanAPI.getSecretStoreAPI().encrypt(did, filesJson, 0);

 Flowable<OrderResult> response = oceanAPI.getAssetsAPI().order(did, SERVICE_DEFINITION_ID);
 boolean result = oceanAPI.getAssetsAPI().consume(orderResult.getServiceAgreementId(), did, SERVICE_DEFINITION_ID, "/tmp");

Using Squid-Java with Barge

If you are using Barge for playing with the Ocean Protocol stack, you can use the following command to run the components necessary to have a fully functional environment:

KEEPER_VERSION=v0.8.5 bash start_ocean.sh --latest --no-pleuston --local-spree-node

After a few minutes, when Keeper has deployed the contracts, the ABI files describing the Smart Contracts can be found in the ${HOME}/.ocean/keeper-contracts/artifacts/ folder. Depending on the network you are using, each ABI includes the address where the Smart Contract is deployed in each network.

If you want to run the integration tests in your local, you can execute the Bash Script src/test/resources/scripts/updateConfAddresses.sh to update the addresses to use in your src/test/resources/application.conf file.

Dealing with Flowables

Squid-java uses web3j to interact with Solidity's Smart Contracts. It relies on RxJava to deal with asynchronous calls. The order method in AssetsAPI returns a Flowable over an OrderResult object. It's your choice if you want to handle this in a synchronous or asynchronous fashion. If you prefer to deal with this method in a synchronous way, you will need to block the current thread until you get a response:

 Flowable<OrderResult> response = oceanAPI.getAssetsAPI().order(did, SERVICE_DEFINITION_ID);
 OrderResult orderResult = response.blockingFirst();

On the contrary, if you want to handle the response asynchronously, you will need to subscribe to the Flowable:

response.subscribe(
     orderResultEvent -> {
         if (orderResultEvent.isAccessGranted())
             System.out.println("Access Granted for Service Agreement " + orderResultEvent.getServiceAgreementId());
         else if (orderResultEvent.isPaymentRefund())
             System.out.println("There was a problem with Service Agreement " + orderResultEvent.getServiceAgreementId() + " .Payment Refund");
     }
 );

The subscribe method will launch a new Thread to react to the events of the Flowable. More information: RxJava , Flowable

Installing the library

Typically in Maven you could add the dependency:

<dependency>
  <groupId>com.oceanprotocol</groupId>
  <artifactId>squid-java</artifactId>
  <version>0.4.0</version>
</dependency>

How to run the tests

Unit Tests

You can execute the unit tests using the following command:

mvn clean test

Integration Tests

The execution of the integration tests require to have running the complete Ocean stack using Ocean Barge.

After having barge in your environment, you can run the components needed running:

KEEPER_VERSION=v0.8.0 bash start_ocean.sh --latest --no-pleuston --local-spree-node --force-pull

If you have older versions of the docker images is recommended to delete all them to be sure you are running the last version of the stack.

You can execute the integration tests using the following command:

mvn clean verify -P integration-test

Documentation

You can generate the Javadoc using the following command:

mvn javadoc:javadoc

All the tests

You can run the unit and integration tests running:

mvn clean verify -P all-test

Code Coverage

The code coverage reports are generated using the JaCoCo Maven plugin. Reports are generated in the target/site folder.

New Version

The bumpversion.sh script helps to bump the project version. You can execute the script using as first argument {major|minor|patch} to bump accordingly the version.

License

Copyright 2018 Ocean Protocol Foundation Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

You can’t perform that action at this time.