-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.java
More file actions
185 lines (167 loc) · 5.52 KB
/
Copy pathBlock.java
File metadata and controls
185 lines (167 loc) · 5.52 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package core;
import java.math.BigInteger;
import java.security.Signature;
import java.util.ArrayList;
import util.Util;
public class Block {
/*
private int blockID;
private String previousBlockHash;
private int nonce;
private String data;
private ArrayList<Transaction> transactionList;
public int getBlockID() {
return blockID;
}
public void setBlockID(int blockID) {
this.blockID = blockID;
}
public int getNonce() {
return nonce;
}
public void setNonce(int nonce) {
this.nonce = nonce;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String getPreviousBlockHash() {
return previousBlockHash;
}
public void setPreviousBlockHash(String previousBlockHash) {
this.previousBlockHash = previousBlockHash;
}
public Block(int blockID, String previousBlockHash, int nonce, String data) {
this.blockID = blockID;
this.nonce = nonce;
this.data = data;
this.previousBlockHash = previousBlockHash;
}
public Block(int blockID, String previousBlockHash, int nonce, ArrayList transactionList) {
this.blockID = blockID;
this.nonce = nonce;
this.previousBlockHash = previousBlockHash;
this.transactionList = transactionList;
}
public void addTransaction(Transaction transaction) {
transactionList.add(transaction);
}
public void getInformation() {
System.out.println("----------------------------");
System.out.println("블록 번호 : "+getBlockID());
System.out.println("이전 해시 : "+getPreviousBlockHash());
System.out.println("채굴 변수 값 : "+getNonce());
System.out.println("블록 데이터 : "+ getData());
System.out.println("블록 해시 : " + getBlockHash());
System.out.println("----------------------------");
}
public void showInformation() {
System.out.println("----------------------------");
System.out.println("블록 번호 : "+getBlockID());
System.out.println("이전 해시 : "+getPreviousBlockHash());
System.out.println("채굴 변수 값 : "+getNonce());
System.out.println("트랜잭션 개수 : "+transactionList.size() + "개");
for (int i = 0; i < transactionList.size(); i++) {
System.out.println(transactionList.get(i).getInformation());
}
System.out.println("블록 해시 : " + getBlockHash());
System.out.println("----------------------------");
}
public String getBlockHash() {
// return Util.getHash(nonce+data+previousBlockHash);
String transactionInformations = "";
for (int i = 0; i < transactionList.size(); i++) {
transactionInformations += transactionList.get(i).getInformation();
}
return Util.getHash(nonce+transactionInformations+previousBlockHash);
}
public void mine() {
while(true) {
if(getBlockHash().substring(0,4).equals("0000")) {
System.out.println(blockID+"번째 블록의 채굴에 성공했습니다.");
break;
}
nonce++;
}
}
*/
// 9강
private static final String ALGORITHM = "SHA1withECDSA";
private int blockID;
private String previousBlockHash;
private int nonce;
private ArrayList<Transaction> transactionList;
public int getBlockID() {
return blockID;
}
public void setBlockID(int blockID) {
this.blockID = blockID;
}
public String getPreviousBlockHash() {
return previousBlockHash;
}
public void setPreviousBlockHash(String previousBlockHash) {
this.previousBlockHash = previousBlockHash;
}
public int getNonce() {
return nonce;
}
public void setNonce(int nonce) {
this.nonce = nonce;
}
public Block(int blockID, String previousBlockHash, int nonce, ArrayList<Transaction> transactionList) {
this.blockID = blockID;
this.previousBlockHash = previousBlockHash;
this.nonce = nonce;
this.transactionList = transactionList;
}
// 특정한 트랜잭션이 정상적인지 검증합니다.
private boolean verifyTransaction(Transaction transaction) throws Exception {
Signature signature;
signature = Signature.getInstance(ALGORITHM);
byte[] baText = transaction.getData().getBytes("UTF-8");
signature.initVerify(transaction.getSender());
signature.update(baText);
return signature.verify(new BigInteger(transaction.getSignature(), 16).toByteArray());
}
// 정상적인 트랜잭션에 한해서 블록에 추가합니다.
public void addTransaction(Transaction transaction) throws Exception{
if(verifyTransaction(transaction)) {
System.out.println("정상적인 트랜잭션을 발견했습니다");
transactionList.add(transaction);
} else {
System.out.println("트랜잭션이 바르게 인증되지 않았습니");
}
}
public void showInformation() {
System.out.println("----------------------------");
System.out.println("블록 번호 : "+getBlockID());
System.out.println("이전 해시 : "+getPreviousBlockHash());
System.out.println("채굴 변수 값 : "+getNonce());
System.out.println("트랜잭션 개수 : "+transactionList.size() + "개");
for (int i = 0; i < transactionList.size(); i++) {
System.out.println(transactionList.get(i).getInformation());
}
System.out.println("블록 해시 : " + getBlockHash());
System.out.println("----------------------------");
}
public String getBlockHash() {
String transactionInformations = "";
for (int i = 0; i < transactionList.size(); i++) {
transactionInformations += transactionList.get(i).getInformation();
}
return Util.getHash(nonce+transactionInformations+previousBlockHash);
}
public void mine() {
while(true) {
if(getBlockHash().substring(0, 4).equals("0000")) {
System.out.println(blockID + "번째 블록의 채굴에 성공했습니다.");
break;
}
nonce++;
}
}
}