connect method

Future<NwcConnection> connect(
  1. String uri, {
  2. bool doGetInfoMethod = false,
  3. dynamic onError(
    1. String?
    )?,
})

Connects to a given nostr+walletconnect:// uri, checking for 13194 event info, and optionally doing a get_info request (default false). It subscribes for notifications

Implementation

Future<NwcConnection> connect(String uri,
    {bool doGetInfoMethod = false, Function(String?)? onError}) async {
  var parsedUri = NostrWalletConnectUri.parseConnectionUri(uri);
  var relay = Uri.decodeFull(parsedUri.relay);
  var filter =
      Filter(kinds: [NwcKind.INFO.value], authors: [parsedUri.walletPubkey]);

  Completer<NwcConnection> completer = Completer();

  _requests
      .query(
          name: "nwc-info",
          explicitRelays: [relay],
          filters: [filter],
          timeout: 5,
          cacheRead: false,
          cacheWrite: false)
      .stream
      .listen((event) async {
    if (event.kind == NwcKind.INFO.value && event.content != "") {
      final connection = NwcConnection(parsedUri);

      connection.permissions = event.content.split(" ").toSet();

      if (connection.permissions.length == 1) {
        connection.permissions =
            connection.permissions.first.split(",").toSet();
      }

      List<String> versionTags = Nip01Event.getTags(event.tags, "v");
      if (versionTags.isNotEmpty) {
        connection.supportedVersions = versionTags.first.split(" ");
      }

      await _subscribeToNotificationsAndResponses(connection);

      if (doGetInfoMethod &&
          connection.permissions.contains(NwcMethod.GET_INFO.name)) {
        await getInfo(connection).then((info) {
          connection.info = info;
        });
      }
      Logger.log.i("NWC ${connection.uri} connected");
      _connections.add(connection);
      completer.complete(connection);
    }
  });
  return completer.future;
}