UNPKG

viem

Version:

TypeScript Interface for Ethereum

57 lines (51 loc) 1.63 kB
import { type P256Credential, type SignParameters, sign } from 'webauthn-p256' import type { ErrorType } from '../../errors/utils.js' import { hashMessage } from '../../utils/signature/hashMessage.js' import { hashTypedData } from '../../utils/signature/hashTypedData.js' import type { WebAuthnAccount } from './types.js' export type ToWebAuthnAccountParameters = { /** * The WebAuthn P256 credential to use. */ credential: { id: P256Credential['id'] publicKey: P256Credential['publicKey'] } /** * Credential request function. Useful for environments that do not support * the WebAuthn API natively (i.e. React Native or testing environments). * * @default window.navigator.credentials.get */ getFn?: SignParameters['getFn'] | undefined /** * The relying party identifier to use. */ rpId?: SignParameters['rpId'] | undefined } export type ToWebAuthnAccountReturnType = WebAuthnAccount export type ToWebAuthnAccountErrorType = ErrorType /** * @description Creates an Account from a WebAuthn Credential. * * @returns A WebAuthn Account. */ export function toWebAuthnAccount( parameters: ToWebAuthnAccountParameters, ): WebAuthnAccount { const { getFn, rpId } = parameters const { id, publicKey } = parameters.credential return { publicKey, async sign({ hash }) { return sign({ credentialId: id, getFn, hash, rpId }) }, async signMessage({ message }) { return this.sign({ hash: hashMessage(message) }) }, async signTypedData(parameters) { return this.sign({ hash: hashTypedData(parameters) }) }, type: 'webAuthn', } }