Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 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. qDebug() << "priv2" << priva.remove("\n").toUtf8();
  76. QByteArray signed_message = e.signMessage(message,priv);
  77. QString Sign;
  78. Sign.append(signed_message);
  79. // qDebug() <<"v1 - >" << Sign;
  80. //int k = Sign.indexOf("-SIGN-");
  81. // Sign.remove(0,k+6);
  82. // Sign = Sign.left(Sign.indexOf("-"));
  83. return Sign;
  84. }
  85.  
  86. void transaction::AddBlocktoChain(QString path,Block myblock)
  87. {
  88. QVector<Block> chain (myblock.Index.toInt());
  89. QFile UserChain(path);
  90. if ((UserChain.exists())&&(UserChain.open(QIODevice::ReadOnly)))
  91. {
  92. for (int i =0 ; i<chain.size(); i++)
  93. {
  94. chain[i].Index = UserChain.readLine();
  95. chain[i].TimeStamp = UserChain.readLine();
  96. chain[i].Sender = UserChain.readLine();
  97. chain[i].Reciever = UserChain.readLine();
  98. chain[i].SumTrans = UserChain.readLine();
  99. chain[i].Sign = UserChain.readLine();
  100. chain[i].HashPrev = UserChain.readLine();
  101. QString buff = UserChain.readLine();
  102. }
  103. }
  104. UserChain.close();
  105. if ((UserChain.exists())&&(UserChain.open(QIODevice::WriteOnly)))
  106. {
  107. QTextStream stream(&UserChain);
  108. stream << myblock.Index << "\n";
  109. stream << myblock.TimeStamp << "\n";
  110. stream << myblock.Sender << "\n";
  111. stream << myblock.Reciever << "\n";
  112. stream << myblock.SumTrans << "\n";
  113. stream << myblock.Sign << "\n";
  114. stream << myblock.HashPrev << "\n";
  115. stream << "----------------------------\n";
  116. }
  117. UserChain.close();
  118. QTextStream stream(&UserChain);
  119. for ( int i = 0; i<chain.size(); i++)
  120. {
  121. if ((UserChain.exists())&&(UserChain.open(QIODevice::Append))){
  122. QTextStream stream(&UserChain);
  123. stream << chain[i].Index;
  124. stream << chain[i].TimeStamp;
  125. stream << chain[i].Sender;
  126. stream << chain[i].Reciever;
  127. stream << chain[i].SumTrans;
  128. stream << chain[i].Sign;
  129. stream << chain[i].HashPrev;
  130. stream << "----------------------------\n";
  131. UserChain.close();
  132. }
  133. }
  134.  
  135. }
  136.  
  137. void transaction::UpdateBalance(QString Login,QString NewBalance){
  138. QFile date("C:\\userswallet\\profiles_users\\" + Login + "\\" + Login + ".txt");
  139. QStringList info;
  140. if ((date.exists())&&(date.open(QIODevice::ReadOnly)))
  141. {
  142. while(!date.atEnd())
  143. {
  144. info << date.readLine();
  145. }
  146. }
  147. date.close();
  148. if (Login == login) info[0] = NewBalance +"\n";
  149. else info[0] = QString::number(NewBalance.toInt()+ info[0].toInt()) +"\n";
  150. foreach(QString s, info) qDebug() << s;
  151. QTextStream NewInfo(&date);
  152. if ((date.exists())&&(date.open(QIODevice::WriteOnly)))
  153. {
  154. foreach(QString s, info)
  155. {
  156. NewInfo << s;
  157. }
  158. }
  159. date.close();
  160. }
  161.  
  162. bool transaction::AcceptNewBlock(Block NewBlock)
  163. {
  164. QFile all_chains("C:\\userswallet\\blockchains.txt");
  165. QStringList nodes;
  166. if ((all_chains.exists())&&(all_chains.open(QIODevice::ReadOnly)))
  167. {
  168. while(!all_chains.atEnd())
  169. {
  170. QString temp = all_chains.readLine();
  171. nodes.push_back(temp.remove("\n"));
  172.  
  173. }
  174. all_chains.close();
  175. }
  176. foreach(QString s, nodes)
  177. {
  178. if (s == "") continue;
  179. if (balanc.toInt() < NewBlock.SumTrans.toInt())
  180. {
  181. QMessageBox::critical(this, " Attention! ", "Other nodes rejected your block. Insufficient funds!");
  182. return false;
  183. }
  184. else
  185. {
  186. QFile key("C:\\userswallet\\profiles_users\\" + NewBlock.Sender + "\\" + NewBlock.Sender + ".txt");
  187. QStringList find_pubkey;
  188. if ((key.exists())&&(key.open(QIODevice::ReadOnly)))
  189. {
  190. while (!key.atEnd())
  191. find_pubkey << key.readLine();
  192. }
  193. key.close();
  194.  
  195. QRSAEncryption e;
  196.  
  197. QByteArray pu,pr;
  198.  
  199. e.generatePairKey(pu,pr,QRSAEncryption::Rsa::RSA_256);
  200. QByteArray message = "test";
  201. qDebug() << "pu" << pu;
  202. qDebug() << "pr" << pr;
  203. QByteArray sigm = e.signMessage(message,pr);
  204. qDebug() << "sign message" << sigm;
  205. if (e.checkSignMessage(sigm,pu))
  206. {
  207. QMessageBox::critical(this, " Attention! ", "YES!");
  208. }
  209. else
  210. {
  211. QMessageBox::critical(this, " Attention! ", "NO!");
  212. }
  213.  
  214.  
  215.  
  216. qDebug() << NewBlock.Sign.toUtf8();
  217. if (!e.checkSignMessage(NewBlock.Sign.toUtf8(),find_pubkey[1].remove("\n").toUtf8()))
  218. {
  219. QMessageBox::critical(this, " Attention! ", "Sign is invalid!");
  220. return false;
  221. }
  222. else
  223. {
  224. QMessageBox::critical(this, " Attention! ", "Sign is valid!");
  225. AddBlocktoChain(s,NewBlock);
  226. }
  227.  
  228. }
  229.  
  230. }
  231. return true;
  232. }
  233.  
  234.  
  235. void transaction::on_pushButton_clicked()
  236. {
  237. Block temp;
  238. temp.SumTrans = ui->lineEdit_3->text();
  239. temp.Sender = login;
  240. temp.Reciever = ui->LogReciv->text();
  241. temp.TimeStamp = QDate::currentDate().toString("d.M.yy") + "," + QTime::currentTime().toString();
  242. Block LatestBlock = GetlatestBlock();
  243. int tmp = LatestBlock.Index.toInt() + 1;
  244. temp.Index = QString::number(tmp);
  245. temp.HashPrev = HashingBlock(LatestBlock);
  246. temp.Sign = Signing(temp);
  247. if (!AcceptNewBlock(temp))
  248. {
  249. hide();
  250. transaction* NewW = new transaction;
  251. NewW->show();
  252. NewW->RecieveData(login,balanc,publ,priva);
  253. return;
  254. }
  255. else
  256. {
  257. QMessageBox::information(this, " Good! ", "Other nodes accept your block!");
  258. balanc = QString::number(balanc.toInt() -temp.SumTrans.toInt());
  259. QString path = "C:\\userswallet\\profiles_users\\" + login + "\\" + login + "_blockchain.txt";
  260. AddBlocktoChain(path,temp);
  261. ui->balan->setText(balanc);
  262. UpdateBalance(login,balanc);
  263. UpdateBalance(ui->LogReciv->text(),temp.SumTrans);
  264. }
  265.  
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement