UNPKG

1.68 kBJavaScriptView Raw
1/**
2 * Block Header
3 * ============
4 *
5 * Every block contains a blockHeader. This is probably not something you will
6 * personally use, but it's here if you need it.
7 */
8'use strict'
9
10import { Bw } from './bw'
11import { Struct } from './struct'
12
13class BlockHeader extends Struct {
14 constructor (
15 versionBytesNum,
16 prevBlockHashBuf,
17 merkleRootBuf,
18 time,
19 bits,
20 nonce
21 ) {
22 super({
23 versionBytesNum,
24 prevBlockHashBuf,
25 merkleRootBuf,
26 time,
27 bits,
28 nonce
29 })
30 }
31
32 fromJSON (json) {
33 this.fromObject({
34 versionBytesNum: json.versionBytesNum,
35 prevBlockHashBuf: Buffer.from(json.prevBlockHashBuf, 'hex'),
36 merkleRootBuf: Buffer.from(json.merkleRootBuf, 'hex'),
37 time: json.time,
38 bits: json.bits,
39 nonce: json.nonce
40 })
41 return this
42 }
43
44 toJSON () {
45 return {
46 versionBytesNum: this.versionBytesNum,
47 prevBlockHashBuf: this.prevBlockHashBuf.toString('hex'),
48 merkleRootBuf: this.merkleRootBuf.toString('hex'),
49 time: this.time,
50 bits: this.bits,
51 nonce: this.nonce
52 }
53 }
54
55 fromBr (br) {
56 this.versionBytesNum = br.readUInt32LE()
57 this.prevBlockHashBuf = br.read(32)
58 this.merkleRootBuf = br.read(32)
59 this.time = br.readUInt32LE()
60 this.bits = br.readUInt32LE()
61 this.nonce = br.readUInt32LE()
62 return this
63 }
64
65 toBw (bw) {
66 if (!bw) {
67 bw = new Bw()
68 }
69 bw.writeUInt32LE(this.versionBytesNum)
70 bw.write(this.prevBlockHashBuf)
71 bw.write(this.merkleRootBuf)
72 bw.writeUInt32LE(this.time)
73 bw.writeUInt32LE(this.bits)
74 bw.writeUInt32LE(this.nonce)
75 return bw
76 }
77}
78
79export { BlockHeader }