WalletActions.grantPermissions
Viem Action for granting permissions to an application. Uses wallet_grantPermissions
under the hood.
Usage
example.ts
import { parseEther } from 'viem'
import { WalletActions } from 'porto/viem'
import { client } from './config'
const token = '0x29f45fc3ed1d0ffafb5e2af9cc6c3ab1555cd5a2'
const permissions = await WalletActions.grantPermissions(client, {
expiry: Math.floor(Date.now() / 1_000) + 60 * 60, // 1 hour
permissions: {
calls: [{
signature: 'transfer(address,uint256)',
to: token
}],
spend: [{
limit: parseEther('50'), // 50 EXP
period: 'day',
token: token,
}]
},
})
App-managed Keys
Applications can also grant permissions to a specific signing key by providing the key
parameter.
example.ts
import { privateKeyToAccount, parseEther } from 'viem'
import { WalletActions } from 'porto/viem'
import { client } from './config'
const token = '0x29f45fc3ed1d0ffafb5e2af9cc6c3ab1555cd5a2'
const account = privateKeyToAccount('0x...')
// Grant permissions with custom key
const permission = await WalletActions.grantPermissions(client, {
expiry: Math.floor(Date.now() / 1000) + 7 * 24 * 60 * 60, // 1 week
key: {
publicKey: account.address,
type: 'secp256k1',
},
permissions: {
calls: [{
signature: 'transfer(address,uint256)',
to: token
}],
spend: [{
limit: parseEther('50'), // 50 EXP
period: 'day',
token: token,
}]
},
})
Parameters
address
Address | undefined
Address of the account to grant permissions on.
expiry
number
Expiry timestamp of the permissions.
key
Key to grant permissions to. Defaults to a wallet-managed key.
type Key = {
/** Public key */
publicKey: `0x${string}`
/** Key type */
type: 'address' | 'p256' | 'secp256k1' | 'webauthn-p256'
}
permissions
Permissions to grant.
type Permissions = {
/** Call permissions */
calls: {
/** Function signature or 4-byte signature */
signature?: string
/** Authorized target address */
to?: `0x${string}`
}[]
/** Spend permissions */
spend: {
/** Spending limit (in wei) per period */
limit: `0x${string}`
/** Period of the spend limit */
period: 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year'
/** ERC20 token to set the limit on (defaults to native token) */
token?: `0x${string}`
}[]
}
Return Value
address
Address
Address of the account.
chainId
number
Chain ID that the permissions are granted on.
expiry
number
Expiry timestamp of the permissions.
id
string
Permission ID.
key
Key to grant permissions to.
type Key = {
/** Public key */
publicKey: `0x${string}`
/** Key type */
type: 'address' | 'p256' | 'secp256k1' | 'webauthn-p256'
}
permissions
Permissions to grant to the account.
type Permissions = {
/** Call permissions */
calls: {
/** Function signature or 4-byte signature */
signature?: string
/** Authorized target address */
to?: `0x${string}`
}[]
/** Spend permissions */
spend: {
/** Spending limit (in wei) per period */
limit: `0x${string}`
/** Period of the spend limit */
period: 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year'
/** ERC20 token to set the limit on (defaults to native token) */
token?: `0x${string}`
}[]
}