nip01 0.2.0+2 copy "nip01: ^0.2.0+2" to clipboard
nip01: ^0.2.0+2 copied to clipboard

This package contains the basic protocol flows for Nostr as described in the NIP-01.

example/nip01_example.dart

import 'package:dartstr_utils/dartstr_utils.dart';
import 'package:nip01/nip01.dart';

void main() async {
  final keyPair = KeyPair.generate();
  print('privateKey: ${keyPair.privateKey}');

  final pool = RelayManagerServiceImpl();
  pool.addRelays(['wss://relay.paywithflash.com']);

  final since = DateTime.now().millisecondsSinceEpoch ~/ 1000;

  final event = Event.unsigned(
    pubkey: keyPair.publicKey,
    createdAt: since,
    kind: EventKind.textNote.value,
    tags: [],
    content: "This is an event.",
  ).sign(keyPair);

  final isPublished = await pool.publishEvent(event);
  if (!isPublished) {
    throw Exception('Failed to publish event');
  }

  final setProfile = SetProfileMetadataUseCase(relayManagerService: pool);
  final isProfileSet = await setProfile.execute(
    userKeyPair: keyPair,
    metadata: Kind0Metadata(name: 'Alice'),
  );
  if (!isProfileSet) {
    throw Exception('Failed to set profile metadata');
  }

  // Subscribe to events on the relay
  final subscription = Subscription(
    id: SecretGenerator.secretHex(
      64, // SecretGenerator is part of the dartstr_utils package
    ),
    filters: [
      Filters(
        authors: [keyPair.publicKey],
        since: since,
      ),
    ],
  );
  final eventStream = await pool.subscribe(
    subscription,
    onEose: (events) {
      print('End of subscription events: $events');
    },
  );

  eventStream.listen((event) {
    print('Received event: $event');
  });
}
2
likes
140
points
37
downloads

Publisher

verified publisherkumuly.dev

Weekly Downloads

This package contains the basic protocol flows for Nostr as described in the NIP-01.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

async, bip340, collection, convert, crypto, dartstr_utils, freezed_annotation, json_annotation, synchronized, web_socket_channel

More

Packages that depend on nip01