2014-10-16 06:30:57 +02:00
|
|
|
var bufferutils = require('./bufferutils')
|
|
|
|
var crypto = require('./crypto')
|
|
|
|
|
|
|
|
var Transaction = require('./transaction')
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
function readVarInt () {
|
2014-10-16 06:30:57 +02:00
|
|
|
var vi = bufferutils.readVarInt(buffer, offset)
|
|
|
|
offset += vi.size
|
|
|
|
return vi.number
|
|
|
|
}
|
|
|
|
|
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)
|
2014-10-16 06:30:57 +02:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
Block.fromHex = function (hex) {
|
2014-10-16 06:30:57 +02:00
|
|
|
return Block.fromBuffer(new Buffer(hex, 'hex'))
|
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
Block.prototype.getHash = function () {
|
2014-11-28 00:30:07 +01:00
|
|
|
return crypto.hash256(this.toBuffer(true))
|
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
Block.prototype.getId = function () {
|
2014-11-28 00:30:07 +01:00
|
|
|
return bufferutils.reverse(this.getHash()).toString('hex')
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
Block.prototype.toBuffer = function (headersOnly) {
|
2014-10-16 06:30:57 +02:00
|
|
|
var buffer = new Buffer(80)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
writeUInt32(this.version)
|
|
|
|
writeSlice(this.prevHash)
|
|
|
|
writeSlice(this.merkleRoot)
|
|
|
|
writeUInt32(this.timestamp)
|
|
|
|
writeUInt32(this.bits)
|
|
|
|
writeUInt32(this.nonce)
|
|
|
|
|
|
|
|
if (headersOnly || !this.transactions) return buffer
|
|
|
|
|
2014-10-17 04:07:14 +02:00
|
|
|
var txLenBuffer = bufferutils.varIntBuffer(this.transactions.length)
|
2015-02-23 00:36:57 +01:00
|
|
|
var txBuffers = this.transactions.map(function (tx) {
|
2014-10-16 06:30:57 +02:00
|
|
|
return tx.toBuffer()
|
|
|
|
})
|
|
|
|
|
|
|
|
return Buffer.concat([buffer, txLenBuffer].concat(txBuffers))
|
|
|
|
}
|
|
|
|
|
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')
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Block
|