You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

code_example.MD 14 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. # 1. maven坐标
  2. ```java
  3. <dependency>
  4. <groupId>com.jd.blockchain</groupId>
  5. <artifactId>sdk-client</artifactId>
  6. <version>1.4.0.RELEASE</version>
  7. </dependency>
  8. <dependency>
  9. <groupId>com.jd.blockchain</groupId>
  10. <artifactId>contract-starter</artifactId>
  11. <version>1.4.0.RELEASE</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>com.jd.blockchain</groupId>
  15. <artifactId>crypto-classic</artifactId>
  16. <version>1.4.0.RELEASE</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>com.jd.blockchain</groupId>
  20. <artifactId>crypto-sm</artifactId>
  21. <version>1.4.0.RELEASE</version>
  22. </dependency>
  23. ```
  24. # 2. 数据快速上链
  25. ## 2.1. 服务连接
  26. ```java
  27. //使用已注册用户信息进行连接;
  28. String GW_PUB_KEY = "3snxxx";
  29. String GW_PRIV_KEY = "177xxx";
  30. String GW_PASSWORD = "xxx";
  31. PrivKey gwPrivkey0 = KeyGenUtils.decodePrivKey(GW_PRIV_KEY, GW_PASSWORD);
  32. PubKey gwPubKey0 = KeyGenUtils.decodePubKey(GW_PUB_KEY);
  33. BlockchainKeypair adminKey = new BlockchainKeypair(gwPubKey0, gwPrivkey0);
  34. //创建服务代理
  35. final String GATEWAY_IP = "127.0.0.1";
  36. final int GATEWAY_PORT = 80;
  37. final boolean SECURE = false;
  38. GatewayServiceFactory serviceFactory = GatewayServiceFactory.connect(GATEWAY_IP, GATEWAY_PORT, SECURE,
  39. adminKey);
  40. // 创建服务代理;
  41. BlockchainService service = serviceFactory.getBlockchainService();
  42. HashDigest[] ledgerHashs = service.getLedgerHashs();
  43. // 获取当前账本Hash
  44. HashDigest ledgerHash = ledgerHashs[0];
  45. ```
  46. ## 2.2. 用户注册
  47. ```java
  48. // 创建服务代理;
  49. BlockchainService service = serviceFactory.getBlockchainService();
  50. // 在本地定义注册账号的 TX;
  51. TransactionTemplate txTemp = service.newTransaction(ledgerHash);
  52. BlockchainKeypair user = BlockchainKeyGenerator.getInstance().generate();
  53. txTemp.users().register(user.getIdentity());
  54. // TX 准备就绪;
  55. PreparedTransaction prepTx = txTemp.prepare();
  56. // 使用私钥进行签名;
  57. prepTx.sign(adminKey);
  58. // 提交交易;
  59. prepTx.commit();
  60. ```
  61. ## 2.3. 数据账户注册
  62. ```java
  63. // 创建服务代理;
  64. BlockchainService service = serviceFactory.getBlockchainService();
  65. // 在本地定义注册账号的 TX;
  66. TransactionTemplate txTemp = service.newTransaction(ledgerHash);
  67. BlockchainKeypair dataAccount = BlockchainKeyGenerator.getInstance().generate();
  68. txTemp.dataAccounts().register(dataAccount.getIdentity());
  69. // TX 准备就绪;
  70. PreparedTransaction prepTx = txTemp.prepare();
  71. // 使用私钥进行签名;
  72. prepTx.sign(adminKey);
  73. // 提交交易;
  74. prepTx.commit();
  75. ```
  76. ## 2.4. 写入数据
  77. ```java
  78. // 创建服务代理;
  79. BlockchainService service = serviceFactory.getBlockchainService();
  80. // 在本地定义注册账号的 TX;
  81. TransactionTemplate txTemp = service.newTransaction(ledgerHash);
  82. // --------------------------------------
  83. // 将商品信息写入到指定的账户中;
  84. // 对象将被序列化为 JSON 形式存储,并基于 JSON 结构建立查询索引;
  85. String commodityDataAccount = "GGhhreGeasdfasfUUfehf9932lkae99ds66jf==";
  86. txTemp.dataAccount(commodityDataAccount).setText("ASSET_CODE", "value1", -1);
  87. // TX 准备就绪;
  88. PreparedTransaction prepTx = txTemp.prepare();
  89. String txHash = Base64Utils.encodeToUrlSafeString(prepTx.getHash().toBytes());
  90. // 使用私钥进行签名;
  91. prepTx.sign(adminKey);
  92. // 提交交易;
  93. prepTx.commit();
  94. ```
  95. ## 2.5. 查询数据
  96. > 注:详细的查询可参考模块sdk-samples中SDK_GateWay_Query_Test_相关测试用例
  97. ```java
  98. // 创建服务代理;
  99. BlockchainService service = serviceFactory.getBlockchainService();
  100. // 查询区块信息;
  101. // 区块高度;
  102. long ledgerNumber = service.getLedger(ledgerHash).getLatestBlockHeight();
  103. // 最新区块;
  104. LedgerBlock latestBlock = service.getBlock(ledgerHash, ledgerNumber);
  105. // 区块中的交易的数量;
  106. long txCount = service.getTransactionCount(ledgerHash, latestBlock.getHash());
  107. // 获取交易列表;
  108. LedgerTransaction[] txList = service.getTransactions(ledgerHash, ledgerNumber, 0, 100);
  109. // 遍历交易列表
  110. for (LedgerTransaction ledgerTransaction : txList) {
  111. TransactionContent txContent = ledgerTransaction.getTransactionContent();
  112. Operation[] operations = txContent.getOperations();
  113. if (operations != null && operations.length > 0) {
  114. for (Operation operation : operations) {
  115. operation = ClientResolveUtil.read(operation);
  116. // 操作类型:数据账户注册操作
  117. if (operation instanceof DataAccountRegisterOperation) {
  118. DataAccountRegisterOperation daro = (DataAccountRegisterOperation) operation;
  119. BlockchainIdentity blockchainIdentity = daro.getAccountID();
  120. }
  121. // 操作类型:用户注册操作
  122. else if (operation instanceof UserRegisterOperation) {
  123. UserRegisterOperation uro = (UserRegisterOperation) operation;
  124. BlockchainIdentity blockchainIdentity = uro.getUserID();
  125. }
  126. // 操作类型:账本注册操作
  127. else if (operation instanceof LedgerInitOperation) {
  128. LedgerInitOperation ledgerInitOperation = (LedgerInitOperation)operation;
  129. LedgerInitSetting ledgerInitSetting = ledgerInitOperation.getInitSetting();
  130. ParticipantNode[] participantNodes = ledgerInitSetting.getConsensusParticipants();
  131. }
  132. // 操作类型:合约发布操作
  133. else if (operation instanceof ContractCodeDeployOperation) {
  134. ContractCodeDeployOperation ccdo = (ContractCodeDeployOperation) operation;
  135. BlockchainIdentity blockchainIdentity = ccdo.getContractID();
  136. }
  137. // 操作类型:合约执行操作
  138. else if (operation instanceof ContractEventSendOperation) {
  139. ContractEventSendOperation ceso = (ContractEventSendOperation) operation;
  140. }
  141. // 操作类型:KV存储操作
  142. else if (operation instanceof DataAccountKVSetOperation) {
  143. DataAccountKVSetOperation.KVWriteEntry[] kvWriteEntries =
  144. ((DataAccountKVSetOperation) operation).getWriteSet();
  145. if (kvWriteEntries != null && kvWriteEntries.length > 0) {
  146. for (DataAccountKVSetOperation.KVWriteEntry kvWriteEntry : kvWriteEntries) {
  147. BytesValue bytesValue = kvWriteEntry.getValue();
  148. DataType dataType = bytesValue.getType();
  149. Object showVal = ClientResolveUtil.readValueByBytesValue(bytesValue);
  150. System.out.println("writeSet.key=" + kvWriteEntry.getKey());
  151. System.out.println("writeSet.value=" + showVal);
  152. System.out.println("writeSet.type=" + dataType);
  153. System.out.println("writeSet.version=" + kvWriteEntry.getExpectedVersion());
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }
  160. // 根据交易的 hash 获得交易;注:客户端生成 PrepareTransaction 时得到交易hash;
  161. HashDigest txHash = txList[0].getTransactionContent().getHash();
  162. Transaction tx = service.getTransactionByContentHash(ledgerHash, txHash);
  163. // 获取数据;
  164. String commerceAccount = "GGhhreGeasdfasfUUfehf9932lkae99ds66jf==";
  165. String[] objKeys = new String[] { "x001", "x002" };
  166. TypedKVEntry[] kvData = service.getDataEntries(ledgerHash, commerceAccount, objKeys);
  167. long payloadVersion = kvData[0].getVersion();
  168. // 获取数据账户下所有的KV列表
  169. TypedKVEntry[] kvDatas = service.getDataEntries(ledgerHash, commerceAccount, 0, 100);
  170. if (kvData != null && kvData.length > 0) {
  171. for (TypedKVEntry kvDatum : kvDatas) {
  172. System.out.println("kvData.key=" + kvDatum.getKey());
  173. System.out.println("kvData.version=" + kvDatum.getVersion());
  174. System.out.println("kvData.type=" + kvDatum.getType());
  175. System.out.println("kvData.value=" + kvDatum.getValue());
  176. }
  177. }
  178. ```
  179. ## 2.6. 合约发布
  180. ```java
  181. // 创建服务代理;
  182. BlockchainService service = serviceFactory.getBlockchainService();
  183. // 在本地定义TX模板
  184. TransactionTemplate txTemp = service.newTransaction(ledgerHash);
  185. // 合约内容读取
  186. byte[] contractBytes = FileUtils.readBytes(new File("CONTRACT_FILE"));
  187. // 生成用户
  188. BlockchainKeypair contractKeyPair = BlockchainKeyGenerator.getInstance().generate();
  189. // 发布合约
  190. txTemp.contracts().deploy(contractKeyPair.getIdentity(), contractBytes);
  191. // TX 准备就绪;
  192. PreparedTransaction prepTx = txTemp.prepare();
  193. // 使用私钥进行签名;
  194. prepTx.sign(adminKey);
  195. // 提交交易;
  196. TransactionResponse transactionResponse = prepTx.commit();
  197. assertTrue(transactionResponse.isSuccess());
  198. // 打印合约地址
  199. System.out.println(contractKeyPair.getIdentity().getAddress().toBase58());
  200. ```
  201. ## 2.7. 合约执行
  202. ```java
  203. // 创建服务代理;
  204. BlockchainService service = serviceFactory.getBlockchainService();
  205. // 在本地定义TX模板
  206. TransactionTemplate txTemp = service.newTransaction(ledgerHash);
  207. // 合约地址
  208. String contractAddress = "";
  209. // 使用接口方式调用合约
  210. TransferContract transferContract = txTemp.contract(contractAddress, TransferContract.class);
  211. // 使用decode方式调用合约内部方法(create方法)
  212. // 返回GenericValueHolder可通过get方法获取结果,但get方法需要在commit调用后执行
  213. String address = "address";
  214. String account = "fill account";
  215. long money = 100000000L;
  216. GenericValueHolder<String> result = ContractReturnValue.decode(transferContract.create(address, account, money));
  217. PreparedTransaction ptx = txTemp.prepare();
  218. ptx.sign(adminKey);
  219. TransactionResponse transactionResponse = ptx.commit();
  220. String cotractExecResult = result.get();
  221. // TransactionResponse也提供了可供查询结果的接口
  222. OperationResult[] operationResults = transactionResponse.getOperationResults();
  223. // 通过OperationResult获取结果
  224. for (int i = 0; i < operationResults.length; i++) {
  225. OperationResult opResult = operationResults[i];
  226. System.out.printf("Operation[%s].result = %s \r\n",
  227. opResult.getIndex(), BytesValueEncoding.decode(opResult.getResult()));
  228. }
  229. ```
  230. ## 2.8. 事件账户注册
  231. ```java
  232. BlockchainService service = serviceFactory.getBlockchainService();
  233. TransactionTemplate txTemp = service.newTransaction(ledgerHash);
  234. BlockchainKeypair eventAccount = BlockchainKeyGenerator.getInstance().generate();
  235. txTemp.eventAccounts().register(eventAccount.getIdentity());
  236. // TX 准备就绪;
  237. PreparedTransaction prepTx = txTemp.prepare();
  238. // 使用私钥进行签名;
  239. prepTx.sign(adminKey);
  240. // 提交交易;
  241. prepTx.commit();
  242. ```
  243. ## 2.9. 事件发布
  244. ```java
  245. BlockchainService service = serviceFactory.getBlockchainService();
  246. TransactionTemplate txTemp = service.newTransaction(ledgerHash);
  247. // 发布事件到指定的账户中;
  248. String eventAccount = "GGhhreGeasdfasfUUfehf9932lkae99ds66jf==";
  249. txTemp.eventAccount(eventAccount).publish("event_name", "string", -1)
  250. .publish("event_name", 0, 0);
  251. // TX 准备就绪;
  252. PreparedTransaction prepTx = txTemp.prepare();
  253. // 使用私钥进行签名;
  254. prepTx.sign(adminKey);
  255. // 提交交易;
  256. prepTx.commit();
  257. ```
  258. ## 2.10. 事件监听
  259. - 系统事件
  260. > 目前仅支持新区块产生事件
  261. ```java
  262. EventListenerHandle<SystemEventPoint> handler = blockchainService.monitorSystemEvent(ledgerHash,
  263. SystemEvent.NEW_BLOCK_CREATED, 0, new SystemEventListener<SystemEventPoint>() {
  264. @Override
  265. public void onEvents(Event[] eventMessages, EventContext<SystemEventPoint> eventContext) {
  266. for (Event eventMessage : eventMessages) {
  267. BytesValue content = eventMessage.getContent();
  268. // content中存放的是当前链上最新高度
  269. System.out.println(BytesUtils.toLong(content.getBytes().toBytes()));
  270. }
  271. // 关闭监听的两种方式:1
  272. eventContext.getHandle().cancel();
  273. }
  274. });
  275. // 关闭监听的两种方式:2
  276. handler.cancel();
  277. ```
  278. - 用户自定义事件
  279. ```java
  280. EventListenerHandle<UserEventPoint> handler = blockchainService.monitorUserEvent(ledgerHash,
  281. eventAccount.getAddress().toBase58(), eventName, 0, new UserEventListener<UserEventPoint>() {
  282. @Override
  283. public void onEvent(Event eventMessage, EventContext<UserEventPoint> eventContext) {
  284. BytesValue content = eventMessage.getContent();
  285. switch (content.getType()) {
  286. case TEXT:
  287. System.out.println(content.getBytes().toUTF8String());
  288. break;
  289. case INT64:
  290. System.out.println(BytesUtils.toLong(content.getBytes().toBytes()));
  291. break;
  292. default:
  293. break;
  294. }
  295. // 关闭监听的两种方式:1
  296. eventContext.getHandle().cancel();
  297. }
  298. });
  299. // 关闭监听的两种方式:2
  300. handler.cancel();
  301. ```