From b24f4dde80731098af29b2523f436107e93b813c Mon Sep 17 00:00:00 2001 From: Sebastix Date: Fri, 25 Oct 2024 12:17:38 +0200 Subject: [PATCH] NIP-19 work in progress --- src/Examples/bech32-encoded-entities.php | 52 ++++++++++ src/Nip19/Nip19.php | 50 ---------- src/Nip19/Nip19Helper.php | 121 +++++++++++++++++++++++ src/Nip19/TLVEnum.php | 16 +++ 4 files changed, 189 insertions(+), 50 deletions(-) create mode 100644 src/Examples/bech32-encoded-entities.php delete mode 100644 src/Nip19/Nip19.php create mode 100644 src/Nip19/Nip19Helper.php create mode 100644 src/Nip19/TLVEnum.php diff --git a/src/Examples/bech32-encoded-entities.php b/src/Examples/bech32-encoded-entities.php new file mode 100644 index 0000000..3aa848c --- /dev/null +++ b/src/Examples/bech32-encoded-entities.php @@ -0,0 +1,52 @@ +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; +} diff --git a/src/Nip19/Nip19.php b/src/Nip19/Nip19.php deleted file mode 100644 index 03b066b..0000000 --- a/src/Nip19/Nip19.php +++ /dev/null @@ -1,50 +0,0 @@ - 90) { - throw new \Exception('Bech32 string cannot exceed 90 characters in length'); - } - if ($length < 8) { - throw new \Exception('Bech32 string is too short'); - } - } - - /** - * @throws \Exception - */ - public function encode(string $value) - { - $prefix = ''; - switch($prefix) { - case 'npub': - break; - case 'nsec': - break; - case 'note': - break; - default: - throw new \Exception('Unexpected value'); - } - } - -} diff --git a/src/Nip19/Nip19Helper.php b/src/Nip19/Nip19Helper.php new file mode 100644 index 0000000..2833996 --- /dev/null +++ b/src/Nip19/Nip19Helper.php @@ -0,0 +1,121 @@ + 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) {} +} diff --git a/src/Nip19/TLVEnum.php b/src/Nip19/TLVEnum.php new file mode 100644 index 0000000..069f159 --- /dev/null +++ b/src/Nip19/TLVEnum.php @@ -0,0 +1,16 @@ +