Crypto/Network.pm

filter: Statementstmt 100% 65 / 65 Branchbran 62.5% 5 / 8 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 13 / 13 Podpod 100% 8 / 8 totaltotal 90.3% 94 / 104 CC 7 SCAR 6.0
CC
7
Cov
92%
get
3.5
unregister
2.0
register
2.0
CRAP 1.8
« Crypto/Transaction/UTXO.pm ↑ Summary Crypto/Role/Compressed.pm »
1 package Bitcoin::Crypto::Network;
2 65 $Bitcoin::Crypto::Network::VERSION = '4.005';
3 65 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 65 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 65 ▶use Mooish::Base -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 65 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 65 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 65 my %networks;
12 my $default_network;
13 65 my $single_network = 0;
14  
15 65 has param 'id' => (
16 isa => Str,
17 );
18  
19 65 has param 'name' => (
20 isa => Str,
21 );
22  
23 65 has param 'p2pkh_byte' => (
24 coerce => ByteStrLen [1],
25 );
26  
27 65 has param 'wif_byte' => (
28 coerce => ByteStrLen [1],
29 );
30  
31 65 has param 'p2sh_byte' => (
32 coerce => ByteStrLen [1],
33 required => 0,
34 );
35  
36 65 has param 'segwit_hrp' => (
37 isa => Str,
38 required => 0,
39 );
40  
41 65 has param 'extprv_version' => (
42 isa => Int,
43 required => 0,
44 );
45  
46 65 has param 'extpub_version' => (
47 isa => Int,
48 required => 0,
49 );
50  
51 65 has param 'extprv_compat_version' => (
52 isa => Int,
53 required => 0,
54 );
55  
56 65 has param 'extpub_compat_version' => (
57 isa => Int,
58 required => 0,
59 );
60  
61 65 has param 'extprv_segwit_version' => (
62 isa => Int,
63 required => 0,
64 );
65  
66 65 has param 'extpub_segwit_version' => (
67 isa => Int,
68 required => 0,
69 );
70  
71 65 has param 'bip44_coin' => (
72 isa => Int,
73 required => 0,
74 );
75  
76 sub single_network
77 674 ▶{
Subroutine single_network: called CC=1 SCAR=0.0
Pod documented
78 674 my ($class) = @_;
79  
80 674 return $single_network;
81 }
82  
83 sub register
84 393 ▶{
Subroutine register: called CC=2 SCAR=7.0
Pod documented
85 393 my ($self, %config) = @_;
86  
87 393 ▶ if (!ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 393 0 393
88 393 $self = $self->new(%config);
89 }
90  
91 392 $networks{$self->id} = $self;
92 392 return $self;
93 }
94  
95 sub unregister
96 13 ▶{
Subroutine unregister: called CC=2 SCAR=7.0
Pod documented
97 13 my ($self) = @_;
98  
99 13 ▶ Bitcoin::Crypto::Exception::NetworkConfig->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $default_network eq $self->id 0 13 13
100 'cannot unregister the default network - set another network as default first'
101 ) if $default_network eq $self->id;
102  
103 13 delete $networks{$self->id};
104 13 return $self;
105 }
106  
107 sub set_default
108 82 ▶{
Subroutine set_default: called CC=2 SCAR=7.0
Pod documented
109 82 my ($self) = @_;
110  
111 Bitcoin::Crypto::Exception::NetworkConfig->raise(
112 'the network needs to be registered before becoming the default one'
113 82 ▶ ) unless defined $networks{$self->id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $networks{$self->id} 0 82 82
114  
115 82 $default_network = $self->id;
116 82 $single_network = 0;
117 82 return $self;
118 }
119  
120 sub set_single
121 3 ▶{
Subroutine set_single: called CC=1 SCAR=0.0
Pod documented
122 3 my ($self) = @_;
123  
124 3 $self->set_default;
125 3 $single_network = 1;
126  
127 3 return $self;
128 }
129  
130 sub supports_segwit
131 135 ▶{
Subroutine supports_segwit: called CC=1 SCAR=0.0
Pod documented
132 135 my ($self) = @_;
133  
134 135 return defined $self->segwit_hrp;
135 }
136  
137 sub find
138 107 ▶{
Subroutine find: called CC=2 SCAR=6.9
Pod documented
139 107 my ($class, $sub) = @_;
140  
141 107 ▶ return keys %networks
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sub 3 104 107
142 unless defined $sub;
143  
144 104 return grep { $sub->($networks{$_}) } keys %networks;
145 }
146  
147 sub get
148 3855 ▶{
Subroutine get: called CC=3 SCAR=12.5
Pod documented
149 3855 ▶ my $id = $_[1] // $default_network;
Condition66%2/3
$_[1] // $default_network
l!l&&r!l&&!r
11537400
MC/DC0%0/2
$_[1] // $default_network
$_[1] $default_network
Truth table66%2/3
A // B
A= $_[1]B= $default_network
ABresult
1X1
011
000
150 3855 ▶ return $networks{$id}
Condition33%1/3
$networks{$id} // "Bitcoin::Crypto::Exception::NetworkConfig"->raise("network $id is not registered")
l!l&&r!l&&!r
385500
MC/DC0%0/2
$networks{$id} // "Bitcoin::Crypto::Exception::NetworkConfig"->raise("network $id is not registered")
$networks{$id} "Bitcoin::Crypto::Exception::NetworkConfig"->raise("network $id is not registered")
Truth table33%1/3
A // B
A= $networks{$id}B= "Bitcoin::Crypto::Exception::NetworkConfig"->raise("network $id is not registered")
ABresult
1X1
011
000
151 // Bitcoin::Crypto::Exception::NetworkConfig->raise(
152 "network $id is not registered"
153 );
154 }
155  
156 ### PREDEFINED NETWORKS SECTION
157 # When adding a network, make sure to:
158 # - code in valid constants of the network below
159 # - provide resources that will confirm these constant values (in the merge request)
160 # - add your network to the POD documentation below
161 # - add your network to test file 17-predefined-networks.t
162  
163 ### BITCOIN
164  
165 65 __PACKAGE__->register(
166 id => 'bitcoin',
167 name => 'Bitcoin Mainnet',
168 p2pkh_byte => "\x00",
169 p2sh_byte => "\x05",
170 wif_byte => "\x80",
171 segwit_hrp => 'bc',
172  
173 extprv_version => 0x0488ade4,
174 extpub_version => 0x0488b21e,
175  
176 extprv_compat_version => 0x049d7878,
177 extpub_compat_version => 0x049d7cb2,
178  
179 extprv_segwit_version => 0x04b2430c,
180 extpub_segwit_version => 0x04b24746,
181  
182 bip44_coin => 0,
183 )->set_default;
184  
185 65 __PACKAGE__->register(
186 id => 'bitcoin_testnet',
187 name => 'Bitcoin Testnet',
188 p2pkh_byte => "\x6f",
189 p2sh_byte => "\xc4",
190 wif_byte => "\xef",
191 segwit_hrp => 'tb',
192  
193 extprv_version => 0x04358394,
194 extpub_version => 0x043587cf,
195  
196 extprv_compat_version => 0x044a4e28,
197 extpub_compat_version => 0x044a5262,
198  
199 extprv_segwit_version => 0x045f18bc,
200 extpub_segwit_version => 0x045f1cf6,
201  
202 bip44_coin => 1,
203 );
204  
205 ### DOGECOIN
206  
207 65 __PACKAGE__->register(
208 id => 'dogecoin',
209 name => 'Dogecoin Mainnet',
210 p2pkh_byte => "\x1e",
211 p2sh_byte => "\x16",
212 wif_byte => "\x9e",
213  
214 extprv_version => 0x02fac398,
215 extpub_version => 0x02facafd,
216  
217 bip44_coin => 3,
218 );
219  
220 65 __PACKAGE__->register(
221 id => 'dogecoin_testnet',
222 name => 'Dogecoin Testnet',
223 p2pkh_byte => "\x71",
224 p2sh_byte => "\xc4",
225 wif_byte => "\xf1",
226  
227 extprv_version => 0x04358394,
228 extpub_version => 0x043587cf,
229  
230 bip44_coin => 1,
231 );
232  
233 ### PEPECOIN
234 # unfortunately Pepecoin mainnet shares a WIF byte with Dogecoin mainnet
235  
236 65 __PACKAGE__->register(
237 id => 'pepecoin',
238 name => 'Pepecoin Mainnet',
239 p2pkh_byte => "\x38",
240 p2sh_byte => "\x16",
241 wif_byte => "\x9e",
242  
243 extprv_version => 0x02fac398,
244 extpub_version => 0x02facafd,
245  
246 bip44_coin => 3434,
247 );
248  
249 # almost all the same as Dogecoin Testnet
250 65 __PACKAGE__->register(
251 id => 'pepecoin_testnet',
252 name => 'Pepecoin Testnet',
253 p2pkh_byte => "\x71",
254 p2sh_byte => "\xc4",
255 wif_byte => "\xf1",
256  
257 extprv_version => 0x04358394,
258 extpub_version => 0x043587cf,
259  
260 bip44_coin => 1,
261 );
262  
263 65 1;
264  
265 __END__
« Crypto/Transaction/UTXO.pm ↑ Summary Crypto/Role/Compressed.pm »