Skip to content

Commit

Permalink
Merge pull request #73 from nostrver-se/dev
Browse files Browse the repository at this point in the history
Support for PHP 8.4
  • Loading branch information
Sebastix authored Nov 24, 2024
2 parents 5642173 + 115784b commit b07eeda
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: ["8.1", "8.2", "8.3"]
php-version: ["8.1", "8.2", "8.3", "8.4"]
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@
"chat": "https://t.me/nostr_php"
},
"require": {
"php": ">=8.1 <8.4",
"php": ">=8.1 <8.5",
"ext-gmp": "*",
"ext-xml": "*",
"lib-gmp": ">=6.2.1",
"bitwasp/bech32": "^0.0.1",
"phrity/websocket": "^3.0",
"simplito/elliptic-php": "^1.0",
Expand Down
3 changes: 1 addition & 2 deletions src/Examples/request-events-filtered-with-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

try {
$subscription = new Subscription();
$subscriptionId = $subscription->setId();

$filter1 = new Filter();
$filter1->setKinds([1]);
Expand Down Expand Up @@ -45,7 +44,7 @@
* If you would like to use || (OR) conditions, you should use multiple filters.
*/
$filters = [$filter1];
$requestMessage = new RequestMessage($subscriptionId, $filters);
$requestMessage = new RequestMessage($subscription->getId(), $filters);
$relay = new Relay('wss://relay.nostr.band');
$request = new Request($relay, $requestMessage);
$response = $request->send();
Expand Down
3 changes: 1 addition & 2 deletions src/Examples/request-events-from-multiple-relays.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

try {
$subscription = new Subscription();
$subscriptionId = $subscription->setId();

$filter1 = new Filter();
$filter1->setAuthors(
Expand All @@ -25,7 +24,7 @@
$filter1->setKinds([1]);
$filter1->setLimit(100);
$filters = [$filter1];
$requestMessage = new RequestMessage($subscriptionId, $filters);
$requestMessage = new RequestMessage($subscription->getId(), $filters);
$relays = [
new Relay('wss://nostr.sebastix.dev'),
new Relay('wss://relay.damus.io'),
Expand Down
3 changes: 1 addition & 2 deletions src/Examples/request-events-with-auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@

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);
$requestMessage = new RequestMessage($subscription->getId(), $filters);
$relay = new Relay('wss://jingle.nostrver.se');
//$relay = new Relay('wss://hotrightnow.nostr1.com');
$request = new Request($relay, $requestMessage);
Expand Down
3 changes: 1 addition & 2 deletions src/Examples/request-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@

try {
$subscription = new Subscription();
$subscriptionId = $subscription->setId();

$filter1 = new Filter();
$filter1->setKinds([1]);
$filter1->setLimit(25);
$filters = [$filter1];
$requestMessage = new RequestMessage($subscriptionId, $filters);
$requestMessage = new RequestMessage($subscription->getId(), $filters);
$relay = new Relay('wss://relay.nostr.band');
$request = new Request($relay, $requestMessage);
$response = $request->send();
Expand Down
7 changes: 3 additions & 4 deletions src/Filter/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
use swentel\nostr\FilterInterface;
use swentel\nostr\Key\Key;

use function PHPUnit\Framework\throwException;

#[\AllowDynamicProperties]
class Filter implements FilterInterface
{
/**
Expand Down Expand Up @@ -46,7 +45,7 @@ class Filter implements FilterInterface
*/
public int $since;

/**
/**t
* An integer unix timestamp in seconds, events must be older than this to pass
*/
public int $until;
Expand Down Expand Up @@ -160,7 +159,7 @@ private function validateTagName($tag): void
{
// Check if tag starts with #.
if (!str_starts_with($tag, '#')) {
throw new \RuntimeException('All tags must start with #');
throw new \RuntimeException('All tags on a filter must start with #');
}
// Check if tag has valid value.
$pattern = '/^#[a-z_-]+$/i';
Expand Down
12 changes: 3 additions & 9 deletions src/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,12 @@ private function getResponseFromRelay(Relay $relay): array | RelayResponse
$client->disconnect();
throw new \Exception('No challenge set in $_SESSION');
}
$aa = new AuthEvent($relay->getUrl(), $_SESSION['challenge']);
$authEvent = new Event();
$authEvent->setKind(22242);
$authEvent->setTags([
['relay', $relay->getUrl()],
['challenge', $_SESSION['challenge']],
]);
$authEvent = new AuthEvent($relay->getUrl(), $_SESSION['challenge']);
$sec = '0000000000000000000000000000000000000000000000000000000000000001';
// todo: use client defined secret key here instead of this default one
$signer = new Sign();
$signer->signEvent($aa, $sec);
$authMessage = new AuthMessage($aa);
$signer->signEvent($authEvent, $sec);
$authMessage = new AuthMessage($authEvent);
$initialMessage = $this->payload;
$this->payload = $authMessage->generate();
$client->text($this->payload);
Expand Down
30 changes: 30 additions & 0 deletions src/Subscription/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,41 @@

class Subscription implements SubscriptionInterface
{
/**
* String of max length 64 chars. It represents a subscription per WebSocket connection.
*
* @var string
*/
private string $id;

/**
*
*/
public function __construct()
{
$this->id = $this->setId();
}

/**
* @param int $length
* @return string
*/
public function setId($length = 64): string
{
if ($length > 64) {
throw new \RuntimeException('Subscription ID is longer than the maximum length of 64 chars.');
}
// String of all alphanumeric character
$str_result = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
// Shuffle the $str_result and returns substring of specified length
return substr(str_shuffle($str_result), 0, $length);
}

/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
}

0 comments on commit b07eeda

Please sign in to comment.