Java实现NEO数据上链服务

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hollycrm.hollyuniproxy.test.HttpJsonBlockListClient;
import com.xczg.blockchain.yibaodapp.bean.BlockChainResultEntity;
import com.xczg.blockchain.yibaodapp.rest.NoticeController;
import com.xczg.blockchain.yibaodapp.service.ISaveBlockService;
import com.xczg.blockchain.yibaodapp.util.ChainUtil;
import com.xczg.blockchain.yibaodapp.util.StringUtil;
import com.xczg.blockchain.yibaodapp.util.UUIDUtil;

@Service("saveBlockServiceImpl")
public class SaveBlcokServiceImpl implements ISaveBlockService {
	private final Logger logger = LoggerFactory.getLogger(NoticeController.class);
		//数据上链服务,参数分别为(上链的value、NEO节点的URL)
	@Override
	public BlockChainResultEntity saveBlcok(String value, String url) {
		BlockChainResultEntity entity = new BlockChainResultEntity();
		String key = UUIDUtil.getUUID();
		Map inParam = new HashMap();
		inParam.put("jsonrpc", "2.0");
		inParam.put("method", "invokefunction");
		inParam.put("id", "3");
		Object paramsArr[] = new Object[4];
		paramsArr[0] = "****************************************";//智能合约地址
		paramsArr[1] = "savePayInfo";//智能合约方法名
		Map map_a1 = new HashMap();
		map_a1.put("type", "String");
		map_a1.put("value", key);
		Map map_a2 = new HashMap();
		map_a2.put("type", "String");
		map_a2.put("value", value);
		List> list_b1 = new ArrayList>();
		list_b1.add(map_a1);
		list_b1.add(map_a2);
		paramsArr[2] = list_b1;
		inParam.put("params", paramsArr);
		String inJson = JSONObject.toJSONString(inParam).toString();
		try {
			String outJson = HttpJsonBlockListClient.requestNeo(url, inJson);
			logger.info("上链返回结果:" + outJson);
			JSONObject jsObj = JSONObject.parseObject(outJson);
			if (jsObj != null && jsObj.containsKey("result")) {
				JSONObject resObj = jsObj.getJSONObject("result");
				if (resObj.containsKey("tx")) {
					String tx = resObj.getString("tx");
					// 广播交易
					logger.info("构造签名的交易hash进行广播交易:" + outJson);
					Object oj = ChainUtil.sendrawtransaction(tx, url);//关键,否则无法上链。
					logger.info("广播交易结果:" + oj);
					entity.setValidKey(key);
					entity.setTxid(txid);
					return entity;

				}
			}
		} catch (Exception e) {
			logger.error("上链失败" + e);
			return null;
		}
		return null;
	}

	//上链查询数据,参数为key  和 节点 地址
	@Override
	public String getInfo(String key, String url) {
		Map inParam = new HashMap();
		inParam.put("jsonrpc", "2.0");
		inParam.put("method", "invokefunction");
		inParam.put("id", "3");
		Object paramsArr[] = new Object[4];
		paramsArr[0] = "*****************************************";//智能合约地址
		paramsArr[1] = "get";
		Map map_a1 = new HashMap();
		map_a1.put("type", "String");
		map_a1.put("value", key);
		List> list_b1 = new ArrayList>();
		list_b1.add(map_a1);
		paramsArr[2] = list_b1;
		inParam.put("params", paramsArr);
		String inJson = JSONObject.toJSONString(inParam).toString();
		try {
			String outJson = HttpJsonBlockListClient.requestNeo(url, inJson);
			logger.info("上链取数据返回结果:" + outJson);
			JSONObject jsObj = JSONObject.parseObject(outJson);
			if (jsObj != null && jsObj.containsKey("result")) {
				JSONObject resObj = jsObj.getJSONObject("result");
				if (resObj != null && resObj.containsKey("stack")) {
					JSONArray stackArr = resObj.getJSONArray("stack");
					if (stackArr != null && stackArr.size() > 0) {
						JSONObject stackObj = stackArr.getJSONObject(0);
						if (stackObj != null && stackObj.containsKey("value")) {
							String value = stackObj.getString("value");
							String resultStr = StringUtil.hexStringToString(value);
							return resultStr;
						}
					}
				}
			}
		} catch (Exception e) {
			logger.error("上链取数据异常:" + e);
			return null;
		}
		return null;
	}
}

你可能感兴趣的:(区块链,NEO)