173 lines
4 KiB
JavaScript
173 lines
4 KiB
JavaScript
var createHash = require('create-hash')
|
|
var bufferutils = require('./bufferutils')
|
|
var bcrypto = require('./crypto')
|
|
var bufferCompare = require('buffer-compare')
|
|
var bufferReverse = require('buffer-reverse')
|
|
|
|
var Transaction = require('./transaction')
|
|
|
|
function Block () {
|
|
this.version = 1
|
|
this.prevHash = null
|
|
this.merkleRoot = null
|
|
this.timestamp = 0
|
|
this.bits = 0
|
|
this.nonce = 0
|
|
}
|
|
|
|
Block.fromBuffer = function (buffer) {
|
|
if (buffer.length < 80) throw new Error('Buffer too small (< 80 bytes)')
|
|
|
|
var offset = 0
|
|
function readSlice (n) {
|
|
offset += n
|
|
return buffer.slice(offset - n, offset)
|
|
}
|
|
|
|
function readUInt32 () {
|
|
var i = buffer.readUInt32LE(offset)
|
|
offset += 4
|
|
return i
|
|
}
|
|
|
|
var block = new Block()
|
|
block.version = readUInt32()
|
|
block.prevHash = readSlice(32)
|
|
block.merkleRoot = readSlice(32)
|
|
block.timestamp = readUInt32()
|
|
block.bits = readUInt32()
|
|
block.nonce = readUInt32()
|
|
|
|
if (buffer.length === 80) return block
|
|
|
|
function readVarInt () {
|
|
var vi = bufferutils.readVarInt(buffer, offset)
|
|
offset += vi.size
|
|
return vi.number
|
|
}
|
|
|
|
function readTransaction () {
|
|
var tx = Transaction.fromBuffer(buffer.slice(offset), true)
|
|
|
|
offset += tx.byteLength()
|
|
return tx
|
|
}
|
|
|
|
var nTransactions = readVarInt()
|
|
block.transactions = []
|
|
|
|
for (var i = 0; i < nTransactions; ++i) {
|
|
var tx = readTransaction()
|
|
block.transactions.push(tx)
|
|
}
|
|
|
|
return block
|
|
}
|
|
|
|
Block.fromHex = function (hex) {
|
|
return Block.fromBuffer(new Buffer(hex, 'hex'))
|
|
}
|
|
|
|
Block.prototype.getHash = function () {
|
|
return bcrypto.hash256(this.toBuffer(true))
|
|
}
|
|
|
|
Block.prototype.getId = function () {
|
|
return bufferReverse(this.getHash()).toString('hex')
|
|
}
|
|
|
|
Block.prototype.getUTCDate = function () {
|
|
var date = new Date(0) // epoch
|
|
date.setUTCSeconds(this.timestamp)
|
|
|
|
return date
|
|
}
|
|
|
|
Block.prototype.toBuffer = function (headersOnly) {
|
|
var buffer = new Buffer(80)
|
|
|
|
var offset = 0
|
|
function writeSlice (slice) {
|
|
slice.copy(buffer, offset)
|
|
offset += slice.length
|
|
}
|
|
|
|
function writeUInt32 (i) {
|
|
buffer.writeUInt32LE(i, offset)
|
|
offset += 4
|
|
}
|
|
|
|
writeUInt32(this.version)
|
|
writeSlice(this.prevHash)
|
|
writeSlice(this.merkleRoot)
|
|
writeUInt32(this.timestamp)
|
|
writeUInt32(this.bits)
|
|
writeUInt32(this.nonce)
|
|
|
|
if (headersOnly || !this.transactions) return buffer
|
|
|
|
var txLenBuffer = bufferutils.varIntBuffer(this.transactions.length)
|
|
var txBuffers = this.transactions.map(function (tx) {
|
|
return tx.toBuffer()
|
|
})
|
|
|
|
return Buffer.concat([buffer, txLenBuffer].concat(txBuffers))
|
|
}
|
|
|
|
Block.prototype.toHex = function (headersOnly) {
|
|
return this.toBuffer(headersOnly).toString('hex')
|
|
}
|
|
|
|
Block.calculateTarget = function (bits) {
|
|
var exponent = ((bits & 0xff000000) >> 24) - 3
|
|
var mantissa = bits & 0x007fffff
|
|
var i = 31 - exponent
|
|
|
|
var target = new Buffer(32)
|
|
target.fill(0)
|
|
|
|
target[i] = mantissa & 0xff
|
|
target[i - 1] = mantissa >> 8
|
|
target[i - 2] = mantissa >> 16
|
|
target[i - 3] = mantissa >> 24
|
|
|
|
return target
|
|
}
|
|
|
|
Block.calculateMerkleRoot = function (transactions) {
|
|
var length = transactions.length
|
|
if (length === 0) throw TypeError('Cannot compute merkle root for zero transactions')
|
|
|
|
var hashes = transactions.map(function (transaction) { return transaction.getHash() })
|
|
|
|
while (length > 1) {
|
|
var j = 0
|
|
|
|
for (var i = 0; i < length; i += 2, ++j) {
|
|
var hasher = createHash('sha256')
|
|
hasher.update(hashes[i])
|
|
hasher.update(i + 1 !== length ? hashes[i + 1] : hashes[i])
|
|
hashes[j] = bcrypto.sha256(hasher.digest())
|
|
}
|
|
|
|
length = j
|
|
}
|
|
|
|
return hashes[0]
|
|
}
|
|
|
|
Block.prototype.checkMerkleRoot = function () {
|
|
if (!this.transactions) return false
|
|
|
|
var actualMerkleRoot = Block.calculateMerkleRoot(this.transactions)
|
|
return bufferCompare(this.merkleRoot, actualMerkleRoot) === 0
|
|
}
|
|
|
|
Block.prototype.checkProofOfWork = function () {
|
|
var hash = bufferReverse(this.getHash())
|
|
var target = Block.calculateTarget(this.bits)
|
|
|
|
return bufferCompare(hash, target) <= 0
|
|
}
|
|
|
|
module.exports = Block
|