nostr_relay_management_api 1.0.1 copy "nostr_relay_management_api: ^1.0.1" to clipboard
nostr_relay_management_api: ^1.0.1 copied to clipboard

Support for Nostr NIP-86 Relay Management API for Dart/Flutter applications.

What is this ? #

This is a package that makes Nostr NIP-86 support and usage easier for Dart/Flutter apps, abstracting away Nip-98 Nostr Auth header...

Installation #

Add this to your package's pubspec.yaml file:

dependencies:
  # add this
  nostr_relay_management_api:

Then run flutter pub get or dart pub get in your terminal.

Usage #

Every method in this package is static and accessible via NostrRelayManagement:

import 'package:nostr_relay_management_api/nostr_relay_management_api.dart';

void main() async {

  final relayManagementService = NostrRelayManagement(
       // needed to make requests using NIP-98's HTTP authentication, if you have no ideon what this is, check https://github.com/nostr-protocol/nips/blob/master/98.md
      hexPrivateKey: "hexPrivateKey",

   // the relay that you want to use for NIP-86 requests
      url: "https://yourRelayThatSupportsNip86.com",
    );
}

supportedmethods #


  final  supportedmethods =
      await relayManagementService.methods.supportedmethods();

  print('supportedmethods: $supportedmethods');

banpubkey #

  final isBanned = await relayManagementService.methods.banpubkey(
    pubkey: "pubkeyToBan",
    reason: "Spammy behavior",
  );

  print("Ban pubkey result: $isBanned");

listbannedpubkeys #

  final isBanned = await relayManagementService.methods.banpubkey(
    pubkey: "pubkeyToBan",
    reason: "Spammy behavior",
  );

  print("Ban pubkey result: $isBanned");

allowpubkey #

  final isAllowed = await relayManagementService.methods.allowpubkey(
    pubkey: "pubkeyToAllow",
    reason: "Spammy behavior",

  );

  print("Allow pubkey result: $isAllowed");

listallowedpubkeys #

  final allowedPubkeys =
      await relayManagementService.methods.listallowedpubkeys();

  print("Allowed pubkeys: $allowedPubkeys");

listeventsneedingmoderation #

  final entries =
      await relayManagementService.methods.listeventsneedingmoderation();

  print(entries.map((e) {
    final eventId = e.id;
    final reason = e.reason;

    return 'Event ID: $eventId, Reason: $reason';
  }).toList());

allowevent #

  final isEventAllowed = await relayManagementService.methods.allowevent(
    eventId: "eventIdToAllow",
    reason: "This event is fine",
  );

  print("Allow event result: $isEventAllowed");

banevent #

    final isEventBanned = await relayManagementService.methods.banevent(
        eventId: "eventIdToBan",
        reason: "This event is inappropriate",
    );

    print("Ban event result: $isEventBanned");

listbannedevents #

  final bannedEvents =
      await relayManagementService.methods.listbannedevents();

  print(entries.map((e) {
    final eventId = e.id;
    final reason = e.reason;

    return 'Event ID: $eventId, Reason: $reason';
  }).toList());

changerelayname #

  final changed = await relayManagementService.methods.changerelayname(
    newName: "my new relay name",
  );

  print(changed);

changerelaydescription #

    final changed = await relayManagementService.methods.changerelaydescription(
        newDescription: "my new relay description",
    );

    print(changed);

changerelayicon #

    final changed = await relayManagementService.methods.changerelayicon(
        newIconUrl: "https://mydomain.com/myicon.png",
    );

    print(changed);

allowkind #

  final isKindAllowed = await relayManagementService.methods.allowkind(
    kind: 1,
  );

  print("Allow kind result: $isKindAllowed");

disallowkind #

  final isKindDisallowed = await relayManagementService.methods.disallowkind(
    kind: 1,
  );

  print("Disallow kind result: $isKindDisallowed");

listallowedkinds #

  final allowedKinds =
      await relayManagementService.methods.listallowedkinds();

  print("Allowed kinds: $allowedKinds");

blockip #

  final blocked = await relayManagementService.methods.blockip(
    ip: "104.0.25.3",
    reason: "maybe DoS attack",
  );

  print(blocked);

unblockip #

  final unblocked = await relayManagementService.methods.unblockip(
    ip: "104.0.25.3",
  );

  print(unblocked);

listblockedips #

  final blockedList = await relayManagementService.methods.listblockedips();

  print(blockedList.map((blockedIp) {
    final ip = blockedIp.ip;
    final reason = blockedIp.reason;

    return 'IP: $ip, Reason: $reason';
  }));

Does your relay contain custom methods ? #

  • The package makes it possible to adapt to custom methods that your relay might have, like for example a method called backupdatabaseInternally that basically does a manual backup for all resources that the relay operate such as events, files, stats..., to support it, we can use the customMethod method:
  RelayBackupDetails? custom = await relayManagementService.methods.customMethod(
    methodName: "backupdatabaseInternally",
    adapter: (result) {
      final details = result as List<dynamic>;
      final detailsIndo = details.firstOrNull as Map<String, dynamic>?;

      final backedUpEvents = detailsIndo?['backedUpEvents'] as int?;
      final backedUpFiles = detailsIndo?['backedUpFiles'] as int?;
      final backupLocation = detailsIndo?['backupLocation'] as String?;

      return RelayBackupDetails(
        backedUpEvents: backedUpEvents ?? 0,
        backedUpFiles: backedUpFiles ?? 0,
        backupLocation: backupLocation ?? 'unknown',
      );
    },
  );

The type ofcustomMethod is a:

  Future<T?> customMethod<T>({
    required String methodName,
    required T Function(Object? result) adapter,
    List<Object?>? params,
  })

which means that the response type is what you return in the adapter field

Contributing #

  • if any more methods are added to NIP-86 in the future, please consider contributing by opening a PR/issue to add them.

  • Also, if you find any bugs or have suggestions for improvements, feel free to open an issue on the GitHub repository.

License #

This project is licensed under the MIT License. See the LICENSE file for details.

1
likes
50
points
2
downloads

Publisher

unverified uploader

Weekly Downloads

Support for Nostr NIP-86 Relay Management API for Dart/Flutter applications.

Repository (GitHub)
View/report issues

Topics

#nostr #relay #management #nip-86 #client

Documentation

Documentation

License

MIT (license)

Dependencies

crypto, dart_nostr, dio

More

Packages that depend on nostr_relay_management_api