A client library for Cardano in Java. This library simplifies the interaction with Cardano blockchain from a Java application.
Latest Stable Version: 0.2.0
Posts
- Cardano-client-lib : A Java Library to interact with Cardano - Part I
- Cardano-client-lib: Transaction with Metadata in Java - Part II
- Cardano-client-lib: Minting a new Native Token in Java - Part III
- Composable functions to build transactions
Examples
Cardano-client-lib examples repository
Features
- Address Generation (Base Address, Enterprise Address)
- Generate Address from Mnemonic phase
- API to build Payment transaction (ADA & Native Tokens)
- CBOR serialization of transaction
- Transaction signing
- To build and submit
- Payment transaction
- Token Minting and token transfer transaction
- To build and submit
- Payment transaction
- Token Minting and token transfer
- Plutus smart contract call
- Token minting with Plutus contract
Examples with Composable Functions
- CIP20 - Transaction Message/Comment metada
- CIP25 - NFT Metadata Standard
- CIP8 - Message Signing
- CIP30 - dApp signData & verify
- Helper to build Metadata
- Converter to conver JSON (No Schema) to Metadata format
- Token Minting transaction builder
- Native script (ScriptAll, ScriptAny, ScriptAtLeast, ScriptPubKey, RequireTimeAfter, RequireTimeBefore)
- Policy Id generation
The library also provides integration with Cardano node through different backend services. Out of box, the library currently supports integration with following providers through the Backend api.
- Blockfrost
- Module : cardano-client-backend-blockfrost README
- Status : Stable
- Koios
- Module : cardano-client-backend-koios README
- Status : Beta
- Ogmios
- Module : cardano-client-backend-koios README
- Status : Experimental
- Supported Apis : submitTransaction, evaluateTx
- cardano-graphql
- Module : cardano-client-backend-gql README
- Status : Deprecated
Following Backend apis are currently available
- TransactionService (Submit transaction, Get transaction, Evaluate ExUnits for Script Txn)
- AddressService (Get address details)
- UtxoService (Get utxos for an address)
- AssetService
- BlockService
- NetworkInfoService
- EpochService
- MetadataService
For Maven, add the following dependencies to project's pom.xml
- Core module
<dependency>
<groupId>com.bloxbean.cardano</groupId>
<artifactId>cardano-client-lib</artifactId>
<version>0.2.0</version>
</dependency>
- Backend modules
- For backend support, use one of the following supported backend module
<!-- For Blockfrost backend -->
<dependency>
<groupId>com.bloxbean.cardano</groupId>
<artifactId>cardano-client-backend-blockfrost</artifactId>
<version>0.2.0</version>
</dependency>
<!-- For Koios backend -->
<dependency>
<groupId>com.bloxbean.cardano</groupId>
<artifactId>cardano-client-backend-koios</artifactId>
<version>0.2.0</version>
</dependency>
<!-- For Ogmios backend -->
<dependency>
<groupId>com.bloxbean.cardano</groupId>
<artifactId>cardano-client-backend-ogmios</artifactId>
<version>0.2.0</version>
</dependency>
<!-- For Cardano Graphql backend -->
<dependency>
<groupId>com.bloxbean.cardano</groupId>
<artifactId>cardano-client-backend-gql</artifactId>
<version>0.2.0</version>
</dependency>
For Gradle, add the following dependencies to build.gradle
- Core Module
implementation 'com.bloxbean.cardano:cardano-client-lib:0.2.0'
- Backend modules
- For backend support, use one of the following supported backend module
//For Blockfrost
implementation 'com.bloxbean.cardano:cardano-client-backend-blockfrost:0.2.0'
//For Koios
implementation 'com.bloxbean.cardano:cardano-client-backend-koios:0.2.0'
//For Ogmios
implementation 'com.bloxbean.cardano:cardano-client-backend-ogmios:0.2.0'
//For Cardano Graphql
implementation 'com.bloxbean.cardano:cardano-client-backend-gql:0.2.0'
SNAPSHOT_VERSION : 0.3.0-SNAPSHOT (Please verify the latest snapshot version in gradle.properties)
- For Maven, add the following dependencies and repository to project's pom.xml
<dependencies>
<dependency>
<groupId>com.bloxbean.cardano</groupId>
<artifactId>cardano-client-lib</artifactId>
<version>{SNAPSHOT_VERSION}</version>
</dependency>
<dependency>
<groupId>com.bloxbean.cardano</groupId>
<artifactId>cardano-client-backend-blockfrost</artifactId>
<version>{SNAPSHOT_VERSION}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>snapshots-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
- For Gradle, add the following dependencies and repository to build.gradle
repositories {
...
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
}
}
implementation 'com.bloxbean.cardano:cardano-client-lib:{SNAPSHOT_VERSION}'
implementation 'com.bloxbean.cardano:cardano-client-backend-blockfrost:{SNAPSHOT_VERSION}'
- Create a New Account
Account account = new Account(); //Create a Mainnet account
Account account = new Account(Networks.mainnet()); //Create a Mainnet account
Account account = new Account(Networks.testnet()); //Create a Testnet account
- Get base address, enterprise address, mnemonic
String baseAddress = account.baseAddress(); //Base address at index=0
String enterpriseAddress = account.account.enterpriseAddress(); //Enterprise address at index = 0
String mnemonic = account.mnemonic(); //Get Mnemonic
- Get Account from Mnemonic
String mnemonic = "...";
Account account = new Account(mnemonic); //Create a Mainnet account from Mnemonic
Account account = new Account(Networks.testnet(), mnemonic); //Create a Testnet account from Mnemonic
BackendService backendService =
new BFBackendService(Constants.BLOCKFROST_TESTNET_URL, <BF_PROJECT_ID>);
FeeCalculationService feeCalculationService = backendService.getFeeCalculationService();
TransactionHelperService transactionHelperService = backendService.getTransactionHelperService();
TransactionService transactionService = backendService.getTransactionService();
BlockService blockService = backendService.getBlockService();
AssetService assetService = backendService.getAssetService();
UtxoService utxoService = backendService.getUtxoService();
MetadataService metadataService = backendService.getMetadataService();
EpochService epochService = backendService.getEpochService();
AddressService addressService = backendService.getAddressService();
PaymentTransaction paymentTransaction = PaymentTransaction.builder()
.sender(sender)
.receiver(receiver)
.amount(BigInteger.valueOf(1500000))
.unit("lovelace")
.build();
//Calculate Time to Live
long ttl = blockService.getLastestBlock().getValue().getSlot() + 1000;
TransactionDetailsParams detailsParams =
TransactionDetailsParams.builder()
.ttl(ttl)
.build();
//Calculate fee
BigInteger fee
= feeCalculationService.calculateFee(paymentTransaction, detailsParams, null);
paymentTransaction.setFee(fee);
Result<String> result =
transactionHelperService.transfer(paymentTransaction, detailsParam);
if(result.isSuccessful())
System.out.println("Transaction Id: " + result.getValue());
PaymentTransaction paymentTransaction =
PaymentTransaction.builder()
.sender(sender)
.receiver(receiver)
.amount(BigInteger.valueOf(12))
.unit("329728f73683fe04364631c27a7912538c116d802416ca1eaf2d7a96736174636f696e")
.build();
//Calculate Time to Live
long ttl = blockService.getLastestBlock().getValue().getSlot() + 1000;
TransactionDetailsParams detailsParams =
TransactionDetailsParams.builder()
.ttl(ttl)
.build();
//Calculate fee
BigInteger fee
= feeCalculationService.calculateFee(paymentTransaction, detailsParams, null);
paymentTransaction.setFee(fee);
Result<String> result = transactionHelperService.transfer(paymentTransaction, detailsParam);
if(result.isSuccessful())
System.out.println("Transaction Id: " + result.getValue());
Example: 1
ScriptPubkey scriptPubkey = new ScriptPubkey("ad7a7b87959173fc9eac9a85891cc93892f800dd45c0544128228884")
String policyId = scriptPubkey.getPolicyId();
Example: 2
ScriptPubkey scriptPubkey1 = ...;
SecretKey sk1 = ...;
ScriptPubkey scriptPubkey2 = ...;
SecretKey sk2 = ...;
ScriptPubkey scriptPubkey3 = ...;
SecretKey sk3 = ...;
ScriptAtLeast scriptAtLeast = new ScriptAtLeast(2)
.addScript(scriptPubkey1)
.addScript(scriptPubkey2)
.addScript(scriptPubkey3);
String policyId = scriptAtLeast.getPolicyId();
MultiAsset multiAsset = new MultiAsset();
multiAsset.setPolicyId(policyId);
Asset asset = new Asset("testtoken"), BigInteger.valueOf(250000));
multiAsset.getAssets().add(asset);
MintTransaction mintTransaction = MintTransaction.builder()
.sender(sender)
.receiver(receiver)
.mintAssets(Arrays.asList(multiAsset))
.policyScript(scriptAtLeast)
.policyKeys(Arrays.asList(sk2, sk3))
.build();
//Calculate Time to Live
long ttl = blockService.getLastestBlock().getValue().getSlot() + 1000;
TransactionDetailsParams detailsParams =
TransactionDetailsParams.builder()
.ttl(ttl)
.build();
//Calculate fee
BigInteger fee
= feeCalculationService.calculateFee(mintTransaction, detailsParams, null);
mintTransaction.setFee(fee);
Result<String> result = transactionHelperService.mintToken(mintTransaction,
TransactionDetailsParams.builder().ttl(getTtl()).build());
CBORMetadataMap productDetailsMap
= new CBORMetadataMap()
.put("code", "PROD-800")
.put("slno", "SL20000039484");
CBORMetadataList tagList
= new CBORMetadataList()
.add("laptop")
.add("computer");
Metadata metadata = new CBORMetadata()
.put(new BigInteger("670001"), productDetailsMap)
.put(new BigInteger("670002"), tagList);
PaymentTransaction paymentTransaction =
PaymentTransaction.builder()
...
.build();
long ttl = blockService.getLastestBlock().getValue().getSlot() + 1000;
TransactionDetailsParams detailsParams =
TransactionDetailsParams.builder()
.ttl(ttl)
.build();
//Also add metadata for fee calculation
BigInteger fee = feeCalculationService.calculateFee(paymentTransaction, detailsParams, metadata);
paymentTransaction.setFee(fee);
//Send metadata as 3rd parameter
Result<String> result
= transactionHelperService.transfer(paymentTransaction, detailsParams, metadata);
The utxo selection strategy can be changed by providing a custom implementation of "UtxoSelectionStrategy" interface. By default, the high level api like TransactionHelperService uses a default out-of-box implementation "DefaultUtxoSelectionStrategyImpl". The default strategy is too simple and finds all required utxos sequentially. But it may not be efficient for some usecases.
You can use a custom or different implementation of UtxoSelectionStrategy to change the default utxo selection behaviour.
UtxoSupplier utxoSupplier = new DefaultUtxoSupplier(utxoService);
ProtocolParamsSupplier protocolParamsSupplier = new DefaultProtocolParamsSupplier(epochService);
//Create TransactionHelperService with LargestFirst
TransactionBuilder transactionBuilder = new TransactionBuilder(new LargestFirstUtxoSelectionStrategy(utxoSupplier), protocolParamsSupplier);
TransactionHelperService transactionHelperService = new TransactionHelperService(transactionBuilder, new DefaultTransactionProcessor(transactionService));
//Get FeeCalculationService using the above TransactionHelperService
FeeCalculationService feeCalculationService = backendService.getFeeCalculationService(transactionHelperService);
git clone https://github.com/bloxbean/cardano-client-lib.git
export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8
./gradlew clean build
export BF_PROJECT_ID=<Blockfrost Project Id>
./gradlew :integration-test:integrationTest -PBF_PROJECT_ID=${BF_PROJECT_ID}