This code generates a random private key, converts it to a WIF private key, generates a public key from the private key, generates a Bitcoin address from the public key, checks the balance of the address using the Blockchain.info API, and prints the balance in green if it's non-zero or red if it's zero.
Note that this script is for educational purposes
Note that this script is for educational purposes
Python:
import os
import ecdsa
import hashlib
import base58
import requests
import telebot
# Telegram bot token
TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN'
bot = telebot.TeleBot(TOKEN)
def generate_private_key():
# Generate a random private key
private_key = os.urandom(32)
return private_key.hex()
def generate_public_key(private_key):
# Create a signing key from the private key
signing_key = ecdsa.SigningKey.from_string(bytes.fromhex(private_key), curve=ecdsa.SECP256k1)
# Get the verifying key from the signing key
verifying_key = signing_key.get_verifying_key()
# Convert the verifying key to a public key
public_key = verifying_key.to_string()
return public_key.hex()
def generate_p2pkh_address(public_key):
# Hash the public key using SHA-256 and RIPEMD-160
sha256_hash = hashlib.sha256(bytes.fromhex(public_key)).digest()
ripemd160_hash = hashlib.new('ripemd160')
ripemd160_hash.update(sha256_hash)
# Add the network byte (0x00 for mainnet)
address_hash = b'\x00' + ripemd160_hash.digest()
# Calculate the checksum
checksum = hashlib.sha256(hashlib.sha256(address_hash).digest()).digest()[:4]
# Create the address by concatenating the address hash and checksum
address = base58.b58encode(address_hash + checksum)
return address.decode()
def check_balance(address):
# Use a blockchain API to check the balance of the address
response = requests.get(f'https://blockchain.info/q/addressbalance/{address}')
if response.status_code == 200:
return int(response.text)
else:
return 0
def send_to_telegram(private_key, public_key, address, balance):
# Send the generated keys and address to the Telegram bot
message = f'Private Key: {private_key}\nPublic Key: {public_key}\nAddress: {address}\nBalance: {balance} satoshi'
bot.send_message(chat_id='YOUR_CHAT_ID', text=message)
def main():
private_key = generate_private_key()
public_key = generate_public_key(private_key)
address = generate_p2pkh_address(public_key)
balance = check_balance(address)
if balance > 0:
print(f'\033[92m{address} has a balance of {balance} satoshi\033[0m')
else:
print(f'\033[91m{address} has no balance\033[0m')
send_to_telegram(private_key, public_key, address, balance)
if __name__ == '__main__':
main()