Skip to content

Implement a Casper Kotlin SDK to interact with the Casper network.

License

Notifications You must be signed in to change notification settings

tqhuy2018/Casper-Kotlin-sdk

Repository files navigation

CSPR-Kotlin-SDK

Kotlin SDK library for interacting with a CSPR node.

What is CSPR-Kotlin-SDK?

SDK to streamline the 3rd party Kotlin client integration processes. Such 3rd parties include exchanges & app developers.

System requirement

  • The SDK is built with Kotlin language versioned 1.5

To build and test the SDK you need the flowing things installed in your computer:

Build and test

The package can be built and tested from IntelliJ IDEA or from command line with Maven.

Build and test from command line with Maven

You need to have Maven installed in your machine. Follow the instruction here to install Maven:

https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

Download the Casper-Kotlin-SDK code from Github and put it somewhere in your machine.

Open Terminal if you are using Mac OS or Command Prompt if you are using Windows OS. Enter the root folder of the Casper-Kotlin-SDK, then run this command to test and build the SDK:

mvn package

Somehow the result will be like this

Screen Shot 2022-07-21 at 10 43 11

Screen Shot 2022-07-21 at 10 44 54

Build and test in IntelliJ IDEA

Download the latest IntelliJ IDEA from https://www.jetbrains.com/idea/download

The SDK is developed with IntelliJ IDEA 2021.3.3 (Community Edition). Then a Community Edition is good enough to test the SDK.

Screen Shot 2022-06-29 at 12 39 34

Download or clone the code from github, then open it with IntelliJ IDEA.

Set up environment for ItelliJ IDEA

The first thing to do is to set up the SDK for the Project.

In IntelliJ IDEA hit "File-> Project Structure..."

Screen Shot 2022-04-20 at 06 12 33

You will see this screen shown up

Screen Shot 2022-04-20 at 06 13 39

Click on tab "SDKs" in the left panel to see the list of SDK being installed in your ItelliJ IDEA. You need to install the SDK from 11 or above, the suggested SDK is "openjdk-18".

Screen Shot 2022-04-20 at 06 17 16

By default, the SDKs list is just "Kotlin SDK". To install more SDK, hit the "+" button and choose "Download JDK ..."

Screen Shot 2022-04-20 at 06 26 28

Select your JDK version. You can choose any option in the list box, but the suggested version is 18, then hit the "Download" button

Screen Shot 2022-04-20 at 06 29 07

After the downloading is completed, you'll see the "openjdk-18" item (if you install JDK version 18) together with the Kotlin SDK.

Add more JDK version with the same step if you wish to test on more version of the JDK.

Next in the tab Project, choose the SDK for the project. In this case the choosen SDK is "openjdk-18"

Screen Shot 2022-04-20 at 06 31 20

Just do this for the SDK configuration, left the other configurations with default values. Make sure that the "Language level" is "SDK default" as in the image below.

Screen Shot 2022-04-20 at 09 17 17

Click "Apply" and then "OK" to save the configruation. The next step is to import the necessary Jar files to the SDK.

Import jars file to the SDK

Get the jars file from this link https://drive.google.com/drive/folders/1oflaJ_1m3HX9NYI_5d0PuoT2WNGy6dPq?usp=sharing

Download all the jars file to your local computer, you will have two folders with name "bcutils" and "otherjars" containing the jar files that will be later imported to the SDK.

In IntelliJ IDEA, hit "File-> Project Structure ..."

Screen Shot 2022-06-28 at 10 15 14

A new window appears, click on "Libraries" and then "Maven: net.jemzart:jsonkraken:1.2.0", then click the "+" button

Screen Shot 2022-06-28 at 10 16 45

Browse to the 2 jar folders that you have already downloaded from the link https://drive.google.com/drive/folders/1oflaJ_1m3HX9NYI_5d0PuoT2WNGy6dPq?usp=sharing above. First choose "bcutils" folder

Screen Shot 2022-06-28 at 10 22 31

Select all the files in the "bcutils" folder and click "Open" button

Screen Shot 2022-06-28 at 10 24 57

You will see the jar files imported in the list

Screen Shot 2022-06-28 at 10 25 34

Next step is to import all the files in the "otherjars", click the "+" button again and browse to the "otherjars" folder

Screen Shot 2022-06-28 at 10 25 52

Choose all the files in the "otherjars" folder, then click "Open"

Screen Shot 2022-06-28 at 10 25 57

You will see that all the jars from the two folder "bcutils" and "otherjars" is now imported to the list

Screen Shot 2022-06-28 at 10 26 04

Click "Apply" and then "OK" button.

You will need to restart IntelliJ IDEA to make the Jar files work. In IntelliJ IDEA click "File->Invalidate Caches ..."

Screen Shot 2022-06-28 at 10 38 00

A windows will appear, check all the check box then click "Invalidate and Restart"

Screen Shot 2022-06-28 at 10 39 28

The IntelliJ IDEA will close and restart. You may have to wait for 1 or several minutes to wait for IntelliJ IDEA to be ready for the changes. Then you can build and test the SDK.

Build the sdk:

To build the project, in IntelliJ IDEA, hit "Build->Build Project" as in the image below

Screen Shot 2022-04-18 at 20 31 31

You can see the Build log by clicking the "Build" tab in the bottom of the IntelliJ IDEA window

Screen Shot 2022-06-28 at 10 48 01

Test the sdk:

To run the test, in the Project panel that show the files and folders, under the folder "test", right click on the "kotlin" folder (folder marked with green in the below image), hit "Run 'All Tests'".

Screen Shot 2022-04-19 at 17 19 50

The test list is in the right panel, the result is in the left panel, as shown in the image:

Screen Shot 2022-04-19 at 17 18 28

Information for Secp256k1, Ed25519 Key Wrapper and Put Deploy

Key wrapper specification:

The Key wrapper do the following work:(for both Secp256k1 and Ed25519):

  • (PrivateKey,PublicKey) generation

  • Sign message

  • Verify message

  • Read PrivateKey/PublicKey from PEM file

  • Write PrivateKey/PublicKey to PEM file

The key wrapper is used in account_put_deploy RPC method to generate approvals signature based on deploy hash.

The Crypto task for Ed25519 and Secp256k1 use BouncyCastle library at this address https://bouncycastle.org/, with the Jar files from this address

https://bouncycastle.org/latest_releases.html

The Ed25519 crypto task is implemented in file Ed25519Handle under package com.casper.sdk.crypto

The Secp256k1 crypto task is implemented in file Secp256k1Handle under package com.casper.sdk.crypto

Kotlin version of CLType primitives, Casper Domain Specific Objects and Serialization

CLType primitives

A detail information on CLType primitive can be read here

Casper Kotlin SDK CLType primitive

Casper Domain Specific Objects

All of the main Casper Domain Specific Objects is built in Kotlin with classes like Deploy, DeployHeader, ExecutionDeployItem, NamedArg, Approval, JsonBlock, JsonBlockHeader, JsonEraEnd, JsonEraReport, JsonBlockBody, JsonProof, ValidatorWeight, Reward, ... and so on. All the class belonging to the RPC call is built to store coressponding information.

Documentation for classes and methods

SDK Usage

To use the SDK as external libary in other project, please refer to this document for more detail.

Casper Kotlin SDK usage