Bladeren bron

Please work

Arin Turpin 6 jaren geleden
bovenliggende
commit
6bc3cd6c45

+ 24
- 55
ZipCoinRedJavaServer/src/main/java/com/zipcoin/model/Transaction.java Bestand weergeven

@@ -13,81 +13,50 @@ public class Transaction {
13 13
     @Id
14 14
     @GeneratedValue(strategy = GenerationType.AUTO)
15 15
     // private String transactionId;
16
-    private Wallet sender;
17
-    private Wallet recipient;
18
-    private int transactionHash;
19
-    //    private String sender;
20
-//    private String recipient;
16
+    private String senderPublicKey;
17
+    private String recipientPublicKey;
18
+    private String transactionHash;
21 19
     private Float amount;
22 20
     //@OneToMany
23 21
     //private List<Integer> coins;
24 22
     //public Signature signature;
25 23
 
26
-//    public Transaction(String from, String to, float amount/*,List<Integer> coins*/) {
27
-//        this.sender = from;
28
-//        this.recipient = to;
29
-//        this.amount = amount;
30
-//        //this.coins=coins;
31
-//    }
32
-
33
-    public Transaction(){
24
+    public Transaction(String senderPublicKey, String recipientPublicKey, Float amount){
34 25
 
35 26
     }
36 27
 
37
-    public Transaction(Wallet sender, Wallet recipient, Float amount){
38
-        this.sender = sender;
39
-        this.recipient = recipient;
40
-        this.amount = amount;
41
-        this.transactionHash = (sender.toString() + recipient.toString() + amount.toString()).hashCode();
42
-
28
+    public String getSenderPublicKey() {
29
+        return senderPublicKey;
43 30
     }
44 31
 
45
-    public void transact(Wallet sender, Wallet recipient, Float amount){
46
-        new Transaction(sender, recipient, amount);
47
-
48
-        sender.setAmount(sender.getAmount() - amount);
49
-        recipient.setAmount(recipient.getAmount() + amount);
32
+    public String getRecipientPublicKey() {
33
+        return recipientPublicKey;
50 34
     }
51 35
 
36
+    public Float getAmount() {
37
+        return amount;
38
+    }
52 39
 
53 40
 
54 41
 
42
+    public void calculateAndSetHash() {
43
+        String transactionHash = DigestUtils.sha256Hex(senderPublicKey + recipientPublicKey + Float.toString(amount));
44
+        setTransactionHash(transactionHash);
45
+    }
55 46
 
47
+    public void setTransactionHash(String transactionHash) {
48
+        this.transactionHash = transactionHash;
49
+    }
56 50
 
57 51
 
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
-//    private String calculateHash() {
87
-//        return DigestUtils.sha256Hex(sender + recipient + Float.toString(amount));
52
+//    public Transaction(String from, String to, float amount/*,List<Integer> coins*/) {
53
+//        this.sender = from;
54
+//        this.recipient = to;
55
+//        this.amount = amount;
56
+//        //this.coins=coins;
88 57
 //    }
89 58
 
90
-    //Above commented out by Ken
59
+
91 60
 
92 61
 //    public void generateSignature(PrivateKey privateKey) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException {
93 62
 //        String data = StringUtil.getStringFromKey(sender) + StringUtil.getStringFromKey(reciepient) + Float.toString(value)    ;

+ 5
- 5
ZipCoinRedJavaServer/src/main/java/com/zipcoin/model/Wallet.java Bestand weergeven

@@ -20,13 +20,13 @@ public class Wallet {
20 20
     private int id;
21 21
     private String name;
22 22
     private String publicKey;
23
-    private Double amount;
23
+    private Float amount;
24 24
 
25 25
     public Wallet() {
26 26
         generateKeyPair();
27 27
     }
28 28
 
29
-    public Wallet(String name, String publicKey, double amount) {
29
+    public Wallet(String name, String publicKey, Float amount) {
30 30
         this.name=name;
31 31
         this.publicKey=publicKey;
32 32
         this.amount=amount;
@@ -36,12 +36,12 @@ public class Wallet {
36 36
         return publicKey;
37 37
     }
38 38
 
39
-    public Double getAmount(){
39
+    public Float getAmount(){
40 40
         return amount;
41 41
     }
42 42
 
43
-    public void setAmount(Double x){
44
-        amount = amount + x;
43
+    public void setAmount(Float amount){
44
+        this.amount = amount;
45 45
     }
46 46
 
47 47
     public void generateKeyPair() {

+ 20
- 2
ZipCoinRedJavaServer/src/main/java/com/zipcoin/service/TransactionService.java Bestand weergeven

@@ -1,7 +1,9 @@
1 1
 package com.zipcoin.service;
2 2
 
3 3
 import com.zipcoin.model.Transaction;
4
+import com.zipcoin.model.Wallet;
4 5
 import com.zipcoin.repository.TransactionRepository;
6
+import com.zipcoin.repository.WalletRepository;
5 7
 import org.springframework.beans.factory.annotation.Autowired;
6 8
 import org.springframework.stereotype.Service;
7 9
 
@@ -11,14 +13,27 @@ import java.util.Collection;
11 13
 public class TransactionService {
12 14
 
13 15
     private TransactionRepository transactionRepository;
16
+    private WalletRepository walletRepository;
14 17
 
15 18
     @Autowired
16
-    public TransactionService(TransactionRepository transactionRepository){
19
+    public TransactionService(TransactionRepository transactionRepository, WalletRepository walletRepository){
17 20
         this.transactionRepository = transactionRepository;
21
+        this.walletRepository = walletRepository;
18 22
     }
19 23
 
20 24
     public Transaction createTransaction(Transaction transaction) {
21
-        return transactionRepository.saveAndFlush(transaction);
25
+        Wallet sending = walletRepository.findWalletByPublicKey(transaction.getSenderPublicKey());
26
+        Wallet receiving = walletRepository.findWalletByPublicKey(transaction.getRecipientPublicKey());
27
+
28
+        sending.setAmount(sending.getAmount() - transaction.getAmount());
29
+        receiving.setAmount(receiving.getAmount() + transaction.getAmount());
30
+
31
+        walletRepository.save(sending);
32
+        walletRepository.save(receiving);
33
+
34
+        transaction.calculateAndSetHash();
35
+        return transactionRepository.save(transaction);
36
+
22 37
     }
23 38
 
24 39
     public Collection<Transaction> getAllTransactions(){
@@ -28,4 +43,7 @@ public class TransactionService {
28 43
     public Collection<Transaction> getTransactionsByPublicKey(String publicKey) {
29 44
         return transactionRepository.findTransactionBySenderOrRecipient(publicKey, publicKey);
30 45
     }
46
+
47
+
48
+
31 49
 }