java-api/src/main/java/restImplementation/TransactionImplementation.java
GME fe7ebfdea3 testing, update and security
adding layer of security for transaction & setBalance
testing and updating test
2019-04-05 19:44:43 +02:00

111 lines
5 KiB
Java

package restImplementation;
import blockchain.query.QueryWrapper;
import blockchain.query.TransactionWrapper;
import database.transaction.Transaction;
import database.transaction.TransactionDao;
import database.user.User;
import database.user.UserDao;
import org.hyperledger.fabric.sdk.BlockEvent;
import org.springframework.security.crypto.bcrypt.BCrypt;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import java.io.StringReader;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
public class TransactionImplementation {
public HashMap sendTransaction(String sourceUserHash, String sourceUserPwd, String sourceWalletHash, String destinationWalletHash, double amount, String transactionUnit) throws Exception {
TransactionWrapper transactionWrapper = new TransactionWrapper();
QueryWrapper queryWrapper = new QueryWrapper();
HashMap returnResponse = new HashMap();
UserDao userDao = new UserDao();
User user = userDao.getUserWithHash(sourceUserHash);
if(user != null){
if(BCrypt.checkpw(sourceUserPwd, user.getPassword())){
String response = queryWrapper.sendQuery("readWallet", new String[]{sourceWalletHash});
if (response != null){
JsonReader reader = Json.createReader(new StringReader(response));
JsonObject walletInfo = reader.readObject();
double balance = walletInfo.getJsonNumber("balance").doubleValue();
if(balance-amount<0){ //CHECK WALLET BALANCE
returnResponse.put("success",false);
returnResponse.put("message","Not enough money");
} else {
//CHECK DEST WALLET
String responseDestWallet = queryWrapper.sendQuery("readWallet", new String[]{destinationWalletHash});
if(responseDestWallet!=null){
BlockEvent.TransactionEvent eventResponse = transactionWrapper.sendTransaction("transaction",new String[]{sourceWalletHash,destinationWalletHash,String.valueOf(amount)});
String txID = eventResponse.getTransactionID();
Transaction transaction = new Transaction();
long now = Instant.now().toEpochMilli();
transaction.setTransaction_date(now);
transaction.setTransaction_from(sourceWalletHash);
transaction.setTransaction_to(destinationWalletHash);
transaction.setTransaction_amount(amount);
transaction.setTransaction_hash(txID);
transaction.setTransaction_unit(transactionUnit);
TransactionDao transactionDao = new TransactionDao();
transactionDao.addTransaction(transaction);
returnResponse.put("success",true);
returnResponse.put("message",txID);
WalletImplementation walletImplementation = new WalletImplementation();
walletImplementation.updateWalletBalance(sourceWalletHash,destinationWalletHash,amount);
} else {
returnResponse.put("success",false);
returnResponse.put("message","Destination wallet doesn't exist");
}
}
} else {
returnResponse.put("success",false);
returnResponse.put("message","Error in user wallet");
}
} else {
throw new Exception("NOT ALLOWED");
}
} else {
throw new Exception("NOT ALLOWED");
}
return returnResponse;
}
public List<Transaction> getSentTransaction(String userHash) throws Exception {
TransactionDao transactionDao = new TransactionDao();
return transactionDao.getUserSentTransaction(userHash);
}
public List<Transaction> getReceivedTransaction(String userHash) throws Exception {
TransactionDao transactionDao = new TransactionDao();
return transactionDao.getUserReceivedTransaction(userHash);
}
public Transaction getTransaction(String userHash, String transactionHash) throws Exception {
TransactionDao transactionDao = new TransactionDao();
return transactionDao.getTransaction(userHash,transactionHash);
}
public List<Transaction> getLatestTransactions(String userHash) throws Exception {
TransactionDao transactionDao = new TransactionDao();
return transactionDao.getTenLastUserTransactions(userHash);
}
public List<Transaction> getUserTransactions(String userHash) throws Exception {
TransactionDao transactionDao = new TransactionDao();
return transactionDao.getUserTransactions(userHash);
}
}