Skip to content

Commit b24f4dd

Browse files
committed
NIP-19 work in progress
1 parent 7205986 commit b24f4dd

File tree

4 files changed

+189
-50
lines changed

4 files changed

+189
-50
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
require __DIR__ . '/../../vendor/autoload.php';
6+
7+
use swentel\nostr\Key\Key;
8+
use swentel\nostr\Nip19\Nip19Helper;
9+
10+
try {
11+
$nip19 = new Nip19Helper(); // Helper.
12+
$id = '43fb0422457c1fadec68c5ad18378abb2c626d6b787790973e888d0998f6ced4'; // This is an event hex id.
13+
//$id = 'fb0422457c1fadec68c5ad18378abb2c626d6b787790973e888d0998f6ce'; // Invalid ID.
14+
15+
// Encode it to a bech32 encoded note ID.
16+
$note = $nip19->encodeNote($id);
17+
// Expected result:
18+
// note1g0asggj90s06mmrgckk3sdu2hvkxymtt0pmep9e73zxsnx8kem2qulye77
19+
print $note . PHP_EOL;
20+
21+
// Encode a profile pubkey or npub, this already works.
22+
$key = new Key();
23+
$pubkey = '06639a386c9c1014217622ccbcf40908c4f1a0c33e23f8d6d68f4abf655f8f71';
24+
// Alternative: $npub = $key->convertPublicKeyToBech32($pubkey);
25+
$npub = $nip19->encodeNpub($pubkey);
26+
// Expected result:
27+
// npub1qe3e5wrvnsgpggtkytxteaqfprz0rgxr8c3l34kk3a9t7e2l3acslezefe
28+
print $npub . PHP_EOL;
29+
30+
// Using the more generic encode method
31+
$note1 = $nip19->encode($id, 'note');
32+
print $note1 . PHP_EOL;
33+
34+
// TODO:
35+
// Encode to nevent with TLV data
36+
37+
// Encode it bech32 encoded nevent ID,
38+
// $nevent = $nip19->encodeEvent($id);
39+
// Expected result:
40+
// nevent1qqsy87cyyfzhc8ada35vttgcx79tktrzd44hsausjulg3rgfnrmva4qey0p0j
41+
// print $nevent . PHP_EOL;
42+
43+
// Encode to nprofile with TLV data
44+
45+
// Encode to naddr with TLV data
46+
47+
// Decode a bech32 encoded entity to an event ID.
48+
$nevent = '';
49+
50+
} catch (Exception $e) {
51+
print $e->getMessage() . PHP_EOL;
52+
}

src/Nip19/Nip19.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/Nip19/Nip19Helper.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace swentel\nostr\Nip19;
6+
7+
use BitWasp\Bech32\Exception\Bech32Exception;
8+
use swentel\nostr\Key\Key;
9+
10+
use function BitWasp\Bech32\convertBits;
11+
use function BitWasp\Bech32\encode;
12+
13+
/**
14+
* NIP-19 bech32-encoded entities
15+
*
16+
* Example reference: https://github.com/nbd-wtf/go-nostr/blob/master/nip19/nip19.go
17+
*
18+
* https://github.com/Bit-Wasp/bech32/blob/master/src/bech32.php
19+
*/
20+
class Nip19Helper
21+
{
22+
/**
23+
* @var string $prefix
24+
*/
25+
protected $prefix;
26+
27+
public function __construct() {}
28+
29+
public function decode(string $bech32string)
30+
{
31+
$length = strlen($bech32string);
32+
if ($length > 90) {
33+
throw new \Exception('Bech32 string cannot exceed 90 characters in length');
34+
}
35+
if ($length < 8) {
36+
throw new \Exception('Bech32 string is too short');
37+
}
38+
39+
// switch ($prefix) {
40+
// case 'npub':
41+
// break;
42+
// case 'nsec':
43+
// break;
44+
// case 'note':
45+
// break;
46+
// default:
47+
// throw new \Exception('Unexpected value');
48+
// }
49+
}
50+
51+
public function encode(string $value, string $prefix): string
52+
{
53+
return $this->convertToBech32($value, $prefix);
54+
}
55+
56+
public function encodeNote(string $event_hex): string
57+
{
58+
return $this->convertToBech32($event_hex, 'note');
59+
}
60+
61+
public function encodeEvent(string $event_hex): string
62+
{
63+
$hexInBin = hex2bin($event_hex); // Convert hex formatted string to binary string.
64+
if (strlen($hexInBin) !== 32) {
65+
throw new \Exception(sprintf('This is an invalid ID: %s', $event_hex));
66+
}
67+
// todo process TLV
68+
return $this->convertToBech32($event_hex, 'nevent');
69+
}
70+
71+
public function encodeProfile(string $profile_hex): string
72+
{
73+
// todo
74+
return '';
75+
}
76+
77+
public function encodeAddr(string $event_hex): string
78+
{
79+
// todo
80+
return '';
81+
}
82+
83+
/**
84+
* @param string $pubkey
85+
* @return string
86+
*/
87+
public function encodeNpub(string $pubkey): string
88+
{
89+
$key = new Key();
90+
return $key->convertPublicKeyToBech32($pubkey);
91+
}
92+
93+
/**
94+
* @param string $seckey
95+
* @return string
96+
*/
97+
public function encodeNsec(string $seckey): string
98+
{
99+
$key = new Key();
100+
return $key->convertPrivateKeyToBech32($seckey);
101+
}
102+
103+
private function convertToBech32(string $key, string $prefix): string
104+
{
105+
$str = '';
106+
107+
$dec = [];
108+
$split = str_split($key, 2);
109+
foreach ($split as $item) {
110+
$dec[] = hexdec($item);
111+
}
112+
$bytes = convertBits($dec, count($dec), 8, 5);
113+
$str = encode($prefix, $bytes);
114+
115+
return $str;
116+
}
117+
118+
private function readTLVEntry($value) {}
119+
120+
private function writeTLVEntry($value, string $type) {}
121+
}

src/Nip19/TLVEnum.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace swentel\nostr\Nip19;
6+
7+
/**
8+
* Enum with TLV types.
9+
*/
10+
enum TLVEnum: int
11+
{
12+
case TLVDefault = 0;
13+
case TLVRelay = 1;
14+
case TLVAuthor = 2;
15+
case TLVKind = 3;
16+
}

0 commit comments

Comments
 (0)