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 HashUtil hashUtil = new HashUtil();
|
|
25
|
139
|
26 public BlockHeader(byte[] parentHash, long difficulty, long number, long timestamp) {
|
109
|
27 this.parentHash = parentHash;
|
139
|
28 this.difficulty = difficulty;
|
|
29 this.number = number;
|
109
|
30 this.timestamp = timestamp;
|
|
31 }
|
|
32
|
146
|
33 public byte[] getParamByteArray(boolean withNonce){
|
110
|
34
|
|
35 ByteArrayOutputStream output = new ByteArrayOutputStream();
|
109
|
36
|
|
37 byte[] timestampByte = BigInteger.valueOf(this.timestamp).toByteArray();
|
|
38
|
|
39 try {
|
|
40 output.write(parentHash);
|
|
41 output.write(timestampByte);
|
|
42 } catch (IOException e) {
|
|
43 e.printStackTrace();
|
|
44 }
|
|
45
|
110
|
46 if(withNonce){
|
|
47 byte[] nonceByte = BigInteger.valueOf(this.nonce).toByteArray();
|
|
48 try {
|
|
49 output.write(nonceByte);
|
|
50 } catch (IOException e) {
|
|
51 e.printStackTrace();
|
|
52 }
|
|
53 }
|
|
54
|
|
55 return output.toByteArray();
|
109
|
56
|
|
57 }
|
|
58
|
|
59 public byte[] getParentHash() {
|
146
|
60
|
109
|
61 return parentHash;
|
|
62 }
|
|
63
|
|
64 public byte[] getPresentHash() {
|
146
|
65
|
|
66 return hashUtil.sha256(getParamByteArray(true));
|
109
|
67 }
|
|
68
|
146
|
69 public byte[] getByteArrayWithoutNonce(){
|
|
70
|
|
71 return getParamByteArray(false);
|
110
|
72 }
|
109
|
73
|
139
|
74 public void setTimestamp(long timestamp) {
|
|
75 this.timestamp = timestamp;
|
|
76 }
|
|
77
|
109
|
78 public long getTimestamp() {
|
|
79 return timestamp;
|
|
80 }
|
|
81
|
139
|
82 public void setNonce(long nonce) {
|
|
83 this.nonce = nonce;
|
109
|
84 }
|
|
85
|
|
86 public long getNonce(){
|
|
87 return this.nonce;
|
|
88 }
|
|
89
|
139
|
90 public void setNumber(long number) {
|
|
91 this.number = number;
|
|
92 }
|
|
93
|
|
94 public long getNumber() {
|
|
95 return number;
|
|
96 }
|
|
97
|
|
98 public void setDifficulty(long difficulty) {
|
|
99 this.difficulty = difficulty;
|
|
100 }
|
|
101
|
|
102 public long getDifficulty() {
|
|
103 return difficulty;
|
|
104 }
|
109
|
105 }
|