@@ -44,7 +44,7 @@ sub _traverse
4444 state $leaf_type = Dict [
4545 leaf_version => IntMaxBits [8],
4646 script => BitcoinScript,
47- id => Optional [Int ],
47+ id => Optional [ByteStr ],
4848 hash => Optional [ByteStr],
4949 ];
5050
@@ -59,6 +59,8 @@ sub _traverse
5959 $value -> {hash } =
6060 tagged_hash(' TapLeaf' , join ' ' , pack (' C' , $value -> {leaf_version }), $script_len , $script );
6161 }
62+
63+ $value -> {id } //= $value -> {hash };
6264 }
6365
6466 $leaf_action -> ($value , scalar @stack ) if defined $leaf_action ;
@@ -153,11 +155,16 @@ sub get_merkle_root
153155 return $self -> _tree_cache-> {root }{hash };
154156}
155157
158+ signature_for _get_tapleaf => (
159+ method => !!1,
160+ positional => [ByteStr],
161+ );
162+
156163sub _get_tapleaf
157164{
158165 my ($self , $leaf_id ) = @_ ;
159166
160- my $leaf = first { exists $_ -> {id } && $_ -> { id } == $leaf_id } @{$self -> _tree_cache-> {leaves }};
167+ my $leaf = first { $_ -> {id } eq $leaf_id } @{$self -> _tree_cache-> {leaves }};
161168 Bitcoin::Crypto::Exception::ScriptTree-> raise(
162169 " no such block with id=$leaf_id "
163170 ) unless defined $leaf ;
@@ -218,7 +225,7 @@ sub from_path
218225
219226signature_for get_control_block => (
220227 method => !!1,
221- positional => [Int , InstanceOf [' Bitcoin::Crypto::Key::Public' ]],
228+ positional => [ByteStr , InstanceOf [' Bitcoin::Crypto::Key::Public' ]],
222229);
223230
224231sub get_control_block
@@ -258,7 +265,7 @@ Bitcoin::Crypto::Script::Tree - BIP341 Script trees
258265 {hash => [hex => 'f154e8e8e17c31d3462d7132589ed29353c6fafdb884c5a6e04ea938834f0d9d']},
259266 [
260267 {
261- id => 1 ,
268+ id => 'leaf_1' ,
262269 leaf_version => TAPSCRIPT_LEAF_VERSION,
263270 script => [hex => '20d5094d2dbe9b76e2c245a2b89b6006888952e2faa6a149ae318d69e520617748ac']
264271 },
@@ -281,13 +288,14 @@ trees are used by taproot and are necessary to build custom taproot scripts.
281288Each leaf in the tree is represented with this Perl structure:
282289
283290 {
284- id => integer (optional),
291+ id => bytestring (optional),
285292 leaf_version => integer,
286293 script => Bitcoin::Crypto::Script instance (or its coercible),
287294 }
288295
289296Optional C<id > is used to identify the leaf in the tree, which is used in
290- methods like L</get_control_block> .
297+ methods like L</get_control_block> . If it is not present, it is automatically
298+ assigned as the hash of the leaf.
291299
292300Currently, C<leaf_version > must be equal to
293301L<Bitcoin::Crypto::Constants/TAPSCRIPT_LEAF_VERSION> , since other
@@ -328,18 +336,16 @@ Example structure:
328336 # tree with all scripts known
329337 [
330338 {
331- id => 0,
332339 leaf_version => TAPSCRIPT_LEAF_VERSION,
333340 script => [hex => '2071981521ad9fc9036687364118fb6ccd2035b96a423c59c5430e98310a11abe2ac']
334341 },
335342 [
336343 {
337- id => 1 ,
344+ id => 'interesting leaf' ,
338345 leaf_version => TAPSCRIPT_LEAF_VERSION,
339346 script => [hex => '20d5094d2dbe9b76e2c245a2b89b6006888952e2faa6a149ae318d69e520617748ac']
340347 },
341348 {
342- id => 2,
343349 leaf_version => TAPSCRIPT_LEAF_VERSION,
344350 script => [hex => '20c440b462ad48c7a77f94cd4532d8f2119dcebbd7c9764557e62726419b08ad4cac']
345351 }
0 commit comments