1. 注册用户

package com.hello.jdchain;

import com.jd.blockchain.crypto.HashDigest;

import com.jd.blockchain.crypto.KeyGenUtils;

import com.jd.blockchain.crypto.PrivKey;

import com.jd.blockchain.crypto.PubKey;

import com.jd.blockchain.ledger.BlockchainKeyGenerator;

import com.jd.blockchain.ledger.BlockchainKeypair;

import com.jd.blockchain.ledger.PreparedTransaction;

import com.jd.blockchain.ledger.TransactionTemplate;

import com.jd.blockchain.sdk.BlockchainService;

import com.jd.blockchain.sdk.client.GatewayServiceFactory;

public class App {

public static final String GW_IPADDR = "192.168.1.1";

public static final int GW_PORT = 8090;

/**

* peer节点的公钥

*/

public static final String PUB_KEY = "3snPdw7i7PbvsHumxdGnv8GHySqkfGsdfsdKEPobFQ1Pqgj1hnLUt72P";

/**

* peer节点私钥

*/

public static final String PRIV_KEY = "177gjsghLtEadqgot2JWdB97YbQJWiwNscRV34MUJsdfsdfsdfi9LysdwqjvEy9XgmXtvexz5pGb";

/**

* peer节点密码

*/

public static final String PASSWORD = "123456";

public static BlockchainKeypair adminKey;

public static BlockchainService blockchainService;

public static HashDigest ledgerHash;

public static BlockchainKeypair jdChainUser;

static{

//以peer节点上的用户

// 生成连接网关的账号

PrivKey privKey = KeyGenUtils.decodePrivKeyWithRawPassword(PRIV_KEY, PASSWORD);

PubKey pubKey = KeyGenUtils.decodePubKey(PUB_KEY);

adminKey = new BlockchainKeypair(pubKey, privKey);

// 连接网关

GatewayServiceFactory serviceFactory = GatewayServiceFactory.connect(GW_IPADDR,

GW_PORT, false, adminKey);

// 获取网关对应的Service处理类

blockchainService = serviceFactory.getBlockchainService();

HashDigest[] ledgerHashs = blockchainService.getLedgerHashs();

// 获取当前账本Hash

ledgerHash = ledgerHashs[0];

}

/**

* 注册用户

*/

public static BlockchainKeypair registerUser(){

// 在本地定义注册账号的 TX;

TransactionTemplate txTemp = blockchainService.newTransaction(ledgerHash);

BlockchainKeypair user = BlockchainKeyGenerator.getInstance().generate();

System.out.println("user.getAddress().toBase58():" + user.getAddress().toBase58());

System.out.println("user.getAddress().toString():" + user.getAddress().toString());

System.out.println("user.getIdentity().getAddress().toBase58():" + user.getIdentity().getAddress().toBase58());

System.out.println("user.getIdentity().getAddress().toString():" + user.getIdentity().getAddress().toString());

System.out.println("user.getIdentity().getPubKey().toBase58():" + user.getIdentity().getPubKey().toBase58());

System.out.println("user.getIdentity().getPubKey().toString():" + user.getIdentity().getPubKey().toString());

System.out.println("user.getIdentity().getPubKey().toUTF8String():" + user.getIdentity().getPubKey().toUTF8String());

txTemp.users().register(user.getIdentity());

// TX 准备就绪;

PreparedTransaction prepTx = txTemp.prepare();

prepTx.sign(adminKey);

// 提交交易;

prepTx.commit();

return user;

}

public static void main(String[] args) {

//测试注册用户

jdChainUser = registerUser();

//测试注册数据账号

}

}

image.png

2. 注册数据账户

/**

* 注册数据账户

* @return

*/

