Advertisement
Guest User

XCMIXER

a guest
Jul 16th, 2014
1,008
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.29 KB | None | 0 0
  1. // XC's REV1 - Encrypted Trusted Mixer - LET'S WRITE A MIXER! Support XC's Development Fund - XXVTS3279gzC5kagTCrEe4x5R7urnm6h2b
  2.  
  3.  
  4. - // this command only gets processed by mixer servers
  5. - else if (strCommand == "mixdisc" && GetBoolArg("-xmixer")) {
  6. - CMixDisc md;
  7. - vRecv >> md;
  8. -
  9. - // determine if we have enough coins to handle the request
  10. - Array args;
  11. - Value balance = getbalance(args, false);
  12. -
  13. - if (md.amount * 10 < balance.get_real() && pfrom->addrName != GetLocalAddress(NULL).ToStringIPPort()) {
  14. - // save transaction information in server
  15. - CServerMixTx smtx;
  16. - smtx.addrName = pfrom->addrName;
  17. - smtx.amount = md.amount;
  18. - smtx.stage = 0;
  19. - int64 smtxid;
  20. -
  21. - {
  22. - LOCK(cs_mapServerMixTxs);
  23. - std::map<int64, CServerMixTx>::iterator it = mapServerMixTxs.end();
  24. - if (it == mapServerMixTxs.begin()) smtxid = 1;
  25. - else smtxid = (--it)->first + 1;
  26. -
  27. - mapServerMixTxs[smtxid] = smtx;
  28. - }
  29. -
  30. - unsigned char rnum;
  31. - if (RAND_bytes(&rnum, 1) != 1) {
  32. - std::cerr << "RAND_bytes mixdisc fail; aborting" << std::endl;
  33. - goto abort; // fuck it
  34. - }
  35. - int num = rnum;
  36. -
  37. - boost::this_thread::sleep(boost::posix_time::milliseconds(num*10));
  38. -
  39. - // send the acknowledgement to the client with server ID
  40. - CMixAck ma;
  41. - ma.mtxid = md.mtxid;
  42. - ma.smtxid = smtxid;
  43. - pfrom->PushMessage("mixack", ma);
  44. - }
  45. - }
  46. -
  47. - else if (strCommand == "mixaccept" && GetBoolArg("-xmixer")) {
  48. - CMixAccept ma;
  49. - vRecv >> ma;
  50. -
  51. - bool success = false;
  52. - RSA* servrsa = RSA_new();
  53. -
  54. - if (servrsa != NULL) {
  55. - {
  56. - LOCK(cs_mapServerMixTxs);
  57. - std::map<int64, CServerMixTx>::iterator it = mapServerMixTxs.find(ma.smtxid);
  58. - if (it != mapServerMixTxs.end() &&
  59. - it->second.stage == 0 &&
  60. - it->second.addrName == pfrom->addrName) {
  61. - it->second.stage = 1;
  62. - it->second.servrsa = servrsa;
  63. - success = true;
  64. - }
  65. - }
  66. -
  67. - if (success) {
  68. - // generate a public encryption key and send it over
  69. - if (genrsa(servrsa)) {
  70. - CMixKey mk;
  71. - if (rsa2hex(servrsa, mk.key)) {
  72. - std::cout << "servrsa: " << servrsa << std::endl;
  73. - std::cout << "server RSA size: " << RSA_size(servrsa) << std::endl;
  74. - mk.mtxid = ma.mtxid;
  75. - mk.smtxid = ma.smtxid;
  76. -
  77. - pfrom->PushMessage("mixkey", mk);
  78. - }
  79. - else {
  80. - std::cerr << "Could not convert RSA key to hex" << std::endl;
  81. - }
  82. - }
  83. - else {
  84. - std::cerr << "Could not generate RSA key" << std::endl;
  85. - }
  86. - }
  87. - else {
  88. - std::cerr << "Invalid mixaccept message" << std::endl;
  89. - RSA_free(servrsa);
  90. - }
  91. - }
  92. - else {
  93. - std::cerr << "Could not allocate RSA key" << std::endl;
  94. - }
  95. - }
  96. -
  97. - else if (strCommand == "mixstart" && GetBoolArg("-xmixer")) {
  98. - CMixStart ms;
  99. - vRecv >> ms;
  100. -
  101. - CServerMixTx smtx;
  102. - bool success = false;
  103. -
  104. - {
  105. - LOCK(cs_mapServerMixTxs);
  106. - std::map<int64, CServerMixTx>::iterator it = mapServerMixTxs.find(ms.smtxid);
  107. - if (it != mapServerMixTxs.end() &&
  108. - it->second.stage == 1 &&
  109. - it->second.addrName == pfrom->addrName) {
  110. - smtx = it->second;
  111. - success = true;
  112. - mapServerMixTxs.erase(ms.smtxid); // done with this now!
  113. - }
  114. - }
  115. -
  116. - if (success) {
  117. - // get a new wallet address for client mixer, and encrypt it with the client's key
  118. - Array args;
  119. - Value newaddress = getnewaddress(args, false);
  120. - std::string address = newaddress.get_str();
  121. -
  122. - RSA* rsa = RSA_new();
  123. - if (rsa != NULL) {
  124. - std::cout << "servrsa: " << smtx.servrsa << std::endl;
  125. - std::cout << "server RSA size in mixstart: " << RSA_size(smtx.servrsa) << std::endl;
  126. - //unencrypt final target from client
  127. - std::string to;
  128. - if (strdec(smtx.servrsa, ms.to, to)) {
  129. - // unencrypt client public key and put in RSA
  130. - std::string ckey;
  131. - if (strdec(smtx.servrsa, ms.key, ckey) &&
  132. - hex2rsa(rsa, ckey)) {
  133. - BN_set_word(rsa->e, 17);
  134. - CMixFin mf;
  135. - // encrypt intermediate address with client public key
  136. - //std::cout << "Server's client enc target address: " << ms.to << std::endl;
  137. - std::cout << "Server's client target address: " << to << std::endl;
  138. - //std::cout << "Server's client enc hex key: " << ms.key << std::endl;
  139. - std::cout << "Server's client Hex key: " << ckey << std::endl;
  140. - std::cout << "Server's client RSA size: " << RSA_size(rsa) << std::endl;
  141. - std::cout << "Server's iaddress: " << address << std::endl;
  142. - if (strenc(rsa, address, mf.address)) {
  143. - //std::cout << mf.address << std::endl;
  144. - RSA_free(rsa);
  145. - RSA_free(smtx.servrsa);
  146. -
  147. - mf.mtxid = ms.mtxid;
  148. - pfrom->PushMessage("mixfin", mf);
  149. -
  150. - boost::thread t(ServerMix, address, to, smtx.amount);
  151. - }
  152. - else {
  153. - std::cerr << "Could not encrypt intermediate address" << std::endl;
  154. - }
  155. - }
  156. - else {
  157. - std::cerr << "Could not decrypt client public keys" << std::endl;
  158. - }
  159. - }
  160. - else {
  161. - std::cerr << "Could not decrypt target address" << std::endl;
  162. - }
  163. - }
  164. - else {
  165. - std::cerr << "Could not allocate client RSA object" << std::endl;
  166. - }
  167. - }
  168. - else {
  169. - std::cerr << "Invalid mixstart request" << std::endl;
  170. - }
  171. - }
  172. -
  173. - else if (strCommand == "mixack") {
  174. - // recieved an mixdisc broadcast acknowledgement, so we save the server info and accept
  175. - // the request if we haven't already saved a server for this ID
  176. - CMixAck ma;
  177. - vRecv >> ma;
  178. -
  179. - bool success = false;
  180. -
  181. - {
  182. - LOCK(cs_mapMixTxs);
  183. -
  184. - // find the transaction, ignore invalid mixacks
  185. - std::map<int64, CMixTx>::iterator it = mapMixTxs.find(ma.mtxid);
  186. -
  187. - // stage == 0 means that we haven't found a server yet
  188. - if (it != mapMixTxs.end() && it->second.stage == 0) {
  189. - // save server address for future verification
  190. - it->second.addrName = pfrom->addrName;
  191. - it->second.stage = 1; // indicate that a server has been found (and saved)
  192. - it->second.inprogress = false;
  193. - success = true;
  194. - }
  195. - }
  196. -
  197. - if (success == true) {
  198. - std::cout << "Received mixack from " << pfrom->addrName << std::endl;
  199. - std::cout << "Client ID is " << ma.mtxid << " and server ID is " << ma.smtxid
  200. - << std::endl;
  201. - // tell the server that we have accepted its request
  202. - CMixAccept mac;
  203. - mac.mtxid = ma.mtxid;
  204. - mac.smtxid = ma.smtxid;
  205. -
  206. - pfrom->PushMessage("mixaccept", mac);
  207. - }
  208. - else {
  209. - std::cerr << "Recieved invalid request from server or request already handled."
  210. - << std::endl;
  211. - }
  212. - }
  213. -
  214. - else if (strCommand == "mixkey") {
  215. - CMixKey mk;
  216. - vRecv >> mk;
  217. -
  218. - CMixTx mtx;
  219. - bool success = false;
  220. -
  221. - std::cout << "Recieved mixkey with client ID of " << mk.mtxid << " and server ID of "
  222. - << mk.smtxid << std::endl;
  223. -
  224. - std::cout << "The key is " << mk.key << std::endl;
  225. -
  226. - RSA* rsa = RSA_new();
  227. - RSA* servrsa = RSA_new();
  228. -
  229. - if (rsa != NULL && servrsa != NULL) {
  230. - {
  231. - LOCK(cs_mapMixTxs);
  232. -
  233. - std::map<int64, CMixTx>::iterator it = mapMixTxs.find(mk.mtxid);
  234. - if (it != mapMixTxs.end() // make sure we actually have the request
  235. - && it->second.stage == 1 // make sure we haven't already done this step
  236. - && it->second.inprogress == false
  237. - && it->second.addrName == pfrom->addrName) { // make sure it's the same server
  238. - std::cout << "Filthy liars." << std::endl;
  239. - it->second.stage = 2; // indicate that we have received the mixkey request...
  240. - it->second.inprogress = true; // ...but that we haven't finished processing it yet
  241. - it->second.rsa = rsa;
  242. - mtx = it->second; // save a copy of the information
  243. - success = true; // indicate everything went well
  244. - }
  245. - }
  246. -
  247. - if (success) {
  248. - std::string key;
  249. - if (genrsa(rsa) && rsa2hex(rsa, key)) {
  250. - std::cout << "Client's public hex key: " << key << std::endl;
  251. - std::cout << "client's rsa size: " << RSA_size(rsa) << std::endl;
  252. - if (hex2rsa(servrsa, mk.key)) {
  253. - // encrypt our public keys and target location using the server's public keys
  254. - CMixStart ms;
  255. - std::cout << "Client's server rsa size: " << RSA_size(servrsa) << std::endl;
  256. - if (strenc(servrsa, key, ms.key)) {
  257. - if (strenc(servrsa, mtx.to, ms.to)) {
  258. - std::cout << "Client to: " << mtx.to << std::endl;
  259. -
  260. - RSA_free(servrsa);
  261. -
  262. - ms.smtxid = mk.smtxid;
  263. - ms.mtxid = mk.mtxid;
  264. -
  265. - success = false;
  266. -
  267. - {
  268. - LOCK(cs_mapMixTxs);
  269. - std::map<int64, CMixTx>::iterator it = mapMixTxs.find(mk.mtxid);
  270. - if (it != mapMixTxs.end()) {
  271. - it->second.inprogress = false; // indicate we're done with stage 2
  272. - success = true;
  273. - }
  274. - }
  275. -
  276. - if (success) {
  277. - // send the server our keys and the target
  278. - pfrom->PushMessage("mixstart", ms);
  279. - }
  280. - else {
  281. - std::cerr << "Could not find transaction, maybe someone broke it?"
  282. - << std::endl;
  283. - }
  284. - }
  285. - else {
  286. - std::cerr << "Could not encrypt target address" << std::endl;
  287. - }
  288. - }
  289. - else {
  290. - std::cerr << "Could not encrypt client public key" << std::endl;
  291. - }
  292. - }
  293. - else {
  294. - std::cerr << "Could not convert server key hex to RSA key" << std::endl;
  295. - }
  296. - }
  297. - else {
  298. - std::cerr << "Could not generate client key and convert public key to hex"
  299. - << std::endl;
  300. - }
  301. - }
  302. - else {
  303. - std::cerr << "Received an invalid mixkey transaction from server" << std::endl;
  304. - }
  305. - } else {
  306. - std::cerr << "Could not allocate client and server RSA structures." << std::endl;
  307. - }
  308. - }
  309. -
  310. - else if (strCommand == "mixfin") {
  311. - // last step in the mixing process
  312. - CMixFin mf;
  313. - vRecv >> mf;
  314. -
  315. - CMixTx mtx;
  316. - bool success = false;
  317. -
  318. - {
  319. - LOCK(cs_mapMixTxs);
  320. -
  321. - std::map<int64, CMixTx>::iterator it = mapMixTxs.find(mf.mtxid);
  322. - if (it != mapMixTxs.end() // make sure we actually have the request
  323. - && it->second.stage == 2 // make sure we have completed the mixkey step
  324. - && it->second.inprogress == false
  325. - && it->second.addrName == pfrom->addrName) { // make sure it's the same server
  326. - mtx = it->second; // save a copy of the information
  327. - mapMixTxs.erase(mf.mtxid); // erase the transaction, we won't need it anymore
  328. - success = true; // indicate everything went well
  329. - }
  330. - }
  331. -
  332. - if (success) {
  333. - std::string address;
  334. - if(strdec(mtx.rsa, mf.address, address)) {
  335. - //send the money to the server's wallet in randomly sized transactions
  336. - Array args;
  337. - double amount = mtx.amount;
  338. - double currentTxAmount = 0;
  339. - double totalTransferred = 0;
  340. - unsigned char rnum;
  341. - bool failed_transfer_rest = false;
  342. - double min = (double)MIN_TXOUT_AMOUNT/(double)COIN*2;
  343. -
  344. - while (totalTransferred != amount) {
  345. - if (RAND_bytes(&rnum, 1) != 1) {
  346. - failed_transfer_rest = true;
  347. - rnum = 0;
  348. - std::cerr << "RAND_bytes failed during mixer client transfer; transferring the rest in one go" << std::endl;
  349. - }
  350. -
  351. - std::cout << "Random number: " << (int)rnum << " and percentage: "
  352. - << ((double)rnum/255.0) << std::endl;
  353. -
  354. - // maximum tx size is 90% the total amount, min tx size is a constant
  355. - currentTxAmount = (((double)rnum/255.0)*((amount*0.9)-min))+min;
  356. -
  357. - if (totalTransferred + currentTxAmount > amount || failed_transfer_rest) {
  358. - currentTxAmount = amount - totalTransferred;
  359. - if (currentTxAmount < min) currentTxAmount = min;
  360. - }
  361. -
  362. - totalTransferred += currentTxAmount;
  363. -
  364. - std::cout << "Transfer " << currentTxAmount << " from " << mtx.from << " to "
  365. - << address << "." << std::endl;
  366. -
  367. - args.clear();
  368. - args.push_back(mtx.from);
  369. - args.push_back(address);
  370. - args.push_back(currentTxAmount);
  371. -
  372. - try {
  373. - sendfrom(args, false);
  374. - }
  375. - catch (Object& e) {
  376. - std::cout << "FATAL: ";
  377. - for (Object::iterator i = e.begin(); i != e.end(); ++i) {
  378. - if ((*i).value_.type() == str_type)
  379. - std::cout << ">" << (*i).value_.get_str()<< std::endl;
  380. - }
  381. - break;
  382. - }
  383. - }
  384. - }
  385. - else {
  386. - std::cerr << "Could not decrypt intermediate address" << std::endl;
  387. - }
  388. - }
  389. - else {
  390. - std::cerr << "Invalid mixfin message from server." << std::endl;
  391. - }
  392. - }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement