Skip to content

Commit

Permalink
Merge branch 'master' into 369-generate-pojos-for-annoation-processor…
Browse files Browse the repository at this point in the history
…-from-cip57-plutusblueprints
  • Loading branch information
satran004 committed Jul 15, 2024
2 parents 7a2b41d + 3bd718a commit 8b032a3
Show file tree
Hide file tree
Showing 154 changed files with 7,426 additions and 754 deletions.
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

A client library for Cardano in Java. This library simplifies the interaction with Cardano blockchain from a Java application.

### **Latest Stable Version**: [0.5.0](https://github.com/bloxbean/cardano-client-lib/releases/tag/v0.5.0)
### **Latest Stable Version**: [0.5.1](https://github.com/bloxbean/cardano-client-lib/releases/tag/v0.5.1)

### More details --> [Documentation](https://cardano-client.dev/)

Expand All @@ -21,6 +21,7 @@ A client library for Cardano in Java. This library simplifies the interaction wi
### **Tutorials**
- [Simple Ada transfer](https://cardano-client.dev/docs/gettingstarted/simple-transfer)
- [Multisig transfer using Native Script](https://cardano-client.dev/docs/gettingstarted/multisig-quickstart)
- [Governance Api](https://cardano-client.dev/docs/apis/governance-api)

### **Old Posts**

Expand Down Expand Up @@ -76,7 +77,11 @@ For fine-grained dependency management, add one or more below modules as require
| [cip20](cip/cip20) | cardano-client-cip20 | [CIP 20 - Transaction message/comment metadata](https://cips.cardano.org/cips/cip20/) <br> **Dependencies:** metadata |
| [cip25](cip/cip25) | cardano-client-cip25 | [CIP 25 - Media NFT Metadata Standard](https://cips.cardano.org/cips/cip25/) <br> **Dependencies:** metadata |
| [cip27](cip/cip27) | cardano-client-cip27 | [CIP 27 - CNFT Community Royalties Standard](https://cips.cardano.org/cips/cip27/) <br> **Dependencies:** cip25 |
| [cip30](cip/cip30) | cardano-client-cip30 | [CIP 30 - Cardano dApp-Wallet Web Bridge](https://cips.cardano.org/cips/cip30/) <br> **Dependencies:** cip8, core |
| [cip30](cip/cip30) | cardano-client-cip30 | [CIP 30 - Cardano dApp-Wallet Web Bridge](https://cips.cardano.org/cips/cip30/) <br> **Dependencies:** cip8, core <br/> |
| [cip67](cip/cip67) | cardano-client-cip67 | [CIP 67 - Asset Name Label Registry](https://cips.cardano.org/cips/cip67/) <br> **Dependencies:** crypto, common |
| [cip68](cip/cip68) | cardano-client-cip68 | [CIP 68 - Datum Metadata Standard](https://cips.cardano.org/cips/cip68/) <br> **Dependencies:** cip67, cip25, plutus, transaction-spec |
| [ogmios-supplier](supplier/ogmios-supplier) | cardano-client-ogmios-supplier | Provides Ogmios specific implementation for ProtocolParamsSupplier and TransactionProcessor. |
| [kupo-supplier](supplier/kupo-supplier) | cardano-client-kupo-supplier | Provides Kupo specific implementation for UtxoSupplier. |

## Use as a library in a Java Project

Expand All @@ -90,7 +95,7 @@ For fine-grained dependency management, add one or more below modules as require
<dependency>
<groupId>com.bloxbean.cardano</groupId>
<artifactId>cardano-client-lib</artifactId>
<version>0.5.0</version>
<version>0.5.1</version>
</dependency>
```
- Backend modules
Expand All @@ -101,49 +106,49 @@ For fine-grained dependency management, add one or more below modules as require
<dependency>
<groupId>com.bloxbean.cardano</groupId>
<artifactId>cardano-client-backend-blockfrost</artifactId>
<version>0.5.0</version>
<version>0.5.1</version>
</dependency>

<!-- For Koios backend -->
<dependency>
<groupId>com.bloxbean.cardano</groupId>
<artifactId>cardano-client-backend-koios</artifactId>
<version>0.5.0</version>
<version>0.5.1</version>
</dependency>

<!-- For Ogmios / Kupo backend -->
<dependency>
<groupId>com.bloxbean.cardano</groupId>
<artifactId>cardano-client-backend-ogmios</artifactId>
<version>0.5.0</version>
<version>0.5.1</version>
</dependency>
```

**For Gradle, add the following dependencies to build.gradle**

- Core Module
```
implementation 'com.bloxbean.cardano:cardano-client-lib:0.5.0'
implementation 'com.bloxbean.cardano:cardano-client-lib:0.5.1'
```
- Backend modules
- For backend support, use one of the following supported backend module

```groovy
//For Blockfrost
implementation 'com.bloxbean.cardano:cardano-client-backend-blockfrost:0.5.0'
implementation 'com.bloxbean.cardano:cardano-client-backend-blockfrost:0.5.1'
//For Koios
implementation 'com.bloxbean.cardano:cardano-client-backend-koios:0.5.0'
implementation 'com.bloxbean.cardano:cardano-client-backend-koios:0.5.1'
//For Ogmios / Kupo
implementation 'com.bloxbean.cardano:cardano-client-backend-ogmios:0.5.0'
implementation 'com.bloxbean.cardano:cardano-client-backend-ogmios:0.5.1'
```


### For snapshot binaries

**SNAPSHOT_VERSION :** 0.5.1-SNAPSHOT (Please verify the latest snapshot version in gradle.properties)
**SNAPSHOT_VERSION :** 0.5.2-SNAPSHOT (Please verify the latest snapshot version in gradle.properties)

- For Maven, add the following dependencies and repository to project's pom.xml
```
Expand Down Expand Up @@ -410,9 +415,7 @@ export BF_PROJECT_ID=<Blockfrost Preprod network Project Id>
<a href="https://github.com/djcyr"><img src="https://avatars.githubusercontent.com/u/9329514?s=70&v=4" width="45" height="45" /></a>
</p>

##### If this project helps you reduce time to develop on Cardano or if you just want to support this project, you can delegate to our pool:

[BLOXB](https://www.bloxbean.com/cardano-staking/)
##### If this project helps you reduce time to develop on Cardano or if you just want to support this project :

[Support this project](https://cardano-client.dev/docs/support-this-project)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.bloxbean.cardano.client.address;

import com.bloxbean.cardano.client.exception.AddressRuntimeException;
import com.bloxbean.cardano.client.util.Tuple;

import java.util.Arrays;

/**
* PointerAddress class represents Shelley Pointer address. This class is useful to decode Pointer address and get the Pointer
*/
public class PointerAddress extends Address {
private Pointer pointer;

public PointerAddress(String prefix, byte[] bytes) {
super(prefix, bytes);

if (getAddressType() != AddressType.Ptr)
throw new AddressRuntimeException("Invalid address type. Expected Pointer address type");

decodePointer();
}

public PointerAddress(String address) {
super(address);

if (getAddressType() != AddressType.Ptr)
throw new AddressRuntimeException("Invalid address type. Expected Pointer address type");

decodePointer();
}

public PointerAddress(byte[] addressBytes) {
super(addressBytes);

if (getAddressType() != AddressType.Ptr)
throw new AddressRuntimeException("Invalid address type. Expected Pointer address type");

decodePointer();
}

private void decodePointer() {
byte[] pointerBytes = getDelegationCredentialHash()
.orElseThrow(() -> new AddressRuntimeException("Delegation credential hash not found"));

int index = 0;
Tuple<Long, Integer> slot = variableNatDecode(pointerBytes);
index += slot._2;
Tuple<Long, Integer> txIndex = variableNatDecode(getSubBytes(pointerBytes, index));
index += txIndex._2;
Tuple<Long, Integer> certIndex = variableNatDecode(getSubBytes(pointerBytes, index));

pointer = new Pointer(slot._1, txIndex._1.intValue(), certIndex._1.intValue());
}

private Tuple<Long, Integer> variableNatDecode(byte[] raw) {
long output = 0;
int bytesRead = 0;

for (byte rbyte : raw) {
output = (output << 7) | (rbyte & 0x7F);
bytesRead++;

if ((rbyte & 0x80) == 0) {
return new Tuple<>(output, bytesRead);
}
}

throw new IllegalArgumentException("Invalid variable nat encoding. Unexpected bytes");
}

// Get subarray from startPosition to the end of the array
private byte[] getSubBytes(byte[] array, int startPosition) {
// Validate startPosition
if (startPosition < 0 || startPosition > array.length) {
throw new IllegalArgumentException("Invalid start position");
}

return Arrays.copyOfRange(array, startPosition, array.length);
}

public Pointer getPointer() {
return pointer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.bloxbean.cardano.client.address;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class PointerAddressTest {

@Test
void getPointer() {
PointerAddress pointerAddress = new PointerAddress("addr1gx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer5ph3wczvf2w8lunk");
assertEquals(new Pointer(24157, 177, 42), pointerAddress.getPointer());
}

@Test
void getPointer2() {
PointerAddress pointerAddress = new PointerAddress("addr_test1gz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzerspqgpsqe70et");
assertEquals(new Pointer(1, 2, 3), pointerAddress.getPointer());
}

@Test
void getPointer3() {
PointerAddress pointerAddress = new PointerAddress("addr128phkx6acpnf78fuvxn0mkew3l0fd058hzquvz7w36x4gtupnz75xxcrtw79hu");
assertEquals(new Pointer(2498243, 27, 3), pointerAddress.getPointer());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.bloxbean.cardano.client.api.exception.ApiException;
import com.bloxbean.cardano.client.api.model.Result;
import com.bloxbean.cardano.client.backend.api.AccountService;
import com.bloxbean.cardano.client.backend.api.AddressService;
import com.bloxbean.cardano.client.backend.blockfrost.service.http.AccountApi;
import com.bloxbean.cardano.client.backend.model.*;
import retrofit2.Call;
Expand All @@ -16,10 +17,12 @@
public class BFAccountService extends BFBaseService implements AccountService {

private final AccountApi accountApi;
private final AddressService addressService;

public BFAccountService(String baseUrl, String projectId) {
public BFAccountService(String baseUrl, String projectId, AddressService addressService) {
super(baseUrl, projectId);
this.accountApi = getRetrofit().create(AccountApi.class);
this.addressService = addressService;
}

@Override
Expand Down Expand Up @@ -138,4 +141,56 @@ public Result<List<AccountAsset>> getAccountAssets(String stakeAddress, int coun
throw new ApiException("Error getting accountInformation", e);
}
}

@Override
public Result<List<AddressTransactionContent>> getAccountTransactions(String stakeAddress, int count, int page, OrderEnum order, Integer fromBlockHeight, Integer toBlockHeight) throws ApiException {
Result<List<AccountAddress>> accountAddressesResult = getAllAccountAddresses(stakeAddress);
if (accountAddressesResult.isSuccessful()) {
List<AddressTransactionContent> transactionContents = new ArrayList<>();
List<AccountAddress> accountAddresses = accountAddressesResult.getValue();
try {
for (AccountAddress accountAddress : accountAddresses) {
Result<List<AddressTransactionContent>> listResult = addressService.getTransactions(accountAddress.getAddress(), 100, page, order, fromBlockHeight.toString(), toBlockHeight.toString());
if (listResult.isSuccessful()) {
transactionContents.addAll(listResult.getValue());
}
}
} catch (ApiException e) {
throw new RuntimeException(e);
}
transactionContents.sort((o1, o2) ->
order == OrderEnum.asc ?
Long.compare(o1.getBlockHeight(), o2.getBlockHeight()) :
Long.compare(o2.getBlockHeight(), o1.getBlockHeight()));
return Result.success("SUCCESS").withValue(transactionContents).code(200);
} else {
return Result.error(accountAddressesResult.getResponse()).code(accountAddressesResult.code());
}
}

@Override
public Result<List<AddressTransactionContent>> getAllAccountTransactions(String stakeAddress, OrderEnum order, Integer fromBlockHeight, Integer toBlockHeight) throws ApiException {
Result<List<AccountAddress>> accountAddressesResult = getAllAccountAddresses(stakeAddress);
if (accountAddressesResult.isSuccessful()) {
List<AddressTransactionContent> transactionContents = new ArrayList<>();
List<AccountAddress> accountAddresses = accountAddressesResult.getValue();
try {
for (AccountAddress accountAddress : accountAddresses) {
Result<List<AddressTransactionContent>> listResult = addressService.getAllTransactions(accountAddress.getAddress(), order, fromBlockHeight, toBlockHeight);
if (listResult.isSuccessful()) {
transactionContents.addAll(listResult.getValue());
}
}
} catch (ApiException e) {
throw new RuntimeException(e);
}
transactionContents.sort((o1, o2) ->
order == OrderEnum.asc ?
Long.compare(o1.getBlockHeight(), o2.getBlockHeight()) :
Long.compare(o2.getBlockHeight(), o1.getBlockHeight()));
return Result.success("SUCCESS").withValue(transactionContents).code(200);
} else {
return Result.error(accountAddressesResult.getResponse()).code(accountAddressesResult.code());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.bloxbean.cardano.client.backend.blockfrost.service;

import com.bloxbean.cardano.client.backend.api.AddressService;
import com.bloxbean.cardano.client.api.common.OrderEnum;
import com.bloxbean.cardano.client.api.exception.ApiException;
import com.bloxbean.cardano.client.api.model.Result;
import com.bloxbean.cardano.client.backend.api.AddressService;
import com.bloxbean.cardano.client.backend.blockfrost.service.http.AddressesApi;
import com.bloxbean.cardano.client.backend.model.AddressContent;
import com.bloxbean.cardano.client.backend.model.AddressDetails;
import com.bloxbean.cardano.client.backend.model.AddressTransactionContent;
import com.bloxbean.cardano.client.api.model.Result;
import retrofit2.Call;
import retrofit2.Response;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class BFAddressService extends BFBaseService implements AddressService {
Expand Down Expand Up @@ -68,4 +69,28 @@ public Result<List<AddressTransactionContent>> getTransactions(String address, i
throw new ApiException("Error getting transactions for the address", e);
}
}

public Result<List<AddressTransactionContent>> getAllTransactions(String address, OrderEnum order, Integer fromBlockHeight, Integer toBlockHeight) throws ApiException {
List<AddressTransactionContent> addressTransactionContents = new ArrayList<>();
int page = 1;
Result<List<AddressTransactionContent>> addressTransactionContentsResult = getTransactions(address, 100, page, order, String.valueOf(fromBlockHeight), String.valueOf(toBlockHeight));
while (addressTransactionContentsResult.isSuccessful()) {
addressTransactionContents.addAll(addressTransactionContentsResult.getValue());
if (addressTransactionContentsResult.getValue().size() != 100) {
break;
} else {
page++;
addressTransactionContentsResult = getTransactions(address, 100, page, order, String.valueOf(fromBlockHeight), String.valueOf(toBlockHeight));
}
}
if (!addressTransactionContentsResult.isSuccessful()) {
return addressTransactionContentsResult;
} else {
addressTransactionContents.sort((o1, o2) ->
order == OrderEnum.asc ?
Long.compare(o1.getBlockHeight(), o2.getBlockHeight()) :
Long.compare(o2.getBlockHeight(), o1.getBlockHeight()));
return Result.success(addressTransactionContentsResult.toString()).withValue(addressTransactionContents).code(addressTransactionContentsResult.code());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public NetworkInfoService getNetworkInfoService() {
return new BFNetworkService(getBaseUrl(), getProjectId());
}

@Override
public PoolService getPoolService() {
return new BFPoolService(getBaseUrl(), getProjectId());
}

@Override
public TransactionService getTransactionService() {
return new BFTransactionService(getBaseUrl(), getProjectId());
Expand All @@ -40,7 +45,7 @@ public AddressService getAddressService() {

@Override
public AccountService getAccountService() {
return new BFAccountService(getBaseUrl(), getProjectId());
return new BFAccountService(getBaseUrl(), getProjectId(), getAddressService());
}

@Override
Expand Down
Loading

0 comments on commit 8b032a3

Please sign in to comment.