openConnectionAs method

Future<RelayConnectivity?> openConnectionAs(
  1. String url,
  2. Account account, {
  3. ConnectionSource connectionSource = ConnectionSource.explicit,
})

Opens the connection towards url bound to account. The binding says which identity this socket may ever assume, not that it is already authenticated: relays are free to send their challenge whenever they want, and some only send it once a request needs it.

Implementation

Future<RelayConnectivity?> openConnectionAs(
  String url,
  Account account, {
  ConnectionSource connectionSource = ConnectionSource.explicit,
}) async {
  if (!account.signer.canSign()) {
    Logger.log.w(() => "Cannot bind a connection to ${account.pubkey}");
    return null;
  }
  final key = RelayConnectionKey.authenticated(url, account.pubkey);

  if (isConnectionOpen(key)) {
    return globalState.relays[key];
  }

  final connected = await connectRelay(
    dirtyUrl: url,
    connectionSource: connectionSource,
    authPubkey: account.pubkey,
  );
  final connectivity = globalState.relays[key];
  if (!connected.first || connectivity == null) {
    Logger.log.w(() => "Could not open $key: ${connected.second}");
    return null;
  }
  return connectivity;
}