def gen_key_pair(): pk = random_key() wif = encode_privkey(pk, 'wif') data = encode_pubkey(privtopub(pk), 'bin_compressed') data = data + ripmed160(data)[:4] pubkey = "EOS" + b58encode(data).decode('utf8') return wif, pubkey
def print_pub(sig_hash, sig): ds = DataStream() print("Signature => %s" % sig) ds.pack_signature(sig) sig0 = ds.getvalue()[1:] #print(len(sig0)) #print(sig0) v, r, s = from_byte_to_int(sig0[0]), decode(sig0[1:33], 256), decode(sig0[33:], 256) # v,r,s = decode_sig(sig0[1:]) #print(v,r,s) Q = ecdsa_raw_recover(sig_hash, (v, r, s)) pub = encode_pubkey(Q, 'hex_compressed') if v >= 31 else encode_pubkey( Q, 'hex') #print(pub) ds = DataStream(bytes.fromhex("00" + pub)) pub0 = ds.unpack_public_key() print(" |----> ", pub0)
# Add suffix "01" to indicate a compressed private key compressed_private_key = private_key + '01' print("Private Key Compressed (hex) is: ", compressed_private_key) # Generate a WIF format from the compressed private key (WIF-compressed) wif_compressed_private_key = cryptos.encode_privkey( cryptos.decode_privkey(compressed_private_key, 'hex'), 'wif') print("Private Key (WIF-Compressed) is: ", wif_compressed_private_key) # Multiply the EC generator point G with the private key to get a public key point public_key = cryptos.fast_multiply(cryptos.G, decoded_private_key) print("Public Key (x,y) coordinates is:", public_key) # Encode as hex, prefix 04 hex_encoded_public_key = cryptos.encode_pubkey(public_key, 'hex') print("Public Key (hex) is:", hex_encoded_public_key) # Compress public key, adjust prefix depending on whether y is even or odd (public_key_x, public_key_y) = public_key compressed_prefix = '02' if (public_key_y % 2) == 0 else '03' hex_compressed_public_key = compressed_prefix + (cryptos.encode( public_key_x, 16).zfill(64)) print("Compressed Public Key (hex) is:", hex_compressed_public_key) # Generate bitcoin address from public key print("Bitcoin Address (b58check) is:", cryptos.pubkey_to_address(public_key)) # Generate compressed bitcoin address from compressed public key print("Compressed Bitcoin Address (b58check) is:", cryptos.pubkey_to_address(hex_compressed_public_key))
def get_pub_key(pk): data = encode_pubkey(privtopub(pk), 'bin_compressed') data = data + ripmed160(data)[:4] pubkey = "EOS" + b58encode(data).decode('utf8') return pubkey