All Products
Search
Document Center

Environment Interface

Last Updated: May 24, 2019

Initialize environment

init

Initialize the environment configuration.

  • Function
  1. public MychainBaseResult<Response> init(MychainEnv env)
  • Request parameters
Parameter Required Type Description
env true MychainEnv Contract platform environment
  • Examples
  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. }

Multi-node configuration

The SDK supports multi-node configuration. When the primary node is unavailable, the SDK automatically switches to another available node for re-connection to ensure service stability.

  • Examples
  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. }