| 1 | package Net::Nostr::GiftWrap; | ||
| 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 | use Net::Nostr::Key; | ||
| 11 | use Net::Nostr::Encryption; | ||
| 12 | |||
| 13 | my $TWO_DAYS = 2 * 24 * 60 * 60; | ||
| 14 | my $HEX64 = qr/\A[0-9a-f]{64}\z/; | ||
| 15 | |||
| 16 | sub _random_timestamp { | ||
| 17 | return int(time() - rand($TWO_DAYS)); | ||
| 18 | } | ||
| 19 | |||
| 20 | sub _rumor_hash { | ||
| 21 | my ($rumor) = @_; | ||
| 22 | my %h; | ||
| 23 | for my $field (qw(id pubkey created_at kind tags content)) { | ||
| 24 | $h{$field} = $rumor->$field if defined $rumor->$field; | ||
| 25 | } | ||
| 26 | return \%h; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub create_rumor { | ||
| 30 | my $class = shift; | ||
| 31 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 32 | delete $args{sig}; # rumors are unsigned | ||
| 33 | return Net::Nostr::Event->new(%args); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub seal { | ||
| 37 | my $class = shift; | ||
| 38 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 39 | my $rumor = $args{rumor} // croak "rumor required"; | ||
| 40 | my $sender_key = $args{sender_key} // croak "sender_key required"; | ||
| 41 | my $recipient_pubkey = $args{recipient_pubkey} // croak "recipient_pubkey required"; | ||
| 42 | croak "recipient_pubkey must be 64-char lowercase hex" unless $recipient_pubkey =~ $HEX64; | ||
| 43 | my $created_at = $args{created_at} // _random_timestamp(); | ||
| 44 | my $expiration = $args{expiration}; | ||
| 45 | |||
| 46 | my $rumor_json = JSON->new->utf8->canonical->encode(_rumor_hash($rumor)); | ||
| 47 | |||
| 48 | my $conv_key = Net::Nostr::Encryption->get_conversation_key( | ||
| 49 | $sender_key->privkey_hex, $recipient_pubkey, | ||
| 50 | ); | ||
| 51 | my $encrypted = Net::Nostr::Encryption->encrypt($rumor_json, $conv_key); | ||
| 52 | |||
| 53 | my @tags; | ||
| 54 | push @tags, ['expiration', "$expiration"] if defined $expiration; | ||
| 55 | |||
| 56 | return $sender_key->create_event( | ||
| 57 | kind => 13, | ||
| 58 | content => $encrypted, | ||
| 59 | tags => \@tags, | ||
| 60 | created_at => $created_at, | ||
| 61 | ); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub wrap { | ||
| 65 | my $class = shift; | ||
| 66 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 67 | my $seal = $args{seal} // croak "seal required"; | ||
| 68 | my $recipient_pubkey = $args{recipient_pubkey} // croak "recipient_pubkey required"; | ||
| 69 | croak "recipient_pubkey must be 64-char lowercase hex" unless $recipient_pubkey =~ $HEX64; | ||
| 70 | my $wrapper_key = $args{wrapper_key} // Net::Nostr::Key->new; | ||
| 71 | my $created_at = $args{created_at} // _random_timestamp(); | ||
| 72 | my $expiration = $args{expiration}; | ||
| 73 | my $kind = $args{kind} // 1059; | ||
| 74 | croak "gift wrap kind must be 1059 or 21059" unless $kind == 1059 || $kind == 21059; | ||
| 75 | |||
| 76 | my $seal_json = JSON->new->utf8->canonical->encode($seal->to_hash); | ||
| 77 | |||
| 78 | my $conv_key = Net::Nostr::Encryption->get_conversation_key( | ||
| 79 | $wrapper_key->privkey_hex, $recipient_pubkey, | ||
| 80 | ); | ||
| 81 | my $encrypted = Net::Nostr::Encryption->encrypt($seal_json, $conv_key); | ||
| 82 | |||
| 83 | my @tags = (['p', $recipient_pubkey]); | ||
| 84 | push @tags, ['expiration', "$expiration"] if defined $expiration; | ||
| 85 | |||
| 86 | return $wrapper_key->create_event( | ||
| 87 | kind => $kind, | ||
| 88 | content => $encrypted, | ||
| 89 | tags => \@tags, | ||
| 90 | created_at => $created_at, | ||
| 91 | ); | ||
| 92 | } | ||
| 93 | |||
| 94 | sub seal_and_wrap { | ||
| 95 | my $class = shift; | ||
| 96 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 97 | my $rumor = $args{rumor} // croak "rumor required"; | ||
| 98 | my $sender_key = $args{sender_key} // croak "sender_key required"; | ||
| 99 | my $recipient_pubkey = $args{recipient_pubkey} // croak "recipient_pubkey required"; | ||
| 100 | |||
| 101 | my $seal = $class->seal( | ||
| 102 | rumor => $rumor, | ||
| 103 | sender_key => $sender_key, | ||
| 104 | recipient_pubkey => $recipient_pubkey, | ||
| 105 | (defined $args{seal_expiration} ? (expiration => $args{seal_expiration}) : ()), | ||
| 106 | ); | ||
| 107 | |||
| 108 | return $class->wrap( | ||
| 109 | seal => $seal, | ||
| 110 | recipient_pubkey => $recipient_pubkey, | ||
| 111 | (defined $args{kind} ? (kind => $args{kind}) : ()), | ||
| 112 | (defined $args{expiration} ? (expiration => $args{expiration}) : ()), | ||
| 113 | ); | ||
| 114 | } | ||
| 115 | |||
| 116 | sub unwrap { | ||
| 117 | my $class = shift; | ||
| 118 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 119 | my $event = $args{event} // croak "event required"; | ||
| 120 | my $recipient_key = $args{recipient_key} // croak "recipient_key required"; | ||
| 121 | |||
| 122 | croak "event must be kind 1059 or 21059" | ||
| 123 | unless $event->kind == 1059 || $event->kind == 21059; | ||
| 124 | |||
| 125 | # Decrypt the seal | ||
| 126 | my $conv_key1 = Net::Nostr::Encryption->get_conversation_key( | ||
| 127 | $recipient_key->privkey_hex, $event->pubkey, | ||
| 128 | ); | ||
| 129 | my $seal_json = Net::Nostr::Encryption->decrypt($event->content, $conv_key1); | ||
| 130 | my $seal_data = JSON::decode_json($seal_json); | ||
| 131 | |||
| 132 | croak "seal must be kind 13" unless $seal_data->{kind} == 13; | ||
| 133 | |||
| 134 | # Decrypt the rumor | ||
| 135 | my $conv_key2 = Net::Nostr::Encryption->get_conversation_key( | ||
| 136 | $recipient_key->privkey_hex, $seal_data->{pubkey}, | ||
| 137 | ); | ||
| 138 | my $rumor_json = Net::Nostr::Encryption->decrypt($seal_data->{content}, $conv_key2); | ||
| 139 | my $rumor_data = JSON::decode_json($rumor_json); | ||
| 140 | |||
| 141 | my $rumor = Net::Nostr::Event->new( | ||
| 142 | id => $rumor_data->{id}, | ||
| 143 | pubkey => $rumor_data->{pubkey}, | ||
| 144 | created_at => $rumor_data->{created_at}, | ||
| 145 | kind => $rumor_data->{kind}, | ||
| 146 | tags => $rumor_data->{tags} // [], | ||
| 147 | content => $rumor_data->{content}, | ||
| 148 | ); | ||
| 149 | |||
| 150 | return ($rumor, $seal_data->{pubkey}); | ||
| 151 | } | ||
| 152 | |||
| 153 | 1; | ||
| 154 | |||
| 155 | __END__ | ||