nostr_mail 2.0.1 copy "nostr_mail: ^2.0.1" to clipboard
nostr_mail: ^2.0.1 copied to clipboard

A Dart SDK for sending and receiving emails over the Nostr protocol using NIP-59 gift-wrapped messages.

example/nostr_mail_example.dart

import 'package:blossom_cache/blossom_cache.dart';
import 'package:enough_mail_plus/enough_mail.dart';
import 'package:idb_shim/idb_client_memory.dart';
import 'package:ndk/ndk.dart';
import 'package:ndk/shared/nips/nip01/bip340.dart';
import 'package:nostr_mail/nostr_mail.dart';
import 'package:sembast/sembast_memory.dart';

void main() async {
  // Initialize Sembast database (use sembast_io for file-based storage)
  final db = await databaseFactoryMemory.openDatabase('nostr_mail.db');

  // Initialize NDK with your keys
  final ndk = Ndk(
    NdkConfig(
      bootstrapRelays: ['wss://relay.damus.io', 'wss://nos.lol'],
      eventVerifier: Bip340EventVerifier(),
      cache: MemCacheManager(),
    ),
  );

  final keyPair = Bip340.generatePrivateKey();
  ndk.accounts.loginPrivateKey(
    pubkey: keyPair.publicKey,
    privkey: keyPair.privateKey!,
  );

  // Local store for large-email blobs while they are uploading to Blossom.
  // On web use `idbFactoryBrowser`; on native use `idbFactorySembastIo` so
  // pending uploads survive restarts.
  final BlossomCache blossomCache = await IdbBlossomCache.open(
    factory: newIdbFactoryMemory(),
  );

  // Create the mail client (runs any pending schema migration first)
  final client = await NostrMailClient.create(
    ndk: ndk,
    db: db,
    blossomCache: blossomCache,
  );

  // Sync emails from relays
  await client.sync();

  // Get cached emails
  final emails = await client.getEmails(limit: 10);
  for (final email in emails) {
    print('From: ${email.mime.fromEmail}');
    print('Subject: ${email.mime.decodeSubject()}');
    print('---');
  }

  // Watch for all mail events in real-time
  client.watch().listen((event) {
    switch (event) {
      case EmailReceived():
        print(
          'New email from ${event.email.mime.fromEmail}: ${event.email.mime.decodeSubject()}',
        );
      case EmailDeleted():
        print('Email deleted: ${event.emailId}');
      case LabelAdded():
        print('Label added: ${event.label} to ${event.emailId}');
      case LabelRemoved():
        print('Label removed: ${event.label} from ${event.emailId}');
    }
  });

  // Or use convenience streams
  client.onEmail.listen((email) {
    print('New email: ${email.mime.decodeSubject()}');
  });

  client.onTrash.listen((event) {
    print('Trash event: $event');
  });

  // Send to a Nostr user (npub)
  await client.send(
    to: [MailAddress(null, 'npub1abc123...')],
    subject: 'Hello from Nostr!',
    body: 'This is a test email sent via Nostr.',
  );

  // Send to a NIP-05 identifier
  await client.send(
    to: [MailAddress(null, 'alice@nostr.directory')],
    subject: 'Hello Alice!',
    body: 'Sending via your NIP-05 address.',
  );

  // Send to a legacy email (routed via bridge)
  await client.send(
    to: [MailAddress(null, 'bob@gmail.com')],
    subject: 'Hello from Nostr!',
    body: 'This email was sent via the Nostr bridge.',
  );
}
2
likes
150
points
904
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A Dart SDK for sending and receiving emails over the Nostr protocol using NIP-59 gift-wrapped messages.

Repository (GitHub)
View/report issues

Topics

#nostr #email #encryption #messaging #decentralized

License

MIT (license)

Dependencies

blossom_cache, blossom_upload_queue_shim_for_ndk, broadcast_queue_shim_for_ndk, cryptography, enough_mail_plus, html, http, ndk, rxdart, sembast

More

Packages that depend on nostr_mail