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