Skip to content

Instantly share code, notes, and snippets.

@gitzhou
Created April 9, 2020 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitzhou/71be1299248e636bc897f1b3200e3952 to your computer and use it in GitHub Desktop.
Save gitzhou/71be1299248e636bc897f1b3200e3952 to your computer and use it in GitHub Desktop.
from bitsv.base58 import b58encode, b58decode
from bitsv.crypto import ECPrivateKey, sha256, hash160
from binascii import hexlify
#
# pip install bitsv
#
# https://aaron67.cc/2019/01/04/bitcoin-address/
#
print()
# give wif
wif = 'L2M6qFzrdDEv7eRT7p94Zf4QShLdWjicFpkiCro12CrRbCDb8ke3'
print(wif)
print()
# base58check decode wif
wif_base58_decode = b58decode(wif)
print(hexlify(wif_base58_decode))
print()
# split wif decoded bytes, get raw private key
prefix, raw_private_key, wif_compressed, wif_checksum = wif_base58_decode[0:1], wif_base58_decode[1:-5], wif_base58_decode[-5:-4], wif_base58_decode[-4:]
print(hexlify(prefix))
print(hexlify(raw_private_key))
print(hexlify(wif_compressed))
print(hexlify(wif_checksum))
print()
# raw_private_key --> public_key
uncompressed_public_key = ECPrivateKey(raw_private_key).public_key.format(compressed=False)
print(hexlify(uncompressed_public_key))
compressed_public_key = ECPrivateKey(raw_private_key).public_key.format(compressed=True)
print(hexlify(compressed_public_key))
print()
# compressed_public_key --> public_key_hash
public_key_hash = hash160(compressed_public_key)
print(hexlify(public_key_hash))
# public_key_hash --> p2pkh_address
payload = public_key_hash
s1 = b'\x00' + payload
print(hexlify(s1))
s2 = sha256(sha256(s1))
print(hexlify(s2))
checksum = s2[0:4]
print(hexlify(checksum))
s3 = s1 + checksum
print(hexlify(s3))
p2pkh_address = b58encode(s3)
print(p2pkh_address)
@gitzhou
Copy link
Author

gitzhou commented Apr 9, 2020

Output

L2M6qFzrdDEv7eRT7p94Zf4QShLdWjicFpkiCro12CrRbCDb8ke3

b'8098fd2a819a382f8e142e38242f6caf2a2f6f58e7fe6ca5f23c5b0818b15b4ba601236803e8'

b'80'
b'98fd2a819a382f8e142e38242f6caf2a2f6f58e7fe6ca5f23c5b0818b15b4ba6'
b'01'
b'236803e8'

b'04da52d817a5ae3555f36a94528322eb47016f1334b798f5b4fa614a892dabb3eaf314bf38816673c55c1708cf1e36b55c936db97618d6460c4d223be83bec7788'
b'02da52d817a5ae3555f36a94528322eb47016f1334b798f5b4fa614a892dabb3ea'

b'710a35053db4296dc5f3476d10ae93924bc55c9a'
b'00710a35053db4296dc5f3476d10ae93924bc55c9a'
b'5f91f9a69fc02d537c93e41b127b2f5081fd25f82683c1eebe3c54aae1f83130'
b'5f91f9a6'
b'00710a35053db4296dc5f3476d10ae93924bc55c9a5f91f9a6'
1BJhat1AMGYbT9HYJxVekoCaPaqB9ZyTyF

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