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 29, 2024
1 parent b24f4dd commit 2c0315d
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 19 deletions.
22 changes: 19 additions & 3 deletions src/Examples/bech32-encoded-entities.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,35 @@
// TODO:
// Encode to nevent with TLV data

// Encode it bech32 encoded nevent ID,
// $nevent = $nip19->encodeEvent($id);
$nevent = $nip19->encodeEvent($id, ['wss://nostr.sebastix.dev'], $pubkey, 1);
// Expected result:
// nevent1qqsy87cyyfzhc8ada35vttgcx79tktrzd44hsausjulg3rgfnrmva4qey0p0j
// print $nevent . PHP_EOL;

// Encode to nprofile with TLV data
// Encode to pubkey profile with TLV data
$pubkey = '3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d';
$relays = ['wss://r.x.com', 'wss://djbas.sadkb.com'];
$nprofile = $nip19->encodeProfile($pubkey, $relays);
// Expected result with TLV items:
// - pubkey: 3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d
// - relay: wss://r.x.com
// - relay: wss://djbas.sadkb.com

// TODO
// Encode to naddr with TLV data

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

// TODO
// Decode to event with TLV data
$profile_id = 'nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p';
// Expected result with TLV items:
// - pubkey: 3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d
// - relay: wss://r.x.com
// - relay: wss://djbas.sadkb.com

} catch (Exception $e) {
print $e->getMessage() . PHP_EOL;
}
1 change: 0 additions & 1 deletion src/Message/RequestMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace swentel\nostr\Message;

use swentel\nostr\MessageInterface;
use swentel\nostr\Filter;

class RequestMessage implements MessageInterface
{
Expand Down
94 changes: 83 additions & 11 deletions src/Nip19/Nip19Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

use function BitWasp\Bech32\convertBits;
use function BitWasp\Bech32\encode;
Expand All @@ -24,7 +25,9 @@ class Nip19Helper
*/
protected $prefix;

public function __construct() {}
public function __construct()
{
}

public function decode(string $bech32string)
{
Expand Down Expand Up @@ -58,23 +61,61 @@ public function encodeNote(string $event_hex): string
return $this->convertToBech32($event_hex, 'note');
}

public function encodeEvent(string $event_hex): string
/**
* @param string $event_hex
* @param array $relays
* @param string $author
* @param int $kind
* @return string
* @throws \Exception
*/
public function encodeEvent(string $event_hex, array $relays = [], string $author = '', int $kind = null): 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));
$data = '';
$prefix = 'nevent';
$event_hex_in_bin = hex2bin($event_hex); // Convert hex formatted pubkey string to binary string.
if (strlen($event_hex_in_bin) !== 32) {
throw new \Exception(sprintf('This is an invalid event ID: %s', $event_hex));
}
// TODO: process TLV entries
$tlvEntry = $this->writeTLVEntry(TLVEnum::Special, $event_hex_in_bin);
// Optional
if (!(empty($relays))) {
foreach ($relays as $relay) {
// Encode as ascii.
//$relay = implode('', unpack('C*', $relay));
// Alternative which requires the icon PHP extension installed on the host machine.
// $relay = iconv('UTF-8', 'ASCII', $relay);
// decode ascii relay string
$tlvEntry .= $this->writeTLVEntry(TLVEnum::Relay, urlencode($relay));
}
}
// Optional
if (!(empty($author))) {
if (strlen(hex2bin($author)) !== 32) {
throw new \Exception(sprintf('This is an invalid author ID: %s', $event_hex));
}
// Convert hex formatted pubkey to 32-bit binary value.
$tlvEntry .= $this->writeTLVEntry(TLVEnum::Author, hex2bin($author));
}
// todo process TLV
return $this->convertToBech32($event_hex, 'nevent');
// Optional
if ($kind !== null) {
// Convert kint int to unsigned integer, big-endian.
$v = pack('N', $kind);
$tlvEntry .= $this->writeTLVEntry(TLVEnum::Kind, $v);
}
$data = $tlvEntry;

return $this->encodeBech32($data, $prefix);
}

public function encodeProfile(string $profile_hex): string
public function encodeProfile(string $pubkey, array $relays = []): string
{
// todo
return '';
}

public function encodeAddr(string $event_hex): string
public function encodeAddr(string $event_hex, int $kind, string $DTag, array $relays = []): string
{
// todo
return '';
Expand All @@ -100,6 +141,19 @@ public function encodeNsec(string $seckey): string
return $key->convertPrivateKeyToBech32($seckey);
}

public function encodeBech32(string $value, string $prefix): string
{
// TODO
$bytes = [];
return encode($prefix, $bytes);
}

/**
* @param string $key
* @param string $prefix
* @return string
* @throws Bech32Exception
*/
private function convertToBech32(string $key, string $prefix): string
{
$str = '';
Expand All @@ -115,7 +169,25 @@ private function convertToBech32(string $key, string $prefix): string
return $str;
}

private function readTLVEntry($value) {}
private function readTLVEntry(string $data, TLVEnum $type)
{
}

private function writeTLVEntry($value, string $type) {}
/**
* @param \swentel\nostr\Nip19\TLVEnum $type
* @param string $value
* Binary string.
* @return string
*/
private function writeTLVEntry(TLVEnum $type, string $value)
{
// TODO
return $value;
}

private function encodeTLV(Object $TLV): array
{

return [];
}
}
8 changes: 4 additions & 4 deletions src/Nip19/TLVEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*/
enum TLVEnum: int
{
case TLVDefault = 0;
case TLVRelay = 1;
case TLVAuthor = 2;
case TLVKind = 3;
case Special = 0;
case Relay = 1;
case Author = 2;
case Kind = 3;
}

0 comments on commit 2c0315d

Please sign in to comment.