nip01 0.0.1 nip01: ^0.0.1 copied to clipboard
This package contains the basic protocol flows for Nostr as described in the NIP-01.
Features #
This package contains the basic protocol flows for Nostr as described in the NIP-01. This includes the creation of key pairs, event creation, event publishing, event requests, and event listening among others.
Getting started #
Installation #
In your pubspec.yaml
file add:
dependencies:
nip01: ^0.0.1
Usage #
import 'package:nip01/nip01.dart';
import 'package:dartstr_utils/dartstr_utils.dart';
final keyPair = KeyPair.generate();
print('privateKey: ${keyPair.privateKey}');
final relayCommunication = RelayCommunicationImpl(
RelayConnectionProviderImpl(
'wss://relay01.nostr.org',
),
);
relayCommunication.init();
final partialEvent = Event(
pubkey:
'981cc2078af05b62ee1f98cff325aac755bf5c5836a265c254447b5933c6223b',
createdAt: 1672175320,
kind: EventKind.textNote.value,
tags: [],
content: "This is an event.",
);
final eventId = partialEvent.id;
final signedEvent = partialEvent.copyWith(
id: eventId,
sig: creatorKeyPair.sign(eventId),
);
final isPublished = await relayCommunication.publishEvent(signedEvent);
if (!isPublished) {
throw Exception('Failed to publish event');
}
// Listen to events
final eventsSubscription = relayCommunication.events.listen(
(event) {
// Handle event
},
);
// Subscribe to events on the relay
final String subscriptionId = SecretGenerator.secretHex(64); // SecretGenerator is part of the dartstr_utils package
relayCommunication.requestEvents(
subscriptionId,
[
Filters(
authors: keyPair.publicKey,
since: 1672175320,
)
],
);
Additional information #
This package is part of the Dartstr monorepo, which contains a set of modular and compatible Dart packages of different Nostr NIPS and utilities. Import just the packages of NIPS you need and keep your project lightweight. See the Dartstr monorepo for all available packages.