본문으로 건너뛰기
이 페이지는 영어로 된 기계 번역을 사용하므로 오류나 불명확한 언어가 포함될 수 있습니다. 가장 정확한 정보는 영어 원문을 참조하시기 바랍니다. 잦은 업데이트로 인해 일부 콘텐츠는 원래 영어로 되어 있을 수 있습니다. Crowdin에서 이 페이지의 번역을 개선하는 데 동참하여 도움을 주세요. (Crowdin translation page, Contributing guide)

V3 keystore

This example demonstrates how to encrypt and decrypt keystore V3.

Import necessary classes from the Web3j and kaia libraries(web3j-ext).

Define the keystore password, this password is used for decrypting the keystores

Read keystores from json files, for example we have a list of different accounts: "RoleBased_V4.json", "Multi_V4.json" and "Public_V4.json"

Decrypt the keystore with its password.

After decryped, you can access the public and private key.

keystoreV3.java

package org.web3j.example.utils;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;
import org.web3j.crypto.KaiaWelletUtils;
import org.web3j.crypto.KaiaCredentials;
import java.io.IOException;
import org.web3j.tx.response.PollingTransactionReceiptProcessor;
import org.web3j.tx.response.TransactionReceiptProcessor;
import org.web3j.example.keySample;
public class DecryptKeystoreV3Example implements keySample {
public static void run() throws Exception {
String password = "Ilovekaia";
String[] keyFiles = { "/Legacy_V3.json", "/Public_V3.json" };
for (String keyFile : keyFiles) {
String json = getResourceJSON(keyFile);
// Convert keystore to list of KaiaCredentials
KaiaCredentials credentials = KaiaWelletUtils.loadJsonKaiaCredentials(password, json);
System.out.println("Load KaiaCredentials from keystore file: " + keyFile);
String address = credentials.getAddress();
String privateKey = credentials.getEcKeyPair().getPrivateKey().toString(16);
System.out.println("\tKaiaCredential : " + "Address: " + address + ", Private Key: 0x" + privateKey);
}
}
static String getResourceJSON(String resourcePath) throws IOException {
InputStream inputStream = DecryptKeystoreV3Example.class.getResourceAsStream(resourcePath);
if (inputStream == null) {
throw new IllegalArgumentException("resource not found: " + resourcePath);
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
// String value for keystore JSON
return reader.lines().collect(Collectors.joining(System.lineSeparator()));
} catch (IOException e) {
throw e;
}
}
}

output

❯ java keystoreV3.java
Load KaiaCredentials from keystore file: /Legacy_V3.json
KaiaCredential : Address: 0xa2a8854b1802d8cd5de631e690817c253d6a9153, Private Key: 0xe4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8
Load KaiaCredentials from keystore file: /Public_V3.json
KaiaCredential : Address: 0xa2a8854b1802d8cd5de631e690817c253d6a9153, Private Key: 0xe4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8

페이지를 개선해 주세요