Blockchain
如何檢查比特幣塊的雜湊?
讓我們選擇一個範例比特幣塊:#499583
它開始於
00000020164a1e4a7f34b96b0e201d....
我正在嘗試對其進行雜湊處理,以便
000000000000000000677c4077da7c9f01dde5f332ba2fbff962ee699714d5da
再次進行雜湊處理。這是我的 Javascript 程式碼:var Bitcoin = require('bitcoinjs-lib'); var request = require('request'); var crypto = require('crypto'); function getRawBlock(blockHash) { return new Promise((resolve, reject) => { request('https://insight.bitpay.com/api/rawblock/' + blockHash, // hitting an insight API to get the full block (error, response, body) => { try { var block = JSON.parse(body); // result is in JSON resolve(block.rawblock) } catch (error) { reject(error) } }) }) } getRawBlock('000000000000000000677c4077da7c9f01dde5f332ba2fbff962ee699714d5da') .then((rawBlock) => { var hash = crypto.createHash('sha256').update(rawBlock).digest('hex'); console.log(hash); }) })
但結果是
484cd10be70dbc7615dd9a71b1f91375b100715d9b2a0ecc6a05b9d247a8cda9
。我究竟做錯了什麼?結果應該是000000000000000000677c4077da7c9f01dde5f332ba2fbff962ee699714d5da
。我是否以錯誤的格式散列某些內容?
在我看來,您正在對整個塊進行散列而不是僅對標頭進行散列。拿我的程式碼(在 C++ 中)。
void test ( ) { const MyByteArray header ( QByteArray::fromHex ( "00000020" // version "164a1e4a7f34b96b0e201dcc6a623c63fe3874696e4875000000000000000000" // prev hash "49de8b4f4bfa9fc890d3d28a93156a111f891dc680090cd497b58a7d5c2b09cf" // merkle "2f62345a" // timestamp "edb00018" // bits "ffdfd257" ) ); // nonce const MyKey32 key ( header.sha256d ( ) ); qDebug ( ) << key.toString ( ); }
輸出是:
000000000000000000677c4077da7c9f01dde5f332ba2fbff962ee699714d5da