PaymentTargets.pm untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 183 CC 16 SCAR 29.4
CC
16
Cov
0%
from_event
42.0
new
30.0
_validate_target
20.0
CRAP 19.0
1 package Net::Nostr::PaymentTargets;
2  
3 use strictures 2;
4 use Net::Nostr::_ConstructorArgs ();
5 use Net::Nostr::Event;
6 use Carp qw(croak);
7 use Scalar::Util qw(blessed);
8 use URI::Escape ();
9 use Class::Tiny qw(_tags _content);
10  
11 sub _validate_target {
12 my ($target) = @_;
13 croak 'target must contain exactly a type and an address'
14 unless ref($target) eq 'ARRAY' && @$target == 2;
15 my ($type, $address) = @$target;
16 croak 'type must be a lowercase payment type'
17 unless defined($type) && !ref($type) && $type =~ /\A[a-z][a-z0-9.-]*\z/;
18 croak 'address must be a non-empty string without control characters'
19 unless defined($address) && !ref($address) && length($address) && $address !~ /[[:cntrl:]]/;
20 }
21  
22 sub new {
23 my $class = shift;
24 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
25 my @unknown = grep { $_ ne 'targets' && $_ ne 'content' } keys %args;
26 croak 'unknown argument(s): ' . join(', ', sort @unknown) if @unknown;
27 croak 'targets must be an array reference' unless ref($args{targets}) eq 'ARRAY';
28 $args{content} = '' unless exists $args{content};
29 croak 'content must be a string' unless defined($args{content}) && !ref($args{content});
30 _validate_target($_) for @{$args{targets}};
31 return bless { _tags => [map { ['payto', @$_] } @{$args{targets}}], _content => $args{content} }, $class;
32 }
33  
34 sub targets {
35 my ($self, @args) = @_;
36 croak 'targets is read-only' if @args;
37 return [map { [@$_[1,2]] } grep { $_->[0] eq 'payto' } @{$self->_tags}];
38 }
39  
40 sub to_event {
41 my $self = shift;
42 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
43 croak 'kind, content, and tags are supplied by the payment targets'
44 if grep { exists $args{$_} } qw(kind content tags);
45 return Net::Nostr::Event->new(%args, kind=>10133, content=>$self->_content,
46 tags=>[map { [@$_] } @{$self->_tags}]);
47 }
48  
49 sub from_event {
50 my ($class, $event) = @_;
51 croak 'event must be a Net::Nostr::Event'
52 unless blessed($event) && $event->isa('Net::Nostr::Event');
53 croak 'payment target event must be kind 10133' unless $event->kind == 10133;
54 # Recheck basic formats in case a caller mutated the Event's accessors.
55 my $checked = Net::Nostr::Event->new(%{$event->to_hash});
56 my @targets;
57 for my $tag (@{$checked->tags}) {
58 croak 'target event tags must have a name' unless @$tag;
59 next unless $tag->[0] eq 'payto';
60 croak 'payto target tag must have exactly three fields' unless @$tag == 3;
61 push @targets, [@$tag[1,2]];
62 }
63 my $self = $class->new(targets=>\@targets, content=>$checked->content);
64 $self->_tags([map { [@$_] } @{$checked->tags}]);
65 return $self;
66 }
67  
68 sub uris {
69 my ($self) = @_;
70 return [map {
71 my ($type, $address) = @$_;
72 my $encoded = URI::Escape::uri_escape_utf8($address);
73 ($type eq 'bitcoin' || $type eq 'ethereum')
74 ? "$type:$encoded" : "payto://$type/$encoded";
75 } @{$self->targets}];
76 }
77  
78 1;
79  
80 __END__