Skip to content

ServerActions.upgradeAccount

Viem Action for upgrading an Externally-Owned Account (EOA) to a Porto Account. Uses wallet_prepareUpgradeAccount and wallet_upgradeAccount under the hood.

Usage

The example below demonstrates an upgrade of an EOA to a Porto Account via the account parameter.

example.ts
import { Account, Key, ServerActions } from 'porto/viem'
import { client } from './config'
 
const account = await ServerActions.upgradeAccount(client, { 
  account: Account.fromPrivateKey('0x...'), 
  authorizeKeys: [Key.createSecp256k1({ role: 'admin' })], 
}) 

Prepared Usage

The example below demonstrates "prepared" usage, where the EOA explicitly signs over the digests provded from ServerActions.prepareUpgradeAccount, and then passes the signatures to ServerActions.upgradeAccount.

example.ts
import { Account, Key, ServerActions } from 'porto/viem'
import { client } from './config'
 
const eoa = Account.fromPrivateKey('0x...')
 
// Prepare the upgrade.
const request = await ServerActions.prepareUpgradeAccount(client, {
  address: eoa.address,
  authorizeKeys: [Key.createSecp256k1({ role: 'admin' })],
})
 
// Sign the digests. 
const signatures = { 
  auth: await eoa.sign({ hash: request.digests.auth }), 
  exec: await eoa.sign({ hash: request.digests.exec }), 
} 
 
// Complete the upgrade. 
const account = await ServerActions.upgradeAccount(client, { 
  ...request, 
  signatures, 
}) 

Parameters

The function accepts either unprepared parameters or prepared parameters:

account

  • Type: Account.Account<'privateKey'>

The EOA account to upgrade.

authorizeKeys

  • Type: readonly Key.Key[]

Additional keys to authorize on the account (beyond those already on the account).

context

  • Type: prepareUpgradeAccount.ReturnType['context']

Context from a previous prepareUpgradeAccount call.

signatures

  • Type: { auth: Hex.Hex; exec: Hex.Hex }

Signatures over the auth and exec digests.

Return Value

Returns the upgraded Porto Account with authorized keys.