2018-06-25 08:25:12 +02:00
|
|
|
const Buffer = require('safe-buffer').Buffer
|
|
|
|
const bcrypto = require('./crypto')
|
|
|
|
const fastMerkleRoot = require('merkle-lib/fastRoot')
|
|
|
|
const typeforce = require('typeforce')
|
|
|
|
const types = require('./types')
|
|
|
|
const varuint = require('varuint-bitcoin')
|
|
|
|
|
|
|
|
const Transaction = require('./transaction')
|
2014-10-16 06:30:57 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
function Block () {
|
2014-10-16 06:30:57 +02:00
|
|
|
this.version = 1
|
|
|
|
this.prevHash = null
|
|
|
|
this.merkleRoot = null
|
|
|
|
this.timestamp = 0
|
|
|
|
this.bits = 0
|
|
|
|
this.nonce = 0
|
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
Block.fromBuffer = function (buffer) {
|
2015-08-11 09:03:46 +02:00
|
|
|
if (buffer.length < 80) throw new Error('Buffer too small (< 80 bytes)')
|
2014-10-16 06:30:57 +02:00
|
|
|
|
|
|
|
var offset = 0
|
2015-02-23 00:36:57 +01:00
|
|
|
function readSlice (n) {
|
2014-10-16 06:30:57 +02:00
|
|
|
offset += n
|
|
|
|
return buffer.slice(offset - n, offset)
|
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
function readUInt32 () {
|
2014-10-16 06:30:57 +02:00
|
|
|
var i = buffer.readUInt32LE(offset)
|
|
|
|
offset += 4
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2016-10-12 16:04:03 +02:00
|
|
|
function readInt32 () {
|
|
|
|
var i = buffer.readInt32LE(offset)
|
|
|
|
offset += 4
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2014-10-16 06:30:57 +02:00
|
|
|
var block = new Block()
|
2016-10-12 16:04:03 +02:00
|
|
|
block.version = readInt32()
|
2014-10-16 06:30:57 +02:00
|
|
|
block.prevHash = readSlice(32)
|
|
|
|
block.merkleRoot = readSlice(32)
|
|
|
|
block.timestamp = readUInt32()
|
|
|
|
block.bits = readUInt32()
|
|
|
|
block.nonce = readUInt32()
|
|
|
|
|
|
|
|
if (buffer.length === 80) return block
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
function readVarInt () {
|
2017-04-19 06:41:59 +02:00
|
|
|
var vi = varuint.decode(buffer, offset)
|
|
|
|
offset += varuint.decode.bytes
|
|
|
|
return vi
|
2014-10-16 06:30:57 +02:00
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
function readTransaction () {
|
2015-02-19 08:59:56 +01:00
|
|
|
var tx = Transaction.fromBuffer(buffer.slice(offset), true)
|
2015-08-14 00:23:23 +02:00
|
|
|
offset += tx.byteLength()
|
2014-10-16 06:30:57 +02:00
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
|
|
|
var nTransactions = readVarInt()
|
|
|
|
block.transactions = []
|
|
|
|
|
|
|
|
for (var i = 0; i < nTransactions; ++i) {
|
|
|
|
var tx = readTransaction()
|
|
|
|
block.transactions.push(tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return block
|
|
|
|
}
|
|
|
|
|
2017-04-18 03:56:35 +02:00
|
|
|
Block.prototype.byteLength = function (headersOnly) {
|
|
|
|
if (headersOnly || !this.transactions) return 80
|
|
|
|
|
|
|
|
return 80 + varuint.encodingLength(this.transactions.length) + this.transactions.reduce(function (a, x) {
|
|
|
|
return a + x.byteLength()
|
|
|
|
}, 0)
|
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
Block.fromHex = function (hex) {
|
2017-04-19 09:24:58 +02:00
|
|
|
return Block.fromBuffer(Buffer.from(hex, 'hex'))
|
2014-10-16 06:30:57 +02:00
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
Block.prototype.getHash = function () {
|
2015-08-20 05:37:19 +02:00
|
|
|
return bcrypto.hash256(this.toBuffer(true))
|
2014-11-28 00:30:07 +01:00
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
Block.prototype.getId = function () {
|
2016-12-21 02:52:09 +01:00
|
|
|
return this.getHash().reverse().toString('hex')
|
2014-11-28 00:30:07 +01:00
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
Block.prototype.getUTCDate = function () {
|
2014-11-28 00:30:07 +01:00
|
|
|
var date = new Date(0) // epoch
|
|
|
|
date.setUTCSeconds(this.timestamp)
|
|
|
|
|
|
|
|
return date
|
|
|
|
}
|
|
|
|
|
2017-04-18 03:56:35 +02:00
|
|
|
// TODO: buffer, offset compatibility
|
2015-02-23 00:36:57 +01:00
|
|
|
Block.prototype.toBuffer = function (headersOnly) {
|
2017-04-19 09:24:58 +02:00
|
|
|
var buffer = Buffer.allocUnsafe(this.byteLength(headersOnly))
|
2014-10-16 06:30:57 +02:00
|
|
|
|
|
|
|
var offset = 0
|
2015-02-23 00:36:57 +01:00
|
|
|
function writeSlice (slice) {
|
2014-10-16 06:30:57 +02:00
|
|
|
slice.copy(buffer, offset)
|
|
|
|
offset += slice.length
|
|
|
|
}
|
|
|
|
|
2016-10-12 16:04:03 +02:00
|
|
|
function writeInt32 (i) {
|
|
|
|
buffer.writeInt32LE(i, offset)
|
|
|
|
offset += 4
|
|
|
|
}
|
2015-02-23 00:36:57 +01:00
|
|
|
function writeUInt32 (i) {
|
2014-10-16 06:30:57 +02:00
|
|
|
buffer.writeUInt32LE(i, offset)
|
|
|
|
offset += 4
|
|
|
|
}
|
|
|
|
|
2016-10-12 16:04:03 +02:00
|
|
|
writeInt32(this.version)
|
2014-10-16 06:30:57 +02:00
|
|
|
writeSlice(this.prevHash)
|
|
|
|
writeSlice(this.merkleRoot)
|
|
|
|
writeUInt32(this.timestamp)
|
|
|
|
writeUInt32(this.bits)
|
|
|
|
writeUInt32(this.nonce)
|
|
|
|
|
|
|
|
if (headersOnly || !this.transactions) return buffer
|
|
|
|
|
2017-04-18 03:56:35 +02:00
|
|
|
varuint.encode(this.transactions.length, buffer, offset)
|
|
|
|
offset += varuint.encode.bytes
|
|
|
|
|
|
|
|
this.transactions.forEach(function (tx) {
|
|
|
|
var txSize = tx.byteLength() // TODO: extract from toBuffer?
|
|
|
|
tx.toBuffer(buffer, offset)
|
|
|
|
offset += txSize
|
2014-10-16 06:30:57 +02:00
|
|
|
})
|
|
|
|
|
2017-04-18 03:56:35 +02:00
|
|
|
return buffer
|
2014-10-16 06:30:57 +02:00
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
Block.prototype.toHex = function (headersOnly) {
|
2014-10-16 06:30:57 +02:00
|
|
|
return this.toBuffer(headersOnly).toString('hex')
|
|
|
|
}
|
|
|
|
|
2015-12-09 03:35:29 +01:00
|
|
|
Block.calculateTarget = function (bits) {
|
|
|
|
var exponent = ((bits & 0xff000000) >> 24) - 3
|
2015-12-09 22:02:58 +01:00
|
|
|
var mantissa = bits & 0x007fffff
|
2017-04-19 09:24:58 +02:00
|
|
|
var target = Buffer.alloc(32, 0)
|
2017-03-28 14:10:57 +02:00
|
|
|
target.writeUInt32BE(mantissa, 28 - exponent)
|
2015-12-09 22:02:58 +01:00
|
|
|
return target
|
2015-12-09 03:35:29 +01:00
|
|
|
}
|
|
|
|
|
2016-05-04 16:42:05 +02:00
|
|
|
Block.calculateMerkleRoot = function (transactions) {
|
2016-10-08 03:08:07 +02:00
|
|
|
typeforce([{ getHash: types.Function }], transactions)
|
2016-10-07 08:03:12 +02:00
|
|
|
if (transactions.length === 0) throw TypeError('Cannot compute merkle root for zero transactions')
|
2016-05-04 16:42:05 +02:00
|
|
|
|
2016-10-07 08:03:12 +02:00
|
|
|
var hashes = transactions.map(function (transaction) {
|
|
|
|
return transaction.getHash()
|
|
|
|
})
|
2016-05-04 16:42:05 +02:00
|
|
|
|
2016-10-07 08:03:12 +02:00
|
|
|
return fastMerkleRoot(hashes, bcrypto.hash256)
|
2016-05-04 16:42:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Block.prototype.checkMerkleRoot = function () {
|
|
|
|
if (!this.transactions) return false
|
|
|
|
|
|
|
|
var actualMerkleRoot = Block.calculateMerkleRoot(this.transactions)
|
2016-10-06 12:30:35 +02:00
|
|
|
return this.merkleRoot.compare(actualMerkleRoot) === 0
|
2016-05-04 16:42:05 +02:00
|
|
|
}
|
|
|
|
|
2016-01-04 02:49:28 +01:00
|
|
|
Block.prototype.checkProofOfWork = function () {
|
2016-12-21 02:52:09 +01:00
|
|
|
var hash = this.getHash().reverse()
|
2015-12-11 02:39:09 +01:00
|
|
|
var target = Block.calculateTarget(this.bits)
|
2015-12-08 08:51:35 +01:00
|
|
|
|
2016-10-06 12:30:35 +02:00
|
|
|
return hash.compare(target) <= 0
|
2015-12-08 08:51:35 +01:00
|
|
|
}
|
|
|
|
|
2014-10-16 06:30:57 +02:00
|
|
|
module.exports = Block
|