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
|
|
16 private long timestamp;
|
|
17
|
|
18 private long nonce;
|
|
19
|
110
|
20 HashUtil hashUtil = new HashUtil();
|
|
21
|
109
|
22 public BlockHeader(byte[] parentHash, long timestamp) {
|
|
23 this.parentHash = parentHash;
|
|
24 this.timestamp = timestamp;
|
|
25 }
|
|
26
|
110
|
27 public byte[] getHash(boolean withNonce){
|
|
28
|
|
29 ByteArrayOutputStream output = new ByteArrayOutputStream();
|
109
|
30
|
|
31 byte[] timestampByte = BigInteger.valueOf(this.timestamp).toByteArray();
|
|
32
|
|
33 try {
|
|
34 output.write(parentHash);
|
|
35 output.write(timestampByte);
|
|
36 } catch (IOException e) {
|
|
37 e.printStackTrace();
|
|
38 }
|
|
39
|
110
|
40 if(withNonce){
|
|
41 byte[] nonceByte = BigInteger.valueOf(this.nonce).toByteArray();
|
|
42 try {
|
|
43 output.write(nonceByte);
|
|
44 } catch (IOException e) {
|
|
45 e.printStackTrace();
|
|
46 }
|
|
47 }
|
|
48
|
|
49 return output.toByteArray();
|
109
|
50
|
|
51 }
|
|
52
|
|
53 public byte[] getParentHash() {
|
|
54 return parentHash;
|
|
55 }
|
|
56
|
|
57 public byte[] getPresentHash() {
|
110
|
58 return hashUtil.sha256(getHash(true));
|
109
|
59 }
|
|
60
|
110
|
61 public byte[] getPresentHashWithoutNonce(){
|
|
62 return getHash(false);
|
|
63 }
|
109
|
64
|
|
65 public long getTimestamp() {
|
|
66 return timestamp;
|
|
67 }
|
|
68
|
|
69 public void setTimestamp(long timestamp) {
|
|
70 this.timestamp = timestamp;
|
|
71 }
|
|
72
|
110
|
73 public void setNonce(long nonce) { this.nonce = nonce; }
|
109
|
74
|
|
75 public long getNonce(){
|
|
76 return this.nonce;
|
|
77 }
|
|
78
|
|
79 }
|