Skip to content

Account.fromWebCryptoP256

Instantiates an Account from a WebCrypto P256 key pair.

Usage

import { Account } from 'viem/tempo'
import { WebCryptoP256 } from 'ox'
 
const keyPair = await WebCryptoP256.createKeyPair()
 
const account = Account.fromWebCryptoP256(keyPair)
 
console.log('Address:', account.address)
Address: 0x...

Access Keys

Create an account that acts as an access key for a parent account:

example.ts
import { Account, WebCryptoP256 } from 'viem/tempo'
import { client } from './viem.config'
 
// Create root account
const account = Account.fromSecp256k1(
  '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'
)
 
// Create access key 
const keyPair = await WebCryptoP256.createKeyPair()
const accessKey = Account.fromWebCryptoP256(keyPair, { access: account })
 
// Sign a key authorization
const keyAuthorization = await account.signKeyAuthorization(accessKey, {
  expiry: Math.floor(Date.now() / 1000) + 86400, // 24 hour expiry
})
 
// Attach to next transaction
const receipt = await client.sendTransactionSync({
  account: accessKey,
  keyAuthorization,
  to: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef',
})
 
// Now any subsequent transaction can be used with `accessKey`
// WITHOUT the `keyAuthorization` parameter!

Return Type

The return type of Account.fromWebCryptoP256 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'
  
  /** Assigns a key authorization to the next transaction */
  assignKeyAuthorization: (keyAuthorization: KeyAuthorization) => Promise<void>
  /** Signs a raw digest */
  sign: (parameters: { hash: Hex }) => Promise<Hex>
  /** Signs an EIP-7702 authorization */
  signAuthorization: (parameters: SignAuthorizationParameters) => Promise<Authorization>
  /** Signs a key authorization */
  signKeyAuthorization: (
    key: { accessKeyAddress: Address; keyType: string },
    parameters?: { expiry?: bigint; limits?: Limits }
  ) => Promise<KeyAuthorization>
  /** 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

keyPair

  • Type: { publicKey: PublicKey; privateKey: CryptoKey }

The WebCrypto P256 key pair from WebCryptoP256.createKeyPair().

options (optional)

options.access

  • Type: Address | Account

Parent account to access.