Skip to content

Instantly share code, notes, and snippets.

@aeharvlee
Created Feb 7, 2022
Embed
What would you like to do?
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.klaytn.caver.Caver;
import com.klaytn.caver.methods.response.AccountKey;
import io.github.cdimascio.dotenv.Dotenv;
import okhttp3.Credentials;
import org.web3j.protocol.http.HttpService;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Hello world for caver-java Klaytn SDK! Please read the README before starting :)
* Related article - Korean: https://ko.docs.klaytn.com/bapp/sdk/caver-java/getting-started
* Related reference - English: https://docs.klaytn.com/bapp/sdk/caver-java/getting-started
*/
public class CaverExample {
// You can directly input values for the variables below, or you can enter values in the caver-java-examples/.env file.
private static String nodeApiUrl = "https://node-api.klaytnapi.com/v1/klaytn"; // e.g. "https://node-api.klaytnapi.com/v1/klaytn";
private static String accessKeyId = ""; // e.g. "KASK1LVNO498YT6KJQFUPY8S";
private static String secretAccessKey = ""; // e.g. "aP/reVYHXqjw3EtQrMuJP4A3/hOb69TjnBT3ePKG";
private static String chainId = "1001"; // e.g. "1001" or "8217";
public static void main(String[] args) {
try {
loadEnv();
run();
} catch (Exception e) {
e.printStackTrace();
}
}
public static String objectToString(Object value) throws JsonProcessingException {
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
return ow.writeValueAsString(value);
}
public static void loadEnv() {
Dotenv env;
String workingDirectory = System.getProperty("user.dir");
Path workingDirectoryPath = Paths.get(workingDirectory);
String projectRootDirectory = "caver-java-examples";
String currentDirectoryName = workingDirectoryPath.getName(workingDirectoryPath.getNameCount() - 1).toString();
String envDirectory = currentDirectoryName.equals(projectRootDirectory) ?
workingDirectoryPath.toString() :
workingDirectoryPath.getParent().toString();
// Read `/path/to/caver-java-examples/.env` file.
env = Dotenv.configure().directory(envDirectory).load();
nodeApiUrl = nodeApiUrl.equals("") ? env.get("NODE_API_URL") : nodeApiUrl;
accessKeyId = accessKeyId.equals("") ? env.get("ACCESS_KEY_ID") : accessKeyId;
secretAccessKey = secretAccessKey.equals("") ? env.get("SECRET_ACCESS_KEY") : secretAccessKey;
chainId = chainId.equals("") ? env.get("CHAIN_ID") : chainId;
}
public static void run() throws Exception {
// In the caver, almost everything starts with the caver. Let's create a caver instance
HttpService httpService = new HttpService(nodeApiUrl);
httpService.addHeader("Authorization", Credentials.basic(accessKeyId, secretAccessKey));
httpService.addHeader("x-chain-id", chainId);
Caver caver = new Caver(httpService);
AccountKey result = caver.rpc.klay.getAccountKey("0xe82ed0bb7c9318c6ae0b8b3988ad5182cd64a28d").send();
System.out.println(objectToString(result));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment