109
|
1 package christie.blockchain;
|
|
2
|
110
|
3 import org.bouncycastle.util.BigIntegers;
|
|
4
|
109
|
5 import java.io.ByteArrayOutputStream;
|
|
6 import java.io.IOException;
|
|
7 import java.math.BigInteger;
|
|
8 import java.nio.charset.Charset;
|
|
9
|
|
10 public class BlockHeader {
|
|
11
|
|
12 private byte[] presentHash;
|
|
13
|
|
14 private byte[] parentHash;
|
|
15
|
139
|
16 private long difficulty;
|
|
17
|
|
18 private long number;
|
|
19
|
109
|
20 private long timestamp;
|
|
21
|
|
22 private long nonce;
|
|
23
|
110
|
24
|
139
|
25 public BlockHeader(byte[] parentHash, long difficulty, long number, long timestamp) {
|
109
|
26 this.parentHash = parentHash;
|
139
|
27 this.difficulty = difficulty;
|
|
28 this.number = number;
|
109
|
29 this.timestamp = timestamp;
|
|
30 }
|
|
31
|
146
|
32 public byte[] getParamByteArray(boolean withNonce){
|
110
|
33
|
|
34 ByteArrayOutputStream output = new ByteArrayOutputStream();
|
109
|
35
|
|
36 byte[] timestampByte = BigInteger.valueOf(this.timestamp).toByteArray();
|
|
37
|
|
38 try {
|
|
39 output.write(parentHash);
|
|
40 output.write(timestampByte);
|
|
41 } catch (IOException e) {
|
|
42 e.printStackTrace();
|
|
43 }
|
|
44
|
110
|
45 if(withNonce){
|
|
46 byte[] nonceByte = BigInteger.valueOf(this.nonce).toByteArray();
|
|
47 try {
|
|
48 output.write(nonceByte);
|
|
49 } catch (IOException e) {
|
|
50 e.printStackTrace();
|
|
51 }
|
|
52 }
|
|
53
|
|
54 return output.toByteArray();
|
109
|
55
|
|
56 }
|
|
57
|
|
58 public byte[] getParentHash() {
|
146
|
59
|
109
|
60 return parentHash;
|
|
61 }
|
|
62
|
|
63 public byte[] getPresentHash() {
|
146
|
64
|
156
|
65 return HashUtil.sha256(getParamByteArray(true));
|
109
|
66 }
|
|
67
|
146
|
68 public byte[] getByteArrayWithoutNonce(){
|
|
69
|
|
70 return getParamByteArray(false);
|
110
|
71 }
|
109
|
72
|
139
|
73 public void setTimestamp(long timestamp) {
|
|
74 this.timestamp = timestamp;
|
|
75 }
|
|
76
|
109
|
77 public long getTimestamp() {
|
|
78 return timestamp;
|
|
79 }
|
|
80
|
139
|
81 public void setNonce(long nonce) {
|
|
82 this.nonce = nonce;
|
109
|
83 }
|
|
84
|
|
85 public long getNonce(){
|
|
86 return this.nonce;
|
|
87 }
|
|
88
|
139
|
89 public void setNumber(long number) {
|
|
90 this.number = number;
|
|
91 }
|
|
92
|
|
93 public long getNumber() {
|
|
94 return number;
|
|
95 }
|
|
96
|
|
97 public void setDifficulty(long difficulty) {
|
|
98 this.difficulty = difficulty;
|
|
99 }
|
|
100
|
|
101 public long getDifficulty() {
|
|
102 return difficulty;
|
|
103 }
|
109
|
104 }
|