Key.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
« GiftWrap.pm ↑ Summary List.pm »
1 package Net::Nostr::Key;
2  
3 7 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 ▶use Net::Nostr::_ConstructorArgs ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Crypt::PK::ECC;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use Crypt::PK::ECC::Schnorr;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use Digest::SHA qw(sha256_hex);
11 use Net::Nostr::Event;
12  
13 use Class::Tiny qw(_cryptpkecc);
14  
15 sub new {
16 my $class = shift;
17 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
18 my %known_args = map { $_ => 1 } qw(privkey pubkey);
19 my @unknown = grep { !exists $known_args{$_} } keys %args;
20 croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown;
21 croak "privkey and pubkey are mutually exclusive"
22 if defined $args{privkey} && defined $args{pubkey};
23 my $self = bless {}, $class;
24 my @key_arg = defined $args{privkey} ? ($args{privkey})
25 : defined $args{pubkey} ? ($args{pubkey})
26 : ();
27 $self->_cryptpkecc(Crypt::PK::ECC->new(@key_arg));
28 $self->_cryptpkecc->generate_key('secp256k1') unless $self->pubkey_loaded;
29 return $self;
30 }
31  
32 sub constructor_keys { qw(privkey pubkey) }
33  
34 sub from_mnemonic {
35 my $class = shift;
36 my $mnemonic = shift;
37 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
38 croak "from_mnemonic requires a mnemonic" unless defined $mnemonic;
39  
40 require Bitcoin::Crypto::Key::ExtPrivate;
41 require Bitcoin::BIP39;
42  
43 Bitcoin::BIP39::bip39_mnemonic_to_entropy(mnemonic => $mnemonic);
44  
45 my $account = $args{account} // 0;
46 my $path = "m/44'/1237'/${account}'/0/0";
47  
48 my $seed = Bitcoin::Crypto::Key::ExtPrivate->from_mnemonic($mnemonic);
49 my $derived = $seed->derive_key($path);
50 my $privkey_bytes = $derived->raw_key("private");
51  
52 my $pk = Crypt::PK::ECC->new;
53 $pk->import_key_raw($privkey_bytes, 'secp256k1');
54  
55 my $self = bless {}, $class;
56 $self->_cryptpkecc($pk);
57 return $self;
58 }
59  
60 sub generate_mnemonic {
61 my $class = shift;
62 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
63 require Bitcoin::BIP39;
64 my $bits = $args{bits} // 128;
65 my $result = Bitcoin::BIP39::gen_bip39_mnemonic(bits => $bits);
66 return ref $result eq 'HASH' ? $result->{mnemonic} : $result;
67 }
68  
69 sub schnorr_sign {
70 my ($self, $msg) = @_;
71 my $sig = Crypt::PK::ECC::Schnorr->new(\$self->privkey_der)->sign_message($msg);
72 return $sig;
73 }
74  
75 sub privkey_loaded {
76 my ($self) = @_;
77 my $is_private = $self->_cryptpkecc->is_private;
78 return 1 if $is_private;
79 return 0;
80 }
81  
82 sub pubkey_loaded {
83 my ($self) = @_;
84 my $is_private = $self->_cryptpkecc->is_private;
85 return 1 if defined $is_private;
86 return 0;
87 }
88  
89 sub pubkey_der {
90 my ($self) = @_;
91 my $der = $self->_cryptpkecc->export_key_der('public');
92 return $der;
93 }
94  
95 sub privkey_der {
96 my ($self) = @_;
97 my $der = $self->_cryptpkecc->export_key_der('private');
98 return $der;
99 }
100  
101 sub pubkey_pem {
102 my ($self) = @_;
103 my $pem = $self->_cryptpkecc->export_key_pem('public');
104 return $pem;
105 }
106  
107 sub privkey_pem {
108 my ($self) = @_;
109 my $pem = $self->_cryptpkecc->export_key_pem('private');
110 return $pem;
111 }
112  
113 sub pubkey_raw {
114 my ($self) = @_;
115 my $raw = $self->_cryptpkecc->export_key_raw('public');
116 return $raw;
117 }
118  
119 sub privkey_raw {
120 my ($self) = @_;
121 my $raw = $self->_cryptpkecc->export_key_raw('private');
122 return $raw;
123 }
124  
125 sub pubkey_hex {
126 my ($self) = @_;
127 my $raw = $self->pubkey_raw;
128 my $x = substr($raw, 1, 32); # skip 04 prefix, take x-only (BIP-340)
129 return unpack 'H*', $x;
130 }
131  
132 sub privkey_hex {
133 my ($self) = @_;
134 my $hex = unpack 'H*', $self->privkey_raw;
135 return $hex;
136 }
137  
138 sub pubkey_npub {
139 my ($self) = @_;
140 require Net::Nostr::Bech32;
141 return Net::Nostr::Bech32::encode_npub($self->pubkey_hex);
142 }
143  
144 sub privkey_nsec {
145 my ($self) = @_;
146 require Net::Nostr::Bech32;
147 return Net::Nostr::Bech32::encode_nsec($self->privkey_hex);
148 }
149  
150 sub save_privkey {
151 my ($self, $path) = @_;
152 croak "no private key loaded" unless $self->privkey_loaded;
153 require Fcntl;
154 sysopen my $fh, $path, Fcntl::O_WRONLY() | Fcntl::O_CREAT() | Fcntl::O_TRUNC(), 0600
155 or croak "cannot open $path: $!";
156 binmode $fh;
157 print $fh $self->privkey_pem;
158 close $fh;
159 }
160  
161 sub save_pubkey {
162 my ($self, $path) = @_;
163 open my $fh, '>', $path or croak "cannot open $path: $!";
164 binmode $fh;
165 print $fh $self->pubkey_pem;
166 close $fh;
167 }
168  
169 sub sign_event {
170 my ($self, $event) = @_;
171 croak "pubkey does not match signing key"
172 unless $event->pubkey eq $self->pubkey_hex;
173 my $expected_id = sha256_hex($event->json_serialize);
174 croak "id does not match event body"
175 unless $event->id eq $expected_id;
176 # NIP-01: the signature is a BIP-340 Schnorr signature over the 32 raw bytes
177 # of the event id, not over its hex-string representation.
178 my $sig_raw = $self->schnorr_sign(pack 'H*', $event->id);
179 my $sig_hex = unpack 'H*', $sig_raw;
180 $event->sig($sig_hex);
181 return $sig_hex;
182 }
183  
184 sub create_event {
185 my $self = shift;
186 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
187 $args{pubkey} = $self->pubkey_hex;
188 my $event = Net::Nostr::Event->new(%args);
189 $self->sign_event($event);
190 return $event;
191 }
192  
193 1;
194  
195 __END__
« GiftWrap.pm ↑ Summary List.pm »