niceBit

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 8c99d101c6376a35bd048c1b9565c0d07e3961e4
parent 948384eb1f1c37d16bf757039f842c2efa45db84
Author: NicholasFarrow <nicholas.w.farrow@gmail.com>
Date:   Wed,  8 Jul 2020 22:24:10 +1000

Reuse base58 functions, general cleanup, increased similarity to tutorial

Diffstat:
McreatePubKey.h | 94++++++++++++++++++++-----------------------------------------------------------
Dlib/base58.c | 205-------------------------------------------------------------------------------
Dlib/libbase58.h | 23-----------------------
MniceBit.c | 25+++++++++++--------------
MwalletImportFormat.h | 15++++++---------
5 files changed, 40 insertions(+), 322 deletions(-)

diff --git a/createPubKey.h b/createPubKey.h @@ -1,99 +1,51 @@ -/* Original, only slightly edited: - * https://rosettacode.org/wiki/Bitcoin/public_point_to_address -*/ - #include <stdio.h> #include <string.h> #include <ctype.h> #include <openssl/sha.h> #include <openssl/ripemd.h> - -#define COIN_VER 0 -const char *coin_err; - +#include "base58.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; } - + void str_to_byte(const char *src, byte *dst, int n) { while (n--) sscanf(src + n * 2, "%2hhx", dst + n); } - -char* base58(byte *s, char *out) { - static const char *tmpl = "123456789" - "ABCDEFGHJKLMNPQRSTUVWXYZ" - "abcdefghijkmnopqrstuvwxyz"; - static char buf[40]; - - int c, i, n; - if (!out) out = buf; - - out[n = 34] = 0; - while (n--) { - for (c = i = 0; i < 25; i++) { - c = c * 256 + s[i]; - s[i] = c / 58; - c %= 58; - } - out[n] = tmpl[c]; - } - - for (n = 0; out[n] == '1'; n++); - memmove(out, out + n, 34 - n); - - return out; -} - + /* char *coin_encode(const char *x, const char *y, char *out) { */ -char *coin_encode(const unsigned char *pubkey64, char *out) { +char *pubkey_to_P2PKH(const unsigned char *pubkey64, char *out) { byte s[65]; byte rmd[5 + RIPEMD160_DIGEST_LENGTH]; - - - /* - if (!is_hex(x) || !(is_hex(y))) { - coin_err = "bad public point string"; - return 0; - } - */ - - /* First byte is equal to 4, - * already handled by secp256k1 - - s[0] = 4; - */ - int j; + int j; for (j = 0; j < 65; j++) { s[j] = pubkey64[j]; } - /* We are working with pubkey64=x+y - * so we can skip this step - str_to_byte(x, s + 1, 32); - str_to_byte(y, s + 33, 32); - */ - - rmd[0] = COIN_VER; + 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); - - return base58(rmd, out); -} -/* -int main(void) { - puts(coin_encode( - "50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352", - "2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6", - 0)); - return 0; + + base58(rmd, 25, out, 34); + + /* Count the number of 1s at the beginning of the address */ + int n = 0; + for (n = 0; out[n] == '1'; n++); + + /* Do we need to remove any 1s? */ + if (n > 1) { + memmove(out, out + (n-1), 34-(n-1)); + + out[34-(n-1)] = '\0'; + } + } -*/ diff --git a/lib/base58.c b/lib/base58.c @@ -1,205 +0,0 @@ -/* - * Copyright 2012-2014 Luke Dashjr - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the standard MIT license. See COPYING for more details. - */ - -#ifndef WIN32 -#include <arpa/inet.h> -#else -#include <winsock2.h> -#endif - -#include <stdbool.h> -#include <stddef.h> -#include <stdint.h> -#include <string.h> - -#include "libbase58.h" - -bool (*b58_sha256_impl)(void *, const void *, size_t) = NULL; - -static const int8_t b58digits_map[] = { - -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, - -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,-1,-1,-1,-1,-1,-1, - -1, 9,10,11,12,13,14,15, 16,-1,17,18,19,20,21,-1, - 22,23,24,25,26,27,28,29, 30,31,32,-1,-1,-1,-1,-1, - -1,33,34,35,36,37,38,39, 40,41,42,43,-1,44,45,46, - 47,48,49,50,51,52,53,54, 55,56,57,-1,-1,-1,-1,-1, -}; - -typedef uint64_t b58_maxint_t; -typedef uint32_t b58_almostmaxint_t; -#define b58_almostmaxint_bits (sizeof(b58_almostmaxint_t) * 8) -static const b58_almostmaxint_t b58_almostmaxint_mask = ((((b58_maxint_t)1) << b58_almostmaxint_bits) - 1); - -bool b58tobin(void *bin, size_t *binszp, const char *b58, size_t b58sz) -{ - size_t binsz = *binszp; - const unsigned char *b58u = (void*)b58; - unsigned char *binu = bin; - size_t outisz = (binsz + sizeof(b58_almostmaxint_t) - 1) / sizeof(b58_almostmaxint_t); - b58_almostmaxint_t outi[outisz]; - b58_maxint_t t; - b58_almostmaxint_t c; - size_t i, j; - uint8_t bytesleft = binsz % sizeof(b58_almostmaxint_t); - b58_almostmaxint_t zeromask = bytesleft ? (b58_almostmaxint_mask << (bytesleft * 8)) : 0; - unsigned zerocount = 0; - - if (!b58sz) - b58sz = strlen(b58); - - for (i = 0; i < outisz; ++i) { - outi[i] = 0; - } - - // Leading zeros, just count - for (i = 0; i < b58sz && b58u[i] == '1'; ++i) - ++zerocount; - - for ( ; i < b58sz; ++i) - { - if (b58u[i] & 0x80) - // High-bit set on invalid digit - return false; - if (b58digits_map[b58u[i]] == -1) - // Invalid base58 digit - return false; - c = (unsigned)b58digits_map[b58u[i]]; - for (j = outisz; j--; ) - { - t = ((b58_maxint_t)outi[j]) * 58 + c; - c = t >> b58_almostmaxint_bits; - outi[j] = t & b58_almostmaxint_mask; - } - if (c) - // Output number too big (carry to the next int32) - return false; - if (outi[0] & zeromask) - // Output number too big (last int32 filled too far) - return false; - } - - j = 0; - if (bytesleft) { - for (i = bytesleft; i > 0; --i) { - *(binu++) = (outi[0] >> (8 * (i - 1))) & 0xff; - } - ++j; - } - - for (; j < outisz; ++j) - { - for (i = sizeof(*outi); i > 0; --i) { - *(binu++) = (outi[j] >> (8 * (i - 1))) & 0xff; - } - } - - // Count canonical base58 byte count - binu = bin; - for (i = 0; i < binsz; ++i) - { - if (binu[i]) - break; - --*binszp; - } - *binszp += zerocount; - - return true; -} - -static -bool my_dblsha256(void *hash, const void *data, size_t datasz) -{ - uint8_t buf[0x20]; - return b58_sha256_impl(buf, data, datasz) && b58_sha256_impl(hash, buf, sizeof(buf)); -} - -int b58check(const void *bin, size_t binsz, const char *base58str, size_t b58sz) -{ - unsigned char buf[32]; - const uint8_t *binc = bin; - unsigned i; - if (binsz < 4) - return -4; - if (!my_dblsha256(buf, bin, binsz - 4)) - return -2; - if (memcmp(&binc[binsz - 4], buf, 4)) - return -1; - - // Check number of zeros is correct AFTER verifying checksum (to avoid possibility of accessing base58str beyond the end) - for (i = 0; binc[i] == '\0' && base58str[i] == '1'; ++i) - {} // Just finding the end of zeros, nothing to do in loop - if (binc[i] == '\0' || base58str[i] == '1') - return -3; - - return binc[0]; -} - -static const char b58digits_ordered[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; - -bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz) -{ - const uint8_t *bin = data; - int carry; - size_t i, j, high, zcount = 0; - size_t size; - - while (zcount < binsz && !bin[zcount]) - ++zcount; - - size = (binsz - zcount) * 138 / 100 + 1; - uint8_t buf[size]; - memset(buf, 0, size); - - for (i = zcount, high = size - 1; i < binsz; ++i, high = j) - { - for (carry = bin[i], j = size - 1; (j > high) || carry; --j) - { - carry += 256 * buf[j]; - buf[j] = carry % 58; - carry /= 58; - if (!j) { - // Otherwise j wraps to maxint which is > high - break; - } - } - } - - for (j = 0; j < size && !buf[j]; ++j); - - if (*b58sz <= zcount + size - j) - { - *b58sz = zcount + size - j + 1; - return false; - } - - if (zcount) - memset(b58, '1', zcount); - for (i = zcount; j < size; ++i, ++j) - b58[i] = b58digits_ordered[buf[j]]; - b58[i] = '\0'; - *b58sz = i + 1; - - return true; -} - -bool b58check_enc(char *b58c, size_t *b58c_sz, uint8_t ver, const void *data, size_t datasz) -{ - uint8_t buf[1 + datasz + 0x20]; - uint8_t *hash = &buf[1 + datasz]; - - buf[0] = ver; - memcpy(&buf[1], data, datasz); - if (!my_dblsha256(hash, buf, datasz + 1)) - { - *b58c_sz = 0; - return false; - } - - return b58enc(b58c, b58c_sz, buf, 1 + datasz + 4); -} diff --git a/lib/libbase58.h b/lib/libbase58.h @@ -1,23 +0,0 @@ -#ifndef LIBBASE58_H -#define LIBBASE58_H - -#include <stdbool.h> -#include <stddef.h> - -#ifdef __cplusplus -extern "C" { -#endif - -extern bool (*b58_sha256_impl)(void *, const void *, size_t); - -extern bool b58tobin(void *bin, size_t *binsz, const char *b58, size_t b58sz); -extern int b58check(const void *bin, size_t binsz, const char *b58, size_t b58sz); - -extern bool b58enc(char *b58, size_t *b58sz, const void *bin, size_t binsz); -extern bool b58check_enc(char *b58c, size_t *b58c_sz, uint8_t ver, const void *data, size_t datasz); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/niceBit.c b/niceBit.c @@ -33,7 +33,7 @@ int gen_keypair(unsigned char *seckey, char *pubaddress, secp256k1_context *ctx) } printf("\n\n"); */ - + /* Apparently there is a 2^-128 chance of * a secret key being invalid. * https://en.bitcoin.it/wiki/Private_key @@ -71,11 +71,8 @@ int gen_keypair(unsigned char *seckey, char *pubaddress, secp256k1_context *ctx) /* Generate Public Address * (from create_pubkey.h) */ - coin_encode(public_key64, pubaddress); + pubkey_to_P2PKH(public_key64, pubaddress); - /* sketchy, force end address after 33 chars*/ - pubaddress[33] = '\0'; - return 1; } @@ -88,10 +85,10 @@ int check_vanity(char *pubaddress, int searchlen) { * ('len' digits in a row) */ for(int len=10; len>=searchlen; len--) { - + /* For each digit 1-9 */ for(int i=0; i<58; i++) { - + /* Comprise compstr of 'len' repeats * of digit 'i' */ @@ -100,10 +97,10 @@ int check_vanity(char *pubaddress, int searchlen) { compstr[j] = possibleChars[i]; j++; } - + /* End string with null char*/ compstr[j] = '\0'; - + /* Check if string in pubaddress */ if(strstr(pubaddress, compstr) != NULL) { printf("Found : %s\n", compstr); @@ -117,7 +114,7 @@ int check_vanity(char *pubaddress, int searchlen) { int main(int argc, char **argv) { unsigned char seckey[32]; - char pubaddress[40]; + char pubaddress[34]; char *p = pubaddress; char *n = "5"; int searchlen; @@ -159,13 +156,13 @@ int main(int argc, char **argv) { printf("\nWIF: "); create_wif(seckey); - printf("Public Address: 1%s\n\n", pubaddress); + printf("Public Address: %s\n\n", pubaddress); } if(i % 100000 == 0) { clock_t currenttime = clock(); - timespent = - (double)((currenttime - starttime) + timespent = + (double)((currenttime - starttime) / CLOCKS_PER_SEC); rate = (double)(i / timespent); printf("Generated %d addresses in %.1fs. Rate:%.1f/s \n", i, timespent, rate); @@ -175,5 +172,5 @@ int main(int argc, char **argv) { } /* Destroy context to free memory */ - secp256k1_context_destroy(ctx); + secp256k1_context_destroy(ctx); } diff --git a/walletImportFormat.h b/walletImportFormat.h @@ -1,7 +1,6 @@ #include <stdio.h> #include <openssl/sha.h> #include <openssl/ripemd.h> -#include "lib/base58.c" /* https://en.bitcoin.it/wiki/Wallet_import_format */ @@ -12,11 +11,9 @@ char *create_wif(const unsigned char *privatekey) { unsigned char checksum[11]; unsigned char combinedKey[75]; - size_t combinedKeySize = 37; - size_t wifSize = 52; - char wif[52]; - - wifSize = 80; + size_t combinedKeySize = 37; + size_t wifSize = 51; + char wif[51]; /* Add 0x80 byte in front */ newKey[0] = 128; @@ -28,7 +25,7 @@ char *create_wif(const unsigned char *privatekey) { SHA256(newKey, 33, SHAkey); /* Perform SHA-256 hash again on the result */ - SHA256(SHAkey, 32, SHAkey2); + SHA256(SHAkey, 32, SHAkey2); /* Checksum is first 4 bytes of 2nd SHA*/ for(int i=0; i<4; i++) { @@ -43,8 +40,8 @@ char *create_wif(const unsigned char *privatekey) { combinedKey[33+i] = checksum[i]; } - /* Encode with base-58 */ - b58enc(wif, &wifSize, combinedKey, combinedKeySize); + /* Encode with base-58 */ + base58(combinedKey, combinedKeySize, wif, wifSize); puts(wif); return 0;