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