如何在超级账本结构的视图页面上使用节点js显示当前哈希,先前哈希 [英] how to display current hash,previous hash using node js on view pages for hyperledger fabric

查看:37
本文介绍了如何在超级账本结构的视图页面上使用节点js显示当前哈希,先前哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试像链表一样遍历区块链.我只能通过提供的块号从该块中找到以前的哈希,如何使用节点js获取块的当前哈希,我有兴趣在视图页面上显示.

I am trying to traverse blockchain like the linked list. I am finding only previous hash from the block by provided block no., how to get the current hash of a block using node js and I'm interested to display on view pages.

推荐答案

节点sdk不提供当前哈希值,但可以使用类似于以下代码的代码从块头中计算出来.

The node sdk does not provide the current hash but can calculate it from the block's header using code similar to this.

var sha = require('js-sha256');
var asn = require('asn1.js');
var calculateBlockHash = function(header) {
  let headerAsn = asn.define('headerAsn', function() {
    this.seq().obj(
      this.key('Number').int(),
      this.key('PreviousHash').octstr(),
     this.key('DataHash').octstr()
   );
 });

  let output = headerAsn.encode({
      Number: parseInt(header.number),
      PreviousHash: Buffer.from(header.previous_hash, 'hex'),
      DataHash: Buffer.from(header.data_hash, 'hex')
    }, 'der');
  let hash = sha.sha256(output);
  return hash;
};

这篇关于如何在超级账本结构的视图页面上使用节点js显示当前哈希,先前哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