backupKeySharesToGoogleDrive method

  1. @override
Future<void> backupKeySharesToGoogleDrive(
  1. String chain,
  2. String accountAddress,
  3. String googleDriveAccessToken,
  4. String? password,
)
override

Backs up the client key share to Google Drive with a host-obtained OAuth token. Password is required only for password-protected wallets; native iCloud backup is not implemented.

Implementation

@override
Future<void> backupKeySharesToGoogleDrive(String chain, String accountAddress, String googleDriveAccessToken, String? password) async {
  Future<void> instrumentationOperation() async {
    final operationScope = _state.flowScope.capture(replace: false);
    await operationScope.ready();
    if (!_state.initialized) {
      throw DynamicStateException("call initialize() before using the client");
    }
    final authToken = _state.token;
    if (authToken == null) {
      throw DynamicStateException("not authenticated — sign in first");
    }

    final waasSessionId = _state.user?.sessionId;
    if (waasSessionId == null) {
      throw DynamicStateException("no sessionId — sign in (or call refreshUser) first");
    }
    operationScope.check();
    final sessionSignature = await _sessionSigner.sign(waasSessionId);
    operationScope.check();
    operationScope.check();
    final nonceResponse = await _api.get<NonceResponse>(path: "/nonce", token: null, sessionPublicKey: _state.sessionPublicKey, parse: NonceResponse.fromJson);
    operationScope.check();
    final nonce = nonceResponse.nonce;
    operationScope.check();
    final nonceSignature = await _sessionSigner.sign(nonce);
    operationScope.check();
    final signedSessionId = "$sessionSignature/$nonceSignature/$nonce";

    if ((_waasState.waasBoundSessionId ?? waasSessionId) != waasSessionId) {
      operationScope.check();
      await _waasEngine.destroy();
      operationScope.check();
    }
    operationScope.check();
    await _waasEngine.initialize(_config.environmentId, (chain == "SOL" ? "SVM" : chain), authToken, _config.apiBaseUrl, _config.universalLink);
    operationScope.check();
    operationScope.commit(() {
        _waasState.waasBoundSessionId = waasSessionId;
        _waasState.waasBoundSessionIdChanged.emit(_waasState.waasBoundSessionId);
    });
    operationScope.check();
    await _waasEngine.backupKeySharesToGoogleDrive((chain == "SOL" ? "SVM" : chain), accountAddress, googleDriveAccessToken, authToken, signedSessionId, _state.sessionPublicKey, password);
    operationScope.check();

    _logger.log("info", "waas wallet key shares backed up to google drive");
  }
  return await _api.instrumentation.runAsync("waas.backupKeySharesToGoogleDrive", instrumentationOperation);
}