# CoseKey.toPublicKey

Converts a CBOR-encoded COSE\_Key to a P256 [`PublicKey.PublicKey`](/api/PublicKey/types#publickey).

Accepts the COSE key as either a hex string or raw bytes. When the `returnByteLength` or `returnDecoded` option is set, the function returns an object containing the public key plus the consumed byte length and/or the decoded CBOR map. This is useful for parsing CBOR streams (such as WebAuthn `authenticatorData`) where the COSE key is followed by trailing data.

## Imports

:::code-group
```ts [Named]
import { CoseKey } from 'ox'
```

```ts [Entrypoint]
import * as CoseKey from 'ox/CoseKey'
```
:::

## Examples

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

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

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

### With Byte Length

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

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

const { publicKey: pk, byteLength } = CoseKey.toPublicKey(
  coseKey,
  {
    returnByteLength: true
  }
)
```

## Definition

```ts
function toPublicKey<options>(
  coseKey: Hex.Hex | Uint8Array,
  options?: options | toPublicKey.Options,
): toPublicKey.ReturnType<options>
```

**Source:** [src/core/CoseKey.ts](https://github.com/wevm/ox/blob/main/src/core/CoseKey.ts#L222)

## Parameters

### coseKey

* **Type:** `Hex.Hex | Uint8Array`

The CBOR-encoded COSE\_Key as hex or bytes.

### options

* **Type:** `options | toPublicKey.Options`
* **Optional**

Decoding options.

## Return Type

The P256 public key, optionally with byte length and decoded CBOR.

`toPublicKey.ReturnType<options>`