public static BlockchainKeypair registerDataAccount(){

TransactionTemplate txTemp = blockchainService.newTransaction(ledgerHash);

//采用KeyGenerator来生成BlockchainKeypair;

BlockchainKeypair dataAccount = BlockchainKeyGenerator.getInstance().generate();

txTemp.dataAccounts().register(dataAccount.getIdentity());

txTemp.dataAccount(dataAccount.getAddress()).setText("address","wuhan",-1);

//add some data for retrieve;

strDataAccount = dataAccount.getAddress().toBase58();

System.out.println("strDataAccount:" + strDataAccount);

System.out.println("current dataAccount= "+ dataAccount.getAddress());

// txTemp.dataAccount(dataAccount.getAddress()).setText("cc-fin01-01","{\"dest\":\"KA001\",\"id\":\"cc-fin01-01\",\"items\":\"FIN001|5000\",\"source\":\"FIN001\"}",-1);

// txTemp.dataAccount(dataAccount.getAddress()).setText("cc-fin02-01","{\"dest\":\"KA001\",\"id\":\"cc-fin02-01\",\"items\":\"FIN002|2000\",\"source\":\"FIN002\"}",-1);

// TX 准备就绪

PreparedTransaction prepTx = txTemp.prepare();

prepTx.sign(adminKey);

prepTx.commit();

return dataAccount;

}

image.png

3. 写入数据

/**

* 写入数据

* dataAccount 数据账户地址

*/

public static void writeData(String dataAccount){

// 在本地定义注册账号的 TX;

TransactionTemplate txTemp = blockchainService.newTransaction(ledgerHash);

long startTime = System.currentTimeMillis();

for(int i=0;i<10;i++){

//add some data for retrieve;

txTemp.dataAccount(dataAccount).setText(i+"", "{\"dest\":\"KA001\",\"id\":\"" + i + "\",\"items\":\"FIN001|5000\",\"source\":\"FIN001\"}",-1);

}

long endTime = System.currentTimeMillis();

System.out.println("cost " + (endTime - startTime)/1000L + " s");

// TX 准备就绪

PreparedTransaction prepTx = txTemp.prepare();

prepTx.sign(adminKey);

// 提交交易;

prepTx.commit();

}

image.png

4. 读取数据

/**

* 查询数据

* @param dataAccount 数据账户地址

*/

