> ## 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-pakket voor het Nostr-protocol. Keybeheer, eventbewerkingen, pool-ondersteuning, NIP-05 / NIP-17 en integratie met Laravel Notifications.

## Overzicht

[revolution/laravel-nostr](https://github.com/invokable/laravel-nostr) is een pakket om het Nostr-protocol vanuit Laravel te gebruiken. Het biedt het genereren en converteren van keys, het ophalen en publiceren van events, pool-ondersteuning (meerdere relays), NIP-05-profielen, NIP-17 Private Direct Messages en integratie met Laravel Notifications.

<Info>
  Omdat de Nostr-specificatie nog volop in ontwikkeling is, wordt ook dit pakket continu doorontwikkeld. De notificatiefunctionaliteit is al goed bruikbaar in de praktijk.
</Info>

## Drivers

Dit pakket heeft twee drivers.

| Driver   | Beschrijving                                                                                                                                |
| -------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| `native` | Implementatie die volledig in PHP werkt. Gebruikt [nostr-php](https://github.com/nostrver-se/nostr-php). Tegenwoordig volstaat deze driver. |
| `node`   | Implementatie die afhankelijk is van een externe [WebAPI](https://github.com/kawax/nostr-vercel-api) (Node.js).                             |

<Tip>
  Het unieke aan de `native`-driver is de implementatie van `WebSocketHttpMixin`. Deze maakt via de HTTP-client van Laravel verbinding met een WebSocket en verbreekt de verbinding direct na het versturen en ontvangen van data. Je hoeft geen WebSocket-server draaiende te houden; het ontwerp is zo dat elke Laravel-gebruiker het kan gebruiken.
</Tip>

<Info>
  De `native`-driver ondersteunt NIP-04 niet.
</Info>

### Standaarddriver instellen

Stel dit in via `config/nostr.php` of `.env`.

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

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

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

Als je geen driver opgeeft, wordt de standaarddriver gebruikt.

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

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

Je kunt de driver ook expliciet opgeven.

```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();
```

## Installatie

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

  <Step title="Configuratiebestand publiceren">
    ```bash theme={null}
    php artisan vendor:publish --tag=nostr-config
    ```
  </Step>
</Steps>

## Keybeheer

### Keys genereren

```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...',
// ]
```

### Keys converteren

Converteren vanuit een nsec.

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

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

Converteren vanuit een secret key.

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

Converteren vanuit een npub (alleen publieke sleutel).

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

Converteren vanuit een public key.

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

## Events ophalen

### Meerdere events ophalen

```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' => '...'],
// ]
```

### Eén event ophalen

```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 publiceren

### Publiceren naar één relay

```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');
}
```

### Publiceren naar meerdere relays (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 is array<string, Response>
// ['wss://relay1' => $response, 'wss://relay2' => $response]

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

## Relayservers configureren

### Welke relayservers worden gebruikt

Als je alleen `Nostr::event()` gebruikt, wordt de eerste relay uit `config/nostr.php` gebruikt. Bij `Nostr::pool()` worden alle relays uit de configuratie gebruikt.

### Relays wijzigen tijdens runtime

```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-profielen

```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 wordt alleen ondersteund door de `native`-driver.
</Info>

### Een privébericht versturen

```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!'
    );
```

### Een privébericht ontsleutelen

```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

Met `NostrChannel` kun je vanuit Laravel Notifications berichten versturen naar Nostr.

### De 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 in content is voor weergave; de HashTag in tags is voor classificatie op protocolniveau
            content: 'hello #laravel',
            tags: [
                HashTag::make(t: 'laravel'),
            ],
        );
    }
}
```

### On-demand notificaties

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

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

### Integratie met het User-model

```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());
```

### Relayservers voor notificaties

Standaard worden alle relays uit `config/nostr.php` gebruikt. Door relays op te geven via `NostrRoute` kun je dit tijdens runtime wijzigen.

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

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

<Info>
  Zie de [GitHub-repository](https://github.com/invokable/laravel-nostr) voor de meest recente informatie.
</Info>


## Related topics

- [我的包](/zh-CN/packages/index.md)
- [Laravel Telescope](/nl/telescope.md)
- [Laravel Boost](/nl/boost.md)
- [Laravel Octane](/nl/octane.md)
- [Laravel Bluesky](/nl/packages/laravel-bluesky/index.md)
