Skip to content

Instantly share code, notes, and snippets.

@HeXenCore
Forked from chenxu2048/address.py
Created May 26, 2021 11:24
Show Gist options
  • Select an option

  • Save HeXenCore/6255a57c97494615b64be24d9feca376 to your computer and use it in GitHub Desktop.

Select an option

Save HeXenCore/6255a57c97494615b64be24d9feca376 to your computer and use it in GitHub Desktop.

Revisions

  1. @chenxu2048 chenxu2048 revised this gist Apr 23, 2020. 2 changed files with 3 additions and 1 deletion.
    2 changes: 1 addition & 1 deletion address.py
    Original file line number Diff line number Diff line change
    @@ -89,7 +89,7 @@ def eth(raw_private_key):
    print("public_key:", public_key.hex().upper())
    address = eth_address(public_key)
    print("raw address:", address)
    print("address:", address[-40:-1])
    print("address:", address[-40:)

    print("===============BITCOIN====================")
    bitcoin(PRIVATE_KEY)
    2 changes: 2 additions & 0 deletions requirements.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    pysha3 >= 1.0.2
    ecdsa >= 0.15
  2. @chenxu2048 chenxu2048 created this gist Apr 4, 2020.
    97 changes: 97 additions & 0 deletions address.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,97 @@
    import hashlib
    from ecdsa import SECP256k1, SigningKey
    from sha3 import keccak_256
    import sys
    import binascii

    BASE58TABLE = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

    def from_bytes (data, big_endian = False):
    if isinstance(data, str):
    data = bytearray(data)
    if big_endian:
    data = reversed(data)
    num = 0
    for offset, byte in enumerate(data):
    num += byte << (offset * 8)
    return num

    def base58_encode(payload):
    result = int.from_bytes(payload, byteorder="big")
    padding = len(payload) - len(payload.lstrip(b'\0'))
    encoded = []
    while result != 0:
    result, remainder = divmod(result, 58)
    encoded.append(BASE58TABLE[remainder])

    return padding * "1" + "".join(encoded)[::-1]

    def btc_private_key(hex_string):
    return bytes.fromhex(hex_string.zfill(64))

    def btc_public_key(private_key):
    public_key = SigningKey.from_string(private_key, curve=SECP256k1).verifying_key
    return bytes.fromhex("04"+ public_key.to_string().hex())

    def btc_public_key_sha256(original):
    return hashlib.sha256(original).digest()

    def btc_public_key_ripemd160(original):
    h = hashlib.new('ripemd160')
    h.update(original)
    return h.digest()

    def btc_hex_address(version, ripemd_hash, checksum):
    checksum = checksum[:4]
    version = bytes.fromhex(version)
    return version + ripemd_hash + checksum

    PRIVATE_KEY="7C7FC7CCECC1FEE3B3E4AC63ADD864BD786696C20CA15683A269AA9AD8639443"

    def bitcoin(raw_private_key):
    private_key = btc_private_key(raw_private_key)
    print("private key:", private_key.hex().upper())

    public_key = btc_public_key(private_key)
    print("public_key:", public_key.hex().upper())

    public_key_hash = btc_public_key_sha256(public_key)
    print("public_key_hash:", public_key_hash.hex().upper())

    public_key_160 = btc_public_key_ripemd160(public_key_hash)
    print("ripemd160:", public_key_160.hex().upper())

    sha256_1 = btc_public_key_sha256(bytes.fromhex("00") + public_key_160)
    print("first sha256:", sha256_1.hex().upper())
    sha256_2 = btc_public_key_sha256(sha256_1)
    print("second sha256:", sha256_2.hex().upper())

    hex_address = btc_hex_address("00", public_key_160, sha256_2)
    print("hex key:", hex_address.hex().upper())

    bitcoin_address = base58_encode(hex_address)
    print("final address:", bitcoin_address)

    def eth_private_key(hex_string):
    return bytes.fromhex(hex_string.zfill(64))

    def eth_public_key(private_key):
    return SigningKey.from_string(private_key, curve=SECP256k1).verifying_key.to_string()

    def eth_address(public_key):
    addr = keccak_256(public_key)
    return addr.hexdigest().upper()

    def eth(raw_private_key):
    private_key = eth_private_key(raw_private_key)
    public_key = eth_public_key(private_key)
    print("private key:", private_key.hex().upper())
    print("public_key:", public_key.hex().upper())
    address = eth_address(public_key)
    print("raw address:", address)
    print("address:", address[-40:-1])

    print("===============BITCOIN====================")
    bitcoin(PRIVATE_KEY)
    print("===============ETHEREUM===================")
    eth(PRIVATE_KEY)