> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# Laravel Nostr

> Laravel-Paket für das Nostr-Protokoll. Key-Verwaltung, Event-Operationen, Pool-Unterstützung, NIP-05 / NIP-17 und Laravel-Notifications-Integration.

## Übersicht

[revolution/laravel-nostr](https://github.com/invokable/laravel-nostr) ist ein Paket, mit dem Sie das Nostr-Protokoll aus Laravel heraus nutzen können. Es bietet Key-Generierung und -Umwandlung, Abrufen und Veröffentlichen von Events, Pool-Unterstützung (mehrere Relays), NIP-05-Profile, NIP-17 Private Direct Messages sowie eine Integration mit Laravel Notifications.

<Info>
  Da sich die Nostr-Spezifikation noch weiterentwickelt, wird auch dieses Paket kontinuierlich weiterentwickelt. Die Benachrichtigungsfunktion ist bereits produktiv nutzbar.
</Info>

## Driver

Dieses Paket verfügt über zwei Driver.

| Driver   | Beschreibung                                                                                                        |
| -------- | ------------------------------------------------------------------------------------------------------------------- |
| `native` | Reine PHP-Implementierung. Verwendet [nostr-php](https://github.com/nostrver-se/nostr-php). Reicht derzeit aus.     |
| `node`   | Implementierung, die auf eine externe [WebAPI](https://github.com/kawax/nostr-vercel-api) (Node.js) angewiesen ist. |

<Tip>
  Das Besondere am `native`-Driver ist die Implementierung von `WebSocketHttpMixin`. Er stellt über den Laravel-HTTP-Client eine WebSocket-Verbindung her und trennt sie sofort nach dem Senden und Empfangen von Daten wieder. Ein dauerhaft laufender WebSocket-Server ist nicht erforderlich – das Design ist so ausgelegt, dass es jeder Laravel-Nutzer verwenden kann.
</Tip>

<Info>
  Der `native`-Driver unterstützt NIP-04 nicht.
</Info>

### Standard-Driver konfigurieren

Konfigurieren Sie ihn in `config/nostr.php` oder `.env`.

```php theme={null}
// config/nostr.php

'driver' => env('NOSTR_DRIVER', 'node'),
```

```dotenv theme={null}
NOSTR_DRIVER=native
```

Wenn Sie keinen Driver angeben, wird der Standard-Driver verwendet.

```php theme={null}
use Revolution\Nostr\Facades\Nostr;

Nostr::event()->list();
```

Sie können den Driver auch explizit angeben.

```php theme={null}
use Revolution\Nostr\Facades\Nostr;

Nostr::driver('node')->event()->list();
Nostr::node()->event()->list();

Nostr::driver('native')->event()->list();
Nostr::native()->event()->list();
```

## Installation

<Steps>
  <Step title="Paket installieren">
    ```bash theme={null}
    composer require revolution/laravel-nostr
    ```
  </Step>

  <Step title="Konfigurationsdatei veröffentlichen">
    ```bash theme={null}
    php artisan vendor:publish --tag=nostr-config
    ```
  </Step>
</Steps>

## Key-Verwaltung

### Key erzeugen

```php theme={null}
use Revolution\Nostr\Facades\Nostr;
use Illuminate\Http\Client\Response;

/** @var Response $response */
$response = Nostr::key()->generate();
$keys = $response->json();
// [
//     'sk'   => 'sk...',
//     'nsec' => 'nsec...',
//     'pk'   => 'pk...',
//     'npub' => 'npub...',
// ]
```

### Key konvertieren

Aus nsec konvertieren.

```php theme={null}
use Revolution\Nostr\Facades\Nostr;

$response = Nostr::key()->fromNsec(nsec: 'nsec');
$keys = $response->json();
// ['sk' => '...', 'nsec' => '...', 'pk' => '...', 'npub' => '...']
```

Aus Secret Key konvertieren.

```php theme={null}
$response = Nostr::key()->fromSecretKey(sk: 'sk');
```

Aus npub konvertieren (nur öffentlicher Schlüssel).

```php theme={null}
$response = Nostr::key()->fromNpub(npub: 'npub');
$keys = $response->json();
// ['pk' => '...', 'npub' => '...']
```

Aus Public Key konvertieren.

```php theme={null}
$response = Nostr::key()->fromPublicKey(pk: 'pk');
```

## Events abrufen

### Mehrere Events abrufen

```php theme={null}
use Illuminate\Http\Client\Response;
use Revolution\Nostr\Facades\Nostr;
use Revolution\Nostr\Filter;
use Revolution\Nostr\Kind;

$filter = Filter::make(
    authors: ['my pk'],
    kinds: [Kind::Text],
    limit: 10,
);

/** @var Response $response */
$response = Nostr::event()->list(filter: $filter);
$events = $response->json('events');
// [
//     ['id' => '...1', 'kind' => 1, 'content' => '...'],
//     ['id' => '...2', 'kind' => 1, 'content' => '...'],
// ]
```

### Ein einzelnes Event abrufen

```php theme={null}
use Revolution\Nostr\Facades\Nostr;
use Revolution\Nostr\Filter;
use Revolution\Nostr\Kind;

$filter = Filter::make(
    authors: ['my pk'],
    kinds: [Kind::Metadata],
);

$response = Nostr::event()->get(filter: $filter);
$event = $response->json('event');
// ['id' => '...', 'kind' => 0, 'content' => '{name: ""}']
```

## Events veröffentlichen

### An ein einzelnes Relay veröffentlichen

```php theme={null}
use Revolution\Nostr\Facades\Nostr;
use Revolution\Nostr\Event;
use Revolution\Nostr\Kind;

$event = Event::make(
    kind: Kind::Text,
    content: 'hello',
    created_at: now()->timestamp,
    tags: [],
);

$sk = 'my sk';

$response = Nostr::event()->publish(event: $event, sk: $sk);

if ($response->successful()) {
    $event = $response->json('event');
}
```

### An mehrere Relays veröffentlichen (Pool)

```php theme={null}
use Revolution\Nostr\Facades\Nostr;
use Revolution\Nostr\Event;
use Revolution\Nostr\Kind;

$event = Event::make(
    kind: Kind::Text,
    content: 'test',
    created_at: now()->timestamp,
    tags: [],
);

$responses = Nostr::pool()->publish(event: $event, sk: 'my sk');
// $responses ist array<string, Response>
// ['wss://relay1' => $response, 'wss://relay2' => $response]

foreach ($responses as $relay => $response) {
    if ($response->failed()) {
        dump($relay . ' : ' . $response->body());
    }
}
```

## Relay-Server konfigurieren

### Verwendete Relay-Server

Wenn Sie nur `Nostr::event()` verwenden, wird das erste Relay aus `config/nostr.php` verwendet. Bei `Nostr::pool()` werden alle in der Konfiguration angegebenen Relays einbezogen.

### Relays zur Laufzeit ändern

```php theme={null}
use Revolution\Nostr\Facades\Nostr;

$response = Nostr::event()->withRelay('wss://')->...;
```

```php theme={null}
use Revolution\Nostr\Facades\Nostr;

$response = Nostr::pool()->withRelays(['wss://', 'wss://'])->...;
```

## NIP-05-Profile

```php theme={null}
use Revolution\Nostr\Facades\Nostr;

$profile = Nostr::nip05()->profile('user@localhost');
// [
//     'user'   => 'user@localhost',
//     'pubkey' => 'pk',
//     'relays' => [],
// ]
```

## NIP-17 Private Direct Messages

<Info>
  NIP-17 wird nur vom `native`-Driver unterstützt.
</Info>

### Private Nachricht senden

```php theme={null}
use Revolution\Nostr\Facades\Nostr;

$response = Nostr::driver('native')
    ->nip17()
    ->sendDirectMessage(
        sk: 'sender-secret-key',
        pk: 'receiver-public-key',
        message: 'Hello, this is a private message!'
    );
```

### Private Nachricht entschlüsseln

```php theme={null}
use Revolution\Nostr\Facades\Nostr;

$response = Nostr::driver('native')
    ->nip17()
    ->decryptDirectMessage(
        giftWrap: $receivedGiftWrap,
        sk: 'receiver-secret-key'
    );

$decryptedMessage = $response->json();
```

## Laravel Notifications

Mit dem `NostrChannel` können Sie Nachrichten aus Laravel Notifications an Nostr senden.

### Notification-Klasse

```php theme={null}
use Illuminate\Notifications\Notification;
use Revolution\Nostr\Notifications\NostrChannel;
use Revolution\Nostr\Notifications\NostrMessage;
use Revolution\Nostr\Tags\HashTag;

class TestNotification extends Notification
{
    public function via(object $notifiable): array
    {
        return [
            'mail',
            NostrChannel::class,
        ];
    }

    public function toNostr(object $notifiable): NostrMessage
    {
        return new NostrMessage(
            // #laravel im Content dient der Anzeige, HashTag in tags der Klassifizierung auf Protokollebene.
            content: 'hello #laravel',
            tags: [
                HashTag::make(t: 'laravel'),
            ],
        );
    }
}
```

### On-Demand-Benachrichtigungen

```php theme={null}
use Illuminate\Support\Facades\Notification;
use Revolution\Nostr\Notifications\NostrRoute;

Notification::route('nostr', NostrRoute::to(sk: 'sk'))
    ->notify(new TestNotification());
```

### Integration in das User-Modell

```php theme={null}
use Illuminate\Notifications\Notifiable;
use Revolution\Nostr\Notifications\NostrRoute;

class User
{
    use Notifiable;

    public function routeNotificationForNostr($notification): NostrRoute
    {
        return NostrRoute::to(sk: $this->sk, relays: ['wss://']);
    }
}
```

```php theme={null}
$user->notify(new TestNotification());
```

### Für Benachrichtigungen verwendete Relay-Server

Standardmäßig werden alle in `config/nostr.php` angegebenen Relays verwendet. Wenn Sie in `NostrRoute` Relays angeben, können Sie sie zur Laufzeit ändern.

```php theme={null}
use Revolution\Nostr\Notifications\NostrRoute;

return NostrRoute::to(sk: 'sk', relays: ['wss://', 'wss://']);
```

<Info>
  Die aktuellsten Informationen finden Sie im [GitHub-Repository](https://github.com/invokable/laravel-nostr).
</Info>


## Related topics

- [Laravel-Paketentwicklung](/de/advanced/package-development.md)
- [Laravel Telescope](/de/telescope.md)
- [Laravel Bluesky](/de/packages/laravel-bluesky/index.md)
- [Laravel Boost](/de/boost.md)
- [Laravel Octane](/de/octane.md)
