Skip to content

createWebAuthnCredential

Registers a WebAuthn Credential designed to be used to create a WebAuthn Account.

Import

import { createWebAuthnCredential } from 'viem/account-abstraction'

Usage

import { 
  createWebAuthnCredential, 
  toWebAuthnAccount 
} from 'viem/account-abstraction'
 
// Register a credential (ie. passkey).
const credential = await createWebAuthnCredential({ 
  name: 'Example', 
}) 
 
// Create a WebAuthn account from the credential.
const account = toWebAuthnAccount({
  credential,
})

Returns

P256Credential

A P-256 WebAuthn Credential.

Parameters

challenge

  • Type: Uint8Array

An ArrayBuffer, TypedArray, or DataView used as a cryptographic challenge.

const credential = await createWebAuthnCredential({
  challenge: new Uint8Array([1, 2, 3]), 
  name: 'Example',
})

createFn

  • Type: (options: CredentialCreationOptions) => Promise<Credential | null>
  • Default: window.navigator.credentials.create

Credential creation function. Useful for environments that do not support the WebAuthn API natively (i.e. React Native or testing environments).

import * as passkey from 'react-native-passkeys'
 
const credential = await createWebAuthnCredential({
  name: 'Example',
  createFn: passkey.create, 
})
 
const account = toWebAuthnAccount({
  credential,
})

excludeCredentialIds

  • Type: string[]

List of credential IDs to exclude from the creation. This property can be used to prevent creation of a credential if it already exists.

const credential = await createWebAuthnCredential({
  excludeCredentialIds: ['abc', 'def'], 
  name: 'Example',
})

name

  • Type: string

Name to identify the credential.

const credential = await createWebAuthnCredential({
  name: 'Example', 
})
 
const account = toWebAuthnAccount({
  credential,
})

rp

  • Type: { id: string; name: string }

An object describing the relying party that requested the credential creation

const credential = await createWebAuthnCredential({
  name: 'Example',
  rp: { 
    id: 'example.com', 
    name: 'Example', 
  }, 
})
 
const account = toWebAuthnAccount({
  credential,
})

timeout

  • Type: number

A numerical hint, in milliseconds, which indicates the time the calling web app is willing to wait for the creation operation to complete.

const credential = await createWebAuthnCredential({
  name: 'Example',
  timeout: 1000, 
})
 
const account = toWebAuthnAccount({
  credential,
})