跳至主要内容
本页面使用机器翻译自英语,可能包含错误或不清楚的语言。如需最准确的信息,请参阅英文原文。由于更新频繁,部分内容可能与英文原文有出入。请加入我们在 Crowdin 上的努力,帮助我们改进本页面的翻译。 (Crowdin translation page, Contributing guide)

V4 keystore

本例演示如何加密和解密 keystore V4

从 Web3j 和 kaia 库(web3j-ext)中导入必要的类。

定义密钥存储密码,该密码用于解密密钥存储

从 json 文件读取密钥存储,例如,我们有一个不同账户的列表:"RoleBased_V4.json ", "Multi_V4.json ""Public_V4.json "

使用密码解密密钥库。

解密后,您可以访问公钥私钥

keystoreV4.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 java.util.List;
import org.web3j.tx.response.PollingTransactionReceiptProcessor;
import org.web3j.tx.response.TransactionReceiptProcessor;
import org.web3j.example.keySample;
public class DecryptKeystoreV4Example implements keySample {
public static void run() throws Exception {
String password = "Ilovekaia";
String[] keyFiles = { "/RoleBased_V4.json", "/Multi_V4.json", "/Public_V4.json" };
for (String keyFile : keyFiles) {
String json = getResourceJSON(keyFile);
// Convert keystore to list of KaiaCredentials
List<List<KaiaCredentials>> credentialsLists = KaiaWelletUtils.loadJsonKaiaCredentialsFromV4(password,
json);
System.out.println("Load KaiaCredentials from keystore file: " + keyFile);
// Print KaiaCredentials
for (int i = 0; i < credentialsLists.size(); i++) {
List<KaiaCredentials> credentialsList = credentialsLists.get(i);
System.out.println("Array " + (i + 1) + ":");
for (KaiaCredentials credentials : credentialsList) {
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 = DecryptKeystoreV4Example.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 keystoreV4.java
Load KaiaCredentials from keystore file: /RoleBased_V4.json
Array 1:
KaiaCredential : Address: 0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea, Private Key: 0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac
Array 2:
KaiaCredential : Address: 0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea, Private Key: 0x9ba8cb8f60044058a9e6f815c5c42d3a216f47044c61a1750b6d29ddc7f34bda
Array 3:
KaiaCredential : Address: 0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea, Private Key: 0xe4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8
Load KaiaCredentials from keystore file: /Multi_V4.json
Array 1:
KaiaCredential : Address: 0x82c6a8d94993d49cfd0c1d30f0f8caa65782cc7e, Private Key: 0xa32c30608667d43be2d652bede413f12a649dd1be93440878e7f712d51a6768a
KaiaCredential : Address: 0x82c6a8d94993d49cfd0c1d30f0f8caa65782cc7e, Private Key: 0xe4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8
KaiaCredential : Address: 0x82c6a8d94993d49cfd0c1d30f0f8caa65782cc7e, Private Key: 0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac
Load KaiaCredentials from keystore file: /Public_V4.json
Array 1:
KaiaCredential : Address: 0xa2a8854b1802d8cd5de631e690817c253d6a9153, Private Key: 0xe4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8

让这个页面变得更好