Overview

This guide covers creating new wallets and initializing existing wallet addresses with the Dynamic Swift SDK.

Prerequisites

Wallet Creation

Create New Wallet Account

To create a new wallet for an authenticated user, use the createWalletAccount function:
import DynamicSwiftSDK

let dynamicClient: DynamicClient

// Create a new wallet account
do {
    let accountAddress = try await createWalletAccount(client: dynamicClient)
    print("✅ Wallet created successfully!")
    print("Account Address: \(accountAddress)")

    // The wallet is automatically added to the user's verified credentials
} catch {
    print("❌ Failed to create wallet: \(error)")
}

Wallet Initialization from Existing Address

Initialize Ethereum Wallet from Address

Once a wallet exists (either created or already present), you can initialize an EthereumWallet instance:
import DynamicSwiftSDK

let dynamicClient: DynamicClient
let walletAddress = "0x1234567890abcdef1234567890abcdef12345678" // Existing wallet address

do {
    let ethereumWallet = try EthereumWallet(
        address: walletAddress,
        client: dynamicClient
    )

    print("Wallet initialized: \(ethereumWallet.address.asString())")
} catch {
    print("Failed to initialize wallet: \(error)")
}

Get Wallet from Verified Credentials

import DynamicSwiftSDK

let dynamicClient: DynamicClient

// Get user's verified credentials and filter for blockchain wallets
if let verifiedCredentials = dynamicClient.user?.verifiedCredentials {
    for credential in verifiedCredentials {
        if credential.format == .blockchain,
           let walletAddress = credential.publicIdentifier {
            do {
                let wallet = try EthereumWallet(address: walletAddress, client: dynamicClient)
                print("Wallet found: \(wallet.address.asString())")
                break
            } catch {
                print("Failed to initialize wallet: \(error)")
            }
        }
    }
}

Error Handling

Common Wallet Creation Errors

do {
    let wallet = try EthereumWallet(address: "invalid_address", client: dynamicClient)
} catch {
    if let nsError = error as NSError? {
        switch nsError.code {
        case 1003:
            print("Could not determine wallet ID from address")
        default:
            print("Wallet creation error: \(error)")
        }
    }
}

Next Steps

After creating or initializing your wallet, you can: