capsul-flask/docs/images/generate_btcpay_keys.py
2021-07-21 21:05:23 +02:00

48 lines
1.4 KiB
Python

from ecdsa import SigningKey, SECP256k1, VerifyingKey
import binascii
import hashlib
def sha_digest(hexastring):
return hashlib.sha256(binascii.unhexlify(hexastring)).hexdigest()
def base58encode(hexastring):
chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
int_val = int(hexastring, 16)
encoded = encode58("", int_val, chars)
return encoded
def encode58(string, int_val, chars):
if int_val == 0:
return string
else:
new_val, rem = divmod(int_val, 58)
new_string = (chars[rem]) + string
return encode58(new_string, new_val, chars)
sk = SigningKey.generate(curve=SECP256k1)
pem = sk.to_pem()
pem = pem.decode("utf-8")
print("")
print(pem)
print("")
public_key = SigningKey.from_pem(pem).get_verifying_key().to_string()
public_key_hex = binascii.hexlify(public_key)
checksum_int = int(public_key_hex, 16)
if(checksum_int % 2 == 0):
prefix = "02"
else:
prefix = "03"
public_key_compressed = prefix + public_key_hex[0:64].decode("utf-8")
hash_of_public_key = sha_digest(public_key_compressed)
ripe_hash_of_public_key = hashlib.new("ripemd160")
ripe_hash_of_public_key.update(binascii.unhexlify(hash_of_public_key))
version_hash = "0F02"+ripe_hash_of_public_key.hexdigest()
version_checksum = sha_digest(sha_digest(version_hash))[0:8]
btcpay_sin = base58encode(version_hash+version_checksum)
print("")
print(btcpay_sin)
print("")