-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnostrpostr.php
72 lines (59 loc) · 2.16 KB
/
nostrpostr.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
require_once(__DIR__ . '/vendor/autoload.php');
class NOSTRpostr {
protected $keys;
protected $signer;
protected $responses = [];
protected $errors = [];
function __construct($relays) {
$this->relays = $relays;
$this->keys = new swentel\nostr\Keys();
$this->signer = new swentel\nostr\Sign();
}
public function send($privateKeyBech32, $note) {
$privateKey = $this->keys->convertToHex($privateKeyBech32);
$publicKey = $this->keys->getPublicKey($privateKey);
$event = [
'pubkey' => $publicKey,
'created_at' => time(),
'kind' => 1,
'tags' => [],
'content' => $note,
];
$event = $this->signer->signEvent($event, $privateKey);
$message = $this->signer->generateEvent($event);
$result = FALSE;
foreach ($this->relays as $relay) {
try {
$client = new WebSocket\Client($relay);
$client->text($message);
$response = $client->receive();
// succeeded posting to at least 1 relay
if (preg_match('/^\["OK/', $response)) $result = TRUE;
$this->responses[] = [
'relay' => $relay,
'event' => $event,
'message' => $response
];
$client->close();
} catch (Exception $e) {
$this->errors[] = [
'relay' => $relay,
'event' => $event,
'message' => $e
];
}
}
return $result;
}
public function getErrors() {
$errors = $this->errors;
$this->errors = [];
return $errors;
}
public function getResponses() {
$responses = $this->responses;
$this->responses = [];
return $responses;
}
}