-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NIP-42 support for client to relay authentication within the library
- Loading branch information
Showing
9 changed files
with
228 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use swentel\nostr\Event\Event; | ||
use swentel\nostr\Filter\Filter; | ||
use swentel\nostr\Message\RequestMessage; | ||
use swentel\nostr\Relay\Relay; | ||
use swentel\nostr\RelayResponse\RelayResponseEvent; | ||
use swentel\nostr\Request\Request; | ||
use swentel\nostr\Subscription\Subscription; | ||
|
||
require __DIR__ . '/../../vendor/autoload.php'; | ||
|
||
try { | ||
$subscription = new Subscription(); | ||
$subscriptionId = $subscription->setId(); | ||
$filter1 = new Filter(); | ||
$filter1->setAuthors([ | ||
'npub1qe3e5wrvnsgpggtkytxteaqfprz0rgxr8c3l34kk3a9t7e2l3acslezefe', | ||
]); | ||
$filter1->setKinds([1]); | ||
$filter1->setLimit(3); | ||
$filters = [$filter1]; | ||
$requestMessage = new RequestMessage($subscriptionId, $filters); | ||
$relay = new Relay('wss://jingle.nostrver.se'); | ||
//$relay = new Relay('wss://hotrightnow.nostr1.com'); | ||
$request = new Request($relay, $requestMessage); | ||
$response = $request->send(); | ||
|
||
foreach ($response as $relay => $messages) { | ||
print 'Received ' . count($response[$relay]) . ' message(s) received from relay ' . $relay . PHP_EOL; | ||
foreach ($messages as $message) { | ||
print $message->type . ': ' . $message->message . PHP_EOL; | ||
if ($message instanceof RelayResponseEvent) { | ||
$rawEvent = $message->event; | ||
$event = new Event(); | ||
$event->setId($rawEvent->id); | ||
$event->setPublicKey($rawEvent->pubkey); | ||
$event->setCreatedAt($rawEvent->created_at); | ||
$event->setKind($rawEvent->kind); | ||
$event->setTags($rawEvent->tags); | ||
$event->setContent($rawEvent->content); | ||
$event->setSignature($rawEvent->sig); | ||
if ($event->verify() === true) { | ||
var_dump($event->getContent()); | ||
} | ||
} | ||
} | ||
} | ||
} catch (Exception $e) { | ||
print $e->getMessage() . PHP_EOL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace swentel\nostr\Message; | ||
|
||
use swentel\nostr\EventInterface; | ||
use swentel\nostr\MessageInterface; | ||
|
||
class AuthMessage implements MessageInterface | ||
{ | ||
/** | ||
* @var string $type | ||
*/ | ||
private string $type; | ||
|
||
/** | ||
* The event. | ||
* | ||
* @var EventInterface | ||
*/ | ||
protected EventInterface $event; | ||
|
||
public function __construct(EventInterface $event) | ||
{ | ||
$this->event = $event; | ||
$this->setType(MessageTypeEnum::AUTH); | ||
} | ||
|
||
/** | ||
* Set message type. | ||
* | ||
* @param MessageTypeEnum $type | ||
* @return void | ||
*/ | ||
public function setType(MessageTypeEnum $type): void | ||
{ | ||
$this->type = $type->value; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function generate(): string | ||
{ | ||
$event = json_encode($this->event->toArray(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); | ||
return '["' . $this->type . '", ' . $event . ']'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace swentel\nostr\Nip42; | ||
|
||
use swentel\nostr\Event\Event; | ||
|
||
/** | ||
* NIP-42: https://github.com/nostr-protocol/nips/blob/master/42.md | ||
* AuthEvent class for canonical authentication event. | ||
*/ | ||
class AuthEvent extends Event | ||
{ | ||
/** | ||
* Event kind for canonical authentication event sent to the relay. | ||
* | ||
* @var int | ||
*/ | ||
protected int $kind = 22242; | ||
|
||
/** | ||
* Base constructor for AuthEvent. | ||
*/ | ||
public function __construct($relayUri, $challenge) | ||
{ | ||
parent::__construct(); | ||
$this->setTags([ | ||
['relay', $relayUri], | ||
['challenge', $challenge], | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters