| 1 | package Bitcoin::Crypto::DerivationPath; | ||||||||||||||||||||||||||||
| 2 | 21 | $Bitcoin::Crypto::DerivationPath::VERSION = '4.005'; | |||||||||||||||||||||||||||
| 3 | 21 | ▶ | use v5.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 21 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 21 | ▶ | use Mooish::Base -standard; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 21 | ▶ | use Bitcoin::Crypto::Constants qw(:key); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 21 | ▶ | use Bitcoin::Crypto::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 21 | has param 'private' => ( | |||||||||||||||||||||||||||
| 12 | isa => Bool, | ||||||||||||||||||||||||||||
| 13 | ); | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 21 | has param 'path' => ( | |||||||||||||||||||||||||||
| 16 | isa => ArrayRef [PositiveOrZeroInt], | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 21 | with qw(Bitcoin::Crypto::Role::WithDerivationPath); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | use overload | ||||||||||||||||||||||||||||
| 22 | 1 | ▶ | q{""} => sub { shift->as_string }, | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 21 | fallback => 1; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub get_derivation_path | ||||||||||||||||||||||||||||
| 26 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get_derivation_path: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | return $self; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | sub get_path_hardened | ||||||||||||||||||||||||||||
| 33 | 25 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get_path_hardened: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 25 | my ($self) = @_; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 25 | my $path = $self->path; | |||||||||||||||||||||||||||
| 37 | return [ | ||||||||||||||||||||||||||||
| 38 | map { | ||||||||||||||||||||||||||||
| 39 | 25 | my $hardened = $_ >= MAX_CHILD_KEYS; | |||||||||||||||||||||||||||
| 40 | 90 | my $value = $_ - ($hardened * MAX_CHILD_KEYS); | |||||||||||||||||||||||||||
| 41 | 90 | [$value, $hardened]; | |||||||||||||||||||||||||||
| 42 | } @$path | ||||||||||||||||||||||||||||
| 43 | ]; | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub from_string | ||||||||||||||||||||||||||||
| 47 | 120 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
from_string: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 120 | my ($class, $string) = @_; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 120 | ▶ | Bitcoin::Crypto::Exception->raise( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | "Invalid derivation path string: not a valid 'm' notation" | ||||||||||||||||||||||||||||
| 52 | ) unless $string =~ m{\A ([mM]) ((?: / \d+ '?)*) \z}x; | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 116 | my ($head, $rest) = ($1, $2); | |||||||||||||||||||||||||||
| 55 | 116 | my @path; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 116 | ▶ | if (defined $rest && length $rest > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $rest and length $rest > 0
MC/DC50%1/2
defined $rest and length $rest > 0
defined $rest
length $rest > 0
Truth table66%2/3
A and B
A= defined $restB= length $rest > 0
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # remove leading slash (after $head) | ||||||||||||||||||||||||||||
| 60 | 110 | substr $rest, 0, 1, ''; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 110 | for my $part (split '/', $rest) { | |||||||||||||||||||||||||||
| 63 | 346 | my $is_hardened = $part =~ tr/'//d; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 346 | ▶ | Bitcoin::Crypto::Exception->raise( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | "Derivation path part too large: $part" | ||||||||||||||||||||||||||||
| 67 | ) if $part >= MAX_CHILD_KEYS; | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 344 | ▶ | $part += MAX_CHILD_KEYS if $is_hardened; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 344 | push @path, $part; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 114 | return $class->new( | |||||||||||||||||||||||||||
| 75 | private => $head eq 'm', | ||||||||||||||||||||||||||||
| 76 | path => \@path, | ||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | sub as_string | ||||||||||||||||||||||||||||
| 81 | 9 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 9 | my ($self) = @_; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 9 | ▶ | my $string = $self->private ? 'm' : 'M'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 9 | foreach my $item (@{$self->get_path_hardened}) { | |||||||||||||||||||||||||||
| 87 | 31 | ▶ | $string .= '/' . $item->[0] . ($item->[1] ? q{'} : ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 9 | return $string; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 21 | 1; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | __END__ | ||||||||||||||||||||||||||||