All Products
Search
Document Center

Development environment on MAC OS

Last Updated: Jan 17, 2019

This document describes how to build a development environment quickly and start a simple Demo project under the Mac OS environment.

Install and configure Java

You can refer to Oracle official documents to learn how to install JDK. Download the img installation package corresponding to the mac and install it.

Note: Java 8 environment is recommended by default. You can also use Java 7 or later with the same configuration as Java 8.

To install Apache Maven, you need to download Apache Maven 3.2.5 or later and then install it locally. Refer to Apache Maven official documents to learn how to install Maven.After installing Maven, configure the following environment variables:

  1. M2_HOME: Configure it as the installation path for Maven.
  2. PATH: Add the %M2_HOME%/bin path.
  3. export M2_HOME=$(/usr/local/Cellar/maven/3.2.2/libexec)
  4. export M2=$M2_HOME/bin
  5. export PATH=$M2:$PATH

Install IDEA

Download IDEA on the Official Website. After downloading, install IDEA according to the prompts.

Generate a certificate request and private key

The following items are required:

  • A certificate request file (CSR. pem)
  • A private key file in pkcs8 format (key_pkcs8.pem)
  • A private key password (ssl_key_password)
  • A certificate file (cert .pem)
  • A certificate TrustStore file (trust.keystore)

The following explains how to generate these files and this content.

  1. Execute the openssl req -newkey rsa:2048 -keyout key_pkcs10.pem -out csr.pem command.

    While this command is being executed, you are required to enter the following information:

    • PEM pass phrase: The password, which is used as the value of the -passin parameter when the private key format is converted to the Private Key Information Standard Format (PKCS#8) in the next step.
    • Country name (2-letter code): Use the International Standards Organization (ISO) format and enter the two-letter country code. For example, enter CN for China.
    • State or province name (full name): The province. For example, Zhejiang.
    • Locality name (for example, city): The city. For example, Hangzhou.
    • Organization name (for example, company): The organizational unit. For example, a company name.
    • Organizational unit name (for example, section): For example, IT Dept.
    • Common name (for example, domain name of your website): The domain name of the website encrypted with SSL.Note:

    This is not just your domain name, but a website name that uses SSL directly, such as pay.abc.com.

    Different URLs are defined here as different website domain names. For example: abc.com, www.abc.com, and pay.abc.com are identified as three different website domain names.

    The website domain name here should be the same as the SMTP/POP3 server name set by the mail client software.

    • Email Address: email address (optional)
    • Challenge password: (optional)
    • Optional company name: (optional)
  2. Convert the private key file in the preceding step from the certificate request format (PKCS#10) to the Private Key Information Standard Format (PKCS#8).

    1. # For the -passin parameter, enter the password when you create the certificate application in the preceding step. For the -passout parameter, set a new SSL private key password, which you will use when configuring the client
    2. openssl pkcs8 -topk8 -in key_pkcs10.pem -passin pass:****** -out key_pkcs8.pem -passout pass:******

    Note: Be sure to save your private key file key_pkcs8.pem, as you need it to initialize the client when you start a Demo.

  3. Upload the certificate application csr.pem.

    upload_cert_req

  4. After the certificate application has been uploaded and submitted, it is reviewed in the background. Typically, the following two statuses are displayed

    • Not Applied: The consortium chain has not yet been applied for.
    • Approved: Congratulations, you have successfully joined the consortium chain. You can download the signed user certificate and develop the SDK.

    apply_succeed

Start a demo

This section explains how to configure the project quickly to complete the first Demo development.

  1. Log on to the BaaS platform, select Blockchain List in the left-side navigation pane, find the blockchain you want to access in the list, then select More on the right, and click Download Signed Certificate and Download SDK.apply_succeed

  2. Unzip the client_sdk.zip package obtained.

  3. Configure the sdk.properties file. The default configuration method is shown here. For more configuration methods, see Initialize the client.

    1. # The address of the primary node to which the client is connected. One and only one primary node can be configured
    2. biz.sdk.primary=### IP configuration does not need to be modified ###
    3. # The API address of the backup node. You can configure n backup nodes, where n=3f : f=1,2...
    4. # When the primary node cannot be connected, the system switches to connect to the backup node. When the primary node recovers, it automatically switches back to connect to the primary node.
    5. biz.sdk.backups =### The IP configuration does not need to be modified ###
    6. # The client connects to the node using TLS mutual authentication, which requires the x509 certificate to be configured.
    7. # The absolute path of the SSL private key files in the pkcs8 format.
    8. biz.sdk.ssl_key=/path/key_pkcs8.pem
    9. # The absolute path of the SSL certificate files in the x509 format.
    10. biz.sdk.ssl_cert=/path/cert.pem
    11. # The SSL private key password set when the private key is created.
    12. biz.sdk.ssl_key_password=******
    13. # The absolute path of the TrustStore file
    14. biz.sdk.trust_store=/path/trust.keystore
    15. # The trust store password, which defaults to mychain and does not need to be modified.
    16. biz.sdk.trust_store_password=mychain

    Note: Replace /path/ with the real absolute path to your file.

  4. Create a new Maven project in IDEA and add the corresponding dependency package to the pom.xml file.

    1. <dependencies>
    2. <dependency>
    3. <groupId>com.alipay.antblockchain</groupId>
    4. <artifactId>antblockchain-gl-biz-sdk</artifactId>
    5. <version>0.6.0.3</version>
    6. <scope>system</scope>
    7. <systemPath>{libdir}/client-sdk.jar</systemPath>
    8. </dependency>
    9. <dependency>
    10. <groupId>com.alipay.antblockchain.sdk.plus</groupId>
    11. <artifactId>antblockchain-sdk-plus</artifactId>
    12. <classifier>jar-with-dependencies</classifier>
    13. <version>0.6.0.3</version>
    14. <scope>system</scope>
    15. <systemPath>{libdir}/sdk-plus.jar</systemPath>
    16. </dependency>
    17. <dependency>
    18. <groupId>javax.validation</groupId>
    19. <artifactId>validation-api</artifactId>
    20. <version>1.1.0. Final</version>
    21. </dependency>
    22. <dependency>
    23. <groupId>org.hibernate</groupId>
    24. <artifactId>hibernate-validator</artifactId>
    25. <version>5.3.5. Final</version>
    26. </dependency>
    27. <dependency>
    28. <groupId>javax.el</groupId>
    29. <artifactId>javax.el-api</artifactId>
    30. <version>2.2.4</version>
    31. </dependency>
    32. <dependency>
    33. <groupId>org.glassfish.web</groupId>
    34. <artifactId>javax.el</artifactId>
    35. <version>2.2.4</version>
    36. </dependency>
    37. <dependency>
    38. <groupId>com.google.code.gson</groupId>
    39. <artifactId>gson</artifactId>
    40. <version>2.8.5</version>
    41. </dependency>
    42. </dependencies>

    Note: Unzip the client-sdk.zip file downloaded from the BaaS platform, and add the client-sdk.jar and sdk-plus.jar dependencies included to the dependency directory. Note that libdir needs to be modified to the real path.

  5. After the configuration is completed, you can create an SDK Client instance in the Main function of the project to obtain the transaction information of the latest block on the chain.

    1. import java.io.FileInputStream;
    2. import java.util.List;
    3. import java.util.Properties;
    4. import com.alipay.mychain.gl.biz.common.Response;
    5. import com.alipay.mychain.gl.biz.sdk.Client;
    6. import com.alipay.mychain.gl.biz.sdk.ClientConfig;
    7. import com.alipay.mychain.gl.biz.sdk.ClientPropertyConfig;
    8. import com.alipay.mychain.gl.common.model.entity.TransactionDO;
    9. import com.alipay.mychain.gl.common.model.pojo.Block;
    10. public class App {
    11. public static void main(String[] args ) throws Exception{
    12. Properties p = new Properties();
    13. // Replace "D:/path/" with the real absolute path where the sdk.properties is stored
    14. p.load(new FileInputStream("D:/path/sdk.properties"));
    15. // Construct the client
    16. ClientConfig config = new ClientPropertyConfig(p);
    17. Client client = new Client(config);
    18. // Obtain the current block height
    19. int height = client.getLatestBlockHeader().getData().getHeight();
    20. // Obtain a block
    21. Response<Block> blockResponse = client.getBlock(height);
    22. // Obtain the transaction information on the block
    23. List<TransactionDO> trans = blockResponse.getData().getTransactions();
    24. // Shut down the client
    25. client.shutdown();
    26. }
    27. }

For more methods of using the SDK, see SDK Development Guide.