Cross-device sessions

Use wallet recovery to enable your users to create multiple signing shares across their devices.

Once users successfully back up their wallets, they can generate a new set of signing shares irrespective of the SDK in use. Furthermore, both the new and old sets of signing shares can be used simultaneously, thanks to portal.recoverWallet. This provisioning relies on the user already having a successful set of backup shares (in the below example we'll assume they are password backup shares).

While recovering, the wallet fetches the encryption key from the user's cloud storage provider to decrypt the encrypted user backup share. Note that our SDK handles the decryption; you only need to supply the encrypted user backup share that was stored by your API.

For wallet recovery support, consult the recovery documentation to establish the necessary /backup/fetch webhook. Also, ensure the user has successfully completed the backup process.

Here's how to implement portal.recoverWallet:

import PortalSwift

struct CipherTextResult: Codable {
  var cipherText: String
}

class ViewController: UIViewController {
  public var portal: Portal?
  public var yourApiUrl: String = "https://YOUR_API_URL.com"
  
  @IBAction func handlePasswordRecover(_: UIButton!) {
    Task {
      do {
        guard let portal = self.portal else {
          throw PortalExampleAppError.portalNotInitialized()
        }

        // Obtain the user's password.
        guard let enteredPassword = await requestPassword(), !enteredPassword.isEmpty else {
          return
        }

        // Set the user's password.
        try portal.setPassword(enteredPassword)

        // Run password recover.
        try await recover(String(user.exchangeUserId), withBackupMethod: .Password)
      } catch {
        // Handle any errors during the recovery process.
      }
    }
  }
  
  public func recover(_ userId: String, withBackupMethod: BackupMethods) async throws -> void {
    guard let portal else {
      throw PortalExampleAppError.portalNotInitialized()
    }

    guard let config else {
      throw PortalExampleAppError.configurationNotSet()
    }

    // Obtain your API's URL for retrieving the encrypted user backup share.
    guard let url = URL(string: "\(yourApiUrl)/users/\(userId)/encrypted-user-backup-shares?backupMethod=\(withBackupMethod.rawValue)") else {
      throw URLError(.badURL)
    }

    // Retrieve the encrypted user backup share on your API.
    let yourApiResponse = try await requests.get(url)
    let decodedResponse = try decoder.decode(CipherTextResult.self, from: yourApiResponse)
    let encryptedUserBackupShare = decodedResponse.encryptedUserBackupShare

    // Run recover.
    try await portal.recoverWallet(withBackupMethod, withCipherText: encryptedUserBackupShare) { status in
      // (Optional) Create a progress indicator here in the progress callback.
    }
    
    // ✅ The user has now recovered with their password successfully!
  }
}

And that's it! You're now equipped to support multiple sessions across different devices for your users.

Last updated