import { hexToBytes, bytesToHex, type Hex } from 'viem' import { PublicKey, WebauthnSignature } from './types' export function parsePublicKey(publicKey: Hex | Uint8Array): PublicKey { const bytes = typeof publicKey === 'string' ? hexToBytes(publicKey) : publicKey const offset = bytes.length === 65 ? 1 : 0 const x = bytes.slice(offset, 32 + offset) const y = bytes.slice(32 + offset, 64 + offset) return { prefix: bytes.length === 65 ? bytes[0] : undefined, x: BigInt(bytesToHex(x)), y: BigInt(bytesToHex(y)), } } export function parseSignature(signature: Hex | Uint8Array): WebauthnSignature { const bytes = typeof signature === 'string' ? hexToBytes(signature) : signature const r = bytes.slice(0, 32) const s = bytes.slice(32, 64) return { r: BigInt(bytesToHex(r)), s: BigInt(bytesToHex(s)), } }