Nostr/Identifier.pm

filter: Statementstmt 95.9% 117 / 122 Branchbran 95.0% 57 / 60 Conditioncond 70.3% 19 / 27 MC/DCmcdc 16.6% 3 / 18 Subroutinesub 100% 19 / 19 Podpod 100% 8 / 8 totaltotal 87.8% 223 / 254 CC 40 SCAR 16.5
CC
40
Cov
92%
parse
8.0
__ANON__
7.4
verify
7.0
CRAP 5.2
« Nostr/Bech32.pm ↑ Summary Nostr/_ConstructorArgs.pm »
1 package Net::Nostr::Identifier;
2  
3 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Net::Nostr::_ConstructorArgs ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use AnyEvent::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 use Class::Tiny qw(
12 base_url
13 2 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub new {
16 27 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
17 27 my $self = bless { Net::Nostr::_ConstructorArgs::normalize(@_) }, $class;
18 27 my %known; @known{Class::Tiny->get_all_attributes_for($class)} = ();
19 27 my @unknown = grep { !exists $known{$_} } keys %$self;
20 27 ▶ croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 26 27
21 26 return $self;
22 }
23  
24 sub parse {
25 66 ▶ my ($class, $identifier) = @_;
Subroutine parse: called CC=8 SCAR=20.8
Pod documented
26 66 ▶ croak "invalid NIP-05 identifier: missing \@"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $identifier and $identifier =~ /@/ 5 61 66
Condition100%3/3
defined $identifier and $identifier =~ /@/
!ll&&!rl&&r
1461
MC/DC100%2/2
defined $identifier and $identifier =~ /@/
defined $identifier $identifier =~ /@/
Truth table100%3/3
A and B
A= defined $identifierB= $identifier =~ /@/
ABresult
0X0
100
111
27 unless defined $identifier && $identifier =~ /@/;
28 61 my @parts = split /@/, $identifier, -1;
29 61 ▶ croak "invalid NIP-05 identifier: multiple \@" if @parts > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @parts > 2 2 59 61
30 59 my ($local, $domain) = @parts;
31 59 ▶ croak "invalid NIP-05 identifier: empty local-part"
Branch: 1 branch
BranchTrueFalseTotal
unless length $local 2 57 59
32 unless length $local;
33 57 ▶ croak "invalid NIP-05 identifier: empty domain"
Branch: 1 branch
BranchTrueFalseTotal
unless length $domain 2 55 57
34 unless length $domain;
35 55 ▶ croak "invalid NIP-05 identifier: local-part contains invalid characters (must be a-z0-9-_.)"
Branch: 1 branch
BranchTrueFalseTotal
unless $local =~ /\A[a-z0-9\-_.]+\z/ 7 48 55
36 unless $local =~ /\A[a-z0-9\-_.]+\z/;
37 48 ▶ croak "invalid NIP-05 identifier: domain contains invalid characters"
Branch: 1 branch
BranchTrueFalseTotal
if $domain =~ m([\s/:?\#\[\]\x00-\x1f\x7f]) 15 33 48
38 if $domain =~ m{[\s/:?\#\[\]\x00-\x1f\x7f]};
39 33 return ($local, $domain);
40 }
41  
42 sub url {
43 5 ▶ my ($class, $identifier) = @_;
Subroutine url: called CC=1 SCAR=0.0
Pod documented
44 5 my ($local, $domain) = $class->parse($identifier);
45 4 return "https://$domain/.well-known/nostr.json?name=$local";
46 }
47  
48 sub display_name {
49 5 ▶ my ($class, $identifier) = @_;
Subroutine display_name: called CC=2 SCAR=6.9
Pod documented
50 5 my ($local, $domain) = $class->parse($identifier);
51 4 ▶ return $domain if $local eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if $local eq "_" 2 2 4
52 2 return $identifier;
53 }
54  
55 sub verify_response {
56 30 ▶ my ($class, $response, $local_part, $pubkey) = @_;
Subroutine verify_response: called CC=6 SCAR=17.9
Pod documented
57 30 ▶ return 0 unless ref $response eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $response eq "HASH" 6 24 30
58 24 ▶ return 0 unless ref($response->{names}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $response->{"names"} eq "HASH" 5 19 24
59 19 my $found = $response->{names}{$local_part};
60 19 ▶ return 0 unless defined $found;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $found 2 17 19
61 # Keys must be 64-char lowercase hex (32-byte pubkey)
62 17 ▶ return 0 unless $found =~ /\A[a-f0-9]{64}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $found =~ /\A[a-f0-9]{64}\z/ 6 11 17
63 11 ▶ return $found eq $pubkey ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$found eq $pubkey ? : 7 4 11
64 }
65  
66 sub extract_relays {
67 15 ▶ my ($class, $response, $pubkey) = @_;
Subroutine extract_relays: called CC=4 SCAR=13.9
Pod documented
68 15 ▶ return [] unless ref $response eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $response eq "HASH" 2 13 15
69 13 ▶ return [] unless ref($response->{relays}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $response->{"relays"} eq "HASH" 4 9 13
70 9 my $list = $response->{relays}{$pubkey};
71 9 ▶ return [] unless ref $list eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 3 6 9
72 6 return $list;
73 }
74  
75 sub _fetch {
76 11 ▶ my ($self, $url, $cb) = @_;
Subroutine _fetch: called CC=1 SCAR=0.0
77 http_get $url,
78 recurse => 0, # MUST ignore redirects
79 sub {
80 11 ▶ my ($body, $headers) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
81 11 $cb->($body, $headers);
82 11 };
83 }
84  
85 sub _build_fetch_url {
86 11 ▶ my ($self, $identifier) = @_;
Subroutine _build_fetch_url: called CC=2 SCAR=7.4
87 11 my ($local, $domain) = Net::Nostr::Identifier->parse($identifier);
88 11 ▶ if (defined $self->base_url) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->base_url) 11 0 11
89 11 return ($self->base_url . "/.well-known/nostr.json?name=$local", $local);
90 }
91 0 return ("https://$domain/.well-known/nostr.json?name=$local", $local);
92 }
93  
94 sub verify {
95 13 ▶ my $self = shift;
Subroutine verify: called CC=7 SCAR=19.5
Pod documented
96 13 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
97 13 ▶ my $identifier = $args{identifier} // croak "identifier required";
Condition66%2/3
$args{"identifier"} // croak("identifier required")
l!l&&r!l&&!r
1210
MC/DC0%0/2
$args{"identifier"} // croak("identifier required")
$args{"identifier"} croak("identifier required")
Truth table33%1/3
A // B
A= $args{"identifier"}B= croak("identifier required")
ABresult
1X1
011
000
98 12 ▶ my $pubkey = $args{pubkey} // croak "pubkey required";
Condition66%2/3
$args{"pubkey"} // croak("pubkey required")
l!l&&r!l&&!r
1110
MC/DC0%0/2
$args{"pubkey"} // croak("pubkey required")
$args{"pubkey"} croak("pubkey required")
Truth table33%1/3
A // B
A= $args{"pubkey"}B= croak("pubkey required")
ABresult
1X1
011
000
99 11 ▶ my $on_success = $args{on_success} // croak "on_success callback required";
Condition66%2/3
$args{"on_success"} // croak("on_success callback required")
l!l&&r!l&&!r
1010
MC/DC0%0/2
$args{"on_success"} // croak("on_success callback required")
$args{"on_success"} croak("on_success callback required")
Truth table33%1/3
A // B
A= $args{"on_success"}B= croak("on_success callback required")
ABresult
1X1
011
000
100 10 ▶ croak "on_success must be a CODE ref" unless ref($on_success) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $on_success eq "CODE" 2 8 10
101 8 ▶ my $on_failure = $args{on_failure} // croak "on_failure callback required";
Condition66%2/3
$args{"on_failure"} // croak("on_failure callback required")
l!l&&r!l&&!r
710
MC/DC0%0/2
$args{"on_failure"} // croak("on_failure callback required")
$args{"on_failure"} croak("on_failure callback required")
Truth table33%1/3
A // B
A= $args{"on_failure"}B= croak("on_failure callback required")
ABresult
1X1
011
000
102 7 ▶ croak "on_failure must be a CODE ref" unless ref($on_failure) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $on_failure eq "CODE" 1 6 7
103  
104 6 my ($url, $local) = $self->_build_fetch_url($identifier);
105  
106 $self->_fetch($url, sub {
107 6 ▶ my ($body, $headers) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.1
108  
109 # Check for redirect (status 3xx)
110 6 ▶ if ($headers->{Status} =~ /^3/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers->{"Status"} =~ /^3/) 1 5 6
111 1 $on_failure->("redirect: server returned HTTP $headers->{Status}");
112 1 return;
113 }
114  
115 5 ▶ unless ($headers->{Status} =~ /^2/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers->{"Status"} =~ /^2/) 1 4 5
116 1 $on_failure->("HTTP error: $headers->{Status} $headers->{Reason}");
117 1 return;
118 }
119  
120 4 my $response = eval { JSON::decode_json($body) };
121 4 ▶ unless ($response) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response) 1 3 4
122 1 $on_failure->("invalid JSON response");
123 1 return;
124 }
125  
126 3 ▶ if (Net::Nostr::Identifier->verify_response($response, $local, $pubkey)) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Net::Nostr::Identifier'->verify_response($response, $local, $pubkey)) { } 2 1 3
127 2 my $relays = Net::Nostr::Identifier->extract_relays($response, $pubkey);
128 2 $on_success->($relays);
129 } else {
130 1 $on_failure->("pubkey mismatch for $local");
131 }
132 6 });
133 }
134  
135 sub lookup {
136 10 ▶ my $self = shift;
Subroutine lookup: called CC=6 SCAR=18.3
Pod documented
137 10 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
138 10 ▶ my $identifier = $args{identifier} // croak "identifier required";
Condition66%2/3
$args{"identifier"} // croak("identifier required")
l!l&&r!l&&!r
910
MC/DC0%0/2
$args{"identifier"} // croak("identifier required")
$args{"identifier"} croak("identifier required")
Truth table33%1/3
A // B
A= $args{"identifier"}B= croak("identifier required")
ABresult
1X1
011
000
139 9 ▶ my $on_success = $args{on_success} // croak "on_success callback required";
Condition66%2/3
$args{"on_success"} // croak("on_success callback required")
l!l&&r!l&&!r
810
MC/DC0%0/2
$args{"on_success"} // croak("on_success callback required")
$args{"on_success"} croak("on_success callback required")
Truth table33%1/3
A // B
A= $args{"on_success"}B= croak("on_success callback required")
ABresult
1X1
011
000
140 8 ▶ croak "on_success must be a CODE ref" unless ref($on_success) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $on_success eq "CODE" 1 7 8
141 7 ▶ my $on_failure = $args{on_failure} // croak "on_failure callback required";
Condition66%2/3
$args{"on_failure"} // croak("on_failure callback required")
l!l&&r!l&&!r
610
MC/DC0%0/2
$args{"on_failure"} // croak("on_failure callback required")
$args{"on_failure"} croak("on_failure callback required")
Truth table33%1/3
A // B
A= $args{"on_failure"}B= croak("on_failure callback required")
ABresult
1X1
011
000
142 6 ▶ croak "on_failure must be a CODE ref" unless ref($on_failure) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $on_failure eq "CODE" 1 5 6
143  
144 5 my ($url, $local) = $self->_build_fetch_url($identifier);
145  
146 $self->_fetch($url, sub {
147 5 ▶ my ($body, $headers) = @_;
Subroutine __ANON__: called CC=7 SCAR=20.1
148  
149 5 ▶ if ($headers->{Status} =~ /^3/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers->{"Status"} =~ /^3/) 1 4 5
150 1 $on_failure->("redirect: server returned HTTP $headers->{Status}");
151 1 return;
152 }
153  
154 4 ▶ unless ($headers->{Status} =~ /^2/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers->{"Status"} =~ /^2/) 0 4 4
155 0 $on_failure->("HTTP error: $headers->{Status} $headers->{Reason}");
156 0 return;
157 }
158  
159 4 my $response = eval { JSON::decode_json($body) };
160 4 ▶ unless ($response) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response) 0 4 4
161 0 $on_failure->("invalid JSON response");
162 0 return;
163 }
164  
165 4 ▶ unless (ref($response->{names}) eq 'HASH' && defined $response->{names}{$local}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $response->{"names"} eq "HASH" and defined $response->{"names"}{$local}) 1 3 4
Condition66%2/3
ref $response->{"names"} eq "HASH" and defined $response->{"names"}{$local}
!ll&&!rl&&r
013
MC/DC50%1/2
ref $response->{"names"} eq "HASH" and defined $response->{"names"}{$local}
ref $response->{"names"} eq "HASH" defined $response->{"names"}{$local}
Truth table66%2/3
A and B
A= ref $response->{"names"} eq "HASH"B= defined $response->{"names"}{$local}
ABresult
0X0
100
111
166 1 $on_failure->("name '$local' not found");
167 1 return;
168 }
169  
170 3 my $pubkey = $response->{names}{$local};
171 3 ▶ unless ($pubkey =~ /\A[a-f0-9]{64}\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pubkey =~ /\A[a-f0-9]{64}\z/) 2 1 3
172 2 $on_failure->("invalid pubkey format");
173 2 return;
174 }
175  
176 1 my $relays = Net::Nostr::Identifier->extract_relays($response, $pubkey);
177 1 $on_success->($pubkey, $relays);
178 5 });
179 }
180  
181 2 1;
182  
183 =head1 NAME
184  
185 Net::Nostr::Identifier - Mapping Nostr keys to DNS-based internet identifiers
186  
187 =head1 SYNOPSIS
188  
189 use AnyEvent;
190 use Net::Nostr::Identifier;
191  
192 my $pubkey = 'b0635d6a9851d3aed0cd6c495b282167acf761729078d975fc341b22650b07b9';
193  
194 # Parse and validate an identifier
195 my ($local, $domain) = Net::Nostr::Identifier->parse('bob@example.com');
196  
197 # Build the well-known URL
198 my $url = Net::Nostr::Identifier->url('bob@example.com');
199 # https://example.com/.well-known/nostr.json?name=bob
200  
201 # Display name (root identifier _@domain shows as domain)
202 Net::Nostr::Identifier->display_name('bob@example.com'); # "bob@example.com"
203 Net::Nostr::Identifier->display_name('_@bob.com'); # "bob.com"
204  
205 # Verify a nostr.json response
206 my $response = { names => { bob => $pubkey } };
207 if (Net::Nostr::Identifier->verify_response($response, 'bob', $pubkey)) {
208 print "Verified!\n";
209 }
210  
211 # Async verification via HTTP (requires an AnyEvent loop)
212 my $cv = AnyEvent->condvar;
213 my $ident = Net::Nostr::Identifier->new;
214 $ident->verify(
215 identifier => 'bob@example.com',
216 pubkey => $pubkey,
217 on_success => sub { my ($relays) = @_; print "Verified!\n"; $cv->send },
218 on_failure => sub { my ($reason) = @_; warn "Failed: $reason"; $cv->send },
219 );
220 $cv->recv;
221  
222 # Async lookup (find pubkey from identifier)
223 $cv = AnyEvent->condvar;
224 $ident->lookup(
225 identifier => 'bob@example.com',
226 on_success => sub { my ($pubkey, $relays) = @_; $cv->send },
227 on_failure => sub { my ($reason) = @_; $cv->send },
228 );
229 $cv->recv;
230  
231 =head1 DESCRIPTION
232  
233 Implements NIP-05 for mapping Nostr public keys to DNS-based internet
234 identifiers. An identifier has the form C<local-part@domain> where the
235 local-part uses only characters C<a-z0-9-_.>.
236  
237 Clients verify an identifier by fetching
238 C<https://E<lt>domainE<gt>/.well-known/nostr.json?name=E<lt>local-partE<gt>>
239 and checking that the returned C<"names"> mapping contains the expected
240 public key.
241  
242 The special identifier C<_@domain> is treated as a root identifier and
243 may be displayed as just the domain.
244  
245 =head1 CLASS METHODS
246  
247 =head2 parse
248  
249 my ($local_part, $domain) = Net::Nostr::Identifier->parse('bob@example.com');
250  
251 Splits an identifier into its local-part and domain. Strictly validates
252 both parts. Croaks if the identifier is invalid:
253  
254 =over 4
255  
256 =item * missing or multiple C<@>
257  
258 =item * empty local-part or domain
259  
260 =item * local-part contains characters outside C<a-z0-9-_.>
261  
262 =item * domain contains whitespace, C</>, C<:>, C<?>, C<#>, C<[>, C<]>, or control characters
263  
264 =back
265  
266 Bracketed IPv6 addresses (e.g. C<[::1]>) are rejected because NIP-05 is
267 DNS-based. Ports are rejected because the spec constructs HTTPS URLs from
268 the domain directly.
269  
270 =head2 url
271  
272 my $url = Net::Nostr::Identifier->url('bob@example.com');
273  
274 Returns the well-known URL for verifying the identifier.
275  
276 =head2 display_name
277  
278 my $name = Net::Nostr::Identifier->display_name('bob@example.com'); # "bob@example.com"
279 my $root = Net::Nostr::Identifier->display_name('_@bob.com'); # "bob.com"
280  
281 Returns the display form of the identifier. Root identifiers (C<_@domain>)
282 are displayed as just the domain. All other identifiers are returned as-is.
283  
284 =head2 verify_response
285  
286 my $ok = Net::Nostr::Identifier->verify_response($hashref, $local_part, $pubkey);
287  
288 Returns true if the response hashref maps the given local-part to the given
289 pubkey. Keys must be 64-character lowercase hex (32-byte public keys).
290  
291 =head2 extract_relays
292  
293 my $relays = Net::Nostr::Identifier->extract_relays($hashref, $pubkey);
294  
295 Returns an arrayref of relay URLs for the given pubkey, or an empty arrayref
296 if none are found.
297  
298 =head1 CONSTRUCTOR
299  
300 =head2 new
301  
302 Accepts named arguments as either a flat list or a single hash reference.
303  
304 my $ident = Net::Nostr::Identifier->new(%args);
305  
306 Croaks on unknown arguments.
307  
308 =over 4
309  
310 =item base_url
311  
312 Optional base URL override for testing. When set, HTTP requests go to
313 this URL instead of C<https://E<lt>domainE<gt>>.
314  
315 =back
316  
317 =head1 INSTANCE METHODS
318  
319 =head2 verify
320  
321 $ident->verify(
322 identifier => 'bob@example.com',
323 pubkey => $hex_pubkey,
324 on_success => sub { my ($relays) = @_ },
325 on_failure => sub { my ($reason) = @_ },
326 );
327  
328 Asynchronously fetches the well-known URL and verifies that the identifier
329 maps to the given pubkey. HTTP redirects are ignored per the spec. Requires
330 an L<AnyEvent> event loop to be running. Croaks if any required argument is
331 missing or if C<on_success>/C<on_failure> are not CODE refs.
332  
333 The C<on_success> callback receives an arrayref of relay URLs (may be empty).
334 The C<on_failure> callback receives an error reason string.
335  
336 =head2 lookup
337  
338 $ident->lookup(
339 identifier => 'bob@example.com',
340 on_success => sub { my ($pubkey, $relays) = @_ },
341 on_failure => sub { my ($reason) = @_ },
342 );
343  
344 Asynchronously fetches the well-known URL and returns the pubkey associated
345 with the identifier. HTTP redirects are ignored per the spec. Requires
346 an L<AnyEvent> event loop to be running. Croaks if any required argument is
347 missing or if C<on_success>/C<on_failure> are not CODE refs.
348  
349 The C<on_success> callback receives the hex pubkey and an arrayref of relay
350 URLs. The C<on_failure> callback receives an error reason string.
351  
352 =head1 SECURITY
353  
354 The C</.well-known/nostr.json> endpoint B<MUST NOT> return any HTTP redirects.
355 This module ignores all HTTP redirects, treating them as verification failures.
356  
357 =head1 SEE ALSO
358  
359 L<NIP-05|https://github.com/nostr-protocol/nips/blob/master/05.md>
360  
361 =cut
« Nostr/Bech32.pm ↑ Summary Nostr/_ConstructorArgs.pm »