| 1 | package Net::Nostr::Wallet; | ||
| 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 | 0 | ▶ | use Net::Nostr::Event; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | |||
| 11 | use Class::Tiny qw( | ||
| 12 | privkey | ||
| 13 | mints | ||
| 14 | mint | ||
| 15 | unit | ||
| 16 | proofs | ||
| 17 | del | ||
| 18 | direction | ||
| 19 | amount | ||
| 20 | e_tags | ||
| 21 | redeemed_ids | ||
| 22 | mint_url | ||
| 23 | expiration | ||
| 24 | ); | ||
| 25 | |||
| 26 | my $json = JSON->new->utf8->canonical; | ||
| 27 | my $HEX64 = qr/\A[0-9a-f]{64}\z/; | ||
| 28 | |||
| 29 | sub new { | ||
| 30 | my $class = shift; | ||
| 31 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 32 | $args{mints} //= []; | ||
| 33 | $args{proofs} //= []; | ||
| 34 | $args{del} //= []; | ||
| 35 | $args{e_tags} //= []; | ||
| 36 | $args{redeemed_ids} //= []; | ||
| 37 | my $self = bless { %args }, $class; | ||
| 38 | my %known; @known{Class::Tiny->get_all_attributes_for($class)} = (); | ||
| 39 | my @unknown = grep { !exists $known{$_} } keys %$self; | ||
| 40 | croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown; | ||
| 41 | return $self; | ||
| 42 | } | ||
| 43 | |||
| 44 | # === Event creation === | ||
| 45 | |||
| 46 | sub wallet_event { | ||
| 47 | my $class = shift; | ||
| 48 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 49 | |||
| 50 | my $pubkey = $args{pubkey} // croak "wallet_event requires 'pubkey'"; | ||
| 51 | my $content = $args{content} // croak "wallet_event requires 'content'"; | ||
| 52 | |||
| 53 | delete @args{qw(content)}; | ||
| 54 | return Net::Nostr::Event->new(%args, kind => 17375, content => $content, tags => []); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub token_event { | ||
| 58 | my $class = shift; | ||
| 59 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 60 | |||
| 61 | my $pubkey = $args{pubkey} // croak "token_event requires 'pubkey'"; | ||
| 62 | my $content = $args{content} // croak "token_event requires 'content'"; | ||
| 63 | |||
| 64 | delete @args{qw(content)}; | ||
| 65 | return Net::Nostr::Event->new(%args, kind => 7375, content => $content, tags => []); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub history_event { | ||
| 69 | my $class = shift; | ||
| 70 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 71 | |||
| 72 | my $pubkey = $args{pubkey} // croak "history_event requires 'pubkey'"; | ||
| 73 | my $content = $args{content} // croak "history_event requires 'content'"; | ||
| 74 | my $redeemed_ids = $args{redeemed_ids} // []; | ||
| 75 | |||
| 76 | my @tags; | ||
| 77 | for my $entry (@$redeemed_ids) { | ||
| 78 | croak "redeemed event_id must be 64-char lowercase hex" unless $entry->[0] =~ $HEX64; | ||
| 79 | push @tags, ['e', $entry->[0], $entry->[1] // '', 'redeemed']; | ||
| 80 | } | ||
| 81 | |||
| 82 | delete @args{qw(content redeemed_ids)}; | ||
| 83 | return Net::Nostr::Event->new(%args, kind => 7376, content => $content, tags => \@tags); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub quote_event { | ||
| 87 | my $class = shift; | ||
| 88 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 89 | |||
| 90 | my $pubkey = $args{pubkey} // croak "quote_event requires 'pubkey'"; | ||
| 91 | my $content = $args{content} // croak "quote_event requires 'content'"; | ||
| 92 | my $mint_url = $args{mint_url} // croak "quote_event requires 'mint_url'"; | ||
| 93 | my $expiration = $args{expiration} // croak "quote_event requires 'expiration'"; | ||
| 94 | |||
| 95 | my @tags = ( | ||
| 96 | ['expiration', '' . $expiration], | ||
| 97 | ['mint', $mint_url], | ||
| 98 | ); | ||
| 99 | |||
| 100 | delete @args{qw(content mint_url expiration)}; | ||
| 101 | return Net::Nostr::Event->new(%args, kind => 7374, content => $content, tags => \@tags); | ||
| 102 | } | ||
| 103 | |||
| 104 | sub delete_token { | ||
| 105 | my $class = shift; | ||
| 106 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 107 | |||
| 108 | my $pubkey = $args{pubkey} // croak "delete_token requires 'pubkey'"; | ||
| 109 | my $event_ids = $args{event_ids} // croak "delete_token requires 'event_ids'"; | ||
| 110 | |||
| 111 | my @tags; | ||
| 112 | for my $id (@$event_ids) { | ||
| 113 | croak "event_id must be 64-char lowercase hex" unless $id =~ $HEX64; | ||
| 114 | push @tags, ['e', $id]; | ||
| 115 | } | ||
| 116 | push @tags, ['k', '7375']; | ||
| 117 | |||
| 118 | delete @args{qw(event_ids)}; | ||
| 119 | return Net::Nostr::Event->new(%args, kind => 5, content => '', tags => \@tags); | ||
| 120 | } | ||
| 121 | |||
| 122 | # === Content builders (return JSON for NIP-44 encryption) === | ||
| 123 | |||
| 124 | sub wallet_content { | ||
| 125 | my $class = shift; | ||
| 126 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 127 | |||
| 128 | my $privkey = $args{privkey} // croak "wallet_content requires 'privkey'"; | ||
| 129 | my $mints = $args{mints} // croak "wallet_content requires 'mints'"; | ||
| 130 | croak "wallet_content requires one or more mints" unless @$mints; | ||
| 131 | |||
| 132 | my @data = (['privkey', $privkey]); | ||
| 133 | for my $url (@$mints) { | ||
| 134 | push @data, ['mint', $url]; | ||
| 135 | } | ||
| 136 | return $json->encode(\@data); | ||
| 137 | } | ||
| 138 | |||
| 139 | sub token_content { | ||
| 140 | my $class = shift; | ||
| 141 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 142 | |||
| 143 | my $mint = $args{mint} // croak "token_content requires 'mint'"; | ||
| 144 | my $proofs = $args{proofs} // croak "token_content requires 'proofs'"; | ||
| 145 | my $unit = $args{unit} // 'sat'; | ||
| 146 | |||
| 147 | my %data = ( | ||
| 148 | mint => $mint, | ||
| 149 | unit => $unit, | ||
| 150 | proofs => $proofs, | ||
| 151 | ); | ||
| 152 | $data{del} = $args{del} if defined $args{del}; | ||
| 153 | |||
| 154 | return $json->encode(\%data); | ||
| 155 | } | ||
| 156 | |||
| 157 | sub history_content { | ||
| 158 | my $class = shift; | ||
| 159 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 160 | |||
| 161 | my $direction = $args{direction} // croak "history_content requires 'direction'"; | ||
| 162 | my $amount = $args{amount} // croak "history_content requires 'amount'"; | ||
| 163 | my $unit = $args{unit} // 'sat'; | ||
| 164 | my $e_tags = $args{e_tags} // croak "history_content requires 'e_tags'"; | ||
| 165 | |||
| 166 | my @data = ( | ||
| 167 | ['direction', $direction], | ||
| 168 | ['amount', '' . $amount], | ||
| 169 | ['unit', $unit], | ||
| 170 | ); | ||
| 171 | for my $e (@$e_tags) { | ||
| 172 | push @data, ['e', @$e]; | ||
| 173 | } | ||
| 174 | return $json->encode(\@data); | ||
| 175 | } | ||
| 176 | |||
| 177 | # === Content parsers (accept decrypted JSON) === | ||
| 178 | |||
| 179 | sub parse_wallet_content { | ||
| 180 | my ($class, $plaintext) = @_; | ||
| 181 | my $data = $json->decode($plaintext); | ||
| 182 | |||
| 183 | my ($privkey, @mints); | ||
| 184 | for my $tag (@$data) { | ||
| 185 | if ($tag->[0] eq 'privkey') { | ||
| 186 | $privkey = $tag->[1]; | ||
| 187 | } elsif ($tag->[0] eq 'mint') { | ||
| 188 | push @mints, $tag->[1]; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | return $class->new( | ||
| 193 | privkey => $privkey, | ||
| 194 | mints => \@mints, | ||
| 195 | ); | ||
| 196 | } | ||
| 197 | |||
| 198 | sub parse_token_content { | ||
| 199 | my ($class, $plaintext) = @_; | ||
| 200 | my $data = $json->decode($plaintext); | ||
| 201 | |||
| 202 | return $class->new( | ||
| 203 | mint => $data->{mint}, | ||
| 204 | unit => $data->{unit} // 'sat', | ||
| 205 | proofs => $data->{proofs} // [], | ||
| 206 | del => $data->{del} // [], | ||
| 207 | ); | ||
| 208 | } | ||
| 209 | |||
| 210 | sub parse_history_content { | ||
| 211 | my ($class, $plaintext) = @_; | ||
| 212 | my $data = $json->decode($plaintext); | ||
| 213 | |||
| 214 | my ($direction, $amount, $unit, @e_tags); | ||
| 215 | for my $tag (@$data) { | ||
| 216 | if ($tag->[0] eq 'direction') { | ||
| 217 | $direction = $tag->[1]; | ||
| 218 | } elsif ($tag->[0] eq 'amount') { | ||
| 219 | $amount = $tag->[1]; | ||
| 220 | } elsif ($tag->[0] eq 'unit') { | ||
| 221 | $unit = $tag->[1]; | ||
| 222 | } elsif ($tag->[0] eq 'e') { | ||
| 223 | push @e_tags, [@{$tag}[1 .. $#$tag]]; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | return $class->new( | ||
| 228 | direction => $direction, | ||
| 229 | amount => $amount, | ||
| 230 | unit => $unit // 'sat', | ||
| 231 | e_tags => \@e_tags, | ||
| 232 | ); | ||
| 233 | } | ||
| 234 | |||
| 235 | # === Event parsing === | ||
| 236 | |||
| 237 | sub from_event { | ||
| 238 | my ($class, $event) = @_; | ||
| 239 | my $kind = $event->kind; | ||
| 240 | |||
| 241 | if ($kind == 17375) { | ||
| 242 | return $class->new; | ||
| 243 | } elsif ($kind == 7375) { | ||
| 244 | return $class->new; | ||
| 245 | } elsif ($kind == 7376) { | ||
| 246 | return $class->_parse_history_event($event); | ||
| 247 | } elsif ($kind == 7374) { | ||
| 248 | return $class->_parse_quote_event($event); | ||
| 249 | } | ||
| 250 | return undef; | ||
| 251 | } | ||
| 252 | |||
| 253 | sub _parse_history_event { | ||
| 254 | my ($class, $event) = @_; | ||
| 255 | my @redeemed_ids; | ||
| 256 | |||
| 257 | for my $tag (@{$event->tags}) { | ||
| 258 | if ($tag->[0] eq 'e' && defined $tag->[3] && $tag->[3] eq 'redeemed') { | ||
| 259 | push @redeemed_ids, $tag->[1]; | ||
| 260 | } | ||
| 261 | } | ||
| 262 | |||
| 263 | return $class->new(redeemed_ids => \@redeemed_ids); | ||
| 264 | } | ||
| 265 | |||
| 266 | sub _parse_quote_event { | ||
| 267 | my ($class, $event) = @_; | ||
| 268 | my ($mint_url, $expiration); | ||
| 269 | |||
| 270 | for my $tag (@{$event->tags}) { | ||
| 271 | if ($tag->[0] eq 'mint') { | ||
| 272 | $mint_url //= $tag->[1]; | ||
| 273 | } elsif ($tag->[0] eq 'expiration') { | ||
| 274 | $expiration //= $tag->[1]; | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | return $class->new( | ||
| 279 | mint_url => $mint_url, | ||
| 280 | expiration => $expiration, | ||
| 281 | ); | ||
| 282 | } | ||
| 283 | |||
| 284 | # === Validation === | ||
| 285 | |||
| 286 | sub validate { | ||
| 287 | my ($class, $event) = @_; | ||
| 288 | my $kind = $event->kind; | ||
| 289 | |||
| 290 | if ($kind == 17375) { | ||
| 291 | return 1; # content is encrypted, can't validate structure | ||
| 292 | } elsif ($kind == 7375) { | ||
| 293 | return 1; # content is encrypted | ||
| 294 | } elsif ($kind == 7376) { | ||
| 295 | return 1; # content is encrypted, public tags optional | ||
| 296 | } elsif ($kind == 7374) { | ||
| 297 | my ($has_exp, $has_mint); | ||
| 298 | for my $tag (@{$event->tags}) { | ||
| 299 | $has_exp = 1 if $tag->[0] eq 'expiration'; | ||
| 300 | $has_mint = 1 if $tag->[0] eq 'mint'; | ||
| 301 | } | ||
| 302 | croak "kind 7374 MUST have an expiration tag" unless $has_exp; | ||
| 303 | croak "kind 7374 MUST have a mint tag" unless $has_mint; | ||
| 304 | return 1; | ||
| 305 | } | ||
| 306 | |||
| 307 | croak "wallet event MUST be kind 17375, 7375, 7376, or 7374"; | ||
| 308 | } | ||
| 309 | |||
| 310 | 1; | ||
| 311 | |||
| 312 | __END__ | ||