From 240436174757551e9be8761ef2b4adb08a11ae1e Mon Sep 17 00:00:00 2001 From: Jean-Luc Pons Date: Wed, 3 Apr 2019 08:11:52 +0200 Subject: [PATCH] Added support for BECH32 --- Bech32.cpp | 227 +++++++++++++++++++++++++++++++++++ Bech32.h | 103 ++++++++++++++++ GPU/GPUEngine.cu | 1 + SECP256K1.cpp | 38 +++++- SECP256k1.h | 5 +- Vanity.cpp | 222 +++++++++++++++++++++++----------- VanitySearch.vcxproj | 2 + VanitySearch.vcxproj.filters | 2 + main.cpp | 1 + 9 files changed, 526 insertions(+), 75 deletions(-) create mode 100644 Bech32.cpp create mode 100644 Bech32.h diff --git a/Bech32.cpp b/Bech32.cpp new file mode 100644 index 0000000..4ff5102 --- /dev/null +++ b/Bech32.cpp @@ -0,0 +1,227 @@ +/* Copyright (c) 2017 Pieter Wuille + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include +#include +#include +#include + +#include "Bech32.h" + +uint32_t bech32_polymod_step(uint32_t pre) { + uint8_t b = pre >> 25; + return ((pre & 0x1FFFFFF) << 5) ^ + (-((b >> 0) & 1) & 0x3b6a57b2UL) ^ + (-((b >> 1) & 1) & 0x26508e6dUL) ^ + (-((b >> 2) & 1) & 0x1ea119faUL) ^ + (-((b >> 3) & 1) & 0x3d4233ddUL) ^ + (-((b >> 4) & 1) & 0x2a1462b3UL); +} + +static const char* charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"; + +static const int8_t charset_rev[128] = { + -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, + 15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -1, -1, -1, -1, -1, + -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, + 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1, + -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, + 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1 +}; + +int bech32_encode(char *output, const char *hrp, const uint8_t *data, size_t data_len) { + uint32_t chk = 1; + size_t i = 0; + while (hrp[i] != 0) { + int ch = hrp[i]; + if (ch < 33 || ch > 126) { + return 0; + } + + if (ch >= 'A' && ch <= 'Z') return 0; + chk = bech32_polymod_step(chk) ^ (ch >> 5); + ++i; + } + if (i + 7 + data_len > 90) return 0; + chk = bech32_polymod_step(chk); + while (*hrp != 0) { + chk = bech32_polymod_step(chk) ^ (*hrp & 0x1f); + *(output++) = *(hrp++); + } + *(output++) = '1'; + for (i = 0; i < data_len; ++i) { + if (*data >> 5) return 0; + chk = bech32_polymod_step(chk) ^ (*data); + *(output++) = charset[*(data++)]; + } + for (i = 0; i < 6; ++i) { + chk = bech32_polymod_step(chk); + } + chk ^= 1; + for (i = 0; i < 6; ++i) { + *(output++) = charset[(chk >> ((5 - i) * 5)) & 0x1f]; + } + *output = 0; + return 1; +} + +int bech32_decode_nocheck(uint8_t *data, size_t *data_len, const char *input) { + + uint8_t acc=0; + uint8_t acc_len=8; + size_t out_len=0; + + size_t input_len = strlen(input); + for (int i = 0; i < input_len; i++) { + + if(input[i]&0x80) + return false; + + int8_t c = charset_rev[tolower(input[i])]; + if(c<0) + return false; + + if (acc_len >= 5) { + acc |= c << (acc_len - 5); + acc_len -= 5; + } else { + int shift = 5 - acc_len; + data[out_len++] = acc | (c >> shift); + acc_len = 8-shift; + acc = c << acc_len; + } + + } + + data[out_len++] = acc; + *data_len = out_len; + + return true; + +} + +int bech32_decode(char* hrp, uint8_t *data, size_t *data_len, const char *input) { + uint32_t chk = 1; + size_t i; + size_t input_len = strlen(input); + size_t hrp_len; + int have_lower = 0, have_upper = 0; + if (input_len < 8 || input_len > 90) { + return 0; + } + *data_len = 0; + while (*data_len < input_len && input[(input_len - 1) - *data_len] != '1') { + ++(*data_len); + } + hrp_len = input_len - (1 + *data_len); + if (1 + *data_len >= input_len || *data_len < 6) { + return 0; + } + *(data_len) -= 6; + for (i = 0; i < hrp_len; ++i) { + int ch = input[i]; + if (ch < 33 || ch > 126) { + return 0; + } + if (ch >= 'a' && ch <= 'z') { + have_lower = 1; + } else if (ch >= 'A' && ch <= 'Z') { + have_upper = 1; + ch = (ch - 'A') + 'a'; + } + hrp[i] = ch; + chk = bech32_polymod_step(chk) ^ (ch >> 5); + } + hrp[i] = 0; + chk = bech32_polymod_step(chk); + for (i = 0; i < hrp_len; ++i) { + chk = bech32_polymod_step(chk) ^ (input[i] & 0x1f); + } + ++i; + while (i < input_len) { + int v = (input[i] & 0x80) ? -1 : charset_rev[(int)input[i]]; + if (input[i] >= 'a' && input[i] <= 'z') have_lower = 1; + if (input[i] >= 'A' && input[i] <= 'Z') have_upper = 1; + if (v == -1) { + return 0; + } + chk = bech32_polymod_step(chk) ^ v; + if (i + 6 < input_len) { + data[i - (1 + hrp_len)] = v; + } + ++i; + } + if (have_lower && have_upper) { + return 0; + } + return chk == 1; +} + +static int convert_bits(uint8_t* out, size_t* outlen, int outbits, const uint8_t* in, size_t inlen, int inbits, int pad) { + uint32_t val = 0; + int bits = 0; + uint32_t maxv = (((uint32_t)1) << outbits) - 1; + while (inlen--) { + val = (val << inbits) | *(in++); + bits += inbits; + while (bits >= outbits) { + bits -= outbits; + out[(*outlen)++] = (val >> bits) & maxv; + } + } + if (pad) { + if (bits) { + out[(*outlen)++] = (val << (outbits - bits)) & maxv; + } + } else if (((val << (outbits - bits)) & maxv) || bits >= inbits) { + return 0; + } + return 1; +} + +int segwit_addr_encode(char *output, const char *hrp, int witver, const uint8_t *witprog, size_t witprog_len) { + uint8_t data[65]; + size_t datalen = 0; + if (witver > 16) return 0; + if (witver == 0 && witprog_len != 20 && witprog_len != 32) return 0; + if (witprog_len < 2 || witprog_len > 40) return 0; + data[0] = witver; + convert_bits(data + 1, &datalen, 5, witprog, witprog_len, 8, 1); + ++datalen; + return bech32_encode(output, hrp, data, datalen); +} + +int segwit_addr_decode(int* witver, uint8_t* witdata, size_t* witdata_len, const char* hrp, const char* addr) { + uint8_t data[84]; + char hrp_actual[84]; + size_t data_len; + if (!bech32_decode(hrp_actual, data, &data_len, addr)) return 0; + if (data_len == 0 || data_len > 65) return 0; + if (strncmp(hrp, hrp_actual, 84) != 0) return 0; + if (data[0] > 16) return 0; + *witdata_len = 0; + if (!convert_bits(witdata, witdata_len, 8, data + 1, data_len - 1, 5, 0)) return 0; + if (*witdata_len < 2 || *witdata_len > 40) return 0; + if (data[0] == 0 && *witdata_len != 20 && *witdata_len != 32) return 0; + *witver = data[0]; + return 1; +} diff --git a/Bech32.h b/Bech32.h new file mode 100644 index 0000000..03800ee --- /dev/null +++ b/Bech32.h @@ -0,0 +1,103 @@ +/* Copyright (c) 2017 Pieter Wuille + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef _SEGWIT_ADDR_H_ +#define _SEGWIT_ADDR_H_ 1 + +#include + + /** Encode a SegWit address + * + * Out: output: Pointer to a buffer of size 73 + strlen(hrp) that will be + * updated to contain the null-terminated address. + * In: hrp: Pointer to the null-terminated human readable part to use + * (chain/network specific). + * ver: Version of the witness program (between 0 and 16 inclusive). + * prog: Data bytes for the witness program (between 2 and 40 bytes). + * prog_len: Number of data bytes in prog. + * Returns 1 if successful. + */ +int segwit_addr_encode( + char *output, + const char *hrp, + int ver, + const uint8_t *prog, + size_t prog_len +); + +/** Decode a SegWit address + * + * Out: ver: Pointer to an int that will be updated to contain the witness + * program version (between 0 and 16 inclusive). + * prog: Pointer to a buffer of size 40 that will be updated to + * contain the witness program bytes. + * prog_len: Pointer to a size_t that will be updated to contain the length + * of bytes in prog. + * hrp: Pointer to the null-terminated human readable part that is + * expected (chain/network specific). + * addr: Pointer to the null-terminated address. + * Returns 1 if successful. + */ +int segwit_addr_decode( + int* ver, + uint8_t* prog, + size_t* prog_len, + const char* hrp, + const char* addr +); + +/** Encode a Bech32 string + * + * Out: output: Pointer to a buffer of size strlen(hrp) + data_len + 8 that + * will be updated to contain the null-terminated Bech32 string. + * In: hrp : Pointer to the null-terminated human readable part. + * data : Pointer to an array of 5-bit values. + * data_len: Length of the data array. + * Returns 1 if successful. + */ +int bech32_encode( + char *output, + const char *hrp, + const uint8_t *data, + size_t data_len +); + +/** Decode a Bech32 string + * + * Out: hrp: Pointer to a buffer of size strlen(input) - 6. Will be + * updated to contain the null-terminated human readable part. + * data: Pointer to a buffer of size strlen(input) - 8 that will + * hold the encoded 5-bit data values. + * data_len: Pointer to a size_t that will be updated to be the number + * of entries in data. + * In: input: Pointer to a null-terminated Bech32 string. + * Returns 1 if succesful. + */ +int bech32_decode( + char *hrp, + uint8_t *data, + size_t *data_len, + const char *input +); + +int bech32_decode_nocheck(uint8_t *data, size_t *data_len, const char *input); + +#endif diff --git a/GPU/GPUEngine.cu b/GPU/GPUEngine.cu index 1039294..faac43d 100644 --- a/GPU/GPUEngine.cu +++ b/GPU/GPUEngine.cu @@ -419,6 +419,7 @@ bool GPUEngine::callKernel() { comp_keys_p2sh << < nbThread / NB_TRHEAD_PER_GROUP, NB_TRHEAD_PER_GROUP >> > (searchMode, inputPrefix, inputPrefixLookUp, inputKey, maxFound, outputPrefix); } else { + // P2PKH or BECH32 if (searchMode == SEARCH_COMPRESSED) { comp_keys_comp << < nbThread / NB_TRHEAD_PER_GROUP, NB_TRHEAD_PER_GROUP >> > (inputPrefix, inputPrefixLookUp, inputKey, maxFound, outputPrefix); diff --git a/SECP256K1.cpp b/SECP256K1.cpp index cc5e507..03e1822 100644 --- a/SECP256K1.cpp +++ b/SECP256K1.cpp @@ -19,6 +19,7 @@ #include "hash/sha256.h" #include "hash/ripemd160.h" #include "Base58.h" +#include "Bech32.h" #include Secp256K1::Secp256K1() { @@ -71,8 +72,13 @@ void CheckAddress(Secp256K1 *T,std::string address,std::string privKeyStr) { Point pub = T->ComputePublicKey(&privKey); switch (address.data()[0]) { - case '1': type = P2PKH; break; - case '3': type = P2SH; break; + case '1': + type = P2PKH; break; + case '3': + type = P2SH; break; + case 'b': + case 'B': + type = BECH32; break; default: printf("Failed ! \n%s Address format not supported\n", address.c_str()); return; @@ -135,7 +141,8 @@ void Secp256K1::Check() { CheckAddress(this,"1Tst2RwMxZn9cYY5mQhCdJic3JJrK7Fq7","L1vamTpSeK9CgynRpSJZeqvUXf6dJa25sfjb2uvtnhj65R5TymgF"); CheckAddress(this,"3CyQYcByvcWK8BkYJabBS82yDLNWt6rWSx","KxMUSkFhEzt2eJHscv2vNSTnnV2cgAXgL4WDQBTx7Ubd9TZmACAz"); CheckAddress(this,"31to1KQe67YjoDfYnwFJThsGeQcFhVDM5Q","KxV2Tx5jeeqLHZ1V9ufNv1doTZBZuAc5eY24e6b27GTkDhYwVad7"); - + CheckAddress(this,"bc1q6tqytpg06uhmtnhn9s4f35gkt8yya5a24dptmn","L2wAVD273GwAxGuEDHvrCqPfuWg5wWLZWy6H3hjsmhCvNVuCERAQ"); + // 1ViViGLEawN27xRzGrEhhYPQrZiTKvKLo pub.x.SetBase16(/*04*/"75249c39f38baa6bf20ab472191292349426dc3652382cdc45f65695946653dc"); pub.y.SetBase16("978b2659122fe1df1be132167f27b74e5d4a2f3ecbbbd0b3fbcc2f4983518674"); @@ -349,6 +356,7 @@ void Secp256K1::GetHash160(int type,bool compressed, switch (type) { case P2PKH: + case BECH32: { if (!compressed) { @@ -422,6 +430,7 @@ void Secp256K1::GetHash160(int type, bool compressed, Point &pubKey, unsigned ch switch (type) { case P2PKH: + case BECH32: { unsigned char publicKeyBytes[128]; unsigned char shapk[64]; @@ -452,7 +461,6 @@ void Secp256K1::GetHash160(int type, bool compressed, Point &pubKey, unsigned ch // Redeem Script (1 to 1 P2SH) unsigned char script[64]; - unsigned char address[25]; unsigned char shapk[64]; script[0] = 0x00; // OP_0 @@ -513,12 +521,22 @@ std::string Secp256K1::GetAddress(int type, bool compressed,unsigned char *hash1 unsigned char address[25]; switch(type) { + case P2PKH: address[0] = 0x00; break; + case P2SH: address[0] = 0x05; break; + + case BECH32: + { + char output[128]; + segwit_addr_encode(output, "bc", 0, hash160, 20); + return std::string(output); + } + break; } memcpy(address + 1, hash160,20); @@ -542,9 +560,21 @@ std::string Secp256K1::GetAddress(int type, bool compressed, Point &pubKey) { unsigned char address[25]; switch (type) { + case P2PKH: address[0] = 0x00; break; + + case BECH32: + { + char output[128]; + uint8_t h160[20]; + GetHash160(type, compressed, pubKey, h160); + segwit_addr_encode(output,"bc",0,h160,20); + return std::string(output); + } + break; + case P2SH: address[0] = 0x05; break; diff --git a/SECP256k1.h b/SECP256k1.h index d90c268..f53fcfb 100644 --- a/SECP256k1.h +++ b/SECP256k1.h @@ -23,8 +23,9 @@ #include // Address type -#define P2PKH 0 -#define P2SH 1 +#define P2PKH 0 +#define P2SH 1 +#define BECH32 2 class Secp256K1 { diff --git a/Vanity.cpp b/Vanity.cpp index ce3e66e..fdc3bee 100644 --- a/Vanity.cpp +++ b/Vanity.cpp @@ -17,6 +17,7 @@ #include "Vanity.h" #include "Base58.h" +#include "Bech32.h" #include "hash/sha256.h" #include "hash/sha512.h" #include "IntGroup.h" @@ -212,108 +213,185 @@ bool VanitySearch::initPrefix(std::string &prefix,PREFIX_ITEM *it) { return false; } - if (prefix.data()[0] != '1' && prefix.data()[0] != '3') { - printf("Ignoring prefix \"%s\" (must start with 1 or 3)\n", prefix.c_str()); - return false; - } + int aType = -1; - wrong = !DecodeBase58(prefix, result); - if (wrong) { - printf("Ignoring prefix \"%s\" (0, I, O and l not allowed)\n", prefix.c_str()); + switch (prefix.data()[0]) { + case '1': + aType = P2PKH; + break; + case '3': + aType = P2SH; + break; + case 'b': + case 'B': + std::transform(prefix.begin(), prefix.end(), prefix.begin(), ::tolower); + if(strncmp(prefix.c_str(), "bc1q", 4) == 0) + aType = BECH32; + break; + } + + if (aType==-1) { + printf("Ignoring prefix \"%s\" (must start with 1 or 3 or bc1q)\n", prefix.c_str()); return false; } - int aType = (prefix.data()[0] == '1') ? P2PKH : P2SH; - if( searchType==-1 ) searchType = aType; + if (searchType == -1) searchType = aType; if (aType != searchType) { - printf("Ignoring prefix \"%s\" (Cannot merge P2PKH and P2SH)\n", prefix.c_str()); + printf("Ignoring prefix \"%s\" (P2PKH, P2SH or BECH32 allowed at once)\n", prefix.c_str()); return false; } - // Try to attack a full address ? - if (result.size() > 21) { - - // mamma mia ! - //if (!secp.CheckPudAddress(prefix)) { - // printf("Warning, \"%s\" (address checksum may never match)\n", prefix.c_str()); - //} - it->difficulty = pow(2, 160); - it->isFull = true; - memcpy(it->hash160, result.data() + 1, 20); - it->sPrefix = *(prefix_t *)(it->hash160); - it->lPrefix = *(prefixl_t *)(it->hash160); - it->prefix = (char *)prefix.c_str(); - it->prefixLength = (int)prefix.length(); - it->found = false; - return true; + if (aType == BECH32) { - } - - // Prefix containing only '1' - if (isSingularPrefix(prefix)) { + // BECH32 + uint8_t witprog[40]; + size_t witprog_len; + int witver; + const char* hrp = "bc"; + + int ret = segwit_addr_decode(&witver, witprog, &witprog_len, hrp, prefix.c_str()); + + // Try to attack a full address ? + if (ret && witprog_len==20) { + + // mamma mia ! + it->difficulty = pow(2, 160); + it->isFull = true; + memcpy(it->hash160, witprog, 20); + it->sPrefix = *(prefix_t *)(it->hash160); + it->lPrefix = *(prefixl_t *)(it->hash160); + it->prefix = (char *)prefix.c_str(); + it->prefixLength = (int)prefix.length(); + it->found = false; + return true; + + } - if (prefix.length() > 21) { - printf("Ignoring prefix \"%s\" (Too much 1)\n", prefix.c_str()); + if (prefix.length() < 5) { + printf("Ignoring prefix \"%s\" (too short, length<5 )\n", prefix.c_str()); + return false; + } + + if (prefix.length() >= 36) { + printf("Ignoring prefix \"%s\" (too long, length>36 )\n", prefix.c_str()); + return false; + } + + uint8_t data[64]; + memset(data,0,64); + size_t data_length; + if(!bech32_decode_nocheck(data,&data_length,prefix.c_str()+4)) { + printf("Ignoring prefix \"%s\" (Only \"023456789acdefghjklmnpqrstuvwxyz\" allowed)\n", prefix.c_str()); return false; } // Difficulty - it->difficulty = pow(256, prefix.length()-1); + it->sPrefix = *(prefix_t *)data; + it->difficulty = pow(2, 5*(prefix.length()-4)); it->isFull = false; - it->sPrefix = 0; it->lPrefix = 0; it->prefix = (char *)prefix.c_str(); it->prefixLength = (int)prefix.length(); it->found = false; + return true; - } + } else { - // Search for highest hash160 16bit prefix (most probable) + // P2PKH/P2SH - while (result.size() < 25) { - DecodeBase58(dummy1, result); - if (result.size() < 25) { - dummy1.append("1"); - nbDigit++; - } - } + wrong = !DecodeBase58(prefix, result); - if (searchType == P2SH) { - if (result.data()[0] != 5) { - printf("Ignoring prefix \"%s\" (Unreachable, 31h1 to 3R2c only)\n", prefix.c_str()); + if (wrong) { + printf("Ignoring prefix \"%s\" (0, I, O and l not allowed)\n", prefix.c_str()); return false; } - } - if (result.size() != 25) { - printf("Ignoring prefix \"%s\" (Invalid size)\n", prefix.c_str()); - return false; - } + // Try to attack a full address ? + if (result.size() > 21) { + + // mamma mia ! + //if (!secp.CheckPudAddress(prefix)) { + // printf("Warning, \"%s\" (address checksum may never match)\n", prefix.c_str()); + //} + it->difficulty = pow(2, 160); + it->isFull = true; + memcpy(it->hash160, result.data() + 1, 20); + it->sPrefix = *(prefix_t *)(it->hash160); + it->lPrefix = *(prefixl_t *)(it->hash160); + it->prefix = (char *)prefix.c_str(); + it->prefixLength = (int)prefix.length(); + it->found = false; + return true; - //printf("VanitySearch: Found prefix %s\n",GetHex(result).c_str() ); - it->sPrefix = *(prefix_t *)(result.data() + 1); + } - dummy1.append("1"); - DecodeBase58(dummy1, result); + // Prefix containing only '1' + if (isSingularPrefix(prefix)) { - if (result.size() == 25) { - //printf("VanitySearch: Found prefix %s\n", GetHex(result).c_str()); + if (prefix.length() > 21) { + printf("Ignoring prefix \"%s\" (Too much 1)\n", prefix.c_str()); + return false; + } + + // Difficulty + it->difficulty = pow(256, prefix.length() - 1); + it->isFull = false; + it->sPrefix = 0; + it->lPrefix = 0; + it->prefix = (char *)prefix.c_str(); + it->prefixLength = (int)prefix.length(); + it->found = false; + return true; + + } + + // Search for highest hash160 16bit prefix (most probable) + + while (result.size() < 25) { + DecodeBase58(dummy1, result); + if (result.size() < 25) { + dummy1.append("1"); + nbDigit++; + } + } + + if (searchType == P2SH) { + if (result.data()[0] != 5) { + printf("Ignoring prefix \"%s\" (Unreachable, 31h1 to 3R2c only)\n", prefix.c_str()); + return false; + } + } + + if (result.size() != 25) { + printf("Ignoring prefix \"%s\" (Invalid size)\n", prefix.c_str()); + return false; + } + + //printf("VanitySearch: Found prefix %s\n",GetHex(result).c_str() ); it->sPrefix = *(prefix_t *)(result.data() + 1); - nbDigit++; - } - // Difficulty - it->difficulty = pow(2, 192) / pow(58, nbDigit); - it->isFull = false; - it->lPrefix = 0; - it->prefix = (char *)prefix.c_str(); - it->prefixLength = (int)prefix.length(); - it->found = false; + dummy1.append("1"); + DecodeBase58(dummy1, result); - return true; + if (result.size() == 25) { + //printf("VanitySearch: Found prefix %s\n", GetHex(result).c_str()); + it->sPrefix = *(prefix_t *)(result.data() + 1); + nbDigit++; + } + // Difficulty + it->difficulty = pow(2, 192) / pow(58, nbDigit); + it->isFull = false; + it->lPrefix = 0; + it->prefix = (char *)prefix.c_str(); + it->prefixLength = (int)prefix.length(); + it->found = false; + + return true; + + } } // ---------------------------------------------------------------------------- @@ -440,10 +518,16 @@ void VanitySearch::output(string addr,string pAddr,string pAddrHex) { } fprintf(f, "\nPub Addr: %s\n", addr.c_str()); - if (searchType == P2PKH) { + switch (searchType) { + case P2PKH: fprintf(f, "Priv (WIF): p2pkh:%s\n", pAddr.c_str()); - } else { + break; + case P2SH: fprintf(f, "Priv (WIF): p2wpkh-p2sh:%s\n", pAddr.c_str()); + break; + case BECH32: + fprintf(f, "Priv (WIF): p2wpkh:%s\n", pAddr.c_str()); + break; } fprintf(f, "Priv (HEX): 0x%s\n", pAddrHex.c_str()); diff --git a/VanitySearch.vcxproj b/VanitySearch.vcxproj index 28fa6bd..93cefdd 100644 --- a/VanitySearch.vcxproj +++ b/VanitySearch.vcxproj @@ -16,6 +16,7 @@ + @@ -34,6 +35,7 @@ + diff --git a/VanitySearch.vcxproj.filters b/VanitySearch.vcxproj.filters index ba44333..acdc811 100644 --- a/VanitySearch.vcxproj.filters +++ b/VanitySearch.vcxproj.filters @@ -41,6 +41,7 @@ GPU + @@ -71,6 +72,7 @@ + diff --git a/main.cpp b/main.cpp index 6968fc6..2da970d 100644 --- a/main.cpp +++ b/main.cpp @@ -22,6 +22,7 @@ #include #include #include +#include "Bech32.h" #define RELEASE "1.11"