-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.js
More file actions
120 lines (93 loc) · 2.57 KB
/
block.js
File metadata and controls
120 lines (93 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var assert = require('assert')
var bufferutils = require('./bufferutils')
var crypto = require('./crypto')
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) {
assert(buffer.length >= 80, '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
}
// FIXME: poor performance
function readTransaction () {
var tx = Transaction.fromBuffer(buffer.slice(offset), true)
offset += tx.toBuffer().length
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 crypto.hash256(this.toBuffer(true))
}
Block.prototype.getId = function () {
return bufferutils.reverse(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')
}
module.exports = Block