Pubsfromkeys
Pubsfromkeys
import hashlib
import base58
import bip32utils
import segwit_addr
from ecdsa import SECP256k1, util
from ecdsa.keys import gen_key_from_secret
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
process_keys(input_file, output_dir)