This guide walks you through building and running a Java application against the BaaS platform — from preparing certificates to deploying a smart contract and querying account balances.
Prerequisites
Before you begin, ensure that you have:
Access to the BaaS platform with a target contract chain configured
IntelliJ IDEA and Apache Maven installed
The chain node's IP address and port number (find these in the node details of the target contract chain in the block browser on the BaaS platform)
Run an application
Running an application involves four steps:
Prepare the environment
Write the application
Compile the application
Execute the application
Prepare the environment
Gather SSL connection files and account credentials
To establish an SSL connection with the BaaS platform, prepare the following four files:
| File | Description | How to get it |
|---|---|---|
client.crt | Client certificate | 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 approval. |
client.key | Client private key | Generate with the BaaS platform's key generator. |
trustCa | trustStore for CA certificates | Download from the BaaS platform. The password for this file is mychain. |
user.key | Account private key (required for submitting transactions) | Use the BaaS platform's key generator to manually or automatically generate this file. |
Gather the TEE hardware privacy contract chain file (if applicable)
If your contract chain uses a Trusted Execution Environment (TEE) hardware privacy configuration, also prepare the following file. Skip this step for standard contract chains.
| File | Description | How to get it |
|---|---|---|
tee_rsa_public_key.pem | Node's open RSA public key file | Download from the BaaS platform. |
Write the application
Set up the project structure
Create a Maven project in IntelliJ IDEA.
Inside the project, create a custom package in the
javadirectory — for example,com.example.demo. Download DemoSample.java and copy all its content into the package.Place the four credential files in the
resourcesdirectory:your-project/ ├── src/ │ └── main/ │ ├── java/ │ │ └── com/example/demo/ │ │ └── DemoSample.java # Application entry point │ └── resources/ │ ├── client.crt # Client certificate │ ├── client.key # Client private key │ ├── trustCa # CA trustStore (password: mychain) │ └── user.key # Account private key for transactions └── pom.xml
The demo project requires the compiled bytecode of your smart contract. For information about writing Solidity contracts, see Solidity contract development. For the Solidity compiler, see Solidity contract compiler.
Add Maven dependencies
Add the following to pom.xml. Always use the latest version of the SDK dependencies.
<dependencies>
<dependency>
<groupId>com.alipay.mychainx</groupId>
<artifactId>mychainx-sdk</artifactId>
<!-- Use the latest SDK version. -->
<version>0.10.2.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.8.0-alpha0</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.1</version>
</extension>
</extensions>
</build>If you see a Netty loading error, exclude the netty-tcnative-openssl-static artifact:
<dependencies>
<dependency>
<groupId>com.alipay.mychainx</groupId>
<artifactId>mychainx-sdk</artifactId>
<!-- Use the latest SDK version. -->
<version>0.10.2.12</version>
<exclusions>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-tcnative-openssl-static</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.8.0-alpha0</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.1</version>
</extension>
</extensions>
</build>Configure logging
Add a log4j.properties file to the resources directory:
log4j.rootLogger=INFO, R
# Log outputs are in the console.
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n
# Log outputs are in a file.
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=./sdk.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n
# mychain-sdk log switch configuration
log4j.logger.MychainClient=OFFIf you use logback instead of log4j, add the following to your logback configuration:
<logger name="MychainClient" level="OFF"/>Compile the application
Run the following command in the project's root directory:
mvn clean compileExecute the application
Run DemoSample.java. A log file named sdk.log is created in the project's root directory.
To verify the connection was established successfully, check sdk.log for the message Hand shake success.
The expected output after a successful run:
create testAccount1 success.
create testAccount2 success.
deploy contract success.
issue success.
transfer success.
check account balance success.Example execution process
All steps in DemoSample.java follow a linear flow. Each section below explains what the step does and shows its expected output.
Step 1: Initialize the environment
Set up the logger, initialize the Mychain environment, and start the SDK client.
// step 1: init logger.
initLogger();
// step 2: init mychain env.
env = initMychainEnv();
// step 3: start sdk.
MychainClient sdk = new MychainClient();
sdk.init(env);Step 2: Create accounts
Initialize two test accounts and the private key list used to authorize transactions, then submit the account creation transactions.
// step 4: init account that will be created.
initAccount();
// step 5: init private key list which will be used during transaction.
initPrivateKeyList();
// step 6: execute create two accounts.
createAccount();Expected output:
create testAccount1 success.
create testAccount2 success.Step 3: Deploy a smart contract
Deploy the contract from testAccount1.
// step 7: deploy a contract using testAccount1.
deployContract();Expected output:
deploy contract success.Step 4: Issue credits
Issue 100 credits to testAccount2.
// step 8: issue 100 credits to testAccount2.
issue();Expected output:
issue success.Step 5: Transfer credits
Transfer 50 credits from testAccount2 to testAccount1.
// step 9: transfer 50 credits from testAccount2 to testAccount1
transfer();Expected output:
transfer success.Step 6: Query the account balance
Query testAccount2 and verify the balance equals 50.
// step 10: query testAccount2 whose balance should be 50.
BigInteger balance = query(test2PrivateKeyArrayList, testAccount2);
// step 11: compare to expect balance.
expect(balance, BigInteger.valueOf(50));Expected output:
check account balance success.Step 7: Shut down the SDK
Release the SDK connection cleanly.
// step 12: sdk shut down
sdk.shutDown();Specify a cryptographic kit
Two cryptographic kits are available in the contract chain environment:
| Kit | Algorithms | Default for |
|---|---|---|
classic | SHA-256 digest, ECC public key algorithm, AES symmetric encryption | Standard contract chains |
china-sm | SM3 digest, SM2 public key algorithm, SM4 symmetric encryption | Contract chains using China's national cryptographic standards |
To find out which kit your target contract chain uses, consult the chain administrator.
When building ClientEnv, explicitly specify the SignerBase:
Pkcs8KeyOperator pkcs8KeyOperator = new Pkcs8KeyOperator();
Keypair keyPair = pkcs8KeyOperator.load(privateKeyPath, keyPassword);
SignerBase signerBase = MyCrypto.getInstance().createSigner(keyPair);The cryptographic kit does not affect communications between the SDK and the contract platform. Communications are governed by the PKI authority that issues the certificate.