Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2013
12,948
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.36 KB | None | 0 0
  1. // Copyright "Remember remember the 5th of November" 2013
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <stdbool.h>
  6. #include <inttypes.h>
  7. #include <ctype.h>
  8. #include <string.h>
  9. #include <time.h>
  10. #include <openssl/sha.h>
  11.  
  12. //Copied from Bitcoin source
  13. const uint64_t COIN = 100000000;
  14. const uint64_t CENT = 1000000;
  15.  
  16. uint32_t OP_CHECKSIG = 172; // This is expressed as 0xAC
  17. bool generateBlock = false;
  18. uint32_t startNonce = 0;
  19. uint32_t unixtime = 0;
  20.  
  21. typedef struct {
  22. /* Hash of Tx */
  23. uint8_t merkleHash[32];
  24.  
  25. /* Tx serialization before hashing */
  26. uint8_t *serializedData;
  27.  
  28. /* Tx version */
  29. uint32_t version;
  30.  
  31. /* Input */
  32. uint8_t numInputs; // Program assumes one input
  33. uint8_t prevOutput[32];
  34. uint32_t prevoutIndex;
  35. uint8_t *scriptSig;
  36. uint32_t sequence;
  37.  
  38. /* Output */
  39. uint8_t numOutputs; // Program assumes one output
  40. uint64_t outValue;
  41. uint8_t *pubkeyScript;
  42.  
  43. /* Final */
  44. uint32_t locktime;
  45. } Transaction;
  46.  
  47. // Got this off the internet. Am not sure if it can fail in some circumstances
  48. void byteswap(uint8_t *buf, int length)
  49. {
  50. int i;
  51. uint8_t temp;
  52.  
  53. for(i = 0; i < length / 2; i++)
  54. {
  55. temp = buf[i];
  56. buf[i] = buf[length - i - 1];
  57. buf[length - i - 1] = temp;
  58. }
  59. }
  60.  
  61. // Following two functions are borrowed from cgminer.
  62. char *bin2hex(const unsigned char *p, size_t len)
  63. {
  64. char *s = malloc((len * 2) + 1);
  65. unsigned int i;
  66.  
  67. if (!s)
  68. return NULL;
  69.  
  70. for (i = 0; i < len; i++)
  71. sprintf(s + (i * 2), "%02x", (unsigned int) p[i]);
  72.  
  73. return s;
  74. }
  75.  
  76. size_t hex2bin(unsigned char *p, const char *hexstr, size_t len)
  77. {
  78. int ret = 0;
  79. size_t retlen = len;
  80.  
  81. while (*hexstr && len) {
  82. char hex_byte[4];
  83. unsigned int v;
  84.  
  85. if (!hexstr[1]) {
  86. return ret;
  87. }
  88.  
  89. memset(hex_byte, 0, 4);
  90. hex_byte[0] = hexstr[0];
  91. hex_byte[1] = hexstr[1];
  92.  
  93. if (sscanf(hex_byte, "%x", &v) != 1) {
  94. return ret;
  95. }
  96.  
  97. *p = (unsigned char) v;
  98.  
  99. p++;
  100. hexstr += 2;
  101. len--;
  102. }
  103.  
  104. if (len == 0 && *hexstr == 0)
  105. ret = retlen;
  106.  
  107. return ret;
  108. }
  109.  
  110. Transaction *InitTransaction()
  111. {
  112. Transaction *transaction;
  113.  
  114. transaction = calloc(1, sizeof(*transaction));
  115. if(!transaction)
  116. {
  117. return NULL;
  118. }
  119.  
  120. // Set some initial data that will remain constant throughout the program
  121. transaction->version = 1;
  122. transaction->numInputs = 1;
  123. transaction->numOutputs = 1;
  124. transaction->locktime = 0;
  125. transaction->prevoutIndex = 0xFFFFFFFF;
  126. transaction->sequence = 0xFFFFFFFF;
  127. transaction->outValue = 50*COIN;
  128.  
  129. // We initialize the previous output to 0 as there is none
  130. memset(transaction->prevOutput, 0, 32);
  131.  
  132. return transaction;
  133. }
  134.  
  135. int main(int argc, char *argv[])
  136. {
  137. Transaction *transaction;
  138. unsigned char hash1[32], hash2[32];
  139. char timestamp[255], pubkey[132];
  140. uint32_t timestamp_len = 0, scriptSig_len = 0, pubkey_len = 0, pubkeyScript_len = 0;
  141. uint32_t nBits = 0;
  142.  
  143. if((argc-1) < 3)
  144. {
  145. fprintf(stderr, "Usage: genesisgen [options] <pubkey> \"<timestamp>\" <nBits>\n");
  146. return 0;
  147. }
  148.  
  149. pubkey_len = strlen(argv[1]) / 2; // One byte is represented as two hex characters, thus we divide by two to get real length.
  150. timestamp_len = strlen(argv[2]);
  151.  
  152. if(pubkey_len != 65)
  153. {
  154. fprintf(stderr, "Invalid public key length! %s\n", argv[1]);
  155. return 0;
  156. }
  157.  
  158. if(timestamp_len > 254 || timestamp_len <= 0)
  159. {
  160. fprintf(stderr, "Size of timestamp is 0 or exceeds maximum length of 254 characters!\n");
  161. return 0;
  162. }
  163.  
  164. transaction = InitTransaction();
  165. if(!transaction)
  166. {
  167. fprintf(stderr, "Could not allocate memory! Exiting...\n");
  168. return 0;
  169. }
  170.  
  171. strncpy(pubkey, argv[1], sizeof(pubkey));
  172. strncpy(timestamp, argv[2], sizeof(timestamp));
  173. sscanf(argv[3], "%lu", (long unsigned int *)&nBits);
  174.  
  175. pubkey_len = strlen(pubkey) >> 1;
  176. scriptSig_len = timestamp_len;
  177.  
  178. // Encode pubkey to binary and prepend pubkey size, then append the OP_CHECKSIG byte
  179. transaction->pubkeyScript = malloc((pubkey_len+2)*sizeof(uint8_t));
  180. pubkeyScript_len = hex2bin(transaction->pubkeyScript+1, pubkey, pubkey_len); // No error checking, yeah.
  181. transaction->pubkeyScript[0] = 0x41; // A public key is 32 bytes X coordinate, 32 bytes Y coordinate and one byte 0x04, so 65 bytes i.e 0x41 in Hex.
  182. pubkeyScript_len+=1;
  183. transaction->pubkeyScript[pubkeyScript_len++] = OP_CHECKSIG;
  184.  
  185. // Encode timestamp to binary
  186. transaction->scriptSig = malloc(scriptSig_len*sizeof(uint8_t));
  187. uint32_t scriptSig_pos = 0;
  188.  
  189.  
  190. // This is basically how I believe the size of the nBits is calculated
  191. if(nBits <= 255)
  192. {
  193. transaction->scriptSig[scriptSig_pos++] = 0x01;
  194. transaction->scriptSig[scriptSig_pos++] = (uint8_t)nBits;
  195. }
  196. else if(nBits <= 65535)
  197. {
  198. transaction->scriptSig[scriptSig_pos++] = 0x02;
  199. memcpy(transaction->scriptSig+scriptSig_pos, &nBits, 2);
  200. scriptSig_pos+=2;
  201. }
  202. else if(nBits <= 16777215)
  203. {
  204. transaction->scriptSig[scriptSig_pos++] = 0x03;
  205. memcpy(transaction->scriptSig+scriptSig_pos, &nBits, 3);
  206. scriptSig_pos+=3;
  207. }
  208. else //else if(nBits <= 4294967296LL)
  209. {
  210. transaction->scriptSig[scriptSig_pos++] = 0x04;
  211. memcpy(transaction->scriptSig+scriptSig_pos, &nBits, 4);
  212. scriptSig_pos+=4;
  213. }
  214.  
  215. // Important! In the Bitcoin code there is a statement 'CBigNum(4)'
  216. // i've been wondering for a while what it is but
  217. // seeing as alt-coins keep it the same, we'll do it here as well
  218. // It should essentially mean PUSH 1 byte on the stack which in this case is 0x04 or just 4
  219. transaction->scriptSig[scriptSig_pos++] = 0x01;
  220. transaction->scriptSig[scriptSig_pos++] = 0x04;
  221.  
  222. transaction->scriptSig[scriptSig_pos++] = (uint8_t)scriptSig_len;
  223.  
  224. scriptSig_len += scriptSig_pos;
  225. transaction->scriptSig = realloc(transaction->scriptSig, scriptSig_len*sizeof(uint8_t));
  226. memcpy(transaction->scriptSig+scriptSig_pos, (const unsigned char *)timestamp, timestamp_len);
  227.  
  228. // Here we are asuming some values will have the same size
  229. uint32_t serializedLen =
  230. 4 // tx version
  231. +1 // number of inputs
  232. +32 // hash of previous output
  233. +4 // previous output's index
  234. +1 // 1 byte for the size of scriptSig
  235. +scriptSig_len
  236. +4 // size of sequence
  237. +1 // number of outputs
  238. +8 // 8 bytes for coin value
  239. +1 // 1 byte to represent size of the pubkey Script
  240. +pubkeyScript_len
  241. +4; // 4 bytes for lock time
  242.  
  243. // Now let's serialize the data
  244. uint32_t serializedData_pos = 0;
  245. transaction->serializedData = malloc(serializedLen*sizeof(uint8_t));
  246. memcpy(transaction->serializedData+serializedData_pos, &transaction->version, 4);
  247. serializedData_pos += 4;
  248. memcpy(transaction->serializedData+serializedData_pos, &transaction->numInputs, 1);
  249. serializedData_pos += 1;
  250. memcpy(transaction->serializedData+serializedData_pos, transaction->prevOutput, 32);
  251. serializedData_pos += 32;
  252. memcpy(transaction->serializedData+serializedData_pos, &transaction->prevoutIndex, 4);
  253. serializedData_pos += 4;
  254. memcpy(transaction->serializedData+serializedData_pos, &scriptSig_len, 1);
  255. serializedData_pos += 1;
  256. memcpy(transaction->serializedData+serializedData_pos, transaction->scriptSig, scriptSig_len);
  257. serializedData_pos += scriptSig_len;
  258. memcpy(transaction->serializedData+serializedData_pos, &transaction->sequence, 4);
  259. serializedData_pos += 4;
  260. memcpy(transaction->serializedData+serializedData_pos, &transaction->numOutputs, 1);
  261. serializedData_pos += 1;
  262. memcpy(transaction->serializedData+serializedData_pos, &transaction->outValue, 8);
  263. serializedData_pos += 8;
  264. memcpy(transaction->serializedData+serializedData_pos, &pubkeyScript_len, 1);
  265. serializedData_pos += 1;
  266. memcpy(transaction->serializedData+serializedData_pos, transaction->pubkeyScript, pubkeyScript_len);
  267. serializedData_pos += pubkeyScript_len;
  268. memcpy(transaction->serializedData+serializedData_pos, &transaction->locktime, 4);
  269. serializedData_pos += 4;
  270.  
  271. // Now that the data is serialized
  272. // we hash it with SHA256 and then hash that result to get merkle hash
  273. SHA256(transaction->serializedData, serializedLen, hash1);
  274. SHA256(hash1, 32, hash2);
  275.  
  276. // This copy isn't necessary imo, but here for clarity
  277. memcpy(transaction->merkleHash, hash2, 32);
  278.  
  279. char *merkleHash = bin2hex(transaction->merkleHash, 32);
  280. byteswap(transaction->merkleHash, 32);
  281. char *merkleHashSwapped = bin2hex(transaction->merkleHash, 32);
  282. char *txScriptSig = bin2hex(transaction->scriptSig, scriptSig_len);
  283. char *pubScriptSig = bin2hex(transaction->pubkeyScript, pubkeyScript_len);
  284. printf("\nCoinbase: %s\n\nPubkeyScript: %s\n\nMerkle Hash: %s\nByteswapped: %s\n",txScriptSig, pubScriptSig, merkleHash, merkleHashSwapped);
  285.  
  286. //if(generateBlock)
  287. {
  288. printf("Generating block...\n");
  289. if(!unixtime)
  290. {
  291. unixtime = time(NULL);
  292. }
  293.  
  294. unsigned char block_header[80], block_hash1[32], block_hash2[32];
  295. uint32_t blockversion = 1;
  296. memcpy(block_header, &blockversion, 4);
  297. memset(block_header+4, 0, 32);
  298. byteswap(transaction->merkleHash, 32); // We swapped it before, so do it again now.
  299. memcpy(block_header+36, transaction->merkleHash, 32);
  300. memcpy(block_header+68, &unixtime, 4);
  301. memcpy(block_header+72, &nBits, 4);
  302. memcpy(block_header+76, &startNonce, 4);
  303.  
  304. uint32_t *pNonce = (uint32_t *)(block_header + 76);
  305. uint32_t *pUnixtime = (uint32_t *)(block_header + 68);
  306. unsigned int counter, start = time(NULL);
  307. while(1)
  308. {
  309. SHA256(block_header, 80, block_hash1);
  310. SHA256(block_hash1, 32, block_hash2);
  311.  
  312. unsigned int check = *((uint32_t *)(block_hash2 + 28)); // The hash is in little-endian, so we check the last 4 bytes.
  313. if(check == 0) // \x00\x00\x00\x00
  314. {
  315. byteswap(block_hash2, 32);
  316. char *blockHash = bin2hex(block_hash2, 32);
  317. printf("\nBlock found!\nHash: %s\nNonce: %u\nUnix time: %u", blockHash, startNonce, unixtime);
  318. free(blockHash);
  319. break;
  320. }
  321.  
  322. startNonce++;
  323. counter+=1;
  324. if(time(NULL)-start >= 1)
  325. {
  326. printf("\r%d Hashes/s, Nonce %u\r", counter, startNonce);
  327. counter = 0;
  328. start = time(NULL);
  329. }
  330. *pNonce = startNonce;
  331. if(startNonce > 4294967294LL)
  332. {
  333. //printf("\nBlock found!\nHash: %s\nNonce: %u\nUnix time: %u", blockHash, startNonce, unixtime);
  334. unixtime++;
  335. *pUnixtime = unixtime;
  336. startNonce = 0;
  337. }
  338. }
  339. }
  340.  
  341.  
  342. // Lots of cleanup
  343. free(merkleHash);
  344. free(merkleHashSwapped);
  345. free(txScriptSig);
  346. free(pubScriptSig);
  347. free(transaction->serializedData);
  348. free(transaction->scriptSig);
  349. free(transaction->pubkeyScript);
  350. free(transaction);
  351.  
  352. return 0;
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement