| 1 | package Net::Nostr::Bech32; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strictures 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use Net::Nostr::_ConstructorArgs (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use Bitcoin::Crypto::Bech32 qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | encode_bech32 | ||||||||||||||||||||||||||||
| 11 | translate_5to8 translate_8to5 | ||||||||||||||||||||||||||||
| 12 | 8 | ); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 8 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 15 | encode_npub decode_npub | ||||||||||||||||||||||||||||
| 16 | encode_nsec decode_nsec | ||||||||||||||||||||||||||||
| 17 | encode_note decode_note | ||||||||||||||||||||||||||||
| 18 | encode_nprofile decode_nprofile | ||||||||||||||||||||||||||||
| 19 | encode_nevent decode_nevent | ||||||||||||||||||||||||||||
| 20 | encode_naddr decode_naddr | ||||||||||||||||||||||||||||
| 21 | decode_bech32_entity | ||||||||||||||||||||||||||||
| 22 | encode_nostr_uri decode_nostr_uri | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 8 | my $HEX64 = qr/\A[0-9a-f]{64}\z/; | |||||||||||||||||||||||||||
| 26 | 8 | my $MAX_BECH32_LENGTH = 5000; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # Bech32 decode without BIP-173's 90-char limit (NIP-19 allows up to 5000) | ||||||||||||||||||||||||||||
| 29 | # Reuses polymod/checksum logic from Bitcoin::Crypto::Bech32 internals. | ||||||||||||||||||||||||||||
| 30 | { | ||||||||||||||||||||||||||||
| 31 | 8 | my @ALPHABET = qw( | |||||||||||||||||||||||||||
| 32 | q p z r y 9 x 8 g f 2 t v d w 0 | ||||||||||||||||||||||||||||
| 33 | s 3 j n 5 4 k h c e 6 m u a 7 l | ||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | 8 | my %ALPHABET_MAP = map { $ALPHABET[$_] => $_ } 0 .. $#ALPHABET; | |||||||||||||||||||||||||||
| 36 | 8 | my $CHARS = join '', @ALPHABET; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 119 | ▶ | sub _polymod { | ||||||||||||||||||||||||||
|
Subroutine
_polymod: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 39 | 119 | my ($values) = @_; | |||||||||||||||||||||||||||
| 40 | 119 | my @C = (0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3); | |||||||||||||||||||||||||||
| 41 | 119 | my $chk = 1; | |||||||||||||||||||||||||||
| 42 | 119 | for my $val (@$values) { | |||||||||||||||||||||||||||
| 43 | 10600 | my $b = ($chk >> 25); | |||||||||||||||||||||||||||
| 44 | 10600 | $chk = ($chk & 0x1ffffff) << 5 ^ $val; | |||||||||||||||||||||||||||
| 45 | 10600 | ▶ | for (0 .. 4) { $chk ^= (($b >> $_) & 1) ? $C[$_] : 0 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 119 | return $chk; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 119 | ▶ | sub _hrp_expand { | ||||||||||||||||||||||||||
|
Subroutine
_hrp_expand: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 119 | my @hrp = split //, shift; | |||||||||||||||||||||||||||
| 52 | 119 | return [map({ ord($_) >> 5 } @hrp), 0, map({ ord($_) & 31 } @hrp)]; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 120 | ▶ | sub _nostr_decode_bech32 { | ||||||||||||||||||||||||||
|
Subroutine
_nostr_decode_bech32: called CC=8 SCAR=21.0
| |||||||||||||||||||||||||||||
| 56 | 120 | my ($str) = @_; | |||||||||||||||||||||||||||
| 57 | 120 | ▶ | $str = lc $str if uc $str eq $str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 120 | ▶ | croak "bech32 string exceeds 5000 character limit" if length($str) > $MAX_BECH32_LENGTH; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 120 | ▶ | croak "bech32 string contains mixed case" if lc($str) ne $str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 119 | my @parts = split /1/, $str; | |||||||||||||||||||||||||||
| 62 | 119 | ▶ | croak "bech32 separator missing" if @parts < 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 119 | my $data_part = pop @parts; | |||||||||||||||||||||||||||
| 64 | 119 | my $hrp = join '1', @parts; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 119 | ▶ | croak "invalid bech32 data characters" if $data_part !~ /\A[$CHARS]+\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 119 | ▶ | croak "bech32 data part too short" if length($data_part) < 6; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 119 | my @data_values = map { $ALPHABET_MAP{$_} } split //, $data_part; | |||||||||||||||||||||||||||
| 70 | 119 | my $check_values = [@{_hrp_expand($hrp)}, @data_values]; | |||||||||||||||||||||||||||
| 71 | 119 | ▶ | croak "invalid bech32 checksum" unless _polymod($check_values) == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 118 | my @payload = @data_values[0 .. $#data_values - 6]; | |||||||||||||||||||||||||||
| 74 | 118 | return ($hrp, \@payload); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # TLV type constants | ||||||||||||||||||||||||||||
| 79 | 8 | my $TLV_SPECIAL = 0; | |||||||||||||||||||||||||||
| 80 | 8 | my $TLV_RELAY = 1; | |||||||||||||||||||||||||||
| 81 | 8 | my $TLV_AUTHOR = 2; | |||||||||||||||||||||||||||
| 82 | 8 | my $TLV_KIND = 3; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | ############################################################################### | ||||||||||||||||||||||||||||
| 85 | # Bare keys and ids | ||||||||||||||||||||||||||||
| 86 | ############################################################################### | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 21 | ▶ | sub encode_npub { | ||||||||||||||||||||||||||
|
Subroutine
encode_npub: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 21 | my ($hex) = @_; | |||||||||||||||||||||||||||
| 90 | 21 | ▶ | croak "pubkey must be 64-char lowercase hex" unless defined $hex && $hex =~ $HEX64; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $hex and $hex =~ /$HEX64/
MC/DC50%1/2
defined $hex and $hex =~ /$HEX64/
defined $hex
$hex =~ /$HEX64/
Truth table66%2/3
A and B
A= defined $hexB= $hex =~ /$HEX64/
| |||||||||||||||||||||||||||||
| 91 | 18 | return _encode_bare('npub', $hex); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 5 | ▶ | sub decode_npub { | ||||||||||||||||||||||||||
|
Subroutine
decode_npub: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 5 | my ($bech32) = @_; | |||||||||||||||||||||||||||
| 96 | 5 | return _decode_bare('npub', $bech32); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 11 | ▶ | sub encode_nsec { | ||||||||||||||||||||||||||
|
Subroutine
encode_nsec: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 11 | my ($hex) = @_; | |||||||||||||||||||||||||||
| 101 | 11 | ▶ | croak "private key must be 64-char lowercase hex" unless defined $hex && $hex =~ $HEX64; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $hex and $hex =~ /$HEX64/
MC/DC50%1/2
defined $hex and $hex =~ /$HEX64/
defined $hex
$hex =~ /$HEX64/
Truth table66%2/3
A and B
A= defined $hexB= $hex =~ /$HEX64/
| |||||||||||||||||||||||||||||
| 102 | 8 | return _encode_bare('nsec', $hex); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 2 | ▶ | sub decode_nsec { | ||||||||||||||||||||||||||
|
Subroutine
decode_nsec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | 2 | my ($bech32) = @_; | |||||||||||||||||||||||||||
| 107 | 2 | return _decode_bare('nsec', $bech32); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 9 | ▶ | sub encode_note { | ||||||||||||||||||||||||||
|
Subroutine
encode_note: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 9 | my ($hex) = @_; | |||||||||||||||||||||||||||
| 112 | 9 | ▶ | croak "event id must be 64-char lowercase hex" unless defined $hex && $hex =~ $HEX64; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $hex and $hex =~ /$HEX64/
MC/DC50%1/2
defined $hex and $hex =~ /$HEX64/
defined $hex
$hex =~ /$HEX64/
Truth table66%2/3
A and B
A= defined $hexB= $hex =~ /$HEX64/
| |||||||||||||||||||||||||||||
| 113 | 6 | return _encode_bare('note', $hex); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 2 | ▶ | sub decode_note { | ||||||||||||||||||||||||||
|
Subroutine
decode_note: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 2 | my ($bech32) = @_; | |||||||||||||||||||||||||||
| 118 | 2 | return _decode_bare('note', $bech32); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 32 | ▶ | sub _encode_bare { | ||||||||||||||||||||||||||
|
Subroutine
_encode_bare: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 122 | 32 | my ($hrp, $hex) = @_; | |||||||||||||||||||||||||||
| 123 | 32 | my $bytes = pack 'H*', $hex; | |||||||||||||||||||||||||||
| 124 | 32 | my $data5 = translate_8to5($bytes); | |||||||||||||||||||||||||||
| 125 | 32 | return encode_bech32($hrp, $data5, 'bech32'); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 26 | ▶ | sub _decode_bare { | ||||||||||||||||||||||||||
|
Subroutine
_decode_bare: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 129 | 26 | my ($expected_hrp, $bech32) = @_; | |||||||||||||||||||||||||||
| 130 | 26 | my ($hrp, $data5) = _nostr_decode_bech32($bech32); | |||||||||||||||||||||||||||
| 131 | 25 | ▶ | croak "expected $expected_hrp prefix, got $hrp" unless $hrp eq $expected_hrp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 22 | my $bytes = translate_5to8($data5); | |||||||||||||||||||||||||||
| 133 | 22 | ▶ | croak "$expected_hrp payload must be exactly 32 bytes, got " . length($bytes) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | unless length($bytes) == 32; | ||||||||||||||||||||||||||||
| 135 | 22 | return unpack 'H*', $bytes; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | ############################################################################### | ||||||||||||||||||||||||||||
| 139 | # TLV encoding/decoding helpers | ||||||||||||||||||||||||||||
| 140 | ############################################################################### | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 26 | ▶ | sub _encode_tlv { | ||||||||||||||||||||||||||
|
Subroutine
_encode_tlv: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 143 | 26 | my ($hrp, @tlvs) = @_; | |||||||||||||||||||||||||||
| 144 | 26 | my $payload = ''; | |||||||||||||||||||||||||||
| 145 | 26 | for my $tlv (@tlvs) { | |||||||||||||||||||||||||||
| 146 | 61 | my ($type, $value) = @$tlv; | |||||||||||||||||||||||||||
| 147 | 61 | $payload .= pack('CC', $type, length($value)) . $value; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 26 | my $data5 = translate_8to5($payload); | |||||||||||||||||||||||||||
| 150 | 26 | my $encoded = encode_bech32($hrp, $data5, 'bech32'); | |||||||||||||||||||||||||||
| 151 | 26 | ▶ | croak "bech32 string exceeds 5000 character limit" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | if length($encoded) > $MAX_BECH32_LENGTH; | ||||||||||||||||||||||||||||
| 153 | 26 | return $encoded; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 23 | ▶ | sub _decode_tlv { | ||||||||||||||||||||||||||
|
Subroutine
_decode_tlv: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 157 | 23 | my ($expected_hrp, $bech32) = @_; | |||||||||||||||||||||||||||
| 158 | 23 | my ($hrp, $data5) = _nostr_decode_bech32($bech32); | |||||||||||||||||||||||||||
| 159 | 23 | ▶ | croak "expected $expected_hrp prefix, got $hrp" unless $hrp eq $expected_hrp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 20 | my $payload = translate_5to8($data5); | |||||||||||||||||||||||||||
| 161 | 20 | my @tlvs; | |||||||||||||||||||||||||||
| 162 | 20 | my $pos = 0; | |||||||||||||||||||||||||||
| 163 | 20 | ▶ | while ($pos < length($payload)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 55 | ▶ | croak "truncated TLV: missing length byte at offset $pos" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | unless $pos + 1 < length($payload); | ||||||||||||||||||||||||||||
| 166 | 55 | my $type = unpack('C', substr($payload, $pos, 1)); | |||||||||||||||||||||||||||
| 167 | 55 | my $len = unpack('C', substr($payload, $pos + 1, 1)); | |||||||||||||||||||||||||||
| 168 | 55 | ▶ | croak "truncated TLV: value extends beyond payload at offset $pos" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | unless $pos + 2 + $len <= length($payload); | ||||||||||||||||||||||||||||
| 170 | 55 | my $val = substr($payload, $pos + 2, $len); | |||||||||||||||||||||||||||
| 171 | 55 | push @tlvs, [$type, $val]; | |||||||||||||||||||||||||||
| 172 | 55 | $pos += 2 + $len; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | 20 | return @tlvs; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | ############################################################################### | ||||||||||||||||||||||||||||
| 178 | # nprofile | ||||||||||||||||||||||||||||
| 179 | ############################################################################### | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 11 | ▶ | sub encode_nprofile { | ||||||||||||||||||||||||||
|
Subroutine
encode_nprofile: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 182 | 11 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | |||||||||||||||||||||||||||
| 183 | 11 | ▶ | my $pubkey = $args{pubkey} // croak "nprofile requires 'pubkey'"; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"pubkey"} // croak("nprofile requires 'pubkey'")
MC/DC100%1/1
$args{"pubkey"} // croak("nprofile requires 'pubkey'")
$args{"pubkey"}
Truth table100%2/2
A
A= $args{"pubkey"}
| |||||||||||||||||||||||||||||
| 184 | 10 | ▶ | croak "pubkey must be 64-char lowercase hex" unless $pubkey =~ $HEX64; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 9 | my @tlvs; | |||||||||||||||||||||||||||
| 186 | 9 | push @tlvs, [$TLV_SPECIAL, pack('H*', $pubkey)]; | |||||||||||||||||||||||||||
| 187 | 9 | ▶ | for my $relay (@{$args{relays} // []}) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"relays"} // []
MC/DC100%1/1
$args{"relays"} // []
$args{"relays"}
Truth table100%2/2
A
A= $args{"relays"}
| |||||||||||||||||||||||||||||
| 188 | 7 | push @tlvs, [$TLV_RELAY, $relay]; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | 9 | return _encode_tlv('nprofile', @tlvs); | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 9 | ▶ | sub decode_nprofile { | ||||||||||||||||||||||||||
|
Subroutine
decode_nprofile: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 194 | 9 | my ($bech32) = @_; | |||||||||||||||||||||||||||
| 195 | 9 | my @tlvs = _decode_tlv('nprofile', $bech32); | |||||||||||||||||||||||||||
| 196 | 8 | my %result = (relays => []); | |||||||||||||||||||||||||||
| 197 | 8 | for my $tlv (@tlvs) { | |||||||||||||||||||||||||||
| 198 | 18 | my ($type, $val) = @$tlv; | |||||||||||||||||||||||||||
| 199 | 18 | ▶ | if ($type == $TLV_SPECIAL) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 200 | 8 | ▶ | croak "nprofile: pubkey (type 0) must be exactly 32 bytes, got " . length($val) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | unless length($val) == 32; | ||||||||||||||||||||||||||||
| 202 | 8 | $result{pubkey} = unpack 'H*', $val; | |||||||||||||||||||||||||||
| 203 | } elsif ($type == $TLV_RELAY) { | ||||||||||||||||||||||||||||
| 204 | 10 | push @{$result{relays}}, $val; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | # ignore unknown types per spec | ||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | 8 | ▶ | croak "nprofile: missing required pubkey (type 0)" unless defined $result{pubkey}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 8 | return \%result; | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | ############################################################################### | ||||||||||||||||||||||||||||
| 213 | # nevent | ||||||||||||||||||||||||||||
| 214 | ############################################################################### | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 13 | ▶ | sub encode_nevent { | ||||||||||||||||||||||||||
|
Subroutine
encode_nevent: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 217 | 13 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | |||||||||||||||||||||||||||
| 218 | 13 | ▶ | my $id = $args{id} // croak "nevent requires 'id'"; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"id"} // croak("nevent requires 'id'")
MC/DC100%1/1
$args{"id"} // croak("nevent requires 'id'")
$args{"id"}
Truth table100%2/2
A
A= $args{"id"}
| |||||||||||||||||||||||||||||
| 219 | 12 | ▶ | croak "id must be 64-char lowercase hex" unless $id =~ $HEX64; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 11 | my @tlvs; | |||||||||||||||||||||||||||
| 221 | 11 | push @tlvs, [$TLV_SPECIAL, pack('H*', $id)]; | |||||||||||||||||||||||||||
| 222 | 11 | ▶ | for my $relay (@{$args{relays} // []}) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"relays"} // []
MC/DC100%1/1
$args{"relays"} // []
$args{"relays"}
Truth table100%2/2
A
A= $args{"relays"}
| |||||||||||||||||||||||||||||
| 223 | 3 | push @tlvs, [$TLV_RELAY, $relay]; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | 11 | ▶ | if (defined $args{author}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 4 | ▶ | croak "author must be 64-char lowercase hex" unless $args{author} =~ $HEX64; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | 3 | push @tlvs, [$TLV_AUTHOR, pack('H*', $args{author})]; | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | 10 | ▶ | if (defined $args{kind}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 6 | push @tlvs, [$TLV_KIND, pack('N', $args{kind})]; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | 10 | return _encode_tlv('nevent', @tlvs); | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 7 | ▶ | sub decode_nevent { | ||||||||||||||||||||||||||
|
Subroutine
decode_nevent: called CC=10 SCAR=23.4
Pod
documented
| |||||||||||||||||||||||||||||
| 236 | 7 | my ($bech32) = @_; | |||||||||||||||||||||||||||
| 237 | 7 | my @tlvs = _decode_tlv('nevent', $bech32); | |||||||||||||||||||||||||||
| 238 | 6 | my %result = (relays => []); | |||||||||||||||||||||||||||
| 239 | 6 | for my $tlv (@tlvs) { | |||||||||||||||||||||||||||
| 240 | 16 | my ($type, $val) = @$tlv; | |||||||||||||||||||||||||||
| 241 | 16 | ▶ | if ($type == $TLV_SPECIAL) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 242 | 6 | ▶ | croak "nevent: event id (type 0) must be exactly 32 bytes, got " . length($val) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | unless length($val) == 32; | ||||||||||||||||||||||||||||
| 244 | 6 | $result{id} = unpack 'H*', $val; | |||||||||||||||||||||||||||
| 245 | } elsif ($type == $TLV_RELAY) { | ||||||||||||||||||||||||||||
| 246 | 4 | push @{$result{relays}}, $val; | |||||||||||||||||||||||||||
| 247 | } elsif ($type == $TLV_AUTHOR) { | ||||||||||||||||||||||||||||
| 248 | 2 | ▶ | croak "nevent: author (type 2) must be exactly 32 bytes, got " . length($val) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 249 | unless length($val) == 32; | ||||||||||||||||||||||||||||
| 250 | 2 | $result{author} = unpack 'H*', $val; | |||||||||||||||||||||||||||
| 251 | } elsif ($type == $TLV_KIND) { | ||||||||||||||||||||||||||||
| 252 | 4 | ▶ | croak "nevent: kind (type 3) must be exactly 4 bytes, got " . length($val) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | unless length($val) == 4; | ||||||||||||||||||||||||||||
| 254 | 4 | $result{kind} = unpack 'N', $val; | |||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | 6 | ▶ | croak "nevent: missing required event id (type 0)" unless defined $result{id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | 6 | return \%result; | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | ############################################################################### | ||||||||||||||||||||||||||||
| 262 | # naddr | ||||||||||||||||||||||||||||
| 263 | ############################################################################### | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 10 | ▶ | sub encode_naddr { | ||||||||||||||||||||||||||
|
Subroutine
encode_naddr: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 266 | 10 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | |||||||||||||||||||||||||||
| 267 | 10 | ▶ | croak "naddr requires 'identifier'" unless defined $args{identifier}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | 9 | ▶ | my $pubkey = $args{pubkey} // croak "naddr requires 'pubkey'"; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"pubkey"} // croak("naddr requires 'pubkey'")
MC/DC100%1/1
$args{"pubkey"} // croak("naddr requires 'pubkey'")
$args{"pubkey"}
Truth table100%2/2
A
A= $args{"pubkey"}
| |||||||||||||||||||||||||||||
| 269 | 8 | ▶ | croak "pubkey must be 64-char lowercase hex" unless $pubkey =~ $HEX64; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | 8 | ▶ | my $kind = $args{kind} // croak "naddr requires 'kind'"; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"kind"} // croak("naddr requires 'kind'")
MC/DC100%1/1
$args{"kind"} // croak("naddr requires 'kind'")
$args{"kind"}
Truth table100%2/2
A
A= $args{"kind"}
| |||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 7 | my @tlvs; | |||||||||||||||||||||||||||
| 273 | 7 | push @tlvs, [$TLV_SPECIAL, $args{identifier}]; | |||||||||||||||||||||||||||
| 274 | 7 | ▶ | for my $relay (@{$args{relays} // []}) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"relays"} // []
MC/DC100%1/1
$args{"relays"} // []
$args{"relays"}
Truth table100%2/2
A
A= $args{"relays"}
| |||||||||||||||||||||||||||||
| 275 | 2 | push @tlvs, [$TLV_RELAY, $relay]; | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | 7 | push @tlvs, [$TLV_AUTHOR, pack('H*', $pubkey)]; | |||||||||||||||||||||||||||
| 278 | 7 | push @tlvs, [$TLV_KIND, pack('N', $kind)]; | |||||||||||||||||||||||||||
| 279 | 7 | return _encode_tlv('naddr', @tlvs); | |||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 7 | ▶ | sub decode_naddr { | ||||||||||||||||||||||||||
|
Subroutine
decode_naddr: called CC=11 SCAR=24.5
Pod
documented
| |||||||||||||||||||||||||||||
| 283 | 7 | my ($bech32) = @_; | |||||||||||||||||||||||||||
| 284 | 7 | my @tlvs = _decode_tlv('naddr', $bech32); | |||||||||||||||||||||||||||
| 285 | 6 | my %result = (relays => []); | |||||||||||||||||||||||||||
| 286 | 6 | for my $tlv (@tlvs) { | |||||||||||||||||||||||||||
| 287 | 21 | my ($type, $val) = @$tlv; | |||||||||||||||||||||||||||
| 288 | 21 | ▶ | if ($type == $TLV_SPECIAL) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 289 | 6 | $result{identifier} = $val; | |||||||||||||||||||||||||||
| 290 | } elsif ($type == $TLV_RELAY) { | ||||||||||||||||||||||||||||
| 291 | 3 | push @{$result{relays}}, $val; | |||||||||||||||||||||||||||
| 292 | } elsif ($type == $TLV_AUTHOR) { | ||||||||||||||||||||||||||||
| 293 | 6 | ▶ | croak "naddr: pubkey (type 2) must be exactly 32 bytes, got " . length($val) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | unless length($val) == 32; | ||||||||||||||||||||||||||||
| 295 | 6 | $result{pubkey} = unpack 'H*', $val; | |||||||||||||||||||||||||||
| 296 | } elsif ($type == $TLV_KIND) { | ||||||||||||||||||||||||||||
| 297 | 6 | ▶ | croak "naddr: kind (type 3) must be exactly 4 bytes, got " . length($val) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | unless length($val) == 4; | ||||||||||||||||||||||||||||
| 299 | 6 | $result{kind} = unpack 'N', $val; | |||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | 6 | ▶ | croak "naddr: missing required identifier (type 0)" unless defined $result{identifier}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | 6 | ▶ | croak "naddr: missing required pubkey (type 2)" unless defined $result{pubkey}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 304 | 6 | ▶ | croak "naddr: missing required kind (type 3)" unless defined $result{kind}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 305 | 6 | return \%result; | |||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | ############################################################################### | ||||||||||||||||||||||||||||
| 309 | # Generic decode | ||||||||||||||||||||||||||||
| 310 | ############################################################################### | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 8 | my %BARE_TYPES = map { $_ => 1 } qw(npub nsec note); | |||||||||||||||||||||||||||
| 313 | 8 | my %TLV_DECODERS = ( | |||||||||||||||||||||||||||
| 314 | nprofile => \&decode_nprofile, | ||||||||||||||||||||||||||||
| 315 | nevent => \&decode_nevent, | ||||||||||||||||||||||||||||
| 316 | naddr => \&decode_naddr, | ||||||||||||||||||||||||||||
| 317 | ); | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 32 | ▶ | sub decode_bech32_entity { | ||||||||||||||||||||||||||
|
Subroutine
decode_bech32_entity: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 320 | 32 | my ($bech32) = @_; | |||||||||||||||||||||||||||
| 321 | 32 | my ($hrp) = _nostr_decode_bech32($bech32); | |||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 31 | ▶ | if ($BARE_TYPES{$hrp}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 17 | my $hex = _decode_bare($hrp, $bech32); | |||||||||||||||||||||||||||
| 325 | 17 | return { type => $hrp, data => $hex }; | |||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | 14 | ▶ | if (my $decoder = $TLV_DECODERS{$hrp}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 329 | 14 | return { type => $hrp, data => $decoder->($bech32) }; | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 0 | croak "unknown bech32 entity prefix: $hrp"; | |||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | ############################################################################### | ||||||||||||||||||||||||||||
| 336 | # NIP-21: nostr: URI scheme | ||||||||||||||||||||||||||||
| 337 | ############################################################################### | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 8 | my %NOSTR_URI_TYPES = map { $_ => 1 } qw(npub note nprofile nevent naddr); | |||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 16 | ▶ | sub encode_nostr_uri { | ||||||||||||||||||||||||||
|
Subroutine
encode_nostr_uri: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 342 | 16 | my ($bech32) = @_; | |||||||||||||||||||||||||||
| 343 | 16 | ▶ | croak "bech32 string required" unless defined $bech32 && length $bech32; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $bech32 and length $bech32
MC/DC50%1/2
defined $bech32 and length $bech32
defined $bech32
length $bech32
Truth table66%2/3
A and B
A= defined $bech32B= length $bech32
| |||||||||||||||||||||||||||||
| 344 | 15 | my ($hrp) = _nostr_decode_bech32($bech32); | |||||||||||||||||||||||||||
| 345 | 15 | ▶ | croak "nsec must not be used in nostr: URIs" if $hrp eq 'nsec'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | croak "unsupported bech32 prefix for nostr: URI: $hrp" | ||||||||||||||||||||||||||||
| 347 | 13 | ▶ | unless $NOSTR_URI_TYPES{$hrp}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | 12 | return "nostr:$bech32"; | |||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 28 | ▶ | sub decode_nostr_uri { | ||||||||||||||||||||||||||
|
Subroutine
decode_nostr_uri: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 352 | 28 | my ($uri) = @_; | |||||||||||||||||||||||||||
| 353 | 28 | ▶ | croak "nostr: URI required" unless defined $uri && length $uri; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $uri and length $uri
MC/DC100%2/2
defined $uri and length $uri
defined $uri
length $uri
Truth table100%3/3
A and B
A= defined $uriB= length $uri
| |||||||||||||||||||||||||||||
| 354 | 26 | ▶ | croak "nostr: URI must start with nostr:" unless $uri =~ /\Anostr:/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 355 | 24 | my $bech32 = substr($uri, 6); | |||||||||||||||||||||||||||
| 356 | 24 | my ($hrp) = _nostr_decode_bech32($bech32); | |||||||||||||||||||||||||||
| 357 | 24 | ▶ | croak "nsec must not be used in nostr: URIs" if $hrp eq 'nsec'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 358 | 22 | return decode_bech32_entity($bech32); | |||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | 8 | 1; | |||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | __END__ | ||||||||||||||||||||||||||||