Skip to content
LogoLogo

Create embedded wallets

Configure smart wallets to match your requirements. Openfort iOS SDK lets you:

  • Set recovery methods (automatic or password-based)
  • Adjust wallet creation options and chain preferences
  • Manage wallet state and embedded wallet lifecycle

Getting Encryption Session

import Foundation

struct EncryptionSessionResponse: Decodable {
    let session: String
}

func getEncryptionSession(completion: @escaping (Result<String, Error>) -> Void) {
    guard let url = URL(string: "https://openfort-auth-non-custodial.vercel.app/api/protected-create-encryption-session") else {
        completion(.failure(NSError(domain: "InvalidURL", code: -1, userInfo: nil)))
        return
    }

    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.httpBody = try? JSONSerialization.data(withJSONObject: [:]) // empty body {}

    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        if let error = error {
            completion(.failure(error))
            return
        }
        guard let data = data else {
            completion(.failure(NSError(domain: "NoData", code: -1, userInfo: nil)))
            return
        }
        do {
            let decoded = try JSONDecoder().decode(EncryptionSessionResponse.self, from: data)
            completion(.success(decoded.session))
        } catch {
            completion(.failure(error))
        }
    }
    task.resume()
}

Password recovery

Require that users set a password when the wallet is created, enforcing password-based recovery from the start.

If encrypted by user-provided entropy, only the user can decrypt the recovery share. Openfort never sees the user's password. Therefore, if you're not planning to ever use the automatic recovery mode, you can use the encryption share in the client side of the application.

do {
    let chainId = 80002
    let recoveryParams = OFRecoveryParamsDTO.password(password: password ?? "")
    let result = try await OFSDK.shared.configure(params: OFConfigureEmbeddedWalletDTO(chainId: chainId, recoveryParams: recoveryParams))
    print("Embedded wallet configured successfully: \(result)")
} catch {
    print("Error configuring embedded wallet: \(error)")
}

Automatic recovery

do {
    let chainId = 80002
    getEncryptionSession { result in
        switch result {
        case .success(let session):
            Task {
                let recoveryParams = OFRecoveryParamsDTO.automatic(encryptionSession: session)
                let result = try await OFSDK.shared.configure(params: OFConfigureEmbeddedWalletDTO(chainId: chainId, recoveryParams: recoveryParams))
                    print("Embedded wallet configured successfully: \(result)")
            }
            break
        case .failure(let error):
            print("Error configuring embedded wallet: \(error)")
        }
    }
    
} catch {
    print("Error configuring embedded wallet: \(error)")
}

Parameters

struct OFConfigureEmbeddedWalletDTO: OFCodableSendable {
    let chainId: Int?
    let recoveryParams: OFRecoveryParamsDTO
}

Returns

struct OFConfigureResponse: OFCodableSendable {
    public let ownerAddress: String?
    public let chainId: Int?
    public let owner: OFOwner?
    public let implementationType: String?
    public let chainType: String?
    public let address: String?
}

Throws

{
    method: method,
    success: false,
    error: error
}