0% found this document useful (0 votes)
19 views2 pages

Pubsfromkeys

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

Pubsfromkeys

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import os

import hashlib
import base58
import bip32utils
import segwit_addr
from ecdsa import SECP256k1, util
from ecdsa.keys import gen_key_from_secret

def get_public_address(private_key, address_type="P2PKH"):


"""
Given a private key, derive the public address in the specified format.

address_type options:
- "P2SH"
- "P2WSH"
- "P2WPKH"
- "P2PKH"
"""
# Create the public key from private key
secret = bytes.fromhex(private_key)
sk = util.SigningKey.from_string(secret, curve=SECP256k1)
vk = sk.get_verifying_key()
public_key = vk.to_string()

# P2PKH (Base58)
if address_type == "P2PKH":
sha256_pubkey = hashlib.sha256(public_key).digest()
ripemd160_pubkey = hashlib.new("ripemd160", sha256_pubkey).digest()
address = b'\x00' + ripemd160_pubkey
checksum = hashlib.sha256(hashlib.sha256(address).digest()).digest()[:4]
p2pkh_address = base58.b58encode(address + checksum).decode("utf-8")
return p2pkh_address

# P2SH (Base58)
elif address_type == "P2SH":
# Typically, P2SH addresses are used for multi-sig or SegWit scripts
# For simplicity, assume it's a P2SH wrapped SegWit address (P2WSH)
sha256_pubkey = hashlib.sha256(public_key).digest()
ripemd160_pubkey = hashlib.new("ripemd160", sha256_pubkey).digest()
address = b'\x05' + ripemd160_pubkey
checksum = hashlib.sha256(hashlib.sha256(address).digest()).digest()[:4]
p2sh_address = base58.b58encode(address + checksum).decode("utf-8")
return p2sh_address

# P2WSH (Bech32)
elif address_type == "P2WSH":
# Bech32 encoding for SegWit addresses (P2WSH)
# We need a SegWit program (0x0020 for P2WSH)
segwit_address = segwit_addr.encode("bc", 0,
hashlib.sha256(public_key).digest())
return segwit_address

# P2WPKH (Bech32)
elif address_type == "P2WPKH":
# Bech32 encoding for P2WPKH
# Program (0x00 for P2WPKH)
segwit_address = segwit_addr.encode("bc", 0, hashlib.new("ripemd160",
hashlib.sha256(public_key).digest()).digest())
return segwit_address
return None

def process_keys(input_file, output_dir):


# Check if output directory exists, create if not
if not os.path.exists(output_dir):
os.makedirs(output_dir)

output_file = os.path.join(output_dir, "addresses.txt")

# Open input and output files


with open(input_file, 'r') as infile, open(output_file, 'w') as outfile:
for line in infile:
private_key = line.strip() # Assuming each line is a private key in
hexadecimal
if len(private_key) == 64 and all(c in "0123456789abcdefABCDEF" for c
in private_key): # Valid hex
addresses = []
for address_type in ["P2SH", "P2WSH", "P2WPKH", "P2PKH"]:
address = get_public_address(private_key, address_type)
if address:
addresses.append(address)

# Write all addresses for this key in one line


outfile.write(" | ".join(addresses) + "\n")

print(f"Addresses have been written to {output_file}")

# Set your input file and output directory


input_file = "keys.txt" # Set the path to your keys.txt file
output_dir = "./output" # Set the directory where the output addresses will be
saved

process_keys(input_file, output_dir)

You might also like