6. SDK接入
6.1. JAVA SDK
6.1.1. 环境准备
6.1.1.1. xchain-java-sdk示例
示例xchain-java-sdk源码,包含详细注释
示例存证合约文件
示例存证合约源码
6.1.1.2. 使用本地jar包
下载java sdk
将sdk安装到本地maven仓库
> mvn install:install-file -Dfile="xchain-java-sdk-2.0.0.jar" -DgroupId="org.xbl.xchain" -DartifactId="xchain-java-sdk" -Dversion="2.0.0" -Dpackaging=jar
pom中添加依赖
<dependency>
<groupId>org.xbl.xchain</groupId>
<artifactId>xchain-java-sdk</artifactId>
<version>2.0.0</version>
</dependency>
6.1.1.3. 或使用远程仓库jar包
可选添加maven远程仓库,配置maven的settings.xml
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
pom中添加依赖
<dependency>
<groupId>io.gitee.xachainzyan</groupId>
<artifactId>xchain-java-sdk</artifactId>
<version>2.0.0</version>
</dependency>
6.1.2. sdk使用
根据区块链信息填写sdk配置项
String url = "http://ip:port";
String chainId = "chainId";
String mne = "徙 需 校 慰 末 辞 腊 瞧 逻 冶 铅 讲 觉 残 框 又 频 招 朝 表 离 链 电 闲";
部署合约
SysConfig sysConfig = new SysConfig(Config.url, Config.chainId, "2");
XchainClient xchainClient = new XchainClient(sysConfig);
String contractName = "evidence";
String language = "rust";
String initMsg = "{}";
String contractPath = "src/test/java/wasm/evidence.wasm";
String executePerm = "";
String lable = "";
Account acc = Account.buildAccount(new KeyInfo(Config.mne, AlgorithmType.SM2));
TxResponse txResponse = xchainClient.instantiateContract(acc, contractName, language, contractFile, initMsg, PermissionPolicy.POLICY_DROP, executePerm, label);
assert txResponse.isSuccess();
调用合约
SysConfig sysConfig = new SysConfig(Config.url, Config.chainId, "2");
XchainClient xchainClient = new XchainClient(sysConfig);
Account acc = Account.buildAccount(new KeyInfo(Config.mne, AlgorithmType.SM2));
String contractName = "evidence";
String functionName = "create";
String args = "{\"key\":\"UUID11235\",\"value\":\"65535\"}";
TxResponse txResponse = xchainClient.executeContract(acc, contractName, functionName, args);
System.out.println(txResponse);
assert txResponse.isSuccess();
TxResponse结构
TxResponse(
code=0,
codeSpace=,
log=[
{
"msg_index":0,
"log":"",
"events":[
{
"type":"message",
"attributes":[
{
"key":"action",
"value":"execute"
},
{
"key":"module",
"value":"wasm"
},
{
"key":"signer",
"value":"xchain1fxfh7pegr2vhme8g6cgvsk3h760a7su8d7x3wn"
},
{
"key":"contract_address",
"value":"xchain10pyejy66429refv3g35g2t7am0was7yahxdaan"
}
]
}
]
}
], data=, hash=12D5552E01FD8718EB88D939EFB2DD7710C2A8920485CEA77321020E101DFD97, height=52255, proposalId=null
)
其中hash可用于在sdk中testQueryTxHash查询交易信息,height可用于在sdk中testQueryBlockByHeight查询交易所在区块信息。
查询合约
SysConfig sysConfig = new SysConfig(Config.url, Config.chainId, "2");
XchainClient xchainClient = new XchainClient(sysConfig);
String contractName = "evidence";
String functionName = "find";
String args = "{\"key\":\"UUID11235\"}";
String queryResult = xchainClient.queryContract(contractName, functionName, args);
System.out.println(queryResult);
查询交易信息,其中txHash为执行交易返回txResponse中hash字段
SysConfig sysConfig = new SysConfig(Config.url, Config.chainId, "2");
XchainClient xchainClient = new XchainClient(sysConfig);
String txHash = "6D726651B86BF784B54611388B88A1A5726405F172B696B8A23CB0D3D380AF77";
TxInfo txInfo = xchainClient.queryTx(txHash);
System.out.println(txInfo);
查询区块信息,其中txHash为执行交易返回txResponse中height字段
SysConfig sysConfig = new SysConfig(Config.url, Config.chainId, "2");
XchainClient xchainClient = new XchainClient(sysConfig);
BlockInfo blockInfo = xchainClient.queryBlock("1069");
System.out.println(blockInfo);
System.out.println(blockInfo.getBlockTime());
6.1.3. 事件监听、扫块示例
示例scan-demo源码
示例存证合约文件
示例存证合约源码 与contract-evidence区别在于调用合约时抛出事件