Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. #include "transaction.h"
  2. #include "ui_transaction.h"
  3. #include <QDebug>
  4. //#include "Block.h"
  5. #include <QTime>
  6. #include <QDate>
  7. #include <QMessageBox>
  8. #include <QCryptographicHash>
  9. #include <QVector>
  10.  
  11. transaction::transaction(QWidget *parent) :
  12. QWidget(parent),
  13. ui(new Ui::transaction)
  14. {
  15. ui->setupUi(this);
  16. }
  17.  
  18. transaction::~transaction()
  19. {
  20. delete ui;
  21. }
  22.  
  23. void transaction::RecieveData(QString log, QString balance, QString pub, QString priv)
  24. {
  25. login = log;
  26. balanc = balance;
  27. publ = pub;
  28. priva = priv;
  29. ui->balan->setText(balanc);
  30. }
  31.  
  32. Block transaction:: GetlatestBlock(){
  33. Block Last;
  34. QStringList last_block;
  35. QFile UserBlock("C:\\userswallet\\profiles_users\\" + login + "\\" + login + "_blockchain.txt");
  36. QString last;
  37. if ((UserBlock.exists())&&(UserBlock.open(QIODevice::ReadOnly)))
  38. {
  39. for (int i =0; i<7; i++){
  40. last_block << UserBlock.readLine(); }
  41. Last.Index = last_block[0].remove("\n");
  42. Last.TimeStamp = last_block[1].remove("\n");
  43. Last.Sender = last_block[2].remove("\n");
  44. Last.Reciever = last_block[3].remove("\n");
  45. Last.SumTrans = last_block[4].remove("\n");
  46. Last.Sign = last_block[5].remove("\n");
  47. Last.HashPrev = last_block[6].remove("\n");
  48. return Last;
  49. }
  50. else
  51. {
  52. QMessageBox::critical(this, " Attention! ", "File not found!");
  53. return Last;
  54.  
  55. }
  56.  
  57. }
  58.  
  59. QString transaction::HashingBlock(Block prev)
  60. {
  61. QString DataTohash = prev.Index + prev.TimeStamp + prev.Sender + prev.Reciever + prev.SumTrans + prev.Sign + prev.HashPrev;
  62. QByteArray Data = DataTohash.toUtf8();
  63. QString hash = QCryptographicHash::hash(Data, QCryptographicHash::Sha256).toHex();
  64. return hash;
  65.  
  66. }
  67.  
  68. QString transaction::Signing(Block NewBlock)
  69. {
  70. QString data = NewBlock.Sender + NewBlock.Reciever + NewBlock.SumTrans + NewBlock.TimeStamp;
  71. QByteArray message;
  72. message.append(data);
  73. QRSAEncryption e;
  74. QByteArray priv = priva.remove("\n").toUtf8();
  75. auto signed_message = e.signMessage(message,priv);
  76. QString Sign = signed_message;
  77. int k = Sign.indexOf("-SIGN-");
  78. Sign.remove(0,k+6);
  79. Sign = Sign.left(Sign.indexOf("-"));
  80. return Sign;
  81. }
  82.  
  83. void transaction::AddBlocktoChain(Block myblock)
  84. {
  85. QVector<Block> chain (myblock.Index.toInt());
  86. QFile UserChain("C:\\userswallet\\profiles_users\\" + login + "\\" + login + "_blockchain.txt");
  87. if ((UserChain.exists())&&(UserChain.open(QIODevice::ReadOnly)))
  88. {
  89. for (int i =0 ; i<chain.size(); i++)
  90. {
  91. chain[i].Index = UserChain.readLine();
  92. chain[i].TimeStamp = UserChain.readLine();
  93. chain[i].Sender = UserChain.readLine();
  94. chain[i].Reciever = UserChain.readLine();
  95. chain[i].SumTrans = UserChain.readLine();
  96. chain[i].Sign = UserChain.readLine();
  97. chain[i].HashPrev = UserChain.readLine();
  98. QString buff = UserChain.readLine();
  99. }
  100. }
  101. UserChain.close();
  102. if ((UserChain.exists())&&(UserChain.open(QIODevice::WriteOnly)))
  103. {
  104. QTextStream stream(&UserChain);
  105. stream << myblock.Index << "\n";
  106. stream << myblock.TimeStamp << "\n";
  107. stream << myblock.Sender << "\n";
  108. stream << myblock.Reciever << "\n";
  109. stream << myblock.SumTrans << "\n";
  110. stream << myblock.Sign << "\n";
  111. stream << myblock.HashPrev << "\n";
  112. stream << "----------------------------\n";
  113. }
  114. UserChain.close();
  115. QTextStream stream(&UserChain);
  116. for ( int i = 0; i<chain.size(); i++)
  117. {
  118. if ((UserChain.exists())&&(UserChain.open(QIODevice::Append))){
  119. QTextStream stream(&UserChain);
  120. stream << chain[i].Index;
  121. stream << chain[i].TimeStamp;
  122. stream << chain[i].Sender;
  123. stream << chain[i].Reciever;
  124. stream << chain[i].SumTrans;
  125. stream << chain[i].Sign;
  126. stream << chain[i].HashPrev;
  127. stream << "----------------------------\n";
  128. UserChain.close();
  129. }
  130. }
  131.  
  132. }
  133.  
  134. void transaction::UpdateBalance(QString Login,QString NewBalance){
  135. QFile date("C:\\userswallet\\profiles_users\\" + Login + "\\" + Login + ".txt");
  136. QStringList info;
  137. if ((date.exists())&&(date.open(QIODevice::ReadOnly)))
  138. {
  139. while(!date.atEnd())
  140. {
  141. info << date.readLine();
  142. }
  143. }
  144. date.close();
  145. if (Login == login) info[0] = NewBalance +"\n";
  146. else info[0] = QString::number(NewBalance.toInt()+ info[0].toInt()) +"\n";
  147. foreach(QString s, info) qDebug() << s;
  148. QTextStream NewInfo(&date);
  149. if ((date.exists())&&(date.open(QIODevice::WriteOnly)))
  150. {
  151. foreach(QString s, info)
  152. {
  153. NewInfo << s;
  154. }
  155. }
  156. date.close();
  157. }
  158.  
  159. bool transaction::AcceptNewBlock(Block NewBlock)
  160. {
  161. QFile all_chains("C:\\userswallet\\blockchains.txt");
  162. if ((all_chains.exists())&&(all_chains.open(QIODevice::ReadOnly)))
  163. {
  164. QStringList nodes;
  165. while(!all_chains.atEnd())
  166. {
  167. nodes << all_chains.readLine();
  168. }
  169. all_chains.close();
  170. }
  171. foreach(QString s, all_chains)
  172. {
  173. if (balanc.toInt() < NewBlock.SumTrans.toInt())
  174. {
  175. QMessageBox::critical(this, " Attention! ", "Other nodes rejected your block. Insufficient funds!");
  176. return false;
  177. }
  178. else
  179. {
  180. QFile key("C:\\userswallet\\profiles_users\\" + NewBlock.Sender + "\\" + NewBlock.Sender + ".txt");
  181. QStringList find_pubkey;
  182. if ((key.exists())&&(key.open(QIODevice::ReadOnly)))
  183. {
  184. while (!key.atEnd())
  185. find_pubkey << key.readLine();
  186. }
  187. key.close();
  188.  
  189. QRSAEncryption e;
  190. if (!e.checkSignMessage(NewBlock.Sign.toUtf8(),find_pubkey[1].remove("\n").toUtf8()))
  191. {
  192. QMessageBox::critical(this, " Attention! ", "Sign is invalid!");
  193. return false;
  194. }
  195. else
  196. {
  197.  
  198. }
  199.  
  200. }
  201.  
  202. }
  203. }
  204.  
  205. void transaction::on_pushButton_clicked()
  206. {
  207. Block temp;
  208. temp.SumTrans = ui->lineEdit_3->text();
  209. if (balanc.toInt() < temp.SumTrans.toInt())
  210. {
  211. QMessageBox::critical(this, " Attention! ", "You do not have enough cryptocurrency!");
  212. hide();
  213. transaction* NewW = new transaction;
  214. NewW->show();
  215. NewW->RecieveData(login,balanc,publ,priva);
  216. return;
  217. }
  218. balanc = QString::number(balanc.toInt() -temp.SumTrans.toInt());
  219. temp.Sender = login;
  220. temp.Reciever = ui->LogReciv->text();
  221. temp.TimeStamp = QDate::currentDate().toString() + "," + QTime::currentTime().toString();
  222. Block LatestBlock = GetlatestBlock();
  223. int tmp = LatestBlock.Index.toInt() + 1;
  224. temp.Index = QString::number(tmp);
  225. temp.HashPrev = HashingBlock(LatestBlock);
  226. temp.Sign = Signing(temp);
  227. AddBlocktoChain(temp);
  228. ui->balan->setText(balanc);
  229. UpdateBalance(login,balanc);
  230. UpdateBalance(ui->LogReciv->text(),temp.SumTrans);
  231.  
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement