removeWalletAccount method

  1. @override
Future<void> removeWalletAccount(
  1. String chain,
  2. String accountAddress
)
override

Unlinks verified wallets on the server and refreshes the session. Unverified wallets are disconnected locally only.

Implementation

@override
Future<void> removeWalletAccount(String chain, String accountAddress) async {
  Future<void> instrumentationOperation() async {
    final operationScope = _state.flowScope.capture(replace: false);
    await operationScope.ready();
    final walletConnectProjectId = _state.projectSettings?.sdk?.walletConnect?.projectId;
    if (walletConnectProjectId == null) {
      throw DynamicStateException("This environment has no WalletConnect projectId — enable WalletConnect for it in the Dynamic dashboard");
    }
    operationScope.check();
    await _externalWalletProvider.initialize(walletConnectProjectId, _config.appName, _config.appLogoUrl, _config.nativeLink, _config.universalLink);
    operationScope.check();
    operationScope.check();
    await _externalWalletProvider.disconnect(accountAddress);
    operationScope.check();
    operationScope.commit(() {
        _externalWalletState.externalWalletAccounts = _externalWalletState.externalWalletAccounts?.map((account) => (account.address == accountAddress ? null : account)).where((v) => v != null).map((v) => v!).toList() ?? const [];
        _externalWalletState.externalWalletAccountsChanged.emit(_externalWalletState.externalWalletAccounts);
    });

    final verifiedCredentialId = firstOrNull(_state.user?.verifiedCredentials?.map((vc) => (((vc.chain ?? "") == (switch (chain) { "EVM" => "eip155", "SOL" => "solana", "BTC" => "bip122", _ => chain.toLowerCase() })) && ((vc.address ?? "") == accountAddress) ? vc : null)).where((v) => v != null).map((v) => v!).toList() ?? const [])?.id;
    if (verifiedCredentialId != null) {
      operationScope.check();
      final verify = await _api.post<VerifyResponse>(path: "/verify/unlink", body: <String, dynamic>{"walletId": verifiedCredentialId}, token: _state.token, sessionPublicKey: _state.sessionPublicKey, parse: VerifyResponse.fromJson);
      operationScope.check();

      final token = (verify.minifiedJwt ?? verify.jwt);
      if (token == null) {
        throw DynamicParseException("verify response contained no token");
      }
      final expiresAtMs = (verify.expiresAt ?? 0) * DynamicConstants.millisecondsPerSecond;

      operationScope.commit(() {
          _state.token = token;
          _state.tokenChanged.emit(_state.token);
      });
      operationScope.commit(() {
          _state.sessionExpiresAtMs = expiresAtMs;
          _state.sessionExpiresAtMsChanged.emit(_state.sessionExpiresAtMs);
      });
      operationScope.commit(() {
          _state.user = verify.user;
          _state.userChanged.emit(_state.user);
      });
      operationScope.check();
      await operationScope.persist(() => _secureStorage.setItem("dynamic_${_config.environmentId}_session", jsonEncode(StoredSession(token: token, sessionExpiration: expiresAtMs).toJson())));
      operationScope.check();
      _logger.log("info", "unlinked external wallet");
    }
  }
  return await _api.instrumentation.runAsync("externalWallet.removeWalletAccount", instrumentationOperation);
}