环境初始化
init
接口用于初始化环境配置。
- 函数原型
public MychainBaseResult<Response> init(MychainEnv env)
- 请求参数
参数 | 必选 | 类型 | 说明 |
---|---|---|---|
env | true | MychainEnv | 合约平台依赖的环境相关信息 |
- 示例
MychainEnv env = buildMychainEnv();
Mychain sdk = new Mychain();
sdk.init(env);
private static MychainEnv initMychainEnv() {
return initMychainEnv("test_sdk");
}
private static MychainEnv buildMychainEnv(String identity) {
InetSocketAddress inetSocketAddress = InetSocketAddress.createUnresolved(host, port);
String keyFilePath = "client.key";
String certFilePath = "client.crt";
String trustStoreFilePath = "trustCa";
// any user key for sign message
String userPrivateKeyFile = "user.key";
PrivateKey userPrivateKey = KeyLoder.getPrivateKeyFromPKCS8(DemoSample.class.getClassLoader().getResourceAsStream(userPrivateKeyFile),keyPassword);
// build ssl option
ISslOption sslOption = new SslBytesOption.Builder()
.keyBytes(Utils.readFileToByteArray(DemoSample.class.getClassLoader().getResource(keyFilePath).getPath()))
.certBytes(Utils.readFileToByteArray(DemoSample.class.getClassLoader().getResource(certFilePath).getPath()))
.keyPassword(keyPassword)
.setUserPrivateKey(userPrivateKey)
.trustStorePassword(trustStorePassword)
.trustStoreBytes(
Utils.readFileToByteArray(DemoSample.class.getClassLoader().getResource(trustStoreFilePath).getPath()))
.build();
// multi nodes configuration
List<SocketAddress> backupNodes = new ArrayList<SocketAddress>();
backupNodes.add(InetSocketAddress.createUnresolved("101.132.187.0", 58321));
backupNodes.add(InetSocketAddress.createUnresolved("101.132.46.192", 58322));
backupNodes.add(InetSocketAddress.createUnresolved("106.14.166.129", 58323));
return MychainEnv.build(identity, ClientTypeEnum.TLS, HashTypeEnum.SHA256,
SignTypeEnum.ECDSA, CodecTypeEnum.RLP, inetSocketAddress, sslOption, backupNodes);
}
多节点配置
SDK 支持多节点配置,当主节点不可用时,会自动切换到配置的其他节点进行重连,从而保证服务的稳定性。
- 示例
MychainEnv env = buildMychainEnv();
Mychain sdk = new Mychain();
sdk.init(env);
private static MychainEnv initMychainEnv() {
return initMychainEnv("test_sdk");
}
private static MychainEnv buildMychainEnv(String identity) {
InetSocketAddress inetSocketAddress = InetSocketAddress.createUnresolved(host, port);
String keyFilePath = "client.key";
String certFilePath = "client.crt";
String trustStoreFilePath = "trustCa";
// build ssl option
ISslOption sslOption = new SslBytesOption.Builder()
.keyBytes(Utils.readFileToByteArray(DemoSample.class.getClassLoader().getResource(keyFilePath).getPath()))
.certBytes(Utils.readFileToByteArray(DemoSample.class.getClassLoader().getResource(certFilePath).getPath()))
.keyPassword(keyPassword)
.trustStorePassword(truseStorePassword)
.trustStoreBytes(
Utils.readFileToByteArray(DemoSample.class.getClassLoader().getResource(trustStoreFilePath).getPath()))
.build();
// multi nodes configuration
List<SocketAddress> backupNodes = new ArrayList<SocketAddress>();
backupNodes.add(InetSocketAddress.createUnresolved("101.132.187.0", 58321));
backupNodes.add(InetSocketAddress.createUnresolved("101.132.46.192", 58322));
backupNodes.add(InetSocketAddress.createUnresolved("106.14.166.129", 58323));
return MychainEnv.build(identity, ClientTypeEnum.TLS, HashTypeEnum.SHA256,
SignTypeEnum.ECDSA, CodecTypeEnum.RLP, inetSocketAddress, sslOption, backupNodes);
}
文档内容是否对您有帮助?