-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransaction.java
More file actions
107 lines (88 loc) · 3.2 KB
/
Transaction.java
File metadata and controls
107 lines (88 loc) · 3.2 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
package org.example;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.util.Base64;
public class Transaction {
private final String senderPublicKey;
private final String senderName;
private final String receiverName;
private final String recipientPublicKey;
private final double amount;
private final String previousTransactionHash;
private final PrivateKey senderPrivateKey;
private final User sender;
private final User receiver;
private final String transactionHash;
public User getSender() {
return sender;
}
public User getReceiver() {
return receiver;
}
private final String signature;
public Transaction(User sender, User recipient, double amount, String previousTransactionHash, PrivateKey senderPrivateKey) {
this.senderPublicKey = publicKeyToString(sender.getPublicKey());
this.recipientPublicKey = publicKeyToString(recipient.getPublicKey());
this.senderName = sender.getUsername();
this.receiverName = recipient.getUsername();
this.amount = amount;
this.previousTransactionHash = previousTransactionHash;
this.senderPrivateKey = senderPrivateKey;
this.sender = sender;
this.receiver = recipient;
this.transactionHash = calculateTransactionHash();
this.signature = signTransactionHash();
}
private String calculateTransactionHash() {
String data = previousTransactionHash + recipientPublicKey + amount;
return MerkleTree.calculateHash(data);
}
private String signTransactionHash() {
try {
Signature ecdsaSign = Signature.getInstance("SHA256withECDSA");
ecdsaSign.initSign(senderPrivateKey);
ecdsaSign.update(transactionHash.getBytes());
byte[] signatureBytes = ecdsaSign.sign();
return Base64.getEncoder().encodeToString(signatureBytes);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public String getSenderPublicKey() {
return senderPublicKey;
}
public String getRecipientPublicKey() {
return recipientPublicKey;
}
public double getAmount() {
return amount;
}
public String getPreviousTransactionHash() {
return previousTransactionHash;
}
public String getTransactionHash() {
return transactionHash;
}
public String getSignature() {
return signature;
}
public String getSenderName() {
return senderName;
}
public String getReceiverName() {
return receiverName;
}
private String publicKeyToString(PublicKey publicKey) {
return Base64.getEncoder().encodeToString(publicKey.getEncoded());
}
@Override
public String toString() {
return "Transaction{" +
"amount=" + amount +
", recipientPublicKey='" + recipientPublicKey + '\'' +
", transactionHash='" + transactionHash + '\'' +
", signature='" + signature + '\'' +
'}';
}
}