from cryptography.hazmat.primitives import serialization
import base64

def base64url_encode(data: bytes) -> str:
    return base64.urlsafe_b64encode(data).decode('utf-8').rstrip('=')

# Load PEM public key
with open("vapid_public.pem", "rb") as f:
    pem_data = f.read()

public_key = serialization.load_pem_public_key(pem_data)

# Extract the raw uncompressed public key bytes (0x04 + X + Y)
public_numbers = public_key.public_numbers()
x_bytes = public_numbers.x.to_bytes(32, 'big')
y_bytes = public_numbers.y.to_bytes(32, 'big')
uncompressed_key = b'\x04' + x_bytes + y_bytes

# Base64url encode the uncompressed key bytes
public_key_b64url = base64url_encode(uncompressed_key)

print("Base64url-encoded public key (use in frontend):")
print(public_key_b64url)
