# CoseKey

Utility functions for converting between COSE\_Key and P256 public keys.

COSE\_Key is the key format used in WebAuthn attestation objects, as defined in
[RFC 9053](https://datatracker.ietf.org/doc/html/rfc9053).

## Examples

Below are some examples demonstrating common usages of the `CoseKey` module:

* [Encoding a Public Key to COSE\_Key](#encoding-a-public-key-to-cose_key)

* [Decoding a COSE\_Key to Public Key](#decoding-a-cose_key-to-public-key)

### Encoding a Public Key to COSE\_Key

```ts twoslash
import { CoseKey, P256 } from 'ox'

const { publicKey } = P256.createKeyPair()

const coseKey = CoseKey.fromPublicKey(publicKey)
```

### Decoding a COSE\_Key to Public Key

```ts twoslash
import { CoseKey, P256 } from 'ox'

const { publicKey } = P256.createKeyPair()
const coseKey = CoseKey.fromPublicKey(publicKey)

const publicKey2 = CoseKey.toPublicKey(coseKey)
```

## Functions

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`CoseKey.fromPublicKey`](/api/CoseKey/fromPublicKey) | Converts a P256 [`PublicKey.PublicKey`](/api/PublicKey/types#publickey) to a CBOR-encoded COSE\_Key. |
| [`CoseKey.toPublicKey`](/api/CoseKey/toPublicKey) | Converts a CBOR-encoded COSE\_Key to a P256 [`PublicKey.PublicKey`](/api/PublicKey/types#publickey). |

## Errors

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`CoseKey.InvalidCoseKeyError`](/api/CoseKey/errors#cosekeyinvalidcosekeyerror) | Thrown when a COSE\_Key does not contain valid P256 public key coordinates. |