public static void getData(String dataAccount) {

// 查询区块信息;

// 区块高度;

long ledgerNumber = blockchainService.getLedger(ledgerHash).getLatestBlockHeight();

// 最新区块;

LedgerBlock latestBlock = blockchainService.getBlock(ledgerHash, ledgerNumber);

// 区块中的交易的数量;

long txCount = blockchainService.getTransactionCount(ledgerHash, latestBlock.getHash());

// 获取交易列表;

LedgerTransaction[] txList = blockchainService.getTransactions(ledgerHash, ledgerNumber, 0, 100);

// 遍历交易列表

for (LedgerTransaction ledgerTransaction : txList) {

TransactionContent txContent = ledgerTransaction.getTransactionContent();

Operation[] operations = txContent.getOperations();

if (operations != null && operations.length > 0) {

for (Operation operation : operations) {

operation = ClientResolveUtil.read(operation);

// 操作类型:数据账户注册操作

if (operation instanceof DataAccountRegisterOperation) {

DataAccountRegisterOperation daro = (DataAccountRegisterOperation) operation;

BlockchainIdentity blockchainIdentity = daro.getAccountID();

}

// 操作类型:用户注册操作

else if (operation instanceof UserRegisterOperation) {

UserRegisterOperation uro = (UserRegisterOperation) operation;

BlockchainIdentity blockchainIdentity = uro.getUserID();

}

// 操作类型:账本注册操作

else if (operation instanceof LedgerInitOperation) {

LedgerInitOperation ledgerInitOperation = (LedgerInitOperation)operation;

LedgerInitSetting ledgerInitSetting = ledgerInitOperation.getInitSetting();

ParticipantNode[] participantNodes = ledgerInitSetting.getConsensusParticipants();

}

// 操作类型:合约发布操作

else if (operation instanceof ContractCodeDeployOperation) {

ContractCodeDeployOperation ccdo = (ContractCodeDeployOperation) operation;

BlockchainIdentity blockchainIdentity = ccdo.getContractID();

}

// 操作类型:合约执行操作

else if (operation instanceof ContractEventSendOperation) {

ContractEventSendOperation ceso = (ContractEventSendOperation) operation;

}

// 操作类型:KV存储操作

else if (operation instanceof DataAccountKVSetOperation) {

DataAccountKVSetOperation.KVWriteEntry[] kvWriteEntries =

((DataAccountKVSetOperation) operation).getWriteSet();

if (kvWriteEntries != null && kvWriteEntries.length > 0) {

for (DataAccountKVSetOperation.KVWriteEntry kvWriteEntry : kvWriteEntries) {

BytesValue bytesValue = kvWriteEntry.getValue();

DataType dataType = bytesValue.getType();

Object showVal = ClientResolveUtil.readValueByBytesValue(bytesValue);

System.out.println("writeSet.key=" + kvWriteEntry.getKey());

System.out.println("writeSet.value=" + showVal);

System.out.println("writeSet.type=" + dataType);

System.out.println("writeSet.version=" + kvWriteEntry.getExpectedVersion());

}

}

}

}

}

}

//根据交易的 hash 获得交易;注:客户端生成 PrepareTransaction 时得到交易hash;

HashDigest txHash = txList[0].getTransactionContent().getHash();

// Transaction tx = blockchainService.getTransactionByContentHash(ledgerHash, txHash);

// String[] objKeys = new String[] { "x001", "x002" };

// KVDataEntry[] kvData = blockchainService.getDataEntries(ledgerHash, commerceAccount, objKeys);

// 获取数据账户下所有的KV列表

KVDataEntry[] kvData = blockchainService.getDataEntries(ledgerHash, dataAccount, 0, 100);

if (kvData != null && kvData.length > 0) {

for (KVDataEntry kvDatum : kvData) {

System.out.println("kvData.key=" + kvDatum.getKey());

System.out.println("kvData.version=" + kvDatum.getVersion());

System.out.println("kvData.type=" + kvDatum.getType());

System.out.println("kvData.value=" + kvDatum.getValue());

}

}

}

image.png

