DirectMessage.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Blossom.pm ↑ Summary GiftWrap.pm »
1 package Net::Nostr::DirectMessage;
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 2 ▶use Net::Nostr::_URI qw(validate_relay_url);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use Net::Nostr::Event;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use Net::Nostr::GiftWrap;
11  
12 my $HEX64 = qr/\A[0-9a-f]{64}\z/;
13  
14 sub _build_recipient_tags {
15 my ($recipients) = @_;
16  
17 croak "recipients must contain at least one recipient"
18 unless ref $recipients eq 'ARRAY' && @$recipients;
19  
20 my @tags;
21 for my $r (@$recipients) {
22 my $pk = ref $r eq 'ARRAY' ? $r->[0] : $r;
23 croak "recipient pubkey must be 64-char lowercase hex" unless $pk =~ $HEX64;
24 if (ref $r eq 'ARRAY') {
25 push @tags, ['p', @$r];
26 } else {
27 push @tags, ['p', $r];
28 }
29 }
30  
31 return @tags;
32 }
33  
34 sub _validate_sha256_hex {
35 my ($name, $value) = @_;
36  
37 croak "$name must be 64-char lowercase hex" unless $value =~ $HEX64;
38 }
39  
40 sub _validate_relay_uri {
41 my ($relay) = @_;
42  
43 validate_relay_url($relay, label => 'relay URI');
44 }
45  
46 sub create {
47 my $class = shift;
48 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
49 my $sender_pubkey = $args{sender_pubkey} // croak "sender_pubkey required";
50 my $content = $args{content} // croak "content required";
51 my $recipients = $args{recipients} // croak "recipients required";
52 my $reply_to = $args{reply_to};
53 my $subject = $args{subject};
54 my $quotes = $args{quotes};
55  
56 my @tags = _build_recipient_tags($recipients);
57 if (defined $reply_to) {
58 my $eid = ref $reply_to eq 'ARRAY' ? $reply_to->[0] : $reply_to;
59 croak "reply_to must be 64-char lowercase hex" unless $eid =~ $HEX64;
60 if (ref $reply_to eq 'ARRAY') {
61 push @tags, ['e', @$reply_to];
62 } else {
63 push @tags, ['e', $reply_to];
64 }
65 }
66 push @tags, ['subject', $subject] if defined $subject;
67 if ($quotes) {
68 for my $q (@$quotes) {
69 croak "quote event_id must be 64-char lowercase hex" unless $q->[0] =~ $HEX64;
70 push @tags, ['q', @$q];
71 }
72 }
73  
74 return Net::Nostr::GiftWrap->create_rumor(
75 pubkey => $sender_pubkey,
76 kind => 14,
77 content => $content,
78 tags => \@tags,
79 );
80 }
81  
82 sub create_file {
83 my $class = shift;
84 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
85 my $sender_pubkey = $args{sender_pubkey} // croak "sender_pubkey required";
86 my $content = $args{content} // croak "content required";
87 my $recipients = $args{recipients} // croak "recipients required";
88 my $file_type = $args{file_type} // croak "file_type required";
89 my $encryption_algorithm = $args{encryption_algorithm} // croak "encryption_algorithm required";
90 my $decryption_key = $args{decryption_key} // croak "decryption_key required";
91 my $decryption_nonce = $args{decryption_nonce} // croak "decryption_nonce required";
92 my $x = $args{x} // croak "x required";
93  
94 croak "encryption_algorithm must be aes-gcm"
95 unless $encryption_algorithm eq 'aes-gcm';
96 _validate_sha256_hex('x', $x);
97 _validate_sha256_hex('ox', $args{ox}) if defined $args{ox};
98  
99 my @tags = _build_recipient_tags($recipients);
100 if (defined $args{reply_to}) {
101 my $eid = ref $args{reply_to} eq 'ARRAY' ? $args{reply_to}[0] : $args{reply_to};
102 croak "reply_to must be 64-char lowercase hex" unless $eid =~ $HEX64;
103 if (ref $args{reply_to} eq 'ARRAY') {
104 push @tags, ['e', @{$args{reply_to}}];
105 } else {
106 push @tags, ['e', $args{reply_to}];
107 }
108 }
109 push @tags, ['subject', $args{subject}] if defined $args{subject};
110 push @tags, ['file-type', $file_type];
111 push @tags, ['encryption-algorithm', $encryption_algorithm];
112 push @tags, ['decryption-key', $decryption_key];
113 push @tags, ['decryption-nonce', $decryption_nonce];
114 push @tags, ['x', $x];
115 push @tags, ['ox', $args{ox}] if defined $args{ox};
116 push @tags, ['size', $args{size}] if defined $args{size};
117 push @tags, ['dim', $args{dim}] if defined $args{dim};
118 push @tags, ['thumbhash', $args{thumbhash}] if defined $args{thumbhash};
119 push @tags, ['blurhash', $args{blurhash}] if defined $args{blurhash};
120 push @tags, ['thumb', $args{thumb}] if defined $args{thumb};
121 if ($args{fallback}) {
122 for my $fb (@{$args{fallback}}) {
123 push @tags, ['fallback', $fb];
124 }
125 }
126  
127 return Net::Nostr::GiftWrap->create_rumor(
128 pubkey => $sender_pubkey,
129 kind => 15,
130 content => $content,
131 tags => \@tags,
132 );
133 }
134  
135 sub create_relay_list {
136 my $class = shift;
137 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
138 my $pubkey = $args{pubkey} // croak "pubkey required";
139 my $relays = $args{relays} // croak "relays required";
140  
141 croak "relays must contain at least one relay URI"
142 unless ref $relays eq 'ARRAY' && @$relays;
143  
144 for my $relay (@$relays) {
145 _validate_relay_uri($relay);
146 }
147  
148 my @tags = map { ['relay', $_] } @$relays;
149  
150 return Net::Nostr::Event->new(
151 pubkey => $pubkey,
152 kind => 10050,
153 content => '',
154 tags => \@tags,
155 );
156 }
157  
158 sub wrap_for_recipients {
159 my $class = shift;
160 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
161 my $rumor = $args{rumor} // croak "rumor required";
162 my $sender_key = $args{sender_key} // croak "sender_key required";
163 my $expiration = $args{expiration};
164 my $skip_sender = $args{skip_sender};
165  
166 my @recipients = map { $_->[1] } grep { $_->[0] eq 'p' } @{$rumor->tags};
167  
168 my @wraps;
169 for my $recipient_pubkey (@recipients) {
170 push @wraps, Net::Nostr::GiftWrap->seal_and_wrap(
171 rumor => $rumor,
172 sender_key => $sender_key,
173 recipient_pubkey => $recipient_pubkey,
174 (defined $expiration ? (expiration => $expiration, seal_expiration => $expiration) : ()),
175 );
176 }
177  
178 unless ($skip_sender) {
179 push @wraps, Net::Nostr::GiftWrap->seal_and_wrap(
180 rumor => $rumor,
181 sender_key => $sender_key,
182 recipient_pubkey => $sender_key->pubkey_hex,
183 (defined $expiration ? (expiration => $expiration, seal_expiration => $expiration) : ()),
184 );
185 }
186  
187 return @wraps;
188 }
189  
190 sub receive {
191 my $class = shift;
192 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
193 my $event = $args{event} // croak "event required";
194 my $recipient_key = $args{recipient_key} // croak "recipient_key required";
195  
196 my ($rumor, $sender_pubkey) = Net::Nostr::GiftWrap->unwrap(
197 event => $event,
198 recipient_key => $recipient_key,
199 );
200  
201 croak "sender pubkey mismatch: seal pubkey does not match rumor pubkey"
202 unless $sender_pubkey eq $rumor->pubkey;
203  
204 return $rumor;
205 }
206  
207 sub chat_members {
208 my ($class, $event) = @_;
209 my @members = ($event->pubkey);
210 for my $tag (@{$event->tags}) {
211 push @members, $tag->[1] if $tag->[0] eq 'p';
212 }
213 return @members;
214 }
215  
216 1;
217  
218 __END__
« Blossom.pm ↑ Summary GiftWrap.pm »