Nostr/Marketplace.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
« Nostr/List.pm ↑ Summary Nostr/Relay.pm »
1 package Net::Nostr::Marketplace;
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 Net::Nostr::Event;
Subroutine BEGIN: called CC=1 SCAR=2.6
10  
11 use Class::Tiny qw(
12 stall_id
13 product_id
14 name
15 description
16 currency
17 price
18 quantity
19 images
20 specs
21 shipping
22 categories
23 about
24 ui
25 merchants
26 starting_bid
27 start_date
28 duration
29 amount
30 auction_event_id
31 bid_event_id
32 status
33 message
34 duration_extended
35 checkout_type
36 order_id
37 items
38 shipping_id
39 contact
40 payment_options
41 paid
42 shipped
43 address
44 );
45  
46 my $json = JSON->new->utf8->canonical;
47 my $HEX64 = qr/\A[0-9a-f]{64}\z/;
48  
49 sub new {
50 my $class = shift;
51 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
52 $args{categories} //= [];
53 $args{images} //= [];
54 $args{shipping} //= [];
55 $args{specs} //= [];
56 $args{merchants} //= [];
57 $args{items} //= [];
58 $args{payment_options} //= [];
59 my $self = bless { %args }, $class;
60 my %known; @known{Class::Tiny->get_all_attributes_for($class)} = ();
61 my @unknown = grep { !exists $known{$_} } keys %$self;
62 croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown;
63 return $self;
64 }
65  
66 # === Event creation ===
67  
68 sub stall_event {
69 my $class = shift;
70 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
71  
72 my $pubkey = $args{pubkey} // croak "stall_event requires 'pubkey'";
73 my $id = $args{id} // croak "stall_event requires 'id'";
74 my $name = $args{name} // croak "stall_event requires 'name'";
75 my $currency = $args{currency} // croak "stall_event requires 'currency'";
76 my $shipping = $args{shipping} // croak "stall_event requires 'shipping'";
77  
78 my %content = (
79 id => $id,
80 name => $name,
81 currency => $currency,
82 shipping => _build_stall_shipping($shipping),
83 );
84 $content{description} = $args{description} if defined $args{description};
85  
86 return Net::Nostr::Event->new(
87 pubkey => $pubkey,
88 kind => 30017,
89 content => $json->encode(\%content),
90 tags => [['d', $id]],
91 );
92 }
93  
94 sub _build_stall_shipping {
95 my ($zones) = @_;
96 my @out;
97 for my $z (@$zones) {
98 my %zone = (
99 id => $z->{id},
100 cost => $z->{cost},
101 regions => $z->{regions},
102 );
103 $zone{name} = $z->{name} if defined $z->{name};
104 push @out, \%zone;
105 }
106 return \@out;
107 }
108  
109 sub product_event {
110 my $class = shift;
111 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
112  
113 my $pubkey = $args{pubkey} // croak "product_event requires 'pubkey'";
114 my $id = $args{id} // croak "product_event requires 'id'";
115 my $stall_id = $args{stall_id} // croak "product_event requires 'stall_id'";
116 my $name = $args{name} // croak "product_event requires 'name'";
117 my $currency = $args{currency} // croak "product_event requires 'currency'";
118 croak "product_event requires 'price'" unless exists $args{price};
119  
120 my %content = (
121 id => $id,
122 stall_id => $stall_id,
123 name => $name,
124 currency => $currency,
125 price => $args{price},
126 );
127  
128 if (exists $args{quantity}) {
129 $content{quantity} = $args{quantity};
130 }
131 $content{description} = $args{description} if defined $args{description};
132 $content{images} = $args{images} if defined $args{images};
133 $content{specs} = $args{specs} if defined $args{specs};
134 $content{shipping} = $args{shipping} if defined $args{shipping};
135  
136 my @tags = (['d', $id]);
137 if (defined $args{categories}) {
138 for my $cat (@{$args{categories}}) {
139 push @tags, ['t', $cat];
140 }
141 }
142  
143 return Net::Nostr::Event->new(
144 pubkey => $pubkey,
145 kind => 30018,
146 content => $json->encode(\%content),
147 tags => \@tags,
148 );
149 }
150  
151 sub marketplace_event {
152 my $class = shift;
153 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
154  
155 my $pubkey = $args{pubkey} // croak "marketplace_event requires 'pubkey'";
156 my $id = $args{id} // croak "marketplace_event requires 'id'";
157  
158 my %content;
159 $content{name} = $args{name} if defined $args{name};
160 $content{about} = $args{about} if defined $args{about};
161 $content{ui} = $args{ui} if defined $args{ui};
162 $content{merchants} = $args{merchants} if defined $args{merchants};
163  
164 return Net::Nostr::Event->new(
165 pubkey => $pubkey,
166 kind => 30019,
167 content => $json->encode(\%content),
168 tags => [['d', $id]],
169 );
170 }
171  
172 sub auction_event {
173 my $class = shift;
174 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
175  
176 my $pubkey = $args{pubkey} // croak "auction_event requires 'pubkey'";
177 my $id = $args{id} // croak "auction_event requires 'id'";
178 my $stall_id = $args{stall_id} // croak "auction_event requires 'stall_id'";
179 my $name = $args{name} // croak "auction_event requires 'name'";
180 croak "auction_event requires 'starting_bid'" unless exists $args{starting_bid};
181 croak "auction_event requires 'duration'" unless exists $args{duration};
182  
183 my %content = (
184 id => $id,
185 stall_id => $stall_id,
186 name => $name,
187 starting_bid => $args{starting_bid},
188 duration => $args{duration},
189 );
190  
191 $content{description} = $args{description} if defined $args{description};
192 $content{images} = $args{images} if defined $args{images};
193 $content{start_date} = $args{start_date} if defined $args{start_date};
194 $content{specs} = $args{specs} if defined $args{specs};
195 $content{shipping} = $args{shipping} if defined $args{shipping};
196  
197 return Net::Nostr::Event->new(
198 pubkey => $pubkey,
199 kind => 30020,
200 content => $json->encode(\%content),
201 tags => [['d', $id]],
202 );
203 }
204  
205 sub bid_event {
206 my $class = shift;
207 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
208  
209 my $pubkey = $args{pubkey} // croak "bid_event requires 'pubkey'";
210 croak "bid_event requires 'amount'" unless exists $args{amount};
211 my $auction_event_id = $args{auction_event_id} // croak "bid_event requires 'auction_event_id'";
212 croak "auction_event_id must be 64-char lowercase hex" unless $auction_event_id =~ $HEX64;
213  
214 return Net::Nostr::Event->new(
215 pubkey => $pubkey,
216 kind => 1021,
217 content => '' . $args{amount},
218 tags => [['e', $auction_event_id]],
219 );
220 }
221  
222 sub bid_confirmation_event {
223 my $class = shift;
224 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
225  
226 my $pubkey = $args{pubkey} // croak "bid_confirmation_event requires 'pubkey'";
227 my $bid_event_id = $args{bid_event_id} // croak "bid_confirmation_event requires 'bid_event_id'";
228 my $auction_event_id = $args{auction_event_id} // croak "bid_confirmation_event requires 'auction_event_id'";
229 croak "bid_event_id must be 64-char lowercase hex" unless $bid_event_id =~ $HEX64;
230 croak "auction_event_id must be 64-char lowercase hex" unless $auction_event_id =~ $HEX64;
231 my $status = $args{status} // croak "bid_confirmation_event requires 'status'";
232  
233 my %content = (status => $status);
234 $content{message} = $args{message} if defined $args{message};
235 $content{duration_extended} = $args{duration_extended} if defined $args{duration_extended};
236  
237 return Net::Nostr::Event->new(
238 pubkey => $pubkey,
239 kind => 1022,
240 content => $json->encode(\%content),
241 tags => [['e', $bid_event_id], ['e', $auction_event_id]],
242 );
243 }
244  
245 # === Checkout message builders ===
246  
247 sub order_message {
248 my $class = shift;
249 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
250  
251 my $id = $args{id} // croak "order_message requires 'id'";
252 my $items = $args{items} // croak "order_message requires 'items'";
253 my $shipping_id = $args{shipping_id} // croak "order_message requires 'shipping_id'";
254  
255 my %data = (
256 type => 0,
257 id => $id,
258 items => $items,
259 shipping_id => $shipping_id,
260 );
261 $data{name} = $args{name} if defined $args{name};
262 $data{address} = $args{address} if defined $args{address};
263 $data{message} = $args{message} if defined $args{message};
264 $data{contact} = $args{contact} if defined $args{contact};
265  
266 return $json->encode(\%data);
267 }
268  
269 sub payment_request_message {
270 my $class = shift;
271 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
272  
273 my $id = $args{id} // croak "payment_request_message requires 'id'";
274 my $payment_options = $args{payment_options} // croak "payment_request_message requires 'payment_options'";
275  
276 my %data = (
277 type => 1,
278 id => $id,
279 payment_options => $payment_options,
280 );
281 $data{message} = $args{message} if defined $args{message};
282  
283 return $json->encode(\%data);
284 }
285  
286 sub order_status_message {
287 my $class = shift;
288 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
289  
290 my $id = $args{id} // croak "order_status_message requires 'id'";
291 my $message = $args{message} // croak "order_status_message requires 'message'";
292 croak "order_status_message requires 'paid'" unless exists $args{paid};
293 croak "order_status_message requires 'shipped'" unless exists $args{shipped};
294  
295 return $json->encode({
296 type => 2,
297 id => $id,
298 message => $message,
299 paid => $args{paid},
300 shipped => $args{shipped},
301 });
302 }
303  
304 # === Event parsing ===
305  
306 sub from_event {
307 my ($class, $event) = @_;
308 my $kind = $event->kind;
309  
310 if ($kind == 30017) {
311 return $class->_parse_stall_event($event);
312 } elsif ($kind == 30018) {
313 return $class->_parse_product_event($event);
314 } elsif ($kind == 30019) {
315 return $class->_parse_marketplace_event($event);
316 } elsif ($kind == 30020) {
317 return $class->_parse_auction_event($event);
318 } elsif ($kind == 1021) {
319 return $class->_parse_bid_event($event);
320 } elsif ($kind == 1022) {
321 return $class->_parse_bid_confirmation_event($event);
322 }
323 return undef;
324 }
325  
326 sub _parse_stall_event {
327 my ($class, $event) = @_;
328 my $data = $json->decode($event->content);
329  
330 return $class->new(
331 stall_id => $data->{id},
332 name => $data->{name},
333 description => $data->{description},
334 currency => $data->{currency},
335 shipping => $data->{shipping} // [],
336 );
337 }
338  
339 sub _parse_product_event {
340 my ($class, $event) = @_;
341 my $data = $json->decode($event->content);
342  
343 my @categories;
344 for my $tag (@{$event->tags}) {
345 push @categories, $tag->[1] if $tag->[0] eq 't';
346 }
347  
348 return $class->new(
349 product_id => $data->{id},
350 stall_id => $data->{stall_id},
351 name => $data->{name},
352 description => $data->{description},
353 currency => $data->{currency},
354 price => $data->{price},
355 quantity => $data->{quantity},
356 images => $data->{images} // [],
357 specs => $data->{specs} // [],
358 shipping => $data->{shipping} // [],
359 categories => \@categories,
360 );
361 }
362  
363 sub _parse_marketplace_event {
364 my ($class, $event) = @_;
365 my $data = $json->decode($event->content);
366  
367 return $class->new(
368 name => $data->{name},
369 about => $data->{about},
370 ui => $data->{ui},
371 merchants => $data->{merchants} // [],
372 );
373 }
374  
375 sub _parse_auction_event {
376 my ($class, $event) = @_;
377 my $data = $json->decode($event->content);
378  
379 return $class->new(
380 product_id => $data->{id},
381 stall_id => $data->{stall_id},
382 name => $data->{name},
383 description => $data->{description},
384 starting_bid => $data->{starting_bid},
385 start_date => $data->{start_date},
386 duration => $data->{duration},
387 images => $data->{images} // [],
388 specs => $data->{specs} // [],
389 shipping => $data->{shipping} // [],
390 );
391 }
392  
393 sub _parse_bid_event {
394 my ($class, $event) = @_;
395  
396 my $auction_event_id;
397 for my $tag (@{$event->tags}) {
398 if ($tag->[0] eq 'e') {
399 $auction_event_id = $tag->[1];
400 last;
401 }
402 }
403  
404 return $class->new(
405 amount => 0 + $event->content,
406 auction_event_id => $auction_event_id,
407 );
408 }
409  
410 sub _parse_bid_confirmation_event {
411 my ($class, $event) = @_;
412 my $data = $json->decode($event->content);
413  
414 my ($bid_event_id, $auction_event_id);
415 for my $tag (@{$event->tags}) {
416 if ($tag->[0] eq 'e') {
417 if (!defined $bid_event_id) {
418 $bid_event_id = $tag->[1];
419 } else {
420 $auction_event_id = $tag->[1];
421 }
422 }
423 }
424  
425 return $class->new(
426 status => $data->{status},
427 message => $data->{message},
428 duration_extended => $data->{duration_extended},
429 bid_event_id => $bid_event_id,
430 auction_event_id => $auction_event_id,
431 );
432 }
433  
434 # === Checkout message parsing ===
435  
436 sub parse_checkout_message {
437 my ($class, $msg) = @_;
438 my $data = $json->decode($msg);
439 my $type = $data->{type};
440  
441 if ($type == 0) {
442 return $class->new(
443 checkout_type => 0,
444 order_id => $data->{id},
445 items => $data->{items} // [],
446 shipping_id => $data->{shipping_id},
447 name => $data->{name},
448 address => $data->{address},
449 message => $data->{message},
450 contact => $data->{contact},
451 );
452 } elsif ($type == 1) {
453 return $class->new(
454 checkout_type => 1,
455 order_id => $data->{id},
456 payment_options => $data->{payment_options} // [],
457 message => $data->{message},
458 );
459 } elsif ($type == 2) {
460 return $class->new(
461 checkout_type => 2,
462 order_id => $data->{id},
463 message => $data->{message},
464 paid => $data->{paid},
465 shipped => $data->{shipped},
466 );
467 }
468  
469 croak "unrecognized checkout message type: $type";
470 }
471  
472 # === Validation ===
473  
474 sub validate {
475 my ($class, $event) = @_;
476 my $kind = $event->kind;
477  
478 if ($kind == 30017 || $kind == 30018 || $kind == 30019 || $kind == 30020) {
479 my $has_d;
480 for my $tag (@{$event->tags}) {
481 $has_d = 1 if $tag->[0] eq 'd';
482 }
483 croak "kind $kind requires a d tag" unless $has_d;
484 return 1;
485 } elsif ($kind == 1021) {
486 my $has_e;
487 for my $tag (@{$event->tags}) {
488 $has_e = 1 if $tag->[0] eq 'e';
489 }
490 croak "kind 1021 requires an e tag" unless $has_e;
491 return 1;
492 } elsif ($kind == 1022) {
493 my $e_count = 0;
494 for my $tag (@{$event->tags}) {
495 $e_count++ if $tag->[0] eq 'e';
496 }
497 croak "kind 1022 requires two e tags" unless $e_count >= 2;
498 return 1;
499 }
500  
501 croak "marketplace event must be kind 30017, 30018, 30019, 30020, 1021, or 1022";
502 }
503  
504 1;
505  
506 __END__
« Nostr/List.pm ↑ Summary Nostr/Relay.pm »