import cbor2
import os
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.backends import default_backend

message = 'a40162474206fb41d9140b51fb70b904fb41d98c5831fb7732390103a101a4617681aa626369782655524e3a555643493a30313a47423a313637363333373737324b4344413637593857524d234262636f62474262646e036264746a323032312d31322d3232626973774465706172746d656e74206f66204865616c7468204e49626d616d4f52472d313030303330323135626d706c45552f312f32302f3135323862736403627467693834303533393030366276706a3131313933343930303763646f626a313938372d30342d3137636e616d6266666e644b48414e62676e6652415348414c63666e74644b48414e63676e746652415348414c6376657265312e332e30'

# Assuming the message is stored in the 'message' variable
decoded_message = cbor2.loads(bytes.fromhex(message))

# Extract payload from -260 key
payload = decoded_message[-260]

# Create the COSE message
cose_message = {
    1: decoded_message[1],  # COSE_Sign1
    4: decoded_message[4],  # KID header
    -260: payload
}

# Convert the message to CBOR format
cose_message_cbor = cbor2.dumps(cose_message)

# Load the private key from a PEM file
with open('private-key.pem', 'rb') as f:
    private_key = serialization.load_pem_private_key(
        f.read(),
        password=None,
        backend=default_backend()
    )

# Compute the ECDSA signature of the COSE message
signature = private_key.sign(
    cose_message_cbor,
    ec.ECDSA(hashes.SHA256())
)

# Convert to bytearray to modify the