Guest User

Bitcoin transaction file dumper v2

a guest
Oct 25th, 2011
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.52 KB | None | 0 0
  1. /*
  2. gcc bctrans.c -o bctrans.exe -lcrypto -I /local/ssl/include -L /local/ssl/lib
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stdlib.h>
  8. #include <strings.h>
  9. #include <openssl/sha.h>
  10. #include <openssl/bn.h>
  11.  
  12. static FILE *f;
  13.  
  14. BIGNUM *bn58, dv, mo;
  15. BN_CTX *ctx;
  16.  
  17.  
  18. #define SHA256(p,l,o) { \
  19.     SHA256_CTX shactx; \
  20.     SHA256_Init(&shactx); \
  21.     SHA256_Update(&shactx, (p), (l)); \
  22.     SHA256_Final((o), &shactx); }
  23.  
  24.  
  25. void readfile(unsigned char *p, int len) {
  26.     if (fread(p, 1, len, f)!=len) {
  27.         fprintf(stderr, "File too short\n");
  28.         fclose(f);
  29.         exit(1);
  30.     }
  31. }
  32.  
  33. unsigned long long getle(unsigned char *p, int bytes) {
  34.     unsigned long long res=0;
  35.     while (bytes--) {
  36.         res|= p[bytes]<<(8*bytes);
  37.     }
  38.     return res;
  39. }
  40.  
  41. unsigned long long getvl() {
  42.     unsigned char b[8];
  43.     readfile(b, 1);
  44.     switch (*b) {
  45.         case 0xfd:
  46.             readfile(b, 2);
  47.             return getle(b, 2);
  48.         case 0xfe:
  49.             readfile(b, 4);
  50.             return getle(b, 4);
  51.         case 0xff:
  52.             readfile(b, 8);
  53.             return getle(b, 8);
  54.     }
  55.     return *b;
  56. }
  57.  
  58.  
  59. void prhash(unsigned char *p, unsigned int l) {
  60.     while (l--) printf("%02x", p[l]);
  61. }
  62.  
  63.  
  64. void hexdump(unsigned char *p, unsigned int l) {
  65.     while (l--) printf("%02x", *p++);
  66. }
  67.  
  68.  
  69. void printbtcaddr(unsigned char *p) {
  70.     static const char *chrs = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
  71.     unsigned char mod;
  72.     char out[64];
  73.     int i=0;
  74.     BIGNUM *bn = BN_bin2bn(p, 24, NULL);
  75.     while (!BN_is_zero(bn)) {
  76.         BN_div(&dv, &mo, bn, bn58, ctx);
  77.         if (BN_bn2bin(&mo, &mod)==0)  mod = 0;
  78.         out[i++] = chrs[mod];
  79.         BN_copy(bn, &dv);
  80.     }
  81.     BN_free(bn);
  82.     putchar('1');
  83.     while (i--)  putchar(out[i]);
  84. }
  85.  
  86.  
  87. int main(int argc, char * argv[]) {
  88.     static unsigned char buf[0x10000];
  89.     unsigned long long i, sl, txcnt, v;
  90.     long fpos;
  91.  
  92.     if (argc!=2) {
  93.         printf("Specify the filename of the transaction file (.btd)\n");
  94.         return 1;
  95.     }
  96.  
  97.     f = fopen(argv[1], "rb");
  98.     if (!f) {
  99.         fprintf(stderr, "File %s not found\n", argv[1]);
  100.         return 1;
  101.     }
  102.  
  103.     readfile(buf, 4);
  104.     printf("Version: %llu\n", getle(buf, 4));
  105.  
  106.     txcnt = getvl();
  107.     printf("TX IN cnt: %llu\n", txcnt);
  108.     for (i=0; i<txcnt; i++) {
  109.         readfile(buf, 36);
  110.         sl = getvl();
  111.  
  112.         printf("%6lld) : ", i);
  113.         prhash(buf, 32);
  114.         printf(" Idx=%2lld  sl=%lld", getle(buf+32, 4), sl);
  115.         readfile(buf, sl);
  116.         readfile(buf, 4);
  117.  
  118.         printf(" seq=%x\n", (unsigned)getle(buf, 4));
  119.     }
  120.  
  121.     txcnt = getvl();
  122.     printf("TX OUT cnt: %llu\n", txcnt);
  123.  
  124.     ctx = BN_CTX_new();
  125.     bn58 = BN_bin2bn("\x3a", 1, NULL);
  126.     BN_init(&dv);
  127.     BN_init(&mo);
  128.  
  129.     for (i=0; i<txcnt; i++) {
  130.         readfile(buf, 8);
  131.         sl = getvl();
  132.         v = getle(buf, 8);
  133.         printf("%6lld) : %7lld.%08lld BTC", i, v/100000000, v%100000000, sl);
  134.         readfile(buf, sl);
  135.         if (sl!=25 || memcmp(buf, "\x76\xa9\x14", 3) || buf[23]!=0x88 || buf[24]!=0xac) {
  136.             printf("  WARNING! Unexpected SIG_SCRIPT:\n"); hexdump(buf, sl);
  137.         } else {
  138.             unsigned char dat[24];
  139.             unsigned char sha[SHA256_DIGEST_LENGTH];
  140.             *dat = 0; // version
  141.             memcpy(dat+1, buf+3, 20);
  142.             SHA256(dat, 21, sha);
  143.             SHA256(sha, SHA256_DIGEST_LENGTH, dat);
  144.             memcpy(dat+20, dat, 4); memcpy(dat, buf+3, 20);
  145.             printf("  to address "); printbtcaddr(dat);
  146.         }
  147.         putchar('\n');
  148.     }
  149.  
  150.     BN_free(bn58);
  151.     BN_CTX_free(ctx);
  152.  
  153.     readfile(buf, 4);
  154.     printf("Lock Time: %llu\n", getle(buf, 4));
  155.  
  156.     fpos = ftell(f);
  157.     fseek(f, 0, SEEK_END);
  158.     if (fpos!=ftell(f)) {
  159.         printf("WARNING!!! File too long. Only %ld bytes expected (%ld too many)\n",
  160.             fpos, ftell(f)-fpos);
  161.     } else {
  162.         printf("File size checked OK - %ld bytes\n", fpos);
  163.     }
  164.  
  165.     fclose(f);
  166.     return 0;
  167. }
  168.  
  169.  
Add Comment
Please, Sign In to add comment