1. 申请证书

要使用天德云区块链服务,首先使用者(单体用户或企业用户,本文中若无特殊说明时,使用者,用户,企业用户泛指使用天德云区块链服务的用户)需要在天德云区块链网站进行注册登录,成功登录之后,即可进入个人中心下载证书:
第一,填写证书密码: 如图1-1 证书密码

证书密码
图1-1 证书密码

第二,下载证书:如图1-2 下载证书
下载证书
图1-2 下载证书

参数说明:
① 证书密码:天德云区块链授权证书需要一个口令,这个口令天德云区块链不保存,用户切记保管好,改口令丢失无法找回!
另外,一个用户对应一个证书,申请过证书的用户不可再次申请。
② 证书使用:该证书只能在jbcc sdk上使用,其他工具无法打开。


  1. 引入依赖

<dependency>
        <groupId>cn.tdchain</groupId>
        <artifactId>jbcc</artifactId>
        <version>3.0.0.TD2</version>
</dependency>


  1. 使用服务

(a). 参数配置

                    
protected static String[] iptables = new String[]{
    "open-tdcb-node1.tdchain.cn",
    "open-tdcb-node2.tdchain.cn",
    "open-tdcb-node3.tdchain.cn",
    "open-tdcb-node4.tdchain.cn"};

protected final static String keystorePath = "D:\\04-dev\\keys\\rsa\\87f1a3e2-39d2-4e3b-acbe-73932808d47b.pfx";
protected final static String keystorePasswd = "123456";

//# Access to Tiandeyun block chain port, default is18088
protected final static int port = 18088;
//# Connection timeout, default 3 seconds
protected final static long timeout = 3000;
//# Visit token of Tiandeyun Block Chain and obtain valid certificate after successful application token
protected final static String token = "32581b83-705d-40ef-9f62-1f9a4118fbe1";
//# Authorization files needed to access Tiandeyun block chain can be downloaded after successful login, and keystore Path is the certificate storage path.

//# Set SM or RSA cipher
Cipher.Type type = Cipher.Type.RSA;//or Cipher.Type.SM

//# Declare a global connector
public static Connection connection = null;

static {
    try {
        //# Building configuration information
        ConnectionFactory factory = ConnectionFactory.ConnectionConfig.builder()
                .cipherType(type)
                .iptables(iptables)
                .port(port)  //# No configuration even with default value 18088
                .timeout(timeout)  //# Use default 3 seconds without configuration
                .token(token)
                //.showPrint()
                .keystorePath(keystorePath)
                .keystorePassword(keystorePasswd).build();

        connection = factory.getConnection();
    } catch (Exception e) {
        log.info("Please check the configuration===> error: {}", e);
    }
}
                    
                

    说明
    cipherType:秘钥算法选择,默认是RSA\sha256\AES.可以选择SM国密算法:SM2\SM3\SM4
    iptables:天德云区块链服务节点ip列表,必须配置.
    port: 访问服务的端口,默认18088,可以不配置.
    timeout:获取连接超时,默认3秒,可以不配置.
    token: 访问token,必须配置.
    keystorePath:授权证书存放全路径,必须配置.
    keystorePasswd:授权证书的密码,必须配置.


(b). 发起一笔交易-样例

                    
public static void main(String[] args) {
    //# Construct a transaction information
    Trans trans = new Trans();
    trans.setKey("warne");//# Key is the dimension of current transactions
    Map<String, Object> data = new HashMap<>();
    data.put("name", "warne");
    data.put("age", 20);
    data.put("where", "I am tian de technology.");
    trans.setData(JSON.toJSONString(data));
    trans.setType("user_info");
    trans.setTimestamp(new Date().getTime());

    //# Initiate a transaction to cloud block chain services
    Result<TransHead> result = connection.addTrans(trans);
    if (result.isSuccess()) {
        //# Determine success based on the status of returned results
        log.info("\n===> add trans success.");
    } else {
        log.info("\n===> add trans fail.");
    }
}
                    
                

(c). BQL(BlockChain Query Langage)区块链面向对象查询交易

                    
 public static void main(String[] args) {
    	//第一个条件:姓名等于=xiaoming  的
    	Condition c1 = new Condition("name", BQL.Relationship.equal, "xiaoming");
    	
    	//第二个条件:年龄大于18岁 的
    	Condition c2 = new Condition("age", BQL.Relationship.greater, 18);
    	
    	//条件c1和c2是并且关系,一次查询可同时支持10个条件对象Condition
    	c1.setAnd(c2);//相当于: name=="xiaoming" && age > 18
    	
        //BQL区块链面向对象查询条件,根据k、v条件查询交易列表。
    	BQL bql = new BQL();
    	bql.setPage(1);//默认查询第一页
    	bql.setCondition(c1);
    	
        try {
            Result result = connection.getNewTransByBQL(bql);

            if (result.isSuccess()) {
            	BQLResult bqlResult = result.getEntity();
            	log.info("bqlResult page=" + bqlResult.getPage());//page int 获取本次返回的页码坐标,页码坐标最小是:1 (俗称第一页)
            	log.info("bqlResult size=" + bqlResult.getSize());//size int 本次查询总条数
            	log.info("bqlResult count=" + bqlResult.getCount());//count int 本次实际返回条数,一次查询返回不会超过30条,否则系统会自动分页。
            	log.info("bqlResult getList().size()=" + bqlResult.getList().size());
    			for (Trans t : bqlResult.getList()) {
    				log.info(t.toJsonString());
    			}
                log.info("\n===> query new trans success.");
            } else {
                log.info("\n===> query new trans fail.");
            }
            Tools.printResult(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
                    
                

    


更多调用样例,请移步到 Github: https://github.com/tiande-blockchain/jbcc-samples