元宇宙非小号金色财经交流群社区官网

Java编写初学者的第一款区块链程序

浏览:68|时间:2023-06-16 02:52:22
近期,加密货币的每个块中都包含了所有交易的集合签名。这个签名就是用 Merkle 树实现的。Merkle 树用于产生整个事务集合的整体数字指纹,提供了一个非常有效的过程来验证事务是否包括在块中。

下面是实现 Merkle 树的 Java 代码:

``` package test;

import java.security.MessageDigest; import java.util.ArrayList; import java.util.List;

public class MerkleTrees { // 交易列表 List txList; // Merkle 根节点 String root;

/**构造函数 * @param txList 交易列表 */ public MerkleTrees(List txList) { this.txList = txList; this.root = ""; }

/**执行 Merkle 树 */ public void merkle_tree() { List tempTxList = new ArrayList(); for (int i = 0; i < this.txList.size(); i++) { tempTxList.add(this.txList.get(i)); } List newTxList = getNewTxList(tempTxList); while (newTxList.size() != 1) { newTxList = getNewTxList(newTxList); } this.root = newTxList.get(0); }

/**返回节点哈希列表 * @param tempTxList * @return */ private List getNewTxList(List tempTxList) { List newTxList = new ArrayList(); int index = 0; while (index < tempTxList.size()) { // left String left = tempTxList.get(index); index++; // right String right = ""; if (index != tempTxList.size()) { right = tempTxList.get(index); } // sha2hexvalue String sha2HexValue = getSHA2HexValue(left + right); newTxList.add(sha2HexValue); index++; } return newTxList; }

/**返回 16 进制字符串 * @param str * @return */ public String getSHA2HexValue(String str) { byte[] cipher_byte; try { MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(str.getBytes()); cipher_byte = md.digest(); StringBuilder sb = new StringBuilder(2 * cipher_byte.length); for (byte b : cipher_byte) { sb.append(String.format("%02x", b & 0xff)); } return sb.toString(); } catch (Exception e) { e.printStackTrace(); } return ""; }

/**获取 Merkle 树根节点 * @return */ public String getRoot() { return this.root; } }

```

我们将交易的数据放入到List中:

``` List tempTxList = new ArrayList(); tempTxList.add("a"); tempTxList.add("b"); tempTxList.add("c"); tempTxList.add("d"); tempTxList.add("e"); ```

计算出每个数据的hash值,从左到右逐步组成树的左右节点。执行循环直到最后只剩下一个数据。

``` private List getNewTxList(List tempTxList) { List newTxList = new ArrayList(); int index = 0; while (index < tempTxList.size()) { // left String left = tempTxList.get(index); index++; // right String right = ""; if (index != tempTxList.size()) { right = tempTxList.get(index); } // sha2hexvalue String sha2HexValue = getSHA2HexValue(left + right); newTxList.add(sha2HexValue); index++; } return newTxList; } ```

下面我们进行一个简单的测试:

``` package test;

import java.util.ArrayList; import java.util.List;

public class App { public static void main(String[] args) { List tempTxList = new ArrayList(); tempTxList.add("a"); tempTxList.add("b"); tempTxList.add("c"); tempTxList.add("d"); tempTxList.add("e"); MerkleTrees merkleTrees = new MerkleTrees(tempTxList); merkleTrees.merkle_tree(); System.out.println("root:" + merkleTrees.getRoot()); } } ```

AV 区块 区块链 区块链程序 款区块链

  • 以区块链技术为基础的腾讯发布了首款区块链代币AR手游

    以区块链技术为基础的腾讯发布了首款区块链代币AR手游

    2022-04-06 14:45:28

    凤凰网WEMONEY4月23日,在UP2018在腾讯新文化创意生态大会上,腾讯发布了首次AR手游《一起捉妖》。中链区块链能投资吗?_区块链被攻击的代币_区块链代币可以用法币代替据了解,基于腾讯区块链技术,游戏中的虚拟数字...

  • 全球首款区块链娱乐类公链ETM

    全球首款区块链娱乐类公链ETM

    2022-09-18 23:44:35

    近年来,伴随区块链技术的兴起,“区块链+”时代强势来袭,区块链+泛娱乐更是水到渠成地应运而生。目前,大多数区块链应用基于以太坊开发,但也遇到一些问题,比如高延迟、高费用、扩展性等问题一直没有得到很好的解决,这让很多人对区...

  • 区块链投资是什么,如何进行区块链投资?区块链的优势有什么?

    区块链投资是什么,如何进行区块链投资?区块链的优势有什么?

    2022-02-21 15:10:06

    2021年2月21日,比特币创下58330.57美元的新高,逼近6万美元的关口,十年间上涨6500余倍, Elon Musk(Tesla创始人)获利超过9亿美元。其实比特币只是区块链的众多应用之一。那投资者怎么进行区块链...

  • 区块链科普:区块链是特点什么?区块链和比特币有什么关系?

    区块链科普:区块链是特点什么?区块链和比特币有什么关系?

    2022-02-21 16:16:57

    最近,区块链和比特币这个词非常流行,但什么是区块链,什么是比特币呢?区块链和比特币之间的关系是什么?这些将是本文将涉及的问题。区块链与法律的交错可分为三个方面:首先是区块链行业的监管。以典型的分散比特币为例,由于没有集中...

本站分享的区块链、Web3.0元宇宙、NFT、数字藏品最新消息等相关数藏知识快讯NFR资讯新闻,与金色财经非小号巴比特星球前线Btc中国官网无关,本站资讯观点不作为投资依据,市场有风险,投资需谨慎!不提供社区论坛BBS微博微信交流群等相关币圈信息发布!
本站内容来源于互联网,如存在侵权及违规内容投诉邮箱( zztaobao@vip.qq.com )