Author

Topic: Try create Private Key Recovery (Read 149 times)

newbie
Activity: 49
Merit: 0
August 04, 2023, 08:27:16 AM
#11
Sorry, I'm a bit confused by your statement. please help by writing where should I add or even replace the code in my code above?  Smiley
Start by looking at the existing projects on github that are already doing this (recovering WIF with missing characters). You'll get a good idea.

As for what I meant, I'll give you an example.
Instead of using the line I mentioned above (key = Key(wif=test_wif)) you'd use these 4 lines and under the if branch instead of throwing an exception you'd just skip when the checksum is invalid. And in the else branch (you add) you perform the conversion to address and the rest of the checks.

Sorry,,, can you replace the code above? because I also confused where put your code.  Smiley
legendary
Activity: 3430
Merit: 10505
August 02, 2023, 09:14:34 AM
#10
Sorry, I'm a bit confused by your statement. please help by writing where should I add or even replace the code in my code above?  Smiley
Start by looking at the existing projects on github that are already doing this (recovering WIF with missing characters). You'll get a good idea.

As for what I meant, I'll give you an example.
Instead of using the line I mentioned above (key = Key(wif=test_wif)) you'd use these 4 lines and under the if branch instead of throwing an exception you'd just skip when the checksum is invalid. And in the else branch (you add) you perform the conversion to address and the rest of the checks.
newbie
Activity: 49
Merit: 0
August 02, 2023, 08:25:48 AM
#9
You could use a lib such as multiprocessing (https://docs.python.org/3/library/multiprocessing.html) to run the generate_random_wif function in parallel.

This will most likely improve performance, as you will be running multiple instances of the same function at the same time.

if you wish, please write down the code you mean, so that it runs well. whether the performance can be faster by using the processor as the process?  Smiley

I asked GPT-4 to do this for you.

https://www.talkimg.com/images/2023/08/02/G2iBI.png

This is the answer:
Quote
Sure, I can help you improve the performance of this Python code using the multiprocessing library. Here is an improved version of the code that uses the multiprocessing library to speed up the process of finding the correct WIF:
Code:
import random
from multiprocessing import Pool
from bitcoinlib.keys import Key

def generate_random_wif(wif, target_address):
    alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
    missing_indices = [i for i in range(len(wif)) if wif[i] == '*']
    found = False
    while not found:
        rand_chars = ''.join(random.choice(alphabet) for _ in range(len(missing_indices)))
        test_wif = wif
        for index, char in zip(missing_indices, rand_chars):
            test_wif = test_wif[:index] + char + test_wif[index+1:]
        try:
            key = Key(wif=test_wif)
            address = key.address()
            if address == target_address:
                found = True
                return test_wif, address
            print(f"Scanning WIF: {test_wif}")
        except:
            print(f"Invalid WIF: {test_wif}")

def worker(input_wif, target_address):
    return generate_random_wif(input_wif, target_address)

def main():
    input_wif = input("Enter a WIF string with missing characters (mark with *): ")
    target_address = input("Enter the target Bitcoin address: ")

    if '*' not in input_wif:
        print("Incorrect entry: * sign missing.")
        return

    pool = Pool(processes=4)
    results = pool.starmap(worker, [(input_wif, target_address)]*4)
    pool.close()
    pool.join()

    found_wifs = [result for result in results if result is not None]
    if found_wifs:
        found_wif, found_address = found_wifs[0]
        print("Correct WIF found:", found_wif)
        print("Corresponding Bitcoin address:", found_address)
        with open("found.txt", "w") as file:
            file.write(found_wif)

        with open("found2.txt", "w") as file:
            file.write(f"WIF: {found_wif}\nAddress: {found_address}")

    else:
        print("The correct WIF was not found. try more.")

if __name__ == "__main__":
    main()
This version of the code creates a Pool of worker processes and uses the starmap method to distribute the work of finding the correct WIF across multiple processes. This should significantly speed up the process of finding the correct WIF. You can adjust the number of processes used by changing the processes parameter when creating the Pool. I hope this helps! Let me know if you have any questions or need further assistance.

https://www.talkimg.com/images/2023/08/02/G2Wmc.png

what about multiple imports? like :
Code:
random import
import hashlib
import base58

and I've added the code :
Code:
from multiprocessing import Pool

it seems that an error has occurred which makes the program never stop. I tried with the private key that I took and tried removing the characters to try it.  Lips sealed
newbie
Activity: 49
Merit: 0
August 02, 2023, 08:15:59 AM
#8
You could use a lib such as multiprocessing (https://docs.python.org/3/library/multiprocessing.html) to run the generate_random_wif function in parallel.

This will most likely improve performance, as you will be running multiple instances of the same function at the same time.

if you wish, please write down the code you mean, so that it runs well. whether the performance can be faster by using the processor as the process?  Smiley

I asked GPT-4 to do this for you.

https://www.talkimg.com/images/2023/08/02/G2iBI.png

This is the answer:
Quote
Sure, I can help you improve the performance of this Python code using the multiprocessing library. Here is an improved version of the code that uses the multiprocessing library to speed up the process of finding the correct WIF:
Code:
import random
from multiprocessing import Pool
from bitcoinlib.keys import Key

def generate_random_wif(wif, target_address):
    alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
    missing_indices = [i for i in range(len(wif)) if wif[i] == '*']
    found = False
    while not found:
        rand_chars = ''.join(random.choice(alphabet) for _ in range(len(missing_indices)))
        test_wif = wif
        for index, char in zip(missing_indices, rand_chars):
            test_wif = test_wif[:index] + char + test_wif[index+1:]
        try:
            key = Key(wif=test_wif)
            address = key.address()
            if address == target_address:
                found = True
                return test_wif, address
            print(f"Scanning WIF: {test_wif}")
        except:
            print(f"Invalid WIF: {test_wif}")

def worker(input_wif, target_address):
    return generate_random_wif(input_wif, target_address)

def main():
    input_wif = input("Enter a WIF string with missing characters (mark with *): ")
    target_address = input("Enter the target Bitcoin address: ")

    if '*' not in input_wif:
        print("Incorrect entry: * sign missing.")
        return

    pool = Pool(processes=4)
    results = pool.starmap(worker, [(input_wif, target_address)]*4)
    pool.close()
    pool.join()

    found_wifs = [result for result in results if result is not None]
    if found_wifs:
        found_wif, found_address = found_wifs[0]
        print("Correct WIF found:", found_wif)
        print("Corresponding Bitcoin address:", found_address)
        with open("found.txt", "w") as file:
            file.write(found_wif)

        with open("found2.txt", "w") as file:
            file.write(f"WIF: {found_wif}\nAddress: {found_address}")

    else:
        print("The correct WIF was not found. try more.")

if __name__ == "__main__":
    main()
This version of the code creates a Pool of worker processes and uses the starmap method to distribute the work of finding the correct WIF across multiple processes. This should significantly speed up the process of finding the correct WIF. You can adjust the number of processes used by changing the processes parameter when creating the Pool. I hope this helps! Let me know if you have any questions or need further assistance.

just adding in the import section, does this include modules? do we have to install a new module?
legendary
Activity: 2212
Merit: 5622
Non-custodial BTC Wallet
August 02, 2023, 07:44:01 AM
#7
You could use a lib such as multiprocessing (https://docs.python.org/3/library/multiprocessing.html) to run the generate_random_wif function in parallel.

This will most likely improve performance, as you will be running multiple instances of the same function at the same time.

if you wish, please write down the code you mean, so that it runs well. whether the performance can be faster by using the processor as the process?  Smiley

I asked GPT-4 to do this for you.



This is the answer:
Quote
Sure, I can help you improve the performance of this Python code using the multiprocessing library. Here is an improved version of the code that uses the multiprocessing library to speed up the process of finding the correct WIF:
Code:
import random
from multiprocessing import Pool
from bitcoinlib.keys import Key

def generate_random_wif(wif, target_address):
    alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
    missing_indices = [i for i in range(len(wif)) if wif[i] == '*']
    found = False
    while not found:
        rand_chars = ''.join(random.choice(alphabet) for _ in range(len(missing_indices)))
        test_wif = wif
        for index, char in zip(missing_indices, rand_chars):
            test_wif = test_wif[:index] + char + test_wif[index+1:]
        try:
            key = Key(wif=test_wif)
            address = key.address()
            if address == target_address:
                found = True
                return test_wif, address
            print(f"Scanning WIF: {test_wif}")
        except:
            print(f"Invalid WIF: {test_wif}")

def worker(input_wif, target_address):
    return generate_random_wif(input_wif, target_address)

def main():
    input_wif = input("Enter a WIF string with missing characters (mark with *): ")
    target_address = input("Enter the target Bitcoin address: ")

    if '*' not in input_wif:
        print("Incorrect entry: * sign missing.")
        return

    pool = Pool(processes=4)
    results = pool.starmap(worker, [(input_wif, target_address)]*4)
    pool.close()
    pool.join()

    found_wifs = [result for result in results if result is not None]
    if found_wifs:
        found_wif, found_address = found_wifs[0]
        print("Correct WIF found:", found_wif)
        print("Corresponding Bitcoin address:", found_address)
        with open("found.txt", "w") as file:
            file.write(found_wif)

        with open("found2.txt", "w") as file:
            file.write(f"WIF: {found_wif}\nAddress: {found_address}")

    else:
        print("The correct WIF was not found. try more.")

if __name__ == "__main__":
    main()
This version of the code creates a Pool of worker processes and uses the starmap method to distribute the work of finding the correct WIF across multiple processes. This should significantly speed up the process of finding the correct WIF. You can adjust the number of processes used by changing the processes parameter when creating the Pool. I hope this helps! Let me know if you have any questions or need further assistance.
newbie
Activity: 49
Merit: 0
August 02, 2023, 06:00:43 AM
#6
First of all, using print frequently would slow down your Python script.

You could use a lib such as multiprocessing (https://docs.python.org/3/library/multiprocessing.html) to run the generate_random_wif function in parallel.

This will most likely improve performance, as you will be running multiple instances of the same function at the same time.

With that library, OP probably have to modify his generate_random_wif function a bit where each process check different possible range/combination.

kindly ask, where should i add. The point is this tool can be used to recover lost characters.  Smiley
newbie
Activity: 49
Merit: 0
August 02, 2023, 01:58:27 AM
#5
When you want to write a recovery code (ie. a computation heavy code needing to be heavily optimized) you shouldn't go through the simple route writing a "normal" code like what you'd write for a normal function that encodes/decodes Base58 keys. Instead you should find the bottlenecks in your code and try to go around them by finding alternative computation route that perform faster.

I'm not a python expert but for example in your code when you are replacing * with random chars and then convert it to a key in this line:
Code:
key = Key(wif=test_wif)
you are adding a big bottleneck that slows your code specially since you are throwing and catching an exception which adds additional computational cost.
Changing that code and doing the conversion from base58 in your own code would significantly improve your speed. Of course when optimizing you must always perform benchmarks on your code and the changes you make so that you don't make it slower. And you should add tests so that you don't introduce bugs.

Sorry, I'm a bit confused by your statement. please help by writing where should I add or even replace the code in my code above?  Smiley
legendary
Activity: 3430
Merit: 10505
August 02, 2023, 01:02:06 AM
#4
When you want to write a recovery code (ie. a computation heavy code needing to be heavily optimized) you shouldn't go through the simple route writing a "normal" code like what you'd write for a normal function that encodes/decodes Base58 keys. Instead you should find the bottlenecks in your code and try to go around them by finding alternative computation route that perform faster.

I'm not a python expert but for example in your code when you are replacing * with random chars and then convert it to a key in this line:
Code:
key = Key(wif=test_wif)
you are adding a big bottleneck that slows your code specially since you are throwing and catching an exception which adds additional computational cost.
Changing that code and doing the conversion from base58 in your own code would significantly improve your speed. Of course when optimizing you must always perform benchmarks on your code and the changes you make so that you don't make it slower. And you should add tests so that you don't introduce bugs.
newbie
Activity: 49
Merit: 0
August 01, 2023, 11:58:11 PM
#3
You could use a lib such as multiprocessing (https://docs.python.org/3/library/multiprocessing.html) to run the generate_random_wif function in parallel.

This will most likely improve performance, as you will be running multiple instances of the same function at the same time.

if you wish, please write down the code you mean, so that it runs well. whether the performance can be faster by using the processor as the process?  Smiley
legendary
Activity: 2212
Merit: 5622
Non-custodial BTC Wallet
August 01, 2023, 02:32:13 PM
#2
You could use a lib such as multiprocessing (https://docs.python.org/3/library/multiprocessing.html) to run the generate_random_wif function in parallel.

This will most likely improve performance, as you will be running multiple instances of the same function at the same time.
newbie
Activity: 49
Merit: 0
August 01, 2023, 10:33:01 AM
#1
Hi Guys,,,

I want to try to make a program in python to find or restore a lost private key. The program's goal is also to search for missing characters. Any input, suggestions will be greatly appreciated. If there are deficiencies, please correct them so that they become better.

Code:
import random
import hashlib
import base58
from bitcoinlib.keys import Key

def generate_random_wif(wif, target_address):
    alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
    missing_indices = [i for i in range(len(wif)) if wif[i] == '*']
    found = False
    while not found:
        rand_chars = ''.join(random.choice(alphabet) for _ in range(len(missing_indices)))
        test_wif = wif
        for index, char in zip(missing_indices, rand_chars):
            test_wif = test_wif[:index] + char + test_wif[index+1:]
        try:
            key = Key(wif=test_wif)
            address = key.address()
            if address == target_address:
                found = True
                return test_wif, address
            print(f"Scanning WIF: {test_wif}")
        except:
            print(f"Invalid WIF: {test_wif}")

def main():
    input_wif = input("Enter a WIF string with missing characters (mark with *): ")
    target_address = input("Enter the target Bitcoin address: ")

    if '*' not in input_wif:
        print("Incorrect entry: * sign missing.")
        return

    found_wif, found_address = generate_random_wif(input_wif, target_address)
   
    if found_wif:
        print("Correct WIF found:", found_wif)
        print("Corresponding Bitcoin address:", found_address)
        with open("found.txt", "w") as file:
            file.write(found_wif)

        with open("found2.txt", "w") as file:
            file.write(f"WIF: {found_wif}\nAddress: {found_address}")

    else:
        print("The correct WIF was not found. try more.")

if __name__ == "__main__":
    main()


I want to ask the experts here, how can this program run even faster.

Thank's  Wink
Jump to: