Skip to content

Instantly share code, notes, and snippets.

@circulosmeos
Last active November 25, 2023 17:20
Show Gist options
  • Save circulosmeos/97f2c155777434081cb56886c1b608be to your computer and use it in GitHub Desktop.
Save circulosmeos/97f2c155777434081cb56886c1b608be to your computer and use it in GitHub Desktop.
Easily generate the litecoin address from the public key using Python (compatible with Python 2 and 3)
#!/usr/bin/env python
# patched from https://bitcoin.stackexchange.com/questions/56923/is-this-how-to-generate-a-bitcoin-address-with-python
# https://en.bitcoin.it/wiki/Protocol_documentation#Addresses
# Adapted to litecoin, following https://bitcoin.stackexchange.com/questions/65282/how-is-a-litecoin-address-generated
import hashlib
import base58
# ECDSA bitcoin Public Key
pubkey = '0450863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b23522cd470243453a299fa9e77237716103abc11a1df38855ed6f2ee187e9c582ba6'
# See 'compressed form' at https://en.bitcoin.it/wiki/Protocol_documentation#Signatures
compress_pubkey = True
def hash160(hex_str):
sha = hashlib.sha256()
rip = hashlib.new('ripemd160')
sha.update(hex_str)
rip.update( sha.digest() )
print ( "key_hash = \t" + rip.hexdigest() )
return rip.hexdigest() # .hexdigest() is hex ASCII
if (compress_pubkey):
if (ord(bytearray.fromhex(pubkey[-2:])) % 2 == 0):
pubkey_compressed = '02'
else:
pubkey_compressed = '03'
pubkey_compressed += pubkey[2:66]
hex_str = bytearray.fromhex(pubkey_compressed)
else:
hex_str = bytearray.fromhex(pubkey)
# Obtain key:
key_hash = '30' + hash160(hex_str)
# Obtain signature:
sha = hashlib.sha256()
sha.update( bytearray.fromhex(key_hash) )
checksum = sha.digest()
sha = hashlib.sha256()
sha.update(checksum)
checksum = sha.hexdigest()[0:8]
print ( "checksum = \t" + sha.hexdigest() )
#while (key_hash[0:2] == '00'):
# key_hash=key_hash[2:]
print ( "key_hash + checksum = \t" + key_hash + ' ' + checksum )
print ( "litecoin address = \t" + (base58.b58encode( bytes(bytearray.fromhex(key_hash + checksum)) )).decode('utf-8') )
@ManikandanR2909
Copy link

Good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment