| 1 | package Bitcoin::Crypto::Util::Internal; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 71 | $Bitcoin::Crypto::Util::Internal::VERSION = '4.006'; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 71 | ▶ | use v5.14; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 71 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 71 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 71 | ▶ | use Unicode::Normalize; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 71 | ▶ | use Crypt::KeyDerivation qw(pbkdf2); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 71 | ▶ | use Encode qw(encode); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 71 | ▶ | use Crypt::Digest::RIPEMD160 qw(ripemd160); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 71 | ▶ | use Crypt::Digest::SHA256 qw(sha256); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 71 | ▶ | use Bitcoin::BIP39 qw(gen_bip39_mnemonic entropy_to_bip39_mnemonic); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 71 | ▶ | use Feature::Compat::Try; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 71 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 71 | ▶ | use Types::Common -types, -sigs; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 71 | ▶ | use Bitcoin::Crypto::Helpers qw(parse_formatdesc ecc); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 71 | ▶ | use Bitcoin::Crypto::Constants qw(:key :witness); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 71 | ▶ | use Bitcoin::Crypto::Types -types; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 71 | ▶ | use Bitcoin::Crypto::Exception; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 71 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | validate_wif | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | validate_segwit | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | get_address_type | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | get_key_type | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | get_public_key_compressed | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | generate_mnemonic | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | mnemonic_from_entropy | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | mnemonic_to_seed | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | get_path_info | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | from_format | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | to_format | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | pack_compactsize | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | unpack_compactsize | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | pack_array | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | unpack_array | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | hash160 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | hash256 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | merkle_root | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | tagged_hash | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | lift_x | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | has_even_y | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | get_taproot_ext | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 71 | our %EXPORT_TAGS = (all => [@EXPORT_OK]); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | sub validate_wif | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 22 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_wif: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 22 | my ($wif) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 22 | require Bitcoin::Crypto::Base58; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 22 | my $byte_wif = Bitcoin::Crypto::Base58::decode_base58check($wif); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 21 | my $last_byte = substr $byte_wif, -1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 21 | ▶ | if (length $byte_wif == KEY_MAX_LENGTH + 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 9 | return $last_byte eq WIF_COMPRESSED_BYTE; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 12 | return length $byte_wif == KEY_MAX_LENGTH + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | sub validate_segwit | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 289 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_segwit: called CC=11 SCAR=24.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 289 | my ($program) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 289 | my $version = unpack 'C', $program; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 289 | ▶ | Bitcoin::Crypto::Exception::SegwitProgram->raise( | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%4/8
defined $version and $version >= 0 and $version <= 16
defined $version and $version >= 0
$version // "[null]"
MC/DC33%1/3
defined $version and $version >= 0 and $version <= 16
defined $version
$version >= 0
$version <= 16
MC/DC0%0/1
$version // "[null]"
$version
Truth table50%2/4
A and B and C
A= defined $versionB= $version >= 0C= $version <= 16
Truth table50%1/2
A
A= $version
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 'incorrect witness program version ' . ($version // '[null]') | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | ) unless defined $version && $version >= 0 && $version <= MAX_WITNESS_VERSION; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 287 | $program = substr $program, 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | # common validator | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 287 | ▶ | Bitcoin::Crypto::Exception::SegwitProgram->raise( | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $program >= 2 and length $program <= 40
MC/DC100%2/2
length $program >= 2 and length $program <= 40
length $program >= 2
length $program <= 40
Truth table100%3/3
A and B
A= length $program >= 2B= length $program <= 40
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 'incorrect witness program length' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | ) unless length $program >= 2 && length $program <= 40; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 281 | ▶ | if ($version == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # SegWit validator | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 132 | ▶ | Bitcoin::Crypto::Exception::SegwitProgram->raise( | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $program == 20 or length $program == 32
MC/DC100%2/2
length $program == 20 or length $program == 32
length $program == 20
length $program == 32
Truth table100%3/3
A or B
A= length $program == 20B= length $program == 32
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 'incorrect witness program length (segwit)' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | ) unless length $program == 20 || length $program == 32; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | elsif ($version == 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # Taproot validator | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | # taproot outputs are 32 bytes, but other lengths "remain unencumbered" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | # do not throw this exception to make bip350 test suite pass (10-Bech32.t) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | # Bitcoin::Crypto::Exception::SegwitProgram->raise( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | # 'incorrect witness program length (taproot)' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | # ) unless length $program == 32; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 279 | return $version; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | sub get_address_type | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 80 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_address_type: called CC=19 SCAR=30.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 80 | my ($address, $network_id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 80 | require Bitcoin::Crypto::Base58; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 80 | require Bitcoin::Crypto::Bech32; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 80 | require Bitcoin::Crypto::Network; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 80 | ▶ | my $network = Bitcoin::Crypto::Network->get($network_id // ()); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$network_id // ()
MC/DC0%0/2
$network_id // ()
$network_id
()
Truth table66%2/3
A // B
A= $network_idB= ()
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 80 | my $type; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | # first, try segwit | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 80 | ▶ | if ($network->supports_segwit) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 80 | try { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 80 | ▶ | Bitcoin::Crypto::Exception::SegwitProgram->raise( | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 'invalid human readable part in address' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | ) unless Bitcoin::Crypto::Bech32::get_hrp($address) eq $network->segwit_hrp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 65 | my $data = Bitcoin::Crypto::Bech32::decode_segwit($address); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 65 | my $version = ord substr $data, 0, 1, ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 65 | ▶ | $type = 'P2TR' | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$version == 1 and length $data == 32
MC/DC50%1/2
$version == 1 and length $data == 32
$version == 1
length $data == 32
Truth table66%2/3
A and B
A= $version == 1B= length $data == 32
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | if $version == TAPROOT_WITNESS_VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | && length $data == 32; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 65 | ▶ | return $type if $type; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 29 | ▶ | Bitcoin::Crypto::Exception::SegwitProgram->raise( | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | "invalid segwit address of version $version" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | ) unless $version == SEGWIT_WITNESS_VERSION; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 29 | ▶ | $type = 'P2WPKH' if length $data == 20; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 29 | ▶ | $type = 'P2WSH' if length $data == 32; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 29 | ▶ | return $type if $type; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | Bitcoin::Crypto::Exception::Address->raise( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 'invalid segwit address' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | catch ($ex) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 15 | ▶ | die $ex unless blessed $ex && $ex->isa('Bitcoin::Crypto::Exception::Bech32InputFormat'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($ex) and $ex->isa("Bitcoin::Crypto::Exception::Bech32InputFormat")
MC/DC50%1/2
builtin::blessed($ex) and $ex->isa("Bitcoin::Crypto::Exception::Bech32InputFormat")
builtin::blessed($ex)
$ex->isa("Bitcoin::Crypto::Exception::Bech32InputFormat")
Truth table66%2/3
A and B
A= builtin::blessed($ex)B= $ex->isa("Bitcoin::Crypto::Exception::Bech32InputFormat")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | # then, try legacy | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 14 | try { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 14 | my $data = Bitcoin::Crypto::Base58::decode_base58check($address); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 12 | my $byte = substr $data, 0, 1, ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 12 | ▶ | $type = 'P2PKH' if $byte eq $network->p2pkh_byte; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 12 | ▶ | $type = 'P2SH' if $byte eq $network->p2sh_byte; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 12 | ▶ | Bitcoin::Crypto::Exception::Address->raise( | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 'invalid legacy address' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | ) unless length $data == 20; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 12 | ▶ | return $type if $type; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | Bitcoin::Crypto::Exception::Address->raise( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 'invalid first byte in address' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | catch ($ex) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 2 | ▶ | die $ex unless blessed $ex && $ex->isa('Bitcoin::Crypto::Exception::Base58InputFormat'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($ex) and $ex->isa("Bitcoin::Crypto::Exception::Base58InputFormat")
MC/DC0%0/2
builtin::blessed($ex) and $ex->isa("Bitcoin::Crypto::Exception::Base58InputFormat")
builtin::blessed($ex)
$ex->isa("Bitcoin::Crypto::Exception::Base58InputFormat")
Truth table33%1/3
A and B
A= builtin::blessed($ex)B= $ex->isa("Bitcoin::Crypto::Exception::Base58InputFormat")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | Bitcoin::Crypto::Exception::Address->raise( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | "not an address: $address" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | sub get_key_type | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 1587 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_key_type: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 1587 | my $entropy = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 1587 | ▶ | return undef unless length $entropy; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 1585 | ▶ | return 0 if defined get_public_key_compressed($entropy); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 539 | ▶ | return 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | if length $entropy <= KEY_MAX_LENGTH; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 3 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | sub get_public_key_compressed | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 2123 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_public_key_compressed: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 2123 | my $entropy = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 2123 | my $octet = unpack 'C', $entropy; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 2123 | ▶ | return undef unless defined $octet; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 2123 | ▶ | if ($octet == 0x02 || $octet == 0x03) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$octet == 2 or $octet == 3
MC/DC100%2/2
$octet == 2 or $octet == 3
$octet == 2
$octet == 3
Truth table100%3/3
A or B
A= $octet == 2B= $octet == 3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 1552 | ▶ | return 1 if length $entropy == KEY_MAX_LENGTH + 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | ▶ | elsif ($octet == 0x04 || $octet == 0x06 || $octet == 0x07) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
$octet == 4 or $octet == 6 or $octet == 7
$octet == 4 or $octet == 6
MC/DC100%3/3
$octet == 4 or $octet == 6 or $octet == 7
$octet == 4
$octet == 6
$octet == 7
Truth table100%4/4
A or B or C
A= $octet == 4B= $octet == 6C= $octet == 7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 50 | ▶ | return 0 if length $entropy == 2 * KEY_MAX_LENGTH + 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 539 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | sub mnemonic_to_seed | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 37 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mnemonic_to_seed: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 37 | my ($mnemonic, $password) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 37 | $mnemonic = encode('UTF-8', NFKD($mnemonic)); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 37 | ▶ | $password = encode('UTF-8', NFKD('mnemonic' . ($password // ''))); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$password // ""
MC/DC100%1/1
$password // ""
$password
Truth table100%2/2
A
A= $password
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 37 | return pbkdf2($mnemonic, $password, 2048, 'SHA512', 64); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | sub generate_mnemonic | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 8 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
generate_mnemonic: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 8 | my ($len, $lang) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 8 | my ($min_len, $len_div, $max_len) = (128, 32, 256); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | # bip39 specification values | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 8 | ▶ | Bitcoin::Crypto::Exception::MnemonicGenerate->raise( | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$len < $min_len or $len > $max_len or $len % $len_div != 0
$len < $min_len or $len > $max_len
MC/DC33%1/3
$len < $min_len or $len > $max_len or $len % $len_div != 0
$len < $min_len
$len > $max_len
$len % $len_div != 0
Truth table50%2/4
A or B or C
A= $len < $min_lenB= $len > $max_lenC= $len % $len_div != 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | "required entropy of between $min_len and $max_len bits, divisible by $len_div" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | ) if $len < $min_len || $len > $max_len || $len % $len_div != 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | return Bitcoin::Crypto::Exception::MnemonicGenerate->trap_into( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 7 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 7 | my $ret = gen_bip39_mnemonic(bits => $len, language => $lang); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 7 | $ret->{mnemonic}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 7 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | sub mnemonic_from_entropy | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 12 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mnemonic_from_entropy: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 12 | my ($entropy, $lang) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | return Bitcoin::Crypto::Exception::MnemonicGenerate->trap_into( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 12 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 12 | entropy_to_bip39_mnemonic( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | entropy => $entropy, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | language => $lang | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 12 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | sub get_path_info | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 9 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_path_info: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 9 | my ($path) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | # NOTE: ->coerce may still throw because of exceptions in from_string of DerivationPath | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 9 | try { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 9 | return scalar DerivationPath->assert_coerce($path); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | catch ($e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 4 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | sub from_format ($) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 8 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_format: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 8 | state $sig = signature(positional => [Tuple [FormatStr, Str]]); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 8 | my ($format, $data) = @{($sig->(@_))[0]}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 8 | return parse_formatdesc($format, $data); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | sub to_format ($) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 1191 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_format: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 1191 | state $sig = signature(positional => [Tuple [FormatStr, ByteStr]]); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 1191 | my ($format, $data) = @{($sig->(@_))[0]}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 1191 | return parse_formatdesc($format, $data, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | sub pack_compactsize | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 6688 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pack_compactsize: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 6688 | my $value = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 6688 | ▶ | if ($value <= 0xfc) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 6674 | return pack 'C', $value; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | elsif ($value <= 0xffff) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 12 | return pack 'Cv', 0xfd, $value; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | elsif ($value <= 0xffffffff) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 1 | return pack 'CV', 0xfe, $value; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | # 32 bit archs should not reach this | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 1 | return pack 'CVV', 0xff, $value & 0xffffffff, $value >> 32; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | sub unpack_compactsize | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 6041 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unpack_compactsize: called CC=10 SCAR=23.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 6041 | my ($stream, $pos_ref) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 6041 | my $partial = !!$pos_ref; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 6041 | ▶ | my $pos = $partial ? $$pos_ref : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 6041 | ▶ | Bitcoin::Crypto::Exception->raise( | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | "cannot unpack CompactSize: no data in stream" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | ) unless $pos < length $stream; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | # if the first byte is 0xfd, 0xfe or 0xff, then CompactSize contains 2, 4 or 8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | # bytes respectively | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 6040 | my $value = ord substr $stream, $pos++, 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 6040 | ▶ | if ($value > 0xfc) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 18 | my $length = 1 << ($value - 0xfc); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 18 | ▶ | Bitcoin::Crypto::Exception->raise( | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | "cannot unpack CompactSize: not enough data in stream" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | ) if $pos + $length > length $stream; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 16 | ▶ | if ($length == 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 12 | $value = unpack "\@$pos v", $stream; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | elsif ($length == 4) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 3 | $value = unpack "\@$pos V", $stream; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 1 | Bitcoin::Crypto::Exception->raise( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | "cannot unpack CompactSize: no 64 bit support" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | ) if !Bitcoin::Crypto::Constants::is_64bit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 1 | my ($lower, $higher) = unpack "\@$pos VV", $stream; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 1 | $value = ($higher << 32) + $lower; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 16 | $pos += $length; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | Bitcoin::Crypto::Exception->raise( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 6038 | ▶ | "cannot unpack CompactSize: leftover data in stream" | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $partial and $pos != length $stream
MC/DC100%2/2
not $partial and $pos != length $stream
not $partial
$pos != length $stream
Truth table100%3/3
not(A) and B
A= not $partialB= $pos != length $stream
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | ) if !$partial && $pos != length $stream; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 6037 | ▶ | $$pos_ref = $pos | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | if $partial; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 6037 | return $value; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | sub pack_array | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 239 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pack_array: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 239 | my ($array) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | return join '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 239 | pack_compactsize(scalar @{$array}), | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 239 | (map { pack_compactsize(length $_) . $_ } @{$array}), | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | ; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | sub unpack_array | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 178 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unpack_array: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 178 | my ($serialized, $pos_ref) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 178 | my $partial = !!$pos_ref; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 178 | ▶ | my $pos = $partial ? $$pos_ref : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 178 | my $count = unpack_compactsize $serialized, \$pos; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 178 | my @array; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 178 | for (1 .. $count) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 312 | my $size = unpack_compactsize $serialized, \$pos; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 312 | push @array, substr $serialized, $pos, $size; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 312 | $pos += $size; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | Bitcoin::Crypto::Exception->raise( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 178 | ▶ | 'array data is corruputed' | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $partial and $pos != length $serialized
MC/DC0%0/2
not $partial and $pos != length $serialized
not $partial
$pos != length $serialized
Truth table66%2/3
not(A) and B
A= not $partialB= $pos != length $serialized
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | ) if !$partial && $pos != length $serialized; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 178 | ▶ | $$pos_ref = $pos | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | if $partial; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 178 | return \@array; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | sub hash160 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 619 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hash160: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 619 | return ripemd160(sha256(shift)); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | sub hash256 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 1029 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hash256: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 1029 | return sha256(sha256(shift)); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | sub merkle_root | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 10 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
merkle_root: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 10 | my ($leaves) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 10 | my @parts = map { hash256($_) } @$leaves; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 10 | ▶ | Bitcoin::Crypto::Exception->raise( | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 'need at least one element to calculate a merkle root' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | ) unless @parts; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 10 | ▶ | while (@parts > 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | @parts = map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 59 | ▶ | hash256($parts[$_] . ($parts[$_ + 1] // $parts[$_])) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$parts[$_ + 1] // $parts[$_]
MC/DC0%0/2
$parts[$_ + 1] // $parts[$_]
$parts[$_ + 1]
$parts[$_]
Truth table66%2/3
A // B
A= $parts[$_ + 1]B= $parts[$_]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | } grep { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 19 | $_ % 2 == 0 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | } 0 .. $#parts; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 10 | return $parts[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | sub tagged_hash | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 389 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tagged_hash: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 389 | my ($tag, $message) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 389 | state $tags = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 389 | ▶ | $tags->{$tag} //= sha256(encode 'UTF-8', $tag) x 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$tags->{$tag} //= sha256(&encode("UTF-8", $tag)) x 2
MC/DC100%1/1
$tags->{$tag} //= sha256(&encode("UTF-8", $tag)) x 2
$tags->{$tag}
Truth table100%2/2
A
A= $tags->{$tag}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 389 | return sha256($tags->{$tag} . $message); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | sub lift_x | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 274 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lift_x: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 274 | my ($x) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 274 | ▶ | my $key = "\x02" . ($x // ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$x // ""
MC/DC0%0/1
$x // ""
$x
Truth table50%1/2
A
A= $x
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 274 | ▶ | Bitcoin::Crypto::Exception::KeyCreate->raise( | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | 'invalid xonly public key' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | ) unless ecc->verify_public_key($key); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 270 | return $key; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | sub has_even_y | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 86 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_even_y: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 86 | my ($key) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 86 | ▶ | $key = $key->raw_key if ref $key; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | return Bitcoin::Crypto::Exception->trap_into( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 86 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 86 | $key = ecc->compress_public_key($key); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 85 | return substr($key, 0, 1) eq "\x02"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 86 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | sub get_taproot_ext | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 32 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_taproot_ext: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 32 | my ($ext_flag, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 32 | ▶ | if ($ext_flag == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 1 | return ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | elsif ($ext_flag == 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | # https://github.com/bitcoin/bips/blob/master/bip-0342.mediawiki#common-signature-message-extension | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | return $args->{script_tree}->get_leaf($args->{leaf_id})->hash | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 31 | ▶ | . pack('xV', $args->{codesep_pos} // 0xffffffff); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"codesep_pos"} // 4294967295
MC/DC100%1/1
$args->{"codesep_pos"} // 4294967295
$args->{"codesep_pos"}
Truth table100%2/2
A
A= $args->{"codesep_pos"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 0 | Bitcoin::Crypto::Exception->raise( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | "can not create taproot_ext for unknown ext flag $ext_flag" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 71 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||||||||||||