All Products
Document Center

Java SDK quick start

Last Updated: Dec 18, 2020

This topic describes how to run an application, shows an example execution process, and provides guidance on how to specify a cryptographic kit.

Run an application

Running an application involves the following four steps:

  1. Prepare the environment
  2. Write an application
  3. Compile the application
  4. Execute the application

Prepare the environment

  • Prepare the SSL connection file and account private key file.
    To establish an SSL connection with the BaaS platform, prepare three certificate files: CA root certificate (trustCa), client certificate file (client.crt), and client private key file (client.key). In addition, the account private key file (user.key) is required for submitting transactions. The following table describes these four files.

    File name Description Source
    client.crt The client certificate file. Use the BaaS platform’s key generator to generate a certificate request file client.csr, submit client.csr to the BaaS platform to apply for a certificate, and download the .crt file upon application approval.
    client.key The client private key file. Generate this file with the BaaS platform’s key generator.
    trustCa The trustStore for storing CA certificates. Download this file from the BaaS platform. The password for the trustCa file is mychain.
    user.key The account private key file. Use the BaaS platform’s key generator to manually or automatically generate this file.
  • Prepare the TEE hardware privacy contract chain file.
    To use the TEE hardware privacy contract chain, prepare the tee_rsa_public_key.pem file, which can be applied for from the existing TEE hardware privacy contract chain. This file is not required if you use a standard contract chain.

    File name Description Source
    tee_rsa_public_key.pem The TEE hardware privacy contract chain file, which is a node’s open RSA public key file. Download this file from the BaaS platform.
  • Check the contract chain node information.
    To obtain the IP address and port number of the chain node for interaction with the contract chain, check the node details of the target contract chain in the block browser on the BaaS platform.

Write an application

  1. Use IntelliJ IDEA to create a demo Maven project. The following figure shows the project’s directory structure.

  2. In the java directory, create a custom package named, for example, com.example.demo, copy all content in (click to download to the created package, and place the client.crt, client.key, trustCa, and user.key files required for the SDK in the resources directory, as shown in the following figure.

    Note: The bytecode into which the contract’s source code is compiled must be used in the demo project. For more information about the contract’s source code, see Solidity contract development. For more information about the Solidity compiler, see Solidity contract compiler. For more information about WASM examples, see the mychainx-example, which supports versions and later.
  3. Add dependencies to pom.xml. Introduce the SDK and slf4j-log4j12 for demonstration into the pom.xml file, and add the configuration file of log4j to the resources directory.

    Note: Use the latest version of SDK dependencies.
    For more information about the complete pom dependencies carrying slf4j-log4j12, see the following example.

    1. <dependencies>
    2. <dependency>
    3. <groupId></groupId>
    4. <artifactId>mychainx-sdk</artifactId>
    5. <!-- Use the latest SDK version. -->
    6. <version></version>
    7. </dependency>
    8. <dependency>
    9. <groupId>org.slf4j</groupId>
    10. <artifactId>slf4j-log4j12</artifactId>
    11. <version>1.8.0-alpha0</version>
    12. </dependency>
    13. </dependencies>
    14. <build>
    15. <extensions>
    16. <extension>
    17. <groupId>kr.motd.maven</groupId>
    18. <artifactId>os-maven-plugin</artifactId>
    19. <version>1.6.1</version>
    20. </extension>
    21. </extensions>
    22. </build>

    In some cases, the following error may occur:

    This error may result from a Netty loading failure. Try to modify the pom dependencies as follows:

    1. <dependencies>
    2. <dependency>
    3. <groupId></groupId>
    4. <artifactId>mychainx-sdk</artifactId>
    5. <!-- Use the latest SDK version. -->
    6. <version></version>
    7. <exclusions>
    8. <exclusion>
    9. <groupId>io.netty</groupId>
    10. <artifactId>netty-tcnative-openssl-static</artifactId>
    11. </exclusion>
    12. </exclusions>
    13. </dependency>
    14. <dependency>
    15. <groupId>org.slf4j</groupId>
    16. <artifactId>slf4j-log4j12</artifactId>
    17. <version>1.8.0-alpha0</version>
    18. </dependency>
    19. </dependencies>
    20. <build>
    21. <extensions>
    22. <extension>
    23. <groupId>kr.motd.maven</groupId>
    24. <artifactId>os-maven-plugin</artifactId>
    25. <version>1.6.1</version>
    26. </extension>
    27. </extensions>
    28. </build>

    The content of the file is as follows:

    1. log4j.rootLogger=INFO, R
    2. # The logs are output at the console.
    3. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    4. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    5. log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n
    6. # The logs are output in files.
    7. log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
    8. log4j.appender.R.File=./sdk.log
    9. log4j.appender.R.layout=org.apache.log4j.PatternLayout
    10. log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n

Compile the application

Run mvn clean compile in the project’s root directory to compile the project.

Execute the application

In, run the project. The generated log file sdk.log is in the project’s root directory. If the log file contains “Hand shake success”, a connection has been established successfully with the blockchain platform.

The expected output is as follows:

  1. create testAccount1 success.
  2. create testAccount2 success.
  3. deploy contract success.
  4. issue success.
  5. transfer success.
  6. check account balance success.

Example execution process

  1. Initialize the environment.

    1. // step 1: init logger.
    2. initLogger();
    3. // step 2: init mychain env.
    4. env = initMychainEnv();
  2. Create an account transaction.

    1. // step 4: init account that will be created.
    2. initAccount();
    3. // step 5: init private key list which will be used during transaction.
    4. initPrivateKeyList();
    5. // step 6: execute create two accounts.
    6. createAccount();
  3. Deploy a smart contract.

    1. // step 7: deploy a contract using testAccount1.
    2. deployContract();
  4. Issue credits.

    1. // step 8: issue 100 credits to testAccount2.
    2. issue();
  5. Transfer credits between accounts.

    1. // step 9: transfer 50 credits from testAccount2 to testAccount1
    2. transfer();
  6. Query for the account balance.

    1. // step 10: query testAccount2 whose balance should be 50.
    2. BigInteger balance = query(test2PrivateKeyArrayList,testAccount2);
    3. // step 11: compare to expect balance.
    4. expect(balance,BigInteger.valueOf(50));
  7. Close the SDK connection.

    1. // step 12: sdk shut down
    2. sdk.shutDown();

Specify a cryptographic kit

The following two cryptographic kits can be used in the contract chain environment:

  • classic: uses international commercial cryptographic algorithms, including the SHA-256 digest, ECC public key algorithm, and AES symmetric encryption. This kit is configured for the standard contract chain by default.
  • china-sm: uses China’s national commercial cryptographic algorithms, including the SM3 digest, SM2 public key algorithm, and SM4 symmetric encryption. This kit is configured for the contract chain depending on China’s national encryption algorithms by default.

For more information about the cryptographic kit used for the target contract chain connected to the SDK, you may consult the chain administrator. When building ClientEnv, explicitly specify the SignerBase. The following is an example.

  1. Pkcs8KeyOperator pkcs8KeyOperator = new Pkcs8KeyOperator();
  2. Keypair keyPair = pkcs8KeyOperator.load(privateKeyPath, keyPassword);
  3. SignerBase signerBase = MyCrypto.getInstance().createSigner(keyPair);
Note:The cryptographic kit does not affect the communications between the SDK and the contract platform, which are subject to the PKI authority that issues the certificate.