Skip to content

WalletActions.connect

Viem Action for connecting an account using ERC-7846. Uses wallet_connect under the hood.

Usage

example.ts
import { WalletActions } from 'porto/viem'
import { client } from './config'
 
const result = await WalletActions.connect(client) 

Grant Permissions

You can grant permissions for an application to perform actions on behalf of the account by providing the grantPermissions parameter.

example.ts
import { WalletActions } from 'porto/viem'
import { parseEther, toHex } from 'viem'
import { client } from './config'
 
const token = '0x29f45fc3ed1d0ffafb5e2af9cc6c3ab1555cd5a2'
 
const result = await WalletActions.connect(client, {
  grantPermissions: { 
    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, 
      }], 
    }, 
  }, 
})

Parameters

email

boolean

  • Show optional email input during account creation.
  • Defaults to true

grantPermissions

Permissions to grant to the account.

type GrantPermissions = {
  /** Expiry timestamp for the permissions */
  expiry: number
 
  /** Key to grant permissions to. Defaults to a wallet-managed key. */
  key?: {
    /** Public key. Accepts an address for `address` & `secp256k1` types. */
    publicKey?: `0x${string}`
    /** Key type. */
    type?: 'address' | 'p256' | 'secp256k1' | 'webauthn-p256'
  }
 
  /** Permissions to grant */
  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}`
    }[]
  }
}

signInWithEthereum

ERC-4361 Sign-In with Ethereum options.

type SignInWithEthereum = {
  /* Required fields */
  nonce: string
 
  /* Optional fields */
  chainId?: number | undefined
  domain?: string | undefined // Defaults to window/iframe parent
  expirationTime?: Date | undefined
  issuedAt?: Date | undefined
  notBefore?: Date | undefined
  requestId?: string | undefined
  resources?: string[] | undefined
  scheme?: string | undefined
  statement?: string | undefined
  uri?: string | undefined // Defaults to window/iframe parent
  version?: '1' | undefined
}

Return Value

type ConnectResponse = {
  /** Array of accounts available to the user */
  accounts: {
    /** Account address */
    address: `0x${string}`
    
    /** Optional capabilities associated with the account */ 
    capabilities?: {
      /** Admin keys authorized on the account */
      admins?: {
        /** Key ID */
        id: `0x${string}`
        /** Public key of the admin */
        publicKey: `0x${string}`
        /** Key type */
        type: 'address' | 'p256' | 'secp256k1' | 'webauthn-p256'
      }[]
 
      /** Permissions granted on the account */
      permissions?: {
        /** Account address */
        address: `0x${string}`
        /** Chain ID */
        chainId?: number
        /** Permission expiry timestamp */
        expiry: number
        /** Permission ID */
        id: `0x${string}`
        /** Key associated with the permission */
        key: {
          /** Public key */
          publicKey: `0x${string}`
          /** Key type */
          type: 'address' | 'p256' | 'secp256k1' | 'webauthn-p256'
        }
        /** Permission details */
        permissions: {
          /** Call permissions */
          calls: {
            /** Function signature */
            signature?: string
            /** Target contract address */
            to?: `0x${string}`
          }[]
          /** Spend permissions */
          spend?: {
            /** Spending limit per period */
            limit: `0x${string}`
            /** Period for the spending limit */
            period: 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year'
            /** Token address (defaults to native token if not provided) */
            token?: `0x${string}`
          }[]
        }
      }[]
 
      /** ERC-4361 message and corresponding signature */
      signInWithEthereum?: {
        message: string
        signature: `0x${string}`
      }
    }
  }[]
}