Skip to content

Add app accounts sdk docs #1820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Sub Accounts

Sub Accounts are hierarchical, app-specific accounts that extend from a user's primary Smart Wallet. Each Sub Account operates as a distinct account that can:

- Execute transactions and sign messages
- Maintain separate asset management
- Be fully controlled by both the universal Smart Wallet and authorized application logic

## Technical Details

- Smart Wallet Ownership: The user's Smart Wallet acts as an owner of the Sub Account, allowing it to manage assets on the Sub Account and make calls from it.
- App-Signer Agnostic: Sub Accounts are designed to be agnostic to whatever signing software an app wants to use: whether in-browser CryptoKey or server signers from teams like Privy or Turnkey.
- When an app requests a Sub Account creation and the user approves, all future signing and transaction requests will use the Sub Account.

Refer to the [Sub Account Reference](/identity/smart-wallet/sdk/sub-account-reference) for more details on how to create and manage Sub Accounts.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import SdkParameters from './snippets/sdk-paramaters.mdx'
import SdkParameters from './snippets/sdk-parameters.mdx'

# Getting Started

Expand Down Expand Up @@ -77,4 +77,45 @@ export const sdk = createCoinbaseWalletSDK({

::::

## `createCoinbaseWalletSDK` Returns

An object with related SDK methods

```ts
type CreateAccount = {
type: 'create';
keys: {
type: 'address' | 'p256' | 'webcrypto-p256' | 'webauthn-p256';
key: `0x${string}`;
}[];
};

type SubAccountInfo = {
address: `0x${string}`;
factory?: `0x${string}`;
factoryData?: `0x${string}`;
}

type AddOwnerParams = {
address: `0x${string}`;
publicKey?: never;
chainId: number;
} | {
address?: never;
publicKey: `0x${string}`;
chainId: number;
}

type CoinbaseWalletSDK = {
getProvider: () => CoinbaseWalletProvider;
// Only available v4.4.0+
subaccount: {
setSigner: (() => Promise<OneOf<LocalAccount, WebAuthnAccount>>) => void;
create: (account: CreateAccount) => Promise<SubAccountInfo>;
get: () => Promise<SubAccountInfo>;
addOwner: (params: AddOwnerParams) => Promise<void>;
}
}
```

<SdkParameters />
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import PreferenceParameters from './preference-parameters.mdx'
import PreferenceParameters from './preference-parameters.mdx';

## Parameters

### appName (optional)

- Type: `string`

The app name. This will be displayed to users on connection, transacting, and signing requests.

### appChainIds (optional)

- Type: `number[]`

Array of chain IDs your app supports. Default value is `[1]`.

[What networks are supported?](/identity/smart-wallet/FAQ#what-networks-are-supported)

### appLogoUrl (optional)

- Type: `string`

App logo image URL. Favicon is used if unspecified.
Expand All @@ -23,4 +26,16 @@ App logo image URL. Favicon is used if unspecified.
Local paths are not supported for `appLogoUrl` as the logo is presented on another window as popup. Please provide a non-local URL.
:::

### subaccount (optional)

- Type: `object`

Subaccount configuration. Available version 4.4.0+

#### getSigner

- Type: `() => Promise<OneOf<LocalAccount, WebAuthnAccount>>`

Function that returns a signer for the subaccount. This function should resolve to a Viem LocalAccount or a Viem WebAuthnAccount

<PreferenceParameters />
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# Sub Accounts

## Overview

The Coinbase Wallet SDK now supports Sub Accounts, allowing apps to create and manage additional smart contract accounts for users.
Sub Accounts can have multiple owners and operate as smart contract wallets with enhanced security and flexibility.

## Getting Started

### Installation

```bash
npm install @coinbase/wallet-sdk@canary
```

### Basic Setup with Sub Account Support

```typescript
import { createCoinbaseWalletSDK, getCryptoKeyAccount } from '@coinbase/wallet-sdk';

// Initialize the SDK with Sub Account support
const sdk = createCoinbaseWalletSDK({
appName: 'My Dapp',
appLogoUrl: 'https://example.com/logo.png',
// Set up Sub Account support with a signer function
subaccount: {
getSigner: async () => {
// Return a signer that can be used to authenticate operations
return await getCryptoKeyAccount();
},
},
});

// Get the provider for regular wallet operations
const provider = sdk.getProvider();
```

## Creating a Sub Account

```typescript
// Create a new Sub Account with WebAuthn keys
const account = await sdk.subaccount.create({
type: 'create',
keys: [
{
type: 'webauthn-p256',
// The public key of the owner, this can be retrieved from the account object
key: '0x7da44d4bc972affd138c619a211ef0afe0926b813fec67d15587cf8625b2bf185f5044ae96640a63b32aa1eb6f8f993006bbd26292b81cb07a0672302c69a866',
},
],
});

console.log('Sub account created:', account.address);
```

## Getting a Sub Account

```typescript
// Get the current Sub Account if it exists, otherwise connect to one
const account = await sdk.subaccount.get();
console.log('Sub account address:', account.address);
```

## Adding an Owner to a Sub Account

You can add owners to a Sub Account by providing either an address or a public key.

```typescript
// Add an owner using an address
await sdk.subaccount.addOwner({
chainId: 84532, // Base Sepolia
address: '0xE3cA9Cc9378143a26b9d4692Ca3722dc45910a15',
});

// Or add an owner using a public key
const { account } = await getCryptoKeyAccount();
await sdk.subaccount.addOwner({
chainId: 84532, // Base Sepolia
publicKey: account.publicKey,
});
```

## Using a Sub Account for Transactions

Once a Sub Account is created, you can use it to sign and send transactions:

```typescript
// Connect to the Sub Account
const account = await sdk.subaccount.get();

// Use the provider to send transactions from the Sub Account
const provider = sdk.getProvider();
const signature = await provider.request({
method: 'personal_sign',
params: ['0x48656c6c6f2c20776f726c6421', account.address],
});
```

## API Reference

### SDK Configuration

```typescript
createCoinbaseWalletSDK({
appName: string,
appLogoUrl: string,
subaccount: {
getSigner: () => Promise<{ account: { publicKey: string; address?: string } }>,
},
});
```

### Sub Account Methods

#### `sdk.subaccount.create(account)`

Creates a new Sub Account.

Parameters:

- `account`: An object with details about the account to create
- `type`: 'create' | 'deployed' | 'undeployed'
- `keys`: Array of key objects (for 'create' type)
- `type`: 'address' | 'p256' | 'webcrypto-p256' | 'webauthn-p256'
- `key`: Hex string of the public key

Returns: `Promise<SubAccountInfo>`

#### `sdk.subaccount.get()`

Gets the current Sub Account, or connects to one if none exists.

Returns: `Promise<SubAccountInfo>`

#### `sdk.subaccount.addOwner(options)`

Adds an owner to a Sub Account.

Parameters:

- `options`: Object with details about the owner to add
- `chainId`: Chain ID (number)
- `address`: Address of the owner (optional)
- `publicKey`: Public key of the owner (optional)

Returns: `Promise<string>` - Response from the smart contract call

#### `sdk.subaccount.setSigner(getSigner)`

Sets the signer function for Sub Account operations.

Parameters:

- `getSigner`: Function that returns a Promise resolving to a signer object

## Technical Details

Sub accounts are implemented as smart contract wallets that support multiple owners. The SDK provides a wrapper around the low-level RPC methods used to interact with these accounts:

- `wallet_addSubAccount`: Creates a new Sub Account
- `wallet_connect`: Connects to an existing Sub Account
- `wallet_sendCalls`: Sends transactions from a Sub Account

Each Sub Account can have multiple owners identified by either their address or public key. When performing operations with a Sub Account, the appropriate owner is used to sign the transaction.

## Requirements

- Coinbase Wallet SDK v4.4.0-canary or higher
- An app connected to an EVM-compatible chain
8 changes: 8 additions & 0 deletions apps/base-docs/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,10 @@ export const sidebar: Sidebar = [
text: 'Custom Gas Tokens',
link: '/identity/smart-wallet/features/custom-gas-tokens',
},
{
text: 'Sub Accounts',
link: '/identity/smart-wallet/features/sub-accounts',
},
],
},
],
Expand Down Expand Up @@ -792,6 +796,10 @@ export const sidebar: Sidebar = [
},
],
},
{
text: 'Sub Account Reference',
link: '/identity/smart-wallet/sdk/sub-account-reference',
},
],
},
],
Expand Down