| 1 | package Bitcoin::Crypto::Transaction::Signer::P2TR; | ||||||||||||||||||||||||||||||||||
| 2 | 7 | $Bitcoin::Crypto::Transaction::Signer::P2TR::VERSION = '4.006'; | |||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use v5.14; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Mooish::Base -standard; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Bitcoin::Crypto::Exception; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use Bitcoin::Crypto::Constants qw(:sighash); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use Bitcoin::Crypto::Types -types; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use Bitcoin::Crypto::Script::Runner; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | 7 | ▶ | use Bitcoin::Crypto::Util::Internal qw(get_taproot_ext); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | 7 | extends 'Bitcoin::Crypto::Transaction::Signer::Taproot'; | |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | 7 | has option 'script_tree' => ( | |||||||||||||||||||||||||||||||||
| 17 | coerce => BitcoinScriptTree, | ||||||||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | 7 | has option 'leaf_id' => ( | |||||||||||||||||||||||||||||||||
| 21 | coerce => ByteStr, | ||||||||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | 7 | has option 'public_key' => ( | |||||||||||||||||||||||||||||||||
| 25 | isa => InstanceOf ['Bitcoin::Crypto::Key::Public'], | ||||||||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||
| 28 | 7 | has param 'taproot_ext_flag' => ( | |||||||||||||||||||||||||||||||||
| 29 | isa => PositiveOrZeroInt, | ||||||||||||||||||||||||||||||||||
| 30 | default => 1, | ||||||||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | 7 | has field 'script_spend' => ( | |||||||||||||||||||||||||||||||||
| 34 | lazy => 1, | ||||||||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||
| 37 | 7 | has extended 'script' => ( | |||||||||||||||||||||||||||||||||
| 38 | lazy => 1, | ||||||||||||||||||||||||||||||||||
| 39 | init_arg => undef, | ||||||||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||
| 42 | sub _build_script | ||||||||||||||||||||||||||||||||||
| 43 | 31 | ▶ | { | ||||||||||||||||||||||||||||||||
|
Subroutine
_build_script: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 44 | 31 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||
| 46 | 31 | ▶ | if ($self->script_spend) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 47 | 18 | return $self->script_tree->get_leaf($self->leaf_id)->script; | |||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||
| 49 | else { | ||||||||||||||||||||||||||||||||||
| 50 | 13 | my $segwit_program = $self->transaction->inputs->[$self->signing_index]->utxo->output->locking_script; | |||||||||||||||||||||||||||||||||
| 51 | 13 | my $runner = $segwit_program->run; | |||||||||||||||||||||||||||||||||
| 52 | 13 | my $pubkey = $runner->stack->[-1]; | |||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | 13 | return Bitcoin::Crypto::Script::Common->new(TR => $pubkey); | |||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||
| 58 | sub _build_script_spend | ||||||||||||||||||||||||||||||||||
| 59 | 31 | ▶ | { | ||||||||||||||||||||||||||||||||
|
Subroutine
_build_script_spend: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||||||||
| 60 | 31 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||
| 62 | 31 | ▶ | return $self->has_leaf_id && $self->has_script_tree && $self->has_public_key; | ||||||||||||||||||||||||||||||||
|
Condition66%4/6
$self->has_leaf_id && $self->has_script_tree && $self->has_public_key
$self->has_leaf_id and $self->has_script_tree
MC/DC33%1/3
$self->has_leaf_id && $self->has_script_tree && $self->has_public_key
$self->has_leaf_id
$self->has_script_tree
$self->has_public_key
Truth table50%2/4
A and B && C
A= $self->has_leaf_idB= $self->has_script_treeC= $self->has_public_key
| |||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | sub _build_runner | ||||||||||||||||||||||||||||||||||
| 66 | 31 | ▶ | { | ||||||||||||||||||||||||||||||||
|
Subroutine
_build_runner: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 67 | 31 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||
| 69 | 31 | my $runner = $self->SUPER::_build_runner; | |||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||
| 71 | 31 | ▶ | if ($self->script_spend) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 72 | 18 | $runner->transaction->set_taproot_ext_flag(1); | |||||||||||||||||||||||||||||||||
| 73 | 18 | $runner->transaction->set_script_tree($self->script_tree); | |||||||||||||||||||||||||||||||||
| 74 | 18 | $runner->transaction->set_leaf_id($self->leaf_id); | |||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||
| 77 | 31 | return $runner; | |||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||
| 80 | sub _get_taproot_ext | ||||||||||||||||||||||||||||||||||
| 81 | 22 | ▶ | { | ||||||||||||||||||||||||||||||||
|
Subroutine
_get_taproot_ext: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 82 | 22 | my ($self, $codesep_pos) = @_; | |||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||
| 84 | 22 | ▶ | return undef unless $self->script_spend; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | 9 | my $ext = get_taproot_ext( | |||||||||||||||||||||||||||||||||
| 87 | $self->taproot_ext_flag, { | ||||||||||||||||||||||||||||||||||
| 88 | script_tree => $self->script_tree, | ||||||||||||||||||||||||||||||||||
| 89 | leaf_id => $self->leaf_id, | ||||||||||||||||||||||||||||||||||
| 90 | codesep_pos => $codesep_pos, | ||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||
| 92 | ); | ||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | 9 | return $ext; | |||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||
| 97 | sub _find_next_sigop | ||||||||||||||||||||||||||||||||||
| 98 | 31 | ▶ | { | ||||||||||||||||||||||||||||||||
|
Subroutine
_find_next_sigop: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 99 | 31 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | 31 | my $runner = $self->SUPER::_find_next_sigop; | |||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||
| 103 | # set some budget to avoid failure | ||||||||||||||||||||||||||||||||||
| 104 | 30 | $runner->transaction->set_sigop_budget(0); | |||||||||||||||||||||||||||||||||
| 105 | 30 | return $runner; | |||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||
| 108 | sub _initialize | ||||||||||||||||||||||||||||||||||
| 109 | 31 | ▶ | { | ||||||||||||||||||||||||||||||||
|
Subroutine
_initialize: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 110 | 31 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | 31 | ▶ | if ($self->script_spend) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 113 | 18 | my $control_block = $self->script_tree->get_control_block($self->leaf_id, $self->public_key); | |||||||||||||||||||||||||||||||||
| 114 | 18 | push @{$self->signature}, $control_block->to_serialized, $self->script->to_serialized; | |||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||
| 118 | sub _get_signature | ||||||||||||||||||||||||||||||||||
| 119 | 22 | ▶ | { | ||||||||||||||||||||||||||||||||
|
Subroutine
_get_signature: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||
| 120 | 22 | my ($self, $privkey, $args) = @_; | |||||||||||||||||||||||||||||||||
| 121 | 22 | my $runner = $self->_runner; | |||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||
| 123 | # make sure we have a taproot output key | ||||||||||||||||||||||||||||||||||
| 124 | # NOTE: if this is a script spend, tweaking the key with script tree is | ||||||||||||||||||||||||||||||||||
| 125 | # erroneus - the tree merkle root itself will be affected by the public | ||||||||||||||||||||||||||||||||||
| 126 | # key, so it's not possible to do it that way. | ||||||||||||||||||||||||||||||||||
| 127 | 22 | ▶ | $privkey = $privkey->get_taproot_output_key( | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $self->script_spend and $self->has_script_tree
MC/DC100%2/2
not $self->script_spend and $self->has_script_tree
not $self->script_spend
$self->has_script_tree
Truth table100%3/3
not(A) and B
A= not $self->script_spendB= $self->has_script_tree
| |||||||||||||||||||||||||||||||||||
| 128 | !$self->script_spend && $self->has_script_tree ? $self->script_tree->get_merkle_root : undef | ||||||||||||||||||||||||||||||||||
| 129 | ); | ||||||||||||||||||||||||||||||||||
| 130 | 22 | my $pubkey = $privkey->get_public_key; | |||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||
| 132 | # taproot opcodes simplify things - no need to handle multisig | ||||||||||||||||||||||||||||||||||
| 133 | 22 | my $script_pubkey = $runner->stack->[-1]; | |||||||||||||||||||||||||||||||||
| 134 | 22 | ▶ | Bitcoin::Crypto::Exception::Sign->raise( | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 135 | 'bad private key for public key encountered in script sigop at position ' . $runner->pos | ||||||||||||||||||||||||||||||||||
| 136 | ) unless $script_pubkey eq $pubkey->get_xonly_key; | ||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||
| 138 | my $digest_obj = $runner->transaction->get_digest_object( | ||||||||||||||||||||||||||||||||||
| 139 | sighash => $args->{sighash}, | ||||||||||||||||||||||||||||||||||
| 140 | 22 | taproot_ext => $self->_get_taproot_ext($runner->codeseparator), | |||||||||||||||||||||||||||||||||
| 141 | ); | ||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||
| 143 | 22 | my $signature = $privkey->sign_message($digest_obj->get_digest); | |||||||||||||||||||||||||||||||||
| 144 | 21 | ▶ | if ($digest_obj->sighash != SIGHASH_DEFAULT) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 145 | 10 | $signature .= pack 'C', $digest_obj->sighash; | |||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||
| 148 | 21 | return $signature; | |||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||
| 151 | 7 | 1; | |||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||