commit d31151978a1a409efcea4d2aa0c6257284cb8bf0
parent ae5f1223de708e8606cd46d716ea885dd22040c8
Author: nickfarrow <nicholas.w.farrow@gmail.com>
Date: Wed, 14 Jul 2021 01:15:17 +1000
Code formatting
Diffstat:
M | base58.h | | | 6 | +++--- |
M | createPubKey.h | | | 60 | ++++++++++++++++++++++++++++++++---------------------------- |
M | niceBit.c | | | 266 | +++++++++++++++++++++++++++++++++++++++---------------------------------------- |
M | walletImportFormat.h | | | 84 | ++++++++++++++++++++++++++++++++++++++++---------------------------------------- |
4 files changed, 209 insertions(+), 207 deletions(-)
diff --git a/base58.h b/base58.h
@@ -3,10 +3,10 @@
typedef unsigned char byte;
/* See https://en.wikipedia.org/wiki/Positional_notation#Base_conversion */
-char* base58(byte *s, int s_size, char *out, int out_size) {
+char *base58(byte *s, int s_size, char *out, int out_size) {
static const char *base_chars = "123456789"
- "ABCDEFGHJKLMNPQRSTUVWXYZ"
- "abcdefghijkmnopqrstuvwxyz";
+ "ABCDEFGHJKLMNPQRSTUVWXYZ"
+ "abcdefghijkmnopqrstuvwxyz";
byte s_cp[s_size];
memcpy(s_cp, s, s_size);
diff --git a/createPubKey.h b/createPubKey.h
@@ -1,48 +1,52 @@
-#include <stdio.h>
-#include <string.h>
+#include "base58.h"
#include <ctype.h>
-#include <openssl/sha.h>
#include <openssl/ripemd.h>
-#include "base58.h"
+#include <openssl/sha.h>
+#include <stdio.h>
+#include <string.h>
typedef unsigned char byte;
int is_hex(const char *s) {
- int i;
- for (i = 0; i < 64; i++)
- if (!isxdigit(s[i])) return 0;
- return 1;
+ int i;
+ for (i = 0; i < 64; i++)
+ if (!isxdigit(s[i]))
+ return 0;
+ return 1;
}
void str_to_byte(const char *src, byte *dst, int n) {
- while (n--) sscanf(src + n * 2, "%2hhx", dst + n);
+ while (n--)
+ sscanf(src + n * 2, "%2hhx", dst + n);
}
void pubkey_to_P2PKH(const unsigned char *pubkey64, char *out) {
- byte s[65];
- byte rmd[5 + RIPEMD160_DIGEST_LENGTH];
+ byte s[65];
+ byte rmd[5 + RIPEMD160_DIGEST_LENGTH];
- int j;
- for (j = 0; j < 65; j++) {
- s[j] = pubkey64[j];
- }
+ int j;
+ for (j = 0; j < 65; j++) {
+ s[j] = pubkey64[j];
+ }
- rmd[0] = 0;
- RIPEMD160(SHA256(s, 65, 0), SHA256_DIGEST_LENGTH, rmd + 1);
+ rmd[0] = 0;
+ RIPEMD160(SHA256(s, 65, 0), SHA256_DIGEST_LENGTH, rmd + 1);
- memcpy(rmd + 21, SHA256(SHA256(rmd, 21, 0), SHA256_DIGEST_LENGTH, 0), 4);
+ memcpy(rmd + 21, SHA256(SHA256(rmd, 21, 0), SHA256_DIGEST_LENGTH, 0), 4);
- base58(rmd, 25, out, 34);
+ base58(rmd, 25, out, 34);
- /* Count the number of extra 1s at the beginning of the address */
- int k;
- for (k = 1; out[k] == '1'; k++);
+ /* Count the number of extra 1s at the beginning of the address */
+ int k;
+ for (k = 1; out[k] == '1'; k++)
+ ;
- /* Count the number of extra leading 0x00 bytes */
- int n;
- for (n = 1; rmd[n] == 0x00; n++);
+ /* Count the number of extra leading 0x00 bytes */
+ int n;
+ for (n = 1; rmd[n] == 0x00; n++)
+ ;
- /* Remove k-n leading 1's from the address */
- memmove(out, out + (k-n), 34-(k-n));
- out[34-(k-n)] = '\0';
+ /* Remove k-n leading 1's from the address */
+ memmove(out, out + (k - n), 34 - (k - n));
+ out[34 - (k - n)] = '\0';
}
diff --git a/niceBit.c b/niceBit.c
@@ -1,18 +1,19 @@
+#include "createPubKey.h"
+#include "walletImportFormat.h"
+#include <ctype.h>
+#include <secp256k1.h>
#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
#include <time.h>
-#include <ctype.h>
-#include <secp256k1.h>
-#include "createPubKey.h"
-#include "walletImportFormat.h"
+#include <unistd.h>
#include <unistd.h>
static secp256k1_context *ctx = NULL;
/* Create private & public address pair */
-int gen_keypair(unsigned char *seckey, char *pubaddress, secp256k1_context *ctx) {
+int gen_keypair(unsigned char *seckey, char *pubaddress,
+ secp256k1_context *ctx) {
secp256k1_pubkey pubkey;
unsigned char public_key64[65];
@@ -38,15 +39,14 @@ int gen_keypair(unsigned char *seckey, char *pubaddress, secp256k1_context *ctx)
*/
/* Apparently there is a 2^-128 chance of
- * a secret key being invalid.
- * https://en.bitcoin.it/wiki/Private_key
- */
+ * a secret key being invalid.
+ * https://en.bitcoin.it/wiki/Private_key
+ */
/* Verify secret key is valid */
if (!secp256k1_ec_seckey_verify(ctx, seckey)) {
printf("Invalid secret key\n");
}
-
/* Create Public Key */
if (!secp256k1_ec_pubkey_create(ctx, &pubkey, seckey)) {
printf("Failed to create public key\n");
@@ -54,13 +54,8 @@ int gen_keypair(unsigned char *seckey, char *pubaddress, secp256k1_context *ctx)
}
/* Serialize Public Key */
- secp256k1_ec_pubkey_serialize(
- ctx,
- public_key64,
- &pk_len,
- &pubkey,
- SECP256K1_EC_UNCOMPRESSED
- );
+ secp256k1_ec_pubkey_serialize(ctx, public_key64, &pk_len, &pubkey,
+ SECP256K1_EC_UNCOMPRESSED);
/* Print public key */
/*
@@ -72,8 +67,8 @@ int gen_keypair(unsigned char *seckey, char *pubaddress, secp256k1_context *ctx)
*/
/* Generate Public Address
- * (from create_pubkey.h)
- */
+ * (from create_pubkey.h)
+ */
pubkey_to_P2PKH(public_key64, pubaddress);
return 1;
@@ -81,22 +76,23 @@ int gen_keypair(unsigned char *seckey, char *pubaddress, secp256k1_context *ctx)
int check_nums(char *pubaddress, int searchlen) {
unsigned char compstr[40];
- char possibleChars[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
+ char possibleChars[] =
+ "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
int j;
/* For each vanity length
- * ('len' digits in a row)
- */
- for (int len=10; len>=searchlen; len--) {
+ * ('len' digits in a row)
+ */
+ for (int len = 10; len >= searchlen; len--) {
/* For each digit 1-9 */
- for (int i=0; i<58; i++) {
+ for (int i = 0; i < 58; i++) {
/* Comprise compstr of 'len' repeats
- * of digit 'i'
- */
+ * of digit 'i'
+ */
j = 0;
- while (j<len) {
+ while (j < len) {
compstr[j] = possibleChars[i];
j++;
}
@@ -115,7 +111,7 @@ int check_nums(char *pubaddress, int searchlen) {
}
int check_words(char *pubaddress, char (*words)[34], int nwords) {
- for (int i=0; i<nwords; i++) {
+ for (int i = 0; i < nwords; i++) {
/* printf("%s\n", words[i]);
puts(pubaddress); */
if (strstr(pubaddress, words[i]) != NULL) {
@@ -126,72 +122,73 @@ int check_words(char *pubaddress, char (*words)[34], int nwords) {
return 0;
}
-int check_vanity(char *pubaddress, int searchlen, char (*words)[34], int nwords) {
+int check_vanity(char *pubaddress, int searchlen, char (*words)[34],
+ int nwords) {
if ((nwords > 0) && check_words(pubaddress, words, nwords)) {
return 1;
}
- if ((searchlen >0) && check_nums(pubaddress, searchlen)) {
+ if ((searchlen > 0) && check_nums(pubaddress, searchlen)) {
return 1;
}
return 0;
}
-void all_substitutes(char (*words)[34], char word[34], int *n_words, int len, int start, int a){
- if (start == len){
+void all_substitutes(char (*words)[34], char word[34], int *n_words, int len,
+ int start, int a) {
+ if (start == len) {
strcpy(words[*n_words], word);
- words[*n_words-1][len] = '\0';
+ words[*n_words - 1][len] = '\0';
(*n_words)++;
return;
}
char ch = word[start];
char sub = '\0';
- switch (ch){
- case 'e':
- if (a == 1){
- sub = '3';
- }
- break;
- case '1':
- if (a == 1){
- sub = 'i';
- }
- break;
- case 'o':
- // No base58 sub
- break;
- case 'L':
- // No base58 sub
- break;
- case 'i':
- // No base58 sub
- break;
- default:
- if(ch >= 'a' && ch <= 'z'){
- sub = toupper(ch);
- }
- else if(ch >= 'A' && ch <= 'Z'){
- sub = tolower(ch);
- }
- break;
+ switch (ch) {
+ case 'e':
+ if (a == 1) {
+ sub = '3';
+ }
+ break;
+ case '1':
+ if (a == 1) {
+ sub = 'i';
+ }
+ break;
+ case 'o':
+ // No base58 sub
+ break;
+ case 'L':
+ // No base58 sub
+ break;
+ case 'i':
+ // No base58 sub
+ break;
+ default:
+ if (ch >= 'a' && ch <= 'z') {
+ sub = toupper(ch);
+ } else if (ch >= 'A' && ch <= 'Z') {
+ sub = tolower(ch);
+ }
+ break;
}
- //Skip on special chars
+ // Skip on special chars
if (sub == '\0') {
- all_substitutes(words, word, n_words, len, start+1, a);
- }
- else {
+ all_substitutes(words, word, n_words, len, start + 1, a);
+ } else {
char word2[34];
strcpy(word2, word);
- word2[start]=sub;
- all_substitutes(words, word, n_words, len, start+1, a);
- all_substitutes(words, word2, n_words, len, start+1, a);
+ word2[start] = sub;
+ all_substitutes(words, word, n_words, len, start + 1, a);
+ all_substitutes(words, word2, n_words, len, start + 1, a);
}
}
-void alphanum_combinations(char (*word)[34], char search_list[100000][34], int *n_words, int a) {
+void alphanum_combinations(char (*word)[34], char search_list[100000][34],
+ int *n_words, int a) {
int initial_n_words = *n_words;
*n_words = 0;
- //Generated alphanumeric substitutions
- for (int i=0; i<initial_n_words; i++) {
+ // Generated alphanumeric substitutions
+ for (int i = 0; i < initial_n_words; i++) {
all_substitutes(search_list, word[i], n_words, strlen(word[i]), 0, a);
}
}
@@ -214,19 +211,19 @@ int main(int argc, char **argv) {
/* Get input arguments (length) */
while ((c = getopt(argc, argv, "aCn:f:")) != -1) {
switch (c) {
- case 'n':
+ case 'n':
n = optarg;
break;
- case 'f':
+ case 'f':
filename = optarg;
break;
- case 'a':
+ case 'a':
a = 1;
break;
- case 'C':
+ case 'C':
C = 1;
break;
- case '?':
+ case '?':
printf("Invalid argument: %c\n", optopt);
return 1;
}
@@ -237,82 +234,83 @@ int main(int argc, char **argv) {
n = "6";
}
searchlen = atoi(n);
-
- ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
- int i = 1;
+ ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN |
+ SECP256K1_CONTEXT_VERIFY);
- time_t start_time, current_time;
- double time_spent;
- double rate;
+ int i = 1;
- char words[100][34];
+ time_t start_time, current_time;
+ double time_spent;
+ double rate;
- //Generated alphanumeric substitutions
- char search_list[100000][34];
- int n_words = 0;
- /* Load Dictionary File */
- if (filename != "") {
- puts("Loading words...");
- FILE *fptr = fopen(filename, "r");
+ char words[100][34];
- while (fgets(words[n_words], 34, fptr)) {
- words[n_words][strlen(words[n_words]) - 1] = '\0';
- puts(words[n_words]);
- n_words++;
- }
- if (C){
- alphanum_combinations(words, search_list, &n_words, a);
+ // Generated alphanumeric substitutions
+ char search_list[100000][34];
+ int n_words = 0;
+ /* Load Dictionary File */
+ if (filename != "") {
+ puts("Loading words...");
+ FILE *fptr = fopen(filename, "r");
- for (i=0; i<n_words; i++) {
- printf("word %d is %s \n", i, search_list[i]);
- }
+ while (fgets(words[n_words], 34, fptr)) {
+ words[n_words][strlen(words[n_words]) - 1] = '\0';
+ puts(words[n_words]);
+ n_words++;
+ }
+ if (C) {
+ alphanum_combinations(words, search_list, &n_words, a);
- //search_list[n_words][0] = '\0';
+ for (i = 0; i < n_words; i++) {
+ printf("word %d is %s \n", i, search_list[i]);
}
- else {
- for (i=0; i<n_words; i++) {
- strcpy(search_list[i], words[i]);
- }
+ // search_list[n_words][0] = '\0';
+ } else {
+ for (i = 0; i < n_words; i++) {
+ strcpy(search_list[i], words[i]);
}
- printf("%d words have been loaded.\n", n_words);
}
+ printf("%d words have been loaded.\n", n_words);
+ }
-
- printf("Running on %d processors.\n", number_of_processors);
- puts("Beginning search...\n");
- for (unsigned i = 1; i != number_of_processors; ++i) if ( 0 == fork() ) break;
-
- time(&start_time);
- while (1) {
- if (!gen_keypair(seckey, pubaddress, ctx)) {
- printf("Failed to create keypair\n");
- return 1;
- }
+ printf("Running on %d processors.\n", number_of_processors);
+ puts("Beginning search...\n");
+ for (unsigned i = 1; i != number_of_processors; ++i)
+ if (0 == fork())
+ break;
- if (check_vanity(pubaddress, searchlen, search_list, n_words)) {
- printf("Private key (raw): ");
- for (int j=0; j<32; j++) {
- printf("%02X", seckey[j]);
- }
- printf("Private key (WIF): ");
- create_wif(seckey);
+ time(&start_time);
+ while (1) {
+ if (!gen_keypair(seckey, pubaddress, ctx)) {
+ printf("Failed to create keypair\n");
+ return 1;
+ }
- printf("Public Address: %s\n\n", pubaddress);
+ if (check_vanity(pubaddress, searchlen, search_list, n_words)) {
+ printf("Private key (raw): ");
+ for (int j = 0; j < 32; j++) {
+ printf("%02X", seckey[j]);
}
+ printf("Private key (WIF): ");
+ create_wif(seckey);
- if (i % 100000 == 0) {
- time(¤t_time);
- time_spent = difftime(current_time, start_time);
+ printf("Public Address: %s\n\n", pubaddress);
+ }
- rate = (double)(i / time_spent);
- //printf("Generated %d addresses in %.1fs. Rate:%.1f/s \n", i, time_spent, rate);
- }
+ if (i % 100000 == 0) {
+ time(¤t_time);
+ time_spent = difftime(current_time, start_time);
- i++;
+ rate = (double)(i / time_spent);
+ // printf("Generated %d addresses in %.1fs. Rate:%.1f/s \n", i,
+ // time_spent, rate);
}
- /* Destroy context to free memory */
- secp256k1_context_destroy(ctx);
+ i++;
}
+
+ /* Destroy context to free memory */
+ secp256k1_context_destroy(ctx);
+}
diff --git a/walletImportFormat.h b/walletImportFormat.h
@@ -1,48 +1,48 @@
-#include <stdio.h>
-#include <openssl/sha.h>
#include <openssl/ripemd.h>
+#include <openssl/sha.h>
+#include <stdio.h>
/* https://en.bitcoin.it/wiki/Wallet_import_format */
char *create_wif(const unsigned char *privatekey) {
- unsigned char newKey[65];
- unsigned char SHAkey[65];
- unsigned char SHAkey2[65];
- unsigned char checksum[11];
- unsigned char combinedKey[75];
-
- size_t combinedKeySize = 37;
- size_t wifSize = 51;
- char wif[51];
-
- /* Add 0x80 byte in front */
- newKey[0] = 128;
- for(int i=0; i<32; i++) {
- newKey[i+1] = privatekey[i];
- }
-
- /* Perform SHA-256 hash on the extended key */
- SHA256(newKey, 33, SHAkey);
-
- /* Perform SHA-256 hash again on the result */
- SHA256(SHAkey, 32, SHAkey2);
-
- /* Checksum is first 4 bytes of 2nd SHA*/
- for(int i=0; i<4; i++) {
- checksum[i] = SHAkey2[i];
- }
-
- /* Append checksum to end of 2nd SHA */
- for(int i=0; i<33; i++) {
- combinedKey[i] = newKey[i];
- }
- for(int i=0; i<4; i++) {
- combinedKey[33+i] = checksum[i];
- }
-
- /* Encode with base-58 */
- base58(combinedKey, combinedKeySize, wif, wifSize);
- puts(wif);
-
- return 0;
+ unsigned char newKey[65];
+ unsigned char SHAkey[65];
+ unsigned char SHAkey2[65];
+ unsigned char checksum[11];
+ unsigned char combinedKey[75];
+
+ size_t combinedKeySize = 37;
+ size_t wifSize = 51;
+ char wif[51];
+
+ /* Add 0x80 byte in front */
+ newKey[0] = 128;
+ for (int i = 0; i < 32; i++) {
+ newKey[i + 1] = privatekey[i];
+ }
+
+ /* Perform SHA-256 hash on the extended key */
+ SHA256(newKey, 33, SHAkey);
+
+ /* Perform SHA-256 hash again on the result */
+ SHA256(SHAkey, 32, SHAkey2);
+
+ /* Checksum is first 4 bytes of 2nd SHA*/
+ for (int i = 0; i < 4; i++) {
+ checksum[i] = SHAkey2[i];
+ }
+
+ /* Append checksum to end of 2nd SHA */
+ for (int i = 0; i < 33; i++) {
+ combinedKey[i] = newKey[i];
+ }
+ for (int i = 0; i < 4; i++) {
+ combinedKey[33 + i] = checksum[i];
+ }
+
+ /* Encode with base-58 */
+ base58(combinedKey, combinedKeySize, wif, wifSize);
+ puts(wif);
+
+ return 0;
}