clawchain/viem/site/pages/tempo/accounts/account.fromWebAuthnP256.mdx

118 lines
3.1 KiB
Plaintext

# `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`.
:::warning
It is highly recommended to store the credential's public key in an external store for future use (ie. for future calls to `WebAuthnP256.getCredential`).
:::
```ts twoslash
// @noErrors
import { Account, WebAuthnP256 } from 'viem/tempo'
import { store } from './store'
// 1. Create credential
const credential = await WebAuthnP256.createCredential({ name: 'Example' })
// 2. Instantiate account
const account = Account.fromWebAuthnP256(credential)
// 3. Store public key
await store.set(credential.id, credential.publicKey)
console.log('Address:', account.address)
// @log: Address: 0x...
```
### Loading Credentials
Get a credential from `WebAuthnP256.getCredential` and then instantiate an account with `Account.fromWebAuthnP256`.
:::warning
The `getPublicKey` function is required to fetch the public key paired with the credential from an external store.
The public key is required to derive the account's address.
:::
```ts twoslash
// @noErrors
import { Account, WebAuthnP256 } from 'viem/tempo'
import { store } from './store'
// 1. Get credential
const credential = await WebAuthnP256.getCredential({
async getPublicKey(credential) {
// 2. Get public key from external store.
return await store.get(credential.id)
}
})
// 3. Instantiate account
const account = Account.fromWebAuthnP256(credential)
console.log('Address:', account.address)
// @log: Address: 0x...
```
## Return Type
The return type of `Account.fromWebAuthnP256` is backwards compatible with Viem's `Account` type.
```ts
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 ID
- `publicKey` - 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.