jgilmour / brainwallet-check

Generates a bitcoin address and private key according to a 'brainwallet' string, then checks to see if the wallet is active
27 stars 32 forks source link

Python 2.7 to Python 3.x #4

Open CY0xZ opened 1 year ago

CY0xZ commented 1 year ago

Thank you very much for sharing your code, I really liked it and that is why I have taken the liberty to adapt it to python3.

Original:

pubkey2=hashlib.sha256(binascii.unhexlify('04'+pubkey)).hexdigest()
pubkey4=hashlib.sha256(binascii.unhexlify('00'+pubkey3)).hexdigest()

Final Code:

pubkey2=hashlib.sha256(binascii.unhexlify(b'04'+pubkey)).hexdigest()  # Encode as bytes
pubkey4=hashlib.sha256(binascii.unhexlify(b'00'+pubkey3.encode())).hexdigest()  # Encode '00' and pubkey3 as bytes

In the final version of the code, specific changes have been made to ensure that concatenation operations are carried out correctly. Here's the detailed explanation of the changes:

pubkey2 Encoding:

Originally, pubkey is a string of hexadecimal digits. To use it with the hashlib.sha256 function, you need to convert it into bytes. In the final version, a b is added before the string '04'+pubkey, indicating that it's bytes rather than a string.

pubkey4 Encoding:

Similarly, you need to convert '00'+pubkey3 into bytes before passing it to the hashlib.sha256 function. However, in this case, pubkey3 is a hexadecimal string, so you need to explicitly encode it using .encode(). Hence, b'00'+pubkey3.encode() is used.

Input String Encoding:

Also, in the main code block section, a line has been added to encode the input string (sys.argv[1]) into bytes using .encode('utf-8'). This is necessary to ensure that the input is handled properly in the hash operations. In the original version, the input was used directly, causing issues with hash operations in Python 3.

GET Request with urllib.request:

The part of the code that handles GET requests using urllib.request is well implemented and doesn't require any changes. This allows the script to retrieve information from blockchain.info.

These changes are made to ensure compatibility with Python 3 and to ensure that concatenation and manipulation of strings and bytes are done correctly.