4.8 KiB

description
description
Requests to add a Sub Account.

addSubAccount

Requests to add a Sub Account. See more

What is a Sub Account?

Usage

:::code-group

import { walletClient } from './config'
 
const subAccount = await walletClient.addSubAccount({
  type: 'create',
})
import 'viem/window'
// ---cut---
import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
import { erc7895Actions } from 'viem/experimental'

export const walletClient = createWalletClient({
  chain: mainnet,
  transport: custom(window.ethereum!),
}).extend(erc7895Actions())

:::

Returns

The created Sub Account.

type ReturnType = {
  address: Address
  factory?: Address | undefined
  factoryData?: Hex | undefined
}

Parameters

New Accounts

Allows the wallet to create a Sub Account with a set of known signing keys. Learn more

keys

Set of signing keys that will belong to the Sub Account.

import { walletClient } from './config'
 
const subAccount = await walletClient.addSubAccount({
  keys: [{ // [!code focus]
    publicKey: '0xefd5fb29a274ea6682673d8b3caa9263e936d48d486e5df68893003e01241522', // [!code focus]
    type: 'p256' // [!code focus]
  }], // [!code focus]
  type: 'create',
})

keys.publicKey

  • Type: Hex

The public key of the signing key.

  • This is a 32-byte hexadecimal string.
  • For type: "address", this is a 20-byte address.
import { walletClient } from './config'
 
const subAccount = await walletClient.addSubAccount({
  keys: [{
    publicKey: '0xefd5fb29a274ea6682673d8b3caa9263e936d48d486e5df68893003e01241522', // [!code focus]
    type: 'p256'
  }],
  type: 'create',
})

keys.type

  • Type: 'address' | 'p256' | 'webcrypto-p256' | 'webauthn-p256'

The type of signing key.

import { walletClient } from './config'
 
const subAccount = await walletClient.addSubAccount({
  keys: [{
    publicKey: '0xefd5fb29a274ea6682673d8b3caa9263e936d48d486e5df68893003e01241522',
    type: 'p256' // [!code focus]
  }],
  type: 'create',
})

Deployed Accounts

An existing account that the user wants to link to their global account. Learn more

address

Address of the deployed account.

import { walletClient } from './config'
 
const subAccount = await walletClient.addSubAccount({
  address: '0x0000000000000000000000000000000000000000', // [!code focus]
  type: 'deployed',
})

chainId

The chain ID of the deployed account.

import { walletClient } from './config'
 
const subAccount = await walletClient.addSubAccount({
  address: '0x0000000000000000000000000000000000000000',
  chainId: 1, // [!code focus]
  type: 'deployed',
})

Undeployed Accounts

An account that has been created, but is not yet deployed. The wallet will decide whether or not to deploy it. Learn more

address

Address of the undeployed account.

import { walletClient } from './config'
 
const subAccount = await walletClient.addSubAccount({
  address: '0x0000000000000000000000000000000000000000', // [!code focus]
  factory: '0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce', 
  factoryData: '0xdeadbeef',
  type: 'undeployed',
})

chainId

The chain ID the account will be deployed on.

import { walletClient } from './config'
 
const subAccount = await walletClient.addSubAccount({
  address: '0x0000000000000000000000000000000000000000',
  chainId: 1, // [!code focus]
  factory: '0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce', 
  factoryData: '0xdeadbeef',
  type: 'undeployed',
})

factory

The address of the factory contract.

import { walletClient } from './config'
 
const subAccount = await walletClient.addSubAccount({
  address: '0x0000000000000000000000000000000000000000',
  factory: '0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce', // [!code focus]
  factoryData: '0xdeadbeef',
  type: 'undeployed',
})

factoryData

The data to be passed to the factory contract.

import { walletClient } from './config'
 
const subAccount = await walletClient.addSubAccount({
  address: '0x0000000000000000000000000000000000000000',
  factory: '0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce',
  factoryData: '0xdeadbeef', // [!code focus]
  type: 'undeployed',
})