Skip to main content

Getting Started

This guide walks you through integrating the Privacy Boost iOS SDK into your app.

Overview

The iOS SDK follows a simple flow:
  1. Initialize - Create SDK instance with configuration
  2. Connect - Connect wallet via WalletDelegate
  3. Login - Authenticate and get privacy address
  4. Use - Deposit, withdraw, transfer tokens

Basic Setup

1. Import the SDK

import PrivacyBoost

2. Configure the SDK

let config = SdkConfig(
    indexerUrl: "https://test-api.privacy-boost.sunnyside.io/indexer",
    proverUrl: "https://test-api.privacy-boost.sunnyside.io/prover",
    chainId: 11155420,
    shieldContract: "0xB22fD661b322F10d4B7cd0cFcb9578C485423119",
    wethContract: "0x4200000000000000000000000000000000000006"
)

3. Initialize

do {
    let sdk = try PrivacyBoostSdk(config: config)
} catch {
    print("Failed to initialize SDK: \(error)")
}

Connecting a Wallet

The SDK uses the WalletDelegate protocol to interact with wallets. You must implement this protocol:
class MyWalletDelegate: WalletDelegate {
    func getAddress() throws -> String {
        // Return the connected wallet address
        return "0x..."
    }

    func getChainId() throws -> UInt64 {
        // Return the current chain ID
        return 1
    }

    func signMessage(message: String) throws -> String {
        // Sign using EIP-191 personal_sign
        // Return signature as hex string with 0x prefix
        return "0x..."
    }

    func signTypedData(typedDataJson: String) throws -> String {
        // Sign using EIP-712 eth_signTypedData_v4
        return "0x..."
    }

    func sendTransaction(toAddress: String, value: String, data: String) throws -> String {
        // Submit transaction and return hash
        return "0x..."
    }
}
You can choose any web3 libraries in Swift to integrate with this delegate. Then connect:
let wallet = MyWalletDelegate()
do {
    let result = try sdk.connect(wallet: wallet)
    print("Connected: \(result.walletAddress)")
    print("Privacy Address: \(result.privacyAddress)")
} catch {
    print("Connection failed: \(error)")
}

Authentication

After connecting, authenticate with the backend:
do {
    let loginResult = try sdk.login()
    print("Privacy Address: \(loginResult.privacyAddress)")
    print("MPK: \(loginResult.mpk)")
} catch {
    print("Login failed: \(error)")
}

Core Operations

Check Balance

do {
    let balance = try sdk.getBalance(tokenAddress: "0x...")
    print("Shielded: \(balance.shieldedBalance)")
    print("Wallet: \(balance.walletBalance)")
} catch {
    print("Failed to get balance: \(error)")
}

Deposit Tokens

Move tokens from wallet to shielded pool:
do {
    let result = try sdk.deposit(
        tokenAddress: "0x...",
        amount: "1000000000000000000"  // 1 token (18 decimals)
    )
    print("Deposit TX: \(result.txHash)")
} catch {
    print("Deposit failed: \(error)")
}

Withdraw Tokens

Move tokens from shielded pool to wallet:
do {
    let result = try sdk.withdraw(
        tokenAddress: "0x...",
        amount: "500000000000000000",
        recipient: "0x..."  // Recipient address
    )
    print("Withdraw TX: \(result.txHash)")
} catch {
    print("Withdraw failed: \(error)")
}

Private Transfer

Send tokens privately to another user:
do {
    let result = try sdk.send(
        tokenAddress: "0x...",
        amount: "250000000000000000",
        recipientPrivacyAddress: "0x04..."  // 194-char privacy address
    )
    print("Transfer TX: \(result.txHash)")
} catch {
    print("Transfer failed: \(error)")
}

Error Handling

The SDK throws SDKError for various failure conditions:
do {
    let result = try sdk.deposit(tokenAddress: token, amount: amount)
} catch SDKError.notConnected {
    print("Wallet not connected")
} catch SDKError.notAuthenticated {
    print("Not logged in")
} catch SDKError.insufficientBalance {
    print("Insufficient balance")
} catch SDKError.invalidAmount {
    print("Invalid amount format")
} catch SDKError.walletError(let message) {
    print("Wallet error: \(message)")
} catch SDKError.networkError(let message) {
    print("Network error: \(message)")
} catch {
    print("Unknown error: \(error)")
}

Session Persistence

Save and restore sessions to avoid re-signing:
// Export session
if let session = sdk.exportSession() {
    // Store securely (e.g., Keychain)
    saveToKeychain(session)
}

// Import session
if let session = loadFromKeychain() {
    do {
        let success = try sdk.importSession(session)
        if success {
            print("Session restored")
        }
    } catch {
        print("Session expired or invalid")
    }
}

Next Steps