import { encodeAbiParameters, keccak256, toHex } from 'viem' import { Module } from '../types' import { parsePublicKey } from './utils' import { WebauthnCredential } from './types' import { GLOBAL_CONSTANTS } from '../../constants' export const getWebAuthnValidator = ( webAuthnCredential: WebauthnCredential, ): Module => { let pubKeyX: bigint let pubKeyY: bigint // Distinguish between PublicKey and Hex / byte encoded public key if ( typeof webAuthnCredential.pubKey === 'string' || webAuthnCredential.pubKey instanceof Uint8Array ) { // It's a P256Credential const { x, y, prefix } = parsePublicKey(webAuthnCredential.pubKey) pubKeyX = x pubKeyY = y if (prefix && prefix !== 4) { throw new Error('Only uncompressed public keys are supported') } } else { // It's already a PublicKey pubKeyX = webAuthnCredential.pubKey.x pubKeyY = webAuthnCredential.pubKey.y } return { address: GLOBAL_CONSTANTS.WEBAUTHN_VALIDATOR_ADDRESS, module: GLOBAL_CONSTANTS.WEBAUTHN_VALIDATOR_ADDRESS, initData: encodeAbiParameters( [ { components: [ { name: 'pubKeyX', type: 'uint256', }, { name: 'pubKeyY', type: 'uint256', }, ], type: 'tuple', }, { type: 'bytes32', name: 'authenticatorIdHash', }, ], [ { pubKeyX, pubKeyY, }, keccak256(toHex(webAuthnCredential.authenticatorId)), ], ), deInitData: '0x', hook: webAuthnCredential.hook, additionalContext: '0x', type: 'validator', } }