Skip to content

Commit

Permalink
NIP-19 work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastix committed Oct 25, 2024
1 parent 7205986 commit b24f4dd
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 50 deletions.
52 changes: 52 additions & 0 deletions src/Examples/bech32-encoded-entities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

require __DIR__ . '/../../vendor/autoload.php';

use swentel\nostr\Key\Key;
use swentel\nostr\Nip19\Nip19Helper;

try {
$nip19 = new Nip19Helper(); // Helper.
$id = '43fb0422457c1fadec68c5ad18378abb2c626d6b787790973e888d0998f6ced4'; // This is an event hex id.
//$id = 'fb0422457c1fadec68c5ad18378abb2c626d6b787790973e888d0998f6ce'; // Invalid ID.

// Encode it to a bech32 encoded note ID.
$note = $nip19->encodeNote($id);
// Expected result:
// note1g0asggj90s06mmrgckk3sdu2hvkxymtt0pmep9e73zxsnx8kem2qulye77
print $note . PHP_EOL;

// Encode a profile pubkey or npub, this already works.
$key = new Key();
$pubkey = '06639a386c9c1014217622ccbcf40908c4f1a0c33e23f8d6d68f4abf655f8f71';
// Alternative: $npub = $key->convertPublicKeyToBech32($pubkey);
$npub = $nip19->encodeNpub($pubkey);
// Expected result:
// npub1qe3e5wrvnsgpggtkytxteaqfprz0rgxr8c3l34kk3a9t7e2l3acslezefe
print $npub . PHP_EOL;

// Using the more generic encode method
$note1 = $nip19->encode($id, 'note');
print $note1 . PHP_EOL;

// TODO:
// Encode to nevent with TLV data

// Encode it bech32 encoded nevent ID,
// $nevent = $nip19->encodeEvent($id);
// Expected result:
// nevent1qqsy87cyyfzhc8ada35vttgcx79tktrzd44hsausjulg3rgfnrmva4qey0p0j
// print $nevent . PHP_EOL;

// Encode to nprofile with TLV data

// Encode to naddr with TLV data

// Decode a bech32 encoded entity to an event ID.
$nevent = '';

} catch (Exception $e) {
print $e->getMessage() . PHP_EOL;
}
50 changes: 0 additions & 50 deletions src/Nip19/Nip19.php

This file was deleted.

121 changes: 121 additions & 0 deletions src/Nip19/Nip19Helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

declare(strict_types=1);

namespace swentel\nostr\Nip19;

use BitWasp\Bech32\Exception\Bech32Exception;
use swentel\nostr\Key\Key;

use function BitWasp\Bech32\convertBits;
use function BitWasp\Bech32\encode;

/**
* NIP-19 bech32-encoded entities
*
* Example reference: https://github.com/nbd-wtf/go-nostr/blob/master/nip19/nip19.go
*
* https://github.com/Bit-Wasp/bech32/blob/master/src/bech32.php
*/
class Nip19Helper
{
/**
* @var string $prefix
*/
protected $prefix;

public function __construct() {}

public function decode(string $bech32string)
{
$length = strlen($bech32string);
if ($length > 90) {
throw new \Exception('Bech32 string cannot exceed 90 characters in length');
}
if ($length < 8) {
throw new \Exception('Bech32 string is too short');
}

// switch ($prefix) {
// case 'npub':
// break;
// case 'nsec':
// break;
// case 'note':
// break;
// default:
// throw new \Exception('Unexpected value');
// }
}

public function encode(string $value, string $prefix): string
{
return $this->convertToBech32($value, $prefix);
}

public function encodeNote(string $event_hex): string
{
return $this->convertToBech32($event_hex, 'note');
}

public function encodeEvent(string $event_hex): string
{
$hexInBin = hex2bin($event_hex); // Convert hex formatted string to binary string.
if (strlen($hexInBin) !== 32) {
throw new \Exception(sprintf('This is an invalid ID: %s', $event_hex));
}
// todo process TLV
return $this->convertToBech32($event_hex, 'nevent');
}

public function encodeProfile(string $profile_hex): string
{
// todo
return '';
}

public function encodeAddr(string $event_hex): string
{
// todo
return '';
}

/**
* @param string $pubkey
* @return string
*/
public function encodeNpub(string $pubkey): string
{
$key = new Key();
return $key->convertPublicKeyToBech32($pubkey);
}

/**
* @param string $seckey
* @return string
*/
public function encodeNsec(string $seckey): string
{
$key = new Key();
return $key->convertPrivateKeyToBech32($seckey);
}

private function convertToBech32(string $key, string $prefix): string
{
$str = '';

$dec = [];
$split = str_split($key, 2);
foreach ($split as $item) {
$dec[] = hexdec($item);
}
$bytes = convertBits($dec, count($dec), 8, 5);
$str = encode($prefix, $bytes);

return $str;
}

private function readTLVEntry($value) {}

private function writeTLVEntry($value, string $type) {}
}
16 changes: 16 additions & 0 deletions src/Nip19/TLVEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace swentel\nostr\Nip19;

/**
* Enum with TLV types.
*/
enum TLVEnum: int
{
case TLVDefault = 0;
case TLVRelay = 1;
case TLVAuthor = 2;
case TLVKind = 3;
}

0 comments on commit b24f4dd

Please sign in to comment.