环境接口

环境初始化

init 接口用于初始化环境配置。

  • 函数原型
  1. public MychainBaseResult<Response> init(MychainEnv env)
  • 请求参数
参数 必选 类型 说明
env true MychainEnv 合约平台依赖的环境相关信息
  • 示例
  1. MychainEnv env = buildMychainEnv();
  2. Mychain sdk = new Mychain();
  3. sdk.init(env);
  4. private static MychainEnv initMychainEnv() {
  5. return initMychainEnv("test_sdk");
  6. }
  7. private static MychainEnv buildMychainEnv(String identity) {
  8. InetSocketAddress inetSocketAddress = InetSocketAddress.createUnresolved(host, port);
  9. String keyFilePath = "client.key";
  10. String certFilePath = "client.crt";
  11. String trustStoreFilePath = "trustCa";
  12. // any user key for sign message
  13. String userPrivateKeyFile = "user.key";
  14. PrivateKey userPrivateKey = KeyLoder.getPrivateKeyFromPKCS8(DemoSample.class.getClassLoader().getResourceAsStream(userPrivateKeyFile),keyPassword);
  15. // build ssl option
  16. ISslOption sslOption = new SslBytesOption.Builder()
  17. .keyBytes(Utils.readFileToByteArray(DemoSample.class.getClassLoader().getResource(keyFilePath).getPath()))
  18. .certBytes(Utils.readFileToByteArray(DemoSample.class.getClassLoader().getResource(certFilePath).getPath()))
  19. .keyPassword(keyPassword)
  20. .setUserPrivateKey(userPrivateKey)
  21. .trustStorePassword(trustStorePassword)
  22. .trustStoreBytes(
  23. Utils.readFileToByteArray(DemoSample.class.getClassLoader().getResource(trustStoreFilePath).getPath()))
  24. .build();
  25. // multi nodes configuration
  26. List<SocketAddress> backupNodes = new ArrayList<SocketAddress>();
  27. backupNodes.add(InetSocketAddress.createUnresolved("101.132.187.0", 58321));
  28. backupNodes.add(InetSocketAddress.createUnresolved("101.132.46.192", 58322));
  29. backupNodes.add(InetSocketAddress.createUnresolved("106.14.166.129", 58323));
  30. return MychainEnv.build(identity, ClientTypeEnum.TLS, HashTypeEnum.SHA256,
  31. SignTypeEnum.ECDSA, CodecTypeEnum.RLP, inetSocketAddress, sslOption, backupNodes);
  32. }

多节点配置

SDK 支持多节点配置,当主节点不可用时,会自动切换到配置的其他节点进行重连,从而保证服务的稳定性。

  • 示例
  1. MychainEnv env = buildMychainEnv();
  2. Mychain sdk = new Mychain();
  3. sdk.init(env);
  4. private static MychainEnv initMychainEnv() {
  5. return initMychainEnv("test_sdk");
  6. }
  7. private static MychainEnv buildMychainEnv(String identity) {
  8. InetSocketAddress inetSocketAddress = InetSocketAddress.createUnresolved(host, port);
  9. String keyFilePath = "client.key";
  10. String certFilePath = "client.crt";
  11. String trustStoreFilePath = "trustCa";
  12. // build ssl option
  13. ISslOption sslOption = new SslBytesOption.Builder()
  14. .keyBytes(Utils.readFileToByteArray(DemoSample.class.getClassLoader().getResource(keyFilePath).getPath()))
  15. .certBytes(Utils.readFileToByteArray(DemoSample.class.getClassLoader().getResource(certFilePath).getPath()))
  16. .keyPassword(keyPassword)
  17. .trustStorePassword(truseStorePassword)
  18. .trustStoreBytes(
  19. Utils.readFileToByteArray(DemoSample.class.getClassLoader().getResource(trustStoreFilePath).getPath()))
  20. .build();
  21. // multi nodes configuration
  22. List<SocketAddress> backupNodes = new ArrayList<SocketAddress>();
  23. backupNodes.add(InetSocketAddress.createUnresolved("101.132.187.0", 58321));
  24. backupNodes.add(InetSocketAddress.createUnresolved("101.132.46.192", 58322));
  25. backupNodes.add(InetSocketAddress.createUnresolved("106.14.166.129", 58323));
  26. return MychainEnv.build(identity, ClientTypeEnum.TLS, HashTypeEnum.SHA256,
  27. SignTypeEnum.ECDSA, CodecTypeEnum.RLP, inetSocketAddress, sslOption, backupNodes);
  28. }