|
| 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 | +} |
0 commit comments