110
|
1 package christie.blockchain;
|
|
2
|
|
3 import java.io.ByteArrayOutputStream;
|
|
4 import java.io.IOException;
|
|
5 import java.math.BigInteger;
|
|
6
|
|
7 // インターフェイスにしたほうがいいかもしれない. 後からdataの内容変える可能性がある.
|
|
8 public class Transaction {
|
|
9 private byte[] hash;
|
|
10
|
|
11 private long nonce;
|
|
12
|
|
13 private byte[] sendAddress;
|
|
14
|
|
15 private byte[] receiveAddress;
|
|
16
|
|
17 private byte[] data;
|
|
18
|
|
19 private long timestamp;
|
|
20
|
|
21 private byte[] signature;
|
|
22
|
|
23 //public ArrayList<TransactionInput> inputs = new ArrayList<TransactionInput>();
|
|
24 //public ArrayList<TransactionOutput> outputs = new ArrayList<TransactionOutput>();
|
|
25
|
|
26
|
|
27 public Transaction(byte[] sendAddress, byte[] receiveAddress, byte[] data){
|
|
28 this.sendAddress = sendAddress;
|
|
29 this.receiveAddress = receiveAddress;
|
|
30 this.data = data;
|
|
31
|
|
32 }
|
|
33 /*
|
|
34 public Transaction(byte[] from, byte[] to, byte[] value, ArrayList<TransactionInput> inputs) {
|
|
35 this.sendAddress = from;
|
|
36 this.receiveAddress = to;
|
|
37 this.value = value;
|
|
38 this.inputs = inputs;
|
|
39 }
|
|
40 */
|
|
41 public byte[] calcHash(){
|
|
42 HashUtil hashUtil = new HashUtil();
|
|
43
|
|
44 byte[] timestampByte = BigInteger.valueOf(this.timestamp).toByteArray();
|
|
45
|
|
46 byte[] nonceByte = BigInteger.valueOf(this.nonce).toByteArray();
|
|
47
|
|
48 ByteArrayOutputStream output = new ByteArrayOutputStream();
|
|
49
|
|
50 try {
|
|
51 output.write(hash);
|
|
52 output.write(timestampByte);
|
|
53 output.write(nonceByte);
|
|
54 } catch (IOException e) {
|
|
55 e.printStackTrace();
|
|
56 }
|
|
57
|
|
58 return hashUtil.sha256(output.toByteArray());
|
|
59
|
|
60 }
|
|
61
|
|
62 }
|