Bitcoin Forum
May 27, 2024, 10:10:31 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: I made my own code to Create your own Bitcoin address (Python)  (Read 72 times)
Phobos_Quake (OP)
Newbie
*
Offline Offline

Activity: 7
Merit: 0


View Profile
March 19, 2024, 02:34:27 AM
 #1

So, i haven't found many places online that provides you a way to create your own address.
We have https://www.bitaddress.org (They also have an offline use method) But it doesn't create a bech32 address (Which provides cheaper fees) and i'm not sure they allow you to generate a seed-phrase.

One of the reason as to why i wanted full control on my Bitcoin keys creation is because is hard to verify that non-custodial wallets really don't know your private keys. You would need to use a packet sniffer like Wireshark at the moment that you use your seed phrase to access your wallet to see what packets are sent to the wallet provider, and even then, it's hard to know what information is shared to them.
Of course, you might have to interact with a wallet software the moment you want to withdraw your coins, but creating your own valid address can be useful as paper wallet till then.

Another problem i wanted to address is "Entropy". One of the reasons they warn you to not create your own address is because your entropy method might be weak and could be hacked.

Now, i'm no expert at Bitcoin nor Python, so one of the purposes of sharing my code is also to hear feedback on vulnerabilities or inefficiencies it might have.
To execute this code i used Spyder with anaconda interpreter (Downloaded needed libraries through anaconda)

1) Create your own entropy

import hashlib
import os

def generate_entropy_from_input(user_input):
    salt = os.urandom(16)  # Generate a random salt
    encoded_input = user_input.encode('utf-8')
    sha512_hash = hashlib.sha512(salt + encoded_input).digest()  # Use a stronger hashing algorithm
    entropy = sha512_hash[:16]  # Adjust the length of entropy as per your requirements
    return entropy.hex()

user_variable = "test" #Choose your input here for entropy #IMPORTANT: erase your input after running the program
entropy_code = generate_entropy_from_input(user_variable)
print("Entropy Code:", entropy_code)


On the line "user_variable = "test" #Choose your input here for entropy #IMPORTANT: erase your input after running the program"
We can erase test and input whatever we want in there, you can roll your face over your keyboard, type a book story in different languages,etc and make sure it makes no sense in the end so no one can guess your entropy.

On top of that, i added random salting so each time you run this code, the entropy result code will be different even if the entropy is the same (This is to safe guard in case someone ends up saving the code file with the last entropy input they have used)

Now, when we ran that code, it gave us a result in hex that we can use on the next step.

2) Create your own Bitcoin address

import hashlib
import bip32utils
from mnemonic import Mnemonic
import bech32
import base58

# Input your entropy code
entropy_code = ""  # IMPORTANT: After running the program, erase your code
# Convert the entropy code to bytes
entropy = bytes.fromhex(entropy_code)

# Generate a BIP39 mnemonic phrase
mnemonic = Mnemonic("english")
words = mnemonic.to_mnemonic(entropy)  # 12-word phrase

# Derive a seed from the mnemonic phrase
seed = mnemonic.to_seed(words)

# Derive a master key from the seed
master_key = hashlib.pbkdf2_hmac('sha512', seed, b'mnemonic', 2048)

# Derive the private key from the master key
private_key = master_key[:32]

# Convert private key to WIF format
extended_private_key = b'\x80' + private_key + b'\x01'
checksum = hashlib.sha256(hashlib.sha256(extended_private_key).digest()).digest()[:4]
wif_private_key = base58.b58encode(extended_private_key + checksum).decode()

# Hash the private key
hashed_private_key = hashlib.sha256(private_key).digest()

# Apply RIPEMD-160 hash
ripemd160_hash = hashlib.new('ripemd160')
ripemd160_hash.update(hashed_private_key)
hashed_ripemd160 = ripemd160_hash.digest()

# Add a version byte
version_byte = b'\x00'
extended_hashed_ripemd160 = version_byte + hashed_ripemd160

# Calculate the checksum
checksum = hashlib.sha256(hashlib.sha256(extended_hashed_ripemd160).digest()).digest()[:4]

# Append the checksum
address_bytes = extended_hashed_ripemd160 + checksum

# Determine the appropriate values for hrp, witver, and witprog based on the address type
hrp = "bc"  # Human-readable part for Bitcoin mainnet addresses
witver = 0  # Witness version (0 for Pay-to-Witness-Public-Key-Hash addresses)
witprog = hashed_ripemd160  # Use the hashed RIPEMD-160 instead of address_bytes

# Generate the Bech32 public key
bech32_public_key = bech32.encode(hrp, witver, hashed_ripemd160)

# Print the Bitcoin private key (WIF format), checksum, Bech32 address, and Bech32 public key
print("Bitcoin Private Key (WIF format):", wif_private_key)
print("Bitcoin Private Key (Hexadecimal):", private_key.hex())
print("Checksum:", checksum.hex())
print("BIP39 Mnemonic Phrase:", words)
print("Bech32 Public Key:", bech32_public_key)
print("If your public key ends with a P, try to generate a new one")


Here, all you need to change is the entropy code required at the start, which is the entropy code we got when we ran the first code.

Your seed phrase will be generated from the words pool of "BIP39" which is the standard used for many wallets out there. I was tempted to create seed phrase with numbers and other languages, since that would make them less common, but some wallets require to import a seed phrase that uses BIP39

You can see what the results will be by checking all the "Print" output at the end.
Remember you might need to download the libraries onto your pc first. And this code should be ran with your Internet off

I hope this can be useful to someone and don't hesitate to tell me if my code has some issues or vulnerabilities

And, can you show me a modification to my code to create a Taproot address?

pooya87
Legendary
*
Offline Offline

Activity: 3458
Merit: 10594



View Profile
March 19, 2024, 04:44:33 AM
 #2

One of the reason as to why i wanted full control on my Bitcoin keys creation is because is hard to verify that non-custodial wallets really don't know your private keys. You would need to use a packet sniffer like Wireshark at the moment that you use your seed phrase to access your wallet to see what packets are sent to the wallet provider, and even then, it's hard to know what information is shared to them.
As long as the wallet software you are using is 100% open source, you don't need complicated ways, you can just take a look at its source code to see what it is doing in the background like what data is being sent out to other nodes/servers.
For example you could have just gone though Electrum's source code which happens to be in python to see what it does.

Quote
Another problem i wanted to address is "Entropy". One of the reasons they warn you to not create your own address is because your entropy method might be weak and could be hacked.
Same as above.

Quote
i'm no expert at Bitcoin nor Python
And, can you show me a modification to my code to create a Taproot address?
Well you need to have a good understanding of both Bitcoin and python to be able to write something like that. In any case read my comment here to see how its being done
https://bitcointalk.org/index.php?topic=5461279.msg62621270#msg62621270

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!