京东物流轨迹java_京东区块链 JD chain java demo实现相关推荐

  1. 区块链技术架构 java_《区块链底层设计Java实战》之第二章区块链架构

    第2 章   区块链架构 会当凌绝顶  一览众山小 正如开篇所言:会当凌绝顶,一览众山小.进入区块链底层开发前,我们需要 了解区块链底层的通用架构是如何设计的,从上而下地审视区块链底层的结构,做 到了 ...

  2. 阿里京东苏宁入局 区块链成破局新武器

    在过去的半年时间里,币圈呈现出两种景象.与年初的火热不同,上下漂浮不定的价格以及各种割韭菜的新闻,无论是投资者还是投机者都有些慌了.不过,作为风口上的"区块链"技术,却依旧火热,热 ...

  3. 【京东金融王越国:区块链能够解决欺诈问题】GBCAX

    gbcax链交所 [京东金融王越国:区块链能够解决欺诈问题] 据澎湃新闻消息,京东金融研究院.中国人民大学金融科技与互联网安全研究中心.中国刑事警察学院联合发布<数字金融反欺诈白皮书>,京 ...

  4. java开发区块链_使用Java语言从零开始创建区块链

    目前网络上关于区块链入门.科普的文章不少,本文就不再赘述区块链的基本概念了,如果对区块链不是很了解的话,可以看一下我之前收集的一些入门学习资源: 对区块链技术感到新奇的我们,都想知道区块链在代码上是怎 ...

  5. 区块链公司Chain在纳斯达克区块链…

    区块链初创公司Chain已经允许私人投资者在纳斯达克最近推出的私人市场解决方案Linq购买其发行的股票. 在声明中:纳斯达克称Chain是第一家使用Linq技术发行公司股票的公司,允许私人投资投资.该 ...

  6. 《区块链底层设计Java实战》之第三章密码学

    第3 章 密码学 九层之台,起于累土  千里之行,始于足下 第3 章 密码学 ............................................................. ...

  7. 【面向区块链应用的Java编程】

    目录 面向区块链应用的Java编程 1.什么是区块链 2.如何验证区块链 3.如何挖掘区块 4.区块链的工作方式 5.区块链的应用 5.1.比特币 5.2.智能合约 5.3.医疗 5.4.制造业和供应 ...

  8. 京东与阿里要用区块链打击假货 造假将无可遁形

    区块链技术原本只是为支撑数字货币而生,如今却被开发出了巨大潜力,对很多行业都能产生巨大影响.对于电子商务行业来说,利用区块链技术来打击假货成了最新的热门话题,京东和阿里巴巴两大巨头都希望能在这一方向取 ...

  9. 京东一面+京东物流二面+京东秋招一二面

    京东一面(25分钟) 讲一下你实习经历, 在实习期间都多了什么技术方面的,遇到了什么难题 1.你了解序列化吗? 2.HashMap和TreeMap的区别 3.如何支持多线程访问?(Concurrent ...

最新文章

  1. goland 调试运行路径
  2. 用lisp编写串口助手源代码_实战用python来写个串口助手--界面篇
  3. 【跃迁之路】【636天】程序员高效学习方法论探索系列(实验阶段393-2018.11.09)...
  4. html完整性检测,html - 什么是完整性和crossorigin属性?
  5. Java的5个古怪问题
  6. Office2010-2016官方镜像大全
  7. 利刃 MVVMLight 2:Model、View、ViewModel结构以及全局视图模型注入器的说明
  8. 添加class值_Java 虚拟机(二) - Class 文件结构
  9. Goscan:功能强大的交互式网络扫描工具
  10. python经典程序实例-你不知道的Python语言的经典五大案例
  11. 可变字符串 插入,删除,替换,赋值
  12. !DOCTYPE标签的定义与用法
  13. Linux下创建Django项目并访问
  14. 记一次hw中的上线骚姿势(异速联+用友U8)
  15. AT24C32、AT24C64、AT24C128、AT24C256、AT24C512系列EEPROM芯片单片机读写驱动程序
  16. 上顿号符号_顿号在键盘上怎么打?每日一答
  17. memset,calloc的区别
  18. 箭杆织布机计算机控制系统,高速喷水织布机单片机控制系统设计 毕业设计论文.doc...
  19. 软件工程专业画图工具Jude的安装与Windows找不到javaw文件的解决办法
  20. 红轴和茶轴哪个适合游戏 红轴和茶轴哪个手感好

热门文章

  1. 程序员VS文艺男!!论发型的重要性,堪比整容!
  2. 2019团体程序设计天梯赛L1 L1-1 PTA使我精神焕发L1-2 6翻了L1-3 敲笨钟L1-4 心理阴影面积L1-5 新胖子公式L1-6 幸运彩票L1-7 吃鱼还是吃肉
  3. 专业的web打印插件
  4. 【第八章】文件与文件系统的压缩、打包与备份
  5. 某预约系统分析 某区公共自行车租车卡在线预约,关于如何提高成功概率
  6. DeepLinQ 用于隐私保护的数据共享的分布式多层分类账本
  7. 【入门AUTOSAR网络管理测试】RSS-NOS状态转换
  8. linux ftp 解压缩命令,常用五种Linux环境中的压缩和解压命令示范 | OPS技术联盟
  9. 加快数字创新赋能实体经济 CDEC2021中国数字智能生态大会成都站今日举行
  10. 安卓中QQ登陆源代码