From b15d28b2b776e440b585250b9fb191dcac8c53f1 Mon Sep 17 00:00:00 2001 From: DesWurstes Date: Sun, 4 Mar 2018 15:50:46 +0300 Subject: [PATCH] Add CSV/TSV output format, fix an output bug --- oclvanitygen.c | 57 ++++++++++++++++++++++++++++++++++------ pattern.c | 69 +++++++++++++++++++++++++++--------------------- pattern.h | 1 + vanitygen.c | 71 +++++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 151 insertions(+), 47 deletions(-) diff --git a/oclvanitygen.c b/oclvanitygen.c index 89e5fca..c079f2e 100644 --- a/oclvanitygen.c +++ b/oclvanitygen.c @@ -38,7 +38,7 @@ void usage(const char *name) { fprintf(stderr, -"\x1B[44moclVanitygen Cash %s\x1B[0m(" OPENSSL_VERSION_TEXT ")\n" +"\x1B[44moclVanitygen Cash %s\x1B[0m (" OPENSSL_VERSION_TEXT ")\n" "Usage: %s [-vcqk1NTS] [-d ] [-f |-] [...]\n" "Generates a bitcoin receiving address matching , and outputs the\n" "address and associated private key. The private key may be stored in a safe\n" @@ -72,7 +72,8 @@ usage(const char *name) "-V Enable kernel/OpenCL/hardware verification (SLOW)\n" "-f File containing list of patterns, one per line\n" " (Use \"-\" as the file name for stdin)\n" -"-o Write pattern matches to \n" +"-o Write pattern matches to in TSV format (readable)\n" +"-O Write pattern matches to in CSV format (importable e.g. Excel)\n" "-s Seed random number generator from \n", version, name); } @@ -105,6 +106,7 @@ main(int argc, char **argv) vg_ocl_context_t *vocp = NULL; EC_POINT *pubkey_base = NULL; const char *result_file = NULL; + const char *result_file_csv = NULL; char *devstrs[MAX_DEVS]; int ndevstrs = 0; int opened = 0; @@ -116,7 +118,7 @@ main(int argc, char **argv) int i; while ((opt = getopt(argc, argv, - "vcqk1Tp:P:d:w:t:g:b:VSh?f:o:s:D:")) != -1) { + "vcqk1Tp:P:d:w:t:g:b:VSh?f:o:O:s:D:")) != -1) { switch (opt) { case 'v': verbose = 2; @@ -253,11 +255,19 @@ main(int argc, char **argv) case 'o': if (result_file) { fprintf(stderr, - "Multiple output files specified\n"); + "Multiple TSV output files specified\n"); return 1; } result_file = optarg; break; + case 'O': + if (result_file_csv) { + fprintf(stderr, + "Multiple CSV output files specified\n"); + return 1; + } + result_file_csv = optarg; + break; case 's': if (seedfile != NULL) { fprintf(stderr, @@ -272,10 +282,6 @@ main(int argc, char **argv) } } - if (verbose == 2) { - printf("Built on %s.\n", __DATE__); - } - #if OPENSSL_VERSION_NUMBER < 0x10000000L /* Complain about older versions of OpenSSL */ if (verbose > 0) { @@ -323,8 +329,43 @@ main(int argc, char **argv) vcp = vg_prefix_context_new(addrtype, privtype, testnet); } + if (result_file) { + FILE *fp = fopen(result_file, "a"); + if (!fp) { + fprintf(stderr, + "ERROR: could not open TSV result file: %s\n", + strerror(errno)); + return 1; + } else { + fprintf(fp, "Pattern\tAddress\t"); + if (pubkey_base == NULL) + fprintf(fp, "Private Key\n"); + else + fprintf(fp, "Private Key Part\n"); + fclose(fp); + } + } + + if (result_file_csv) { + FILE *fp = fopen(result_file_csv, "a"); + if (!fp) { + fprintf(stderr, + "ERROR: could not open CSV result file: %s\n", + strerror(errno)); + return 1; + } else { + fprintf(fp, "Pattern\tAddress\t"); + if (pubkey_base == NULL) + fprintf(fp, "Private Key\n"); + else + fprintf(fp, "Private Key Part\n"); + fclose(fp); + } + } + vcp->vc_verbose = verbose; vcp->vc_result_file = result_file; + vcp->vc_result_file_csv = result_file_csv; vcp->vc_remove_on_match = remove_on_match; vcp->vc_only_one = only_one; vcp->vc_pubkeytype = addrtype; diff --git a/pattern.c b/pattern.c index 463debc..afd3022 100644 --- a/pattern.c +++ b/pattern.c @@ -492,11 +492,11 @@ vg_output_timing_console(vg_context_t *vcp, double count, if (time > 1000000) { p = snprintf(&linebuf[p], rem, - "\x1B[34m[%d%% in %e%s]\x1B[0m", + "\x1B[34m[%d%% in %e%s]", (int) (100 * targ), time, unit); } else { p = snprintf(&linebuf[p], rem, - "\x1B[34m[%d%% in %.1f%s]\x1B[0m", + "\x1B[34m[%d%% in %.1f%s]", (int) (100 * targ), time, unit); } assert(p > 0); @@ -509,17 +509,18 @@ vg_output_timing_console(vg_context_t *vcp, double count, if (vcp->vc_found) { if (vcp->vc_remove_on_match) - p = snprintf(&linebuf[p], rem, "\x1B[34m[Found %lld/%ld]\x1B[0m", + p = snprintf(&linebuf[p], rem, "[Found %lld/%ld]", vcp->vc_found, vcp->vc_npatterns_start); else - p = snprintf(&linebuf[p], rem, "\x1B[34m[Found %lld]\x1B[0m", + p = snprintf(&linebuf[p], rem, "[Found %lld]", vcp->vc_found); assert(p > 0); rem -= p; if (rem < 0) rem = 0; } - + p = snprintf(&linebuf[p], rem, + "\x1B[0m "); if (rem) { memset(&linebuf[sizeof(linebuf)-rem], 0x20, rem); linebuf[sizeof(linebuf)-1] = '\0'; @@ -576,27 +577,24 @@ vg_output_match_console(vg_context_t *vcp, EC_KEY *pkey, const char *pattern, in else vg_encode_privkey(pkey, vcp->vc_privtype, privkey_buf); - if (!vcp->vc_result_file || (vcp->vc_verbose > 0)) { + if (vcp->vc_verbose > 0 || !vcp->vc_result_file || !vcp->vc_result_file_csv) { printf("\r%79s\rPattern: \x1b[33m%s\x1b[0m\n", "", pattern); } - if (vcp->vc_verbose > 0) { - if (vcp->vc_verbose > 1) { - pend = key_buf; - len = i2o_ECPublicKey(pkey, &pend); - printf("Pubkey (hex): "); - dumphex(key_buf, len); - printf("Privkey (hex): "); - dumpbn(EC_KEY_get0_private_key(pkey)); - pend = key_buf; - len = i2d_ECPrivateKey(pkey, &pend); - printf("Privkey (ASN1): "); - dumphex(key_buf, len); - } - + if (vcp->vc_verbose > 1) { + pend = key_buf; + len = i2o_ECPublicKey(pkey, &pend); + printf("Pubkey (hex): "); + dumphex(key_buf, len); + printf("Privkey (hex): "); + dumpbn(EC_KEY_get0_private_key(pkey)); + pend = key_buf; + len = i2d_ECPrivateKey(pkey, &pend); + printf("Privkey (ASN1): "); + dumphex(key_buf, len); } - if (!vcp->vc_result_file || (vcp->vc_verbose > 0)) { + if (vcp->vc_verbose > 0 || !vcp->vc_result_file || !vcp->vc_result_file_csv) { if (isscript) { printf("P2SHAddress: \x1b[32m%s\x1b[0m\n", addr2_buf); } else { @@ -609,18 +607,31 @@ vg_output_match_console(vg_context_t *vcp, EC_KEY *pkey, const char *pattern, in FILE *fp = fopen(vcp->vc_result_file, "a"); if (!fp) { fprintf(stderr, - "ERROR: could not open result file: %s\n", + "ERROR: could not open TSV result file: %s\n", + strerror(errno)); + } else { + fprintf(fp, "%s\t", pattern); + if (isscript) + fprintf(fp, "%s\t", addr2_buf); + else + fprintf(fp, "%s\n", addr_buf); + fprintf(fp, "%s\n", privkey_buf); + fclose(fp); + } + } + if (vcp->vc_result_file_csv) { + FILE *fp = fopen(vcp->vc_result_file_csv, "a"); + if (!fp) { + fprintf(stderr, + "ERROR: could not open CSV result file: %s\n", strerror(errno)); } else { - fprintf(fp, - "Pattern: %s\n" - , pattern); + fprintf(fp, "%s,", pattern); if (isscript) - fprintf(fp, "P2SHAddress: %s\n", addr2_buf); + fprintf(fp, "%s,", addr2_buf); else - fprintf(fp, - "Address: %s\n", addr_buf,); - fprintf(fp, "%s: %s\n", keytype, privkey_buf); + fprintf(fp, "%s\n", addr_buf); + fprintf(fp, "%s\n", privkey_buf); fclose(fp); } } diff --git a/pattern.h b/pattern.h index 1fea5e6..ba8ff27 100644 --- a/pattern.h +++ b/pattern.h @@ -103,6 +103,7 @@ struct _vg_context_s { int vc_pattern_generation; double vc_chance; const char *vc_result_file; + const char *vc_result_file_csv; int vc_remove_on_match; int vc_only_one; int vc_verbose; diff --git a/vanitygen.c b/vanitygen.c index 189a053..37b6479 100644 --- a/vanitygen.c +++ b/vanitygen.c @@ -266,6 +266,7 @@ vg_thread_loop(void *arg) npoints = 0; rekey_at = 0; i = nbatch; + goto outloop; break; case 2: goto out; @@ -297,7 +298,7 @@ vg_thread_loop(void *arg) } } } - + outloop: c += i << 1; if (c >= output_interval) { output_interval = vg_output_timing(vcp, c, &tvstart); @@ -369,7 +370,7 @@ void usage(const char *name) { fprintf(stderr, -"\x1B[44mVanitygen Cash %s\x1B[0m(" OPENSSL_VERSION_TEXT ")\n" +"\x1B[44mVanitygen Cash %s\x1B[0m (" OPENSSL_VERSION_TEXT ")\n" "Usage: %s [-vcqnrk1T] [-t ] [-f |-] [...]\n" "Generates a bitcoin receiving address matching , and outputs the\n" "address and associated private key. The private key may be stored in a safe\n" @@ -387,12 +388,13 @@ usage(const char *name) "-k Keep pattern and continue search after finding a match\n" "-1 Stop after first match\n" "-T Generate Bitcoin Cash testnet address\n" -"-F Generate address with the given format (pubkey or script)\n" +"-F Generate address with the given format (pubkey or script) (EXPERTS ONLY!)\n" "-P Specify base public key for piecewise key generation\n" "-t Set number of worker threads (Default: number of CPUs)\n" "-f File containing list of patterns, one per line\n" " (Use \"-\" as the file name for stdin)\n" -"-o Write pattern matches to \n" +"-o Write pattern matches to in TSV format (readable)\n" +"-O Write pattern matches to in CSV format (importable e.g. Excel)\n" "-s Seed random number generator from \n", version, name); } @@ -409,13 +411,14 @@ main(int argc, char **argv) int pubkeytype; enum vg_format format = VCF_PUBKEY; int regex = 0; - int verbose = 1; + unsigned int verbose = 1; int simulate = 0; int remove_on_match = 1; int only_one = 0; int opt; char *seedfile = NULL; const char *result_file = NULL; + const char *result_file_csv = NULL; char **patterns; int npatterns = 0; int nthreads = 0; @@ -428,7 +431,7 @@ main(int argc, char **argv) unsigned int i; - while ((opt = getopt(argc, argv, "cvqnrk1P:TF:t:h?f:o:s:")) != -1) { + while ((opt = getopt(argc, argv, "cvqnrk1P:TF:t:h?f:o:O:s:")) != -1) { switch (opt) { case 'c': fprintf(stderr, "%s\n", @@ -528,11 +531,19 @@ main(int argc, char **argv) case 'o': if (result_file) { fprintf(stderr, - "Multiple output files specified\n"); + "Multiple TSV output files specified\n"); return 1; } result_file = optarg; break; + case 'O': + if (result_file_csv) { + fprintf(stderr, + "Multiple CSV output files specified\n"); + return 1; + } + result_file_csv = optarg; + break; case 's': if (seedfile != NULL) { fprintf(stderr, @@ -546,9 +557,6 @@ main(int argc, char **argv) return 1; } } - if (verbose == 2) { - printf("Built on %s.\n", __DATE__); - } #if OPENSSL_VERSION_NUMBER < 0x10000000L /* Complain about older versions of OpenSSL */ @@ -606,8 +614,51 @@ main(int argc, char **argv) vcp = vg_prefix_context_new(addrtype, privtype, testnet); } + if (result_file) { + FILE *fp = fopen(result_file, "a"); + if (!fp) { + fprintf(stderr, + "ERROR: could not open TSV result file: %s\n", + strerror(errno)); + return 1; + } else { + fprintf(fp, "Pattern\t"); + if (format == VCF_PUBKEY) + fprintf(fp, "Address\t"); + else + fprintf(fp, "P2SH Address\t"); + if (pubkey_base == NULL) + fprintf(fp, "Private Key\n"); + else + fprintf(fp, "Private Key Part\n"); + fclose(fp); + } + } + + if (result_file_csv) { + FILE *fp = fopen(result_file_csv, "a"); + if (!fp) { + fprintf(stderr, + "ERROR: could not open CSV result file: %s\n", + strerror(errno)); + return 1; + } else { + fprintf(fp, "Pattern,"); + if (format == VCF_PUBKEY) + fprintf(fp, "Address,"); + else + fprintf(fp, "P2SH Address,"); + if (pubkey_base == NULL) + fprintf(fp, "Private Key\n"); + else + fprintf(fp, "Private Key Part\n"); + fclose(fp); + } + } + vcp->vc_verbose = verbose; vcp->vc_result_file = result_file; + vcp->vc_result_file_csv = result_file_csv; vcp->vc_remove_on_match = remove_on_match; vcp->vc_only_one = only_one; vcp->vc_format = format;