Account.fromWebAuthnP256
Instantiates an Account from a WebAuthn credential (passkey).
Usage
Creating Credentials
Create a credential with WebAuthnP256.createCredential and then instantiate a Viem Account with Account.fromWebAuthnP256.
import { , } from 'viem/tempo'
import { } from './store'
// 1. Create credential
const = await .createCredential({ : 'Example' })
// 2. Instantiate account
const = .fromWebAuthnP256()
// 3. Store public key
await .set(.id, .publicKey)
.('Address:', .address)
Address: 0x...Loading Credentials
Get a credential from WebAuthnP256.getCredential and then instantiate an account with Account.fromWebAuthnP256.
import { , } from 'viem/tempo'
import { } from './store'
// 1. Get credential
const = await .getCredential({
async () {
// 2. Get public key from external store.
return await .get(.id)
}
})
// 3. Instantiate account
const = .fromWebAuthnP256()
.('Address:', .address)
Address: 0x...Return Type
The return type of Account.fromWebAuthnP256 is backwards compatible with Viem's Account type.
type ReturnType = Account
type Account = {
/** Account address */
address: Address
/** Key type */
keyType: string
/** Public key (hex) */
publicKey: Hex
/** Account source */
source: string
/** Account type */
type: 'local'
/** Signs a raw digest */
sign: (parameters: { hash: Hex }) => Promise<Hex>
/** Signs an EIP-7702 authorization */
signAuthorization: (parameters: SignAuthorizationParameters) => Promise<Authorization>
/** Signs a EIP-191 `personal_sign` message */
signMessage: (parameters: { message: string | { raw: Hex } }) => Promise<Hex>
/** Signs a transaction */
signTransaction: (transaction: TransactionRequest) => Promise<Hex>
/** Signs a EIP-712 typed data */
signTypedData: (typedData: TypedData) => Promise<Hex>
}Parameters
credential
- Type:
{ id: string; publicKey: Hex }
The WebAuthn credential object containing:
id- The credential IDpublicKey- The public key as a hex string
options (optional)
options.getFn
- Type:
(options: CredentialRequestOptions) => Promise<Credential | null>
Custom function to get the WebAuthn credential. Use this to override the default navigator.credentials.get behavior.
options.rpId
- Type:
string
The relying party ID. This should match the domain of your application.