| 1 | package Bitcoin::Crypto::Script::Tree; | ||||||||||||||
| 2 | 29 | $Bitcoin::Crypto::Script::Tree::VERSION = '4.005'; | |||||||||||||
| 3 | 37 | ▶ | use v5.14; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 37 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 37 | ▶ | use Mooish::Base -standard; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 37 | ▶ | use Types::Common -sigs; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 37 | ▶ | use List::Util qw(first); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 37 | ▶ | use Bitcoin::Crypto::Types -types; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 37 | ▶ | use Bitcoin::Crypto::Exception; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 37 | ▶ | use Bitcoin::Crypto::Util::Internal qw(tagged_hash pack_compactsize has_even_y); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 37 | ▶ | use Bitcoin::Crypto::Transaction::ControlBlock; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | # recursive structure - a binary tree | ||||||||||||||
| 16 | 29 | has param 'tree' => ( | |||||||||||||
| 17 | isa => ArrayRef [ArrayRef | HashRef], | ||||||||||||||
| 18 | ); | ||||||||||||||
| 19 | |||||||||||||||
| 20 | 29 | has field '_tree_cache' => ( | |||||||||||||
| 21 | lazy => 1, | ||||||||||||||
| 22 | clearer => -public, | ||||||||||||||
| 23 | ); | ||||||||||||||
| 24 | |||||||||||||||
| 25 | # this is flat traversal algorithm that avoids deep recursion warnings in deep | ||||||||||||||
| 26 | # script trees | ||||||||||||||
| 27 | sub _traverse | ||||||||||||||
| 28 | 38 | ▶ | { | ||||||||||||
|
Subroutine
_traverse: called CC=11 SCAR=24.0
| |||||||||||||||
| 29 | 38 | my ($self, $join_action, $leaf_action) = @_; | |||||||||||||
| 30 | |||||||||||||||
| 31 | 38 | my @stack = ({nodes => [@{$self->tree}], results => []}); | |||||||||||||
| 32 | 38 | my $result; | |||||||||||||
| 33 | |||||||||||||||
| 34 | 38 | while ('avoiding recursion by using stack') { | |||||||||||||
| 35 | 58 | ▶ | while (@{$stack[-1]{nodes}} > 0) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | 99 | my $item = shift @{$stack[-1]{nodes}}; | |||||||||||||
| 37 | 99 | ▶ | if (ref $item eq 'ARRAY') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | |||||||||||||||
| 39 | # this value is the next level of the tree | ||||||||||||||
| 40 | 20 | push @stack, {nodes => [@$item], results => []}; | |||||||||||||
| 41 | } | ||||||||||||||
| 42 | else { | ||||||||||||||
| 43 | 79 | state $precomputed_type = Dict [hash => ByteStr]; | |||||||||||||
| 44 | 79 | state $leaf_type = Dict [ | |||||||||||||
| 45 | leaf_version => IntMaxBits [8], | ||||||||||||||
| 46 | script => BitcoinScript, | ||||||||||||||
| 47 | id => Optional [Int], | ||||||||||||||
| 48 | hash => Optional [ByteStr], | ||||||||||||||
| 49 | ]; | ||||||||||||||
| 50 | |||||||||||||||
| 51 | # this value is a leaf which may need calculating | ||||||||||||||
| 52 | 79 | my $value = $precomputed_type->coerce($item); | |||||||||||||
| 53 | 79 | ▶ | if (!$precomputed_type->check($value)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 54 | 57 | $value = $leaf_type->assert_coerce($item); | |||||||||||||
| 55 | 57 | ▶ | if (!defined $value->{hash}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 54 | my $script = $value->{script}->to_serialized; | |||||||||||||
| 57 | 54 | my $script_len = pack_compactsize(length $script); | |||||||||||||
| 58 | |||||||||||||||
| 59 | $value->{hash} = | ||||||||||||||
| 60 | 54 | tagged_hash('TapLeaf', join '', pack('C', $value->{leaf_version}), $script_len, $script); | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | } | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 79 | ▶ | $leaf_action->($value, scalar @stack) if defined $leaf_action; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 65 | 79 | push @{$stack[-1]{results}}, $value; | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | } | ||||||||||||||
| 68 | |||||||||||||||
| 69 | 58 | my @results = @{$stack[-1]{results}}; | |||||||||||||
| 70 | 58 | ▶ | if (@results == 2) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 71 | |||||||||||||||
| 72 | # sort result so that smaller hash values come first | ||||||||||||||
| 73 | @results = reverse @results | ||||||||||||||
| 74 | 41 | ▶ | if $results[0]{hash} gt $results[1]{hash}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | |||||||||||||||
| 76 | 41 | ▶ | my %data = defined $join_action ? $join_action->(@results, scalar @stack) : (); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | $result = { | ||||||||||||||
| 78 | %data, | ||||||||||||||
| 79 | 41 | hash => tagged_hash('TapBranch', join '', map { $_->{hash} } @results), | |||||||||||||
| 80 | }; | ||||||||||||||
| 81 | } | ||||||||||||||
| 82 | elsif (@results == 1) { | ||||||||||||||
| 83 | 17 | $result = $results[0]; | |||||||||||||
| 84 | } | ||||||||||||||
| 85 | else { | ||||||||||||||
| 86 | 0 | Bitcoin::Crypto::Exception->raise( | |||||||||||||
| 87 | 'invalid taproot script tree, not a binary tree' | ||||||||||||||
| 88 | ); | ||||||||||||||
| 89 | } | ||||||||||||||
| 90 | |||||||||||||||
| 91 | 58 | pop @stack; | |||||||||||||
| 92 | 58 | ▶ | last unless @stack > 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | 20 | push @{$stack[-1]{results}}, $result; | |||||||||||||
| 94 | } | ||||||||||||||
| 95 | |||||||||||||||
| 96 | 38 | return $result; | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | |||||||||||||||
| 99 | sub _tree_paths_action | ||||||||||||||
| 100 | 37 | ▶ | { | ||||||||||||
|
Subroutine
_tree_paths_action: called CC=1 SCAR=0.0
| |||||||||||||||
| 101 | 37 | my ($self) = @_; | |||||||||||||
| 102 | |||||||||||||||
| 103 | 37 | my %paths; | |||||||||||||
| 104 | 39 | ▶ | my $action = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||
| 105 | 39 | my ($node1, $node2) = @_; | |||||||||||||
| 106 | 39 | my @all_ids; | |||||||||||||
| 107 | |||||||||||||||
| 108 | 39 | foreach my $info ([$node1, $node2], [$node2, $node1]) { | |||||||||||||
| 109 | 78 | my ($this_one, $other_one) = @{$info}; | |||||||||||||
| 110 | 78 | ▶ | next unless defined $this_one->{id}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 111 | 40 | ▶ | my @ids = ref $this_one->{id} ? @{$this_one->{id}} : $this_one->{id}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 112 | 40 | push @all_ids, @ids; | |||||||||||||
| 113 | |||||||||||||||
| 114 | 40 | foreach my $id (@ids) { | |||||||||||||
| 115 | 35 | push @{$paths{$id}}, $other_one->{hash}; | |||||||||||||
| 116 | } | ||||||||||||||
| 117 | } | ||||||||||||||
| 118 | |||||||||||||||
| 119 | return ( | ||||||||||||||
| 120 | 39 | id => \@all_ids, | |||||||||||||
| 121 | ); | ||||||||||||||
| 122 | 37 | }; | |||||||||||||
| 123 | |||||||||||||||
| 124 | 37 | return (\%paths, $action); | |||||||||||||
| 125 | } | ||||||||||||||
| 126 | |||||||||||||||
| 127 | sub _build_tree_cache | ||||||||||||||
| 128 | 37 | ▶ | { | ||||||||||||
|
Subroutine
_build_tree_cache: called CC=1 SCAR=0.0
| |||||||||||||||
| 129 | 37 | my ($self) = @_; | |||||||||||||
| 130 | |||||||||||||||
| 131 | 37 | my @leaves; | |||||||||||||
| 132 | 37 | my ($paths, $paths_action) = $self->_tree_paths_action; | |||||||||||||
| 133 | |||||||||||||||
| 134 | my $root = $self->_traverse( | ||||||||||||||
| 135 | $paths_action, | ||||||||||||||
| 136 | 76 | ▶ | sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 137 | 76 | my $leaf = shift; | |||||||||||||
| 138 | 76 | push @leaves, $leaf; | |||||||||||||
| 139 | } | ||||||||||||||
| 140 | 37 | ); | |||||||||||||
| 141 | |||||||||||||||
| 142 | return { | ||||||||||||||
| 143 | 37 | leaves => \@leaves, | |||||||||||||
| 144 | paths => $paths, | ||||||||||||||
| 145 | root => $root, | ||||||||||||||
| 146 | }; | ||||||||||||||
| 147 | } | ||||||||||||||
| 148 | |||||||||||||||
| 149 | sub get_merkle_root | ||||||||||||||
| 150 | 43 | ▶ | { | ||||||||||||
|
Subroutine
get_merkle_root: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 151 | 43 | my ($self) = @_; | |||||||||||||
| 152 | |||||||||||||||
| 153 | 43 | return $self->_tree_cache->{root}{hash}; | |||||||||||||
| 154 | } | ||||||||||||||
| 155 | |||||||||||||||
| 156 | sub _get_tapleaf | ||||||||||||||
| 157 | 52 | ▶ | { | ||||||||||||
|
Subroutine
_get_tapleaf: called CC=2 SCAR=7.0
| |||||||||||||||
| 158 | 52 | my ($self, $leaf_id) = @_; | |||||||||||||
| 159 | |||||||||||||||
| 160 | 52 | ▶ | my $leaf = first { exists $_->{id} && $_->{id} == $leaf_id } @{$self->_tree_cache->{leaves}}; | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||
| 161 | 52 | ▶ | Bitcoin::Crypto::Exception::ScriptTree->raise( | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 162 | "no such block with id=$leaf_id" | ||||||||||||||
| 163 | ) unless defined $leaf; | ||||||||||||||
| 164 | |||||||||||||||
| 165 | 52 | return $leaf; | |||||||||||||
| 166 | } | ||||||||||||||
| 167 | |||||||||||||||
| 168 | sub get_tapleaf_script | ||||||||||||||
| 169 | 10 | ▶ | { | ||||||||||||
|
Subroutine
get_tapleaf_script: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 170 | 10 | my ($self, $leaf_id) = @_; | |||||||||||||
| 171 | |||||||||||||||
| 172 | 10 | return $self->_get_tapleaf($leaf_id)->{script}; | |||||||||||||
| 173 | } | ||||||||||||||
| 174 | |||||||||||||||
| 175 | sub get_tapleaf_hash | ||||||||||||||
| 176 | 20 | ▶ | { | ||||||||||||
|
Subroutine
get_tapleaf_hash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 177 | 20 | my ($self, $leaf_id) = @_; | |||||||||||||
| 178 | |||||||||||||||
| 179 | 20 | return $self->_get_tapleaf($leaf_id)->{hash}; | |||||||||||||
| 180 | } | ||||||||||||||
| 181 | |||||||||||||||
| 182 | sub get_tapleaf_version | ||||||||||||||
| 183 | 22 | ▶ | { | ||||||||||||
|
Subroutine
get_tapleaf_version: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 184 | 22 | my ($self, $leaf_id) = @_; | |||||||||||||
| 185 | |||||||||||||||
| 186 | 22 | return $self->_get_tapleaf($leaf_id)->{leaf_version}; | |||||||||||||
| 187 | } | ||||||||||||||
| 188 | |||||||||||||||
| 189 | sub get_tree_paths | ||||||||||||||
| 190 | 0 | ▶ | { | ||||||||||||
|
Subroutine
get_tree_paths: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 191 | 0 | my ($self) = @_; | |||||||||||||
| 192 | |||||||||||||||
| 193 | 0 | return $self->_tree_cache->{paths}; | |||||||||||||
| 194 | } | ||||||||||||||
| 195 | |||||||||||||||
| 196 | 29 | signature_for from_path => ( | |||||||||||||
| 197 | method => !!1, | ||||||||||||||
| 198 | positional => [HashRef, ArrayRef [ByteStr]], | ||||||||||||||
| 199 | ); | ||||||||||||||
| 200 | |||||||||||||||
| 201 | sub from_path | ||||||||||||||
| 202 | 35 | ▶ | { | ||||||||||||
|
Subroutine
from_path: called CC=3 SCAR=11.0
| |||||||||||||||
| 203 | 35 | my ($class, $leaf, $path) = @_; | |||||||||||||
| 204 | |||||||||||||||
| 205 | 35 | my @path = @$path; | |||||||||||||
| 206 | 35 | ▶ | while (@path) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 207 | 36 | my $this_level = [$leaf, {hash => shift @path}]; | |||||||||||||
| 208 | 36 | $leaf = $this_level; | |||||||||||||
| 209 | } | ||||||||||||||
| 210 | |||||||||||||||
| 211 | 35 | ▶ | $leaf = [$leaf] | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 212 | unless ref $leaf eq 'ARRAY'; | ||||||||||||||
| 213 | |||||||||||||||
| 214 | 18 | return $class->new( | |||||||||||||
| 215 | tree => $leaf | ||||||||||||||
| 216 | ); | ||||||||||||||
| 217 | } | ||||||||||||||
| 218 | |||||||||||||||
| 219 | 29 | signature_for get_control_block => ( | |||||||||||||
| 220 | method => !!1, | ||||||||||||||
| 221 | positional => [Int, InstanceOf ['Bitcoin::Crypto::Key::Public']], | ||||||||||||||
| 222 | ); | ||||||||||||||
| 223 | |||||||||||||||
| 224 | sub get_control_block | ||||||||||||||
| 225 | { | ||||||||||||||
| 226 | 8 | my ($self, $leaf_id, $pubkey) = @_; | |||||||||||||
| 227 | 8 | my $cache = $self->_tree_cache; | |||||||||||||
| 228 | |||||||||||||||
| 229 | 8 | my $leaf_version = $self->get_tapleaf_version($leaf_id); | |||||||||||||
| 230 | 8 | my $tapkey = $pubkey->get_taproot_output_key($cache->{root}{hash}); | |||||||||||||
| 231 | 8 | my $parity = has_even_y($tapkey); | |||||||||||||
| 232 | |||||||||||||||
| 233 | return Bitcoin::Crypto::Transaction::ControlBlock->new( | ||||||||||||||
| 234 | control_byte => $leaf_version | !$parity, | ||||||||||||||
| 235 | public_key => $pubkey, | ||||||||||||||
| 236 | 3 | ▶ | script_blocks => $cache->{paths}{$leaf_id} // [], | ||||||||||||
|
Condition100%2/2
$cache->{"paths"}{$leaf_id} // []
MC/DC100%1/1
$cache->{"paths"}{$leaf_id} // []
$cache->{"paths"}{$leaf_id}
Truth table100%2/2
A
A= $cache->{"paths"}{$leaf_id}
| |||||||||||||||
| 237 | ); | ||||||||||||||
| 238 | } | ||||||||||||||
| 239 | |||||||||||||||
| 240 | 29 | 1; | |||||||||||||
| 241 | |||||||||||||||
| 242 | __END__ | ||||||||||||||