首页 > 解决方案 > 两个 TX 的双重哈希

问题描述

我正在寻找双重哈希两个 tx,以构建merkle 树

const bsv = require("bsv");
var tx1 = '3a459eab5f0cf8394a21e04d2ed3b2beeaa59795912e20b9c680e9db74dfb18c';
var tx2 = 'be38f46f0eccba72416aed715851fd07b881ffb7928b7622847314588e06a6b7';

bsv.crypto.Hash.sha256sha256(Buffer.concat(
    [ tx1, tx2 ].map( v => Buffer.from(v, 'hex') )
)).toString('hex');

在给我

215f8397a3090a0bc8f4a2e98609a10d55fc7b939fa1ecf9803df20b1ee089a2

但应该是

13a3595f2610c8e4d727130daade66c772fdec4bd2463d773fd0f85c20ced32d

我怎样才能得到正确的结果?

标签: javascriptnode.jsbitcoin

解决方案


正如所指出的,您需要先将 tx 散列转换为小端,然后再使用concat()双散列。

由于您使用Buffer它可以有效地完成.reverse()

const bsv = require("bsv");
var tx1 = '3a459eab5f0cf8394a21e04d2ed3b2beeaa59795912e20b9c680e9db74dfb18c';
var tx2 = 'be38f46f0eccba72416aed715851fd07b881ffb7928b7622847314588e06a6b7';

bsv.crypto.Hash.sha256sha256(Buffer.concat(
    [ tx1, tx2 ].map( v => Buffer.from(v, 'hex').reverse() )
)).reverse().toString('hex');

推荐阅读