nostr 2.0.0 copy "nostr: ^2.0.0" to clipboard
nostr: ^2.0.0 copied to clipboard

Pure-Dart Nostr protocol library. Events, signing, NIP-44 encryption, gift wrap, and 35+ NIPs. Transport-agnostic, Flutter Web compatible.

example/nostr_example.dart

import 'dart:io';
import 'package:nostr/nostr.dart';

void main() async {
  // Use the Keys class to manipulate secret/public keys and use handy methods encapsulated from dart-bip340
  final keys = Keys(
    "5ee1c8000ab28edd64d74a7d951ac2dd559814887b1b9e1ac7c5f89e96125c12",
  );
  assert(keys.public ==
      "981cc2078af05b62ee1f98cff325aac755bf5c5836a265c254447b5933c6223b");

  // or generate random keys
  final randomKeys = Keys.generate();
  print(randomKeys.secret);

  // Instantiate an event from raw fields. By default the constructor
  // validates id + signature and throws EventValidationException if
  // either is wrong. Use verify: false when reconstructing untrusted
  // third-party event copies.
  //
  // Here we sign a fresh event with the secret key so the test data is
  // genuinely valid rather than hardcoded.
  final signed = Event.from(
    kind: 1,
    tags: [],
    content: 'Ceci est une analyse du websocket',
    secretKey: keys.secret,
  );
  final oneEvent = Event(
    signed.id,
    signed.pubkey,
    signed.createdAt,
    signed.kind,
    signed.tags,
    signed.content,
    signed.sig,
  );
  assert(oneEvent.id == signed.id);

  // Compose a valid event step-by-step. `Event.unsigned(...)` builds the
  // canonical id from the payload; signing then promotes it to a full
  // event via `copyWith`.
  final unsigned = Event.unsigned(
    pubkey: "981cc2078af05b62ee1f98cff325aac755bf5c5836a265c254447b5933c6223b",
    kind: 1,
    content: "",
    createdAt: currentUnixTimestampSeconds(),
  );
  final signed2 = unsigned.copyWith(
    sig: unsigned.getSignature(
      "5ee1c8000ab28edd64d74a7d951ac2dd559814887b1b9e1ac7c5f89e96125c12",
    ),
    verify: true,
  );
  assert(signed2.isValid() == true);

  // Instantiate an event with a partial data and let the library sign the event with your secret key
  final Event anotherEvent = Event.from(
    kind: 1,
    tags: [],
    content: "vi veri universum vivus vici",
    secretKey:
        "5ee1c8000ab28edd64d74a7d951ac2dd559814887b1b9e1ac7c5f89e96125c12", // DO NOT REUSE THIS PRIVATE KEY
  );

  assert(anotherEvent.pubkey ==
      "981cc2078af05b62ee1f98cff325aac755bf5c5836a265c254447b5933c6223b");

  // Connecting to a nostr relay using websocket
  final WebSocket webSocket = await WebSocket.connect(
    'wss://nos.lol', // or any nostr relay
  );

  // Send an event to the WebSocket server
  webSocket.add(anotherEvent.serialize());

  // Listen for events from the WebSocket server
  await Future.delayed(const Duration(seconds: 1));
  webSocket.listen((event) {
    print('Received event: $event');
  });

  // Close the WebSocket connection
  await webSocket.close();
}
14
likes
160
points
2.36k
downloads

Documentation

API reference

Publisher

verified publisherethicnology.com

Weekly Downloads

Pure-Dart Nostr protocol library. Events, signing, NIP-44 encryption, gift wrap, and 35+ NIPs. Transport-agnostic, Flutter Web compatible.

Repository (GitHub)
View/report issues

Topics

#nostr #protocol #bitcoin #lightning #cryptography

License

LGPL-3.0 (license)

Dependencies

bech32, bip340, convert, elliptic, http, pointycastle

More

Packages that depend on nostr