Key.fromWebCryptoP256
Instantiates a WebCryptoP256 key from existing WebCrypto key pair parameters. This allows you to create a Porto key from an existing WebCrypto CryptoKey pair.
Usage
From WebCrypto Key Pair
import { Key } from 'porto/viem'
import { WebCryptoP256 } from 'ox'
const keyPair = await WebCryptoP256.createKeyPair()
const key = Key.fromWebCryptoP256({
keyPair,
})
Session Key with Permissions
import { Key } from 'porto/viem'
import { WebCryptoP256 } from 'ox'
import { parseEther } from 'viem'
const keyPair = await WebCryptoP256.createKeyPair()
const key = Key.fromWebCryptoP256({
expiry: Math.floor(Date.now() / 1000) + 60 * 60, // 1 hour
keyPair,
permissions: {
calls: [{
signature: 'transfer(address,uint256)',
to: '0xaf3b0a5b4becc4fa1dfafe74580efa19a2ea49fa',
}],
spend: [{
limit: parseEther('50'), // 50 EXP
period: 'day',
token: '0xaf3b0a5b4becc4fa1dfafe74580efa19a2ea49fa',
}],
},
role: 'session',
})
Parameters
expiry
- Type:
number | undefined
The expiry timestamp of the key (Unix timestamp). Defaults to 0 (never expires).
keyPair
- Type:
KeyPair
WebCrypto P256 key pair (required).
type KeyPair = {
/** WebCrypto private key */
privateKey: CryptoKey
/** WebCrypto public key */
publicKey: CryptoKey
}
permissions
The permissions associated with the key.
type Permissions = {
/** Call permissions - which functions the key can call */
calls?: {
/** Function signature or 4-byte selector */
signature?: string | undefined
/** Target contract address */
to?: `0x${string}` | undefined
}[] | undefined
/** Spend permissions - spending limits for tokens */
spend?: {
/** Spending limit per period (in wei) */
limit: `0x${string}`
/** Time period for the spending limit */
period: 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year'
/** ERC20 token address (defaults to native token if not provided) */
token?: `0x${string}` | undefined
}[] | undefined
}
role
- Type:
"admin" | "session" | undefined
The role of the key. Defaults to 'admin'.
Return Value
type WebCryptoKey = {
/** Key expiry timestamp */
expiry: number
/** Key hash identifier */
hash: `0x${string}`
/** Key ID (lowercase public key) */
id: `0x${string}`
/** Optional permissions */
permissions?: Permissions | undefined
/** Whether the key requires prehashing (always true for WebCrypto) */
prehash: true
/** WebCrypto private key */
privateKey: CryptoKey
/** Public key (64 bytes, uncompressed, without 0x04 prefix) */
publicKey: `0x${string}`
/** Key role */
role: 'admin' | 'session'
/** Key type - always 'p256' (reuses P256 type with WebCrypto backend) */
type: 'p256'
}