Skip to content

Commit c9237d3

Browse files
committed
Add P2WPKH PSBT signing and finalizing test, fix P2PKH test
1 parent 57a3123 commit c9237d3

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

lib/Bitcoin/Crypto/Role/PSBT/Finalizer.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ sub _should_finalize_P2WPKH
6868
my @partials = $self->get_all_fields('PSBT_IN_PARTIAL_SIG', $input_index);
6969
return () unless @partials;
7070

71-
my $wpkh = $input->utxo->output->get_raw_address;
71+
my $wpkh = $input->utxo->output->locking_script->get_raw_address;
7272
foreach my $sig (@partials) {
7373
return ({type => 'witness', sigs => [$sig->value, $sig->key->to_serialized]})
7474
if $sig->key->get_hash eq $wpkh;

t/PSBT/roles.t

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use Test2::V0;
2-
use Bitcoin::Crypto qw(btc_prv btc_psbt);
2+
use Bitcoin::Crypto qw(btc_transaction btc_prv btc_psbt);
33
use Bitcoin::Crypto::Constants qw(:transaction);
44
use Bitcoin::Crypto::Util qw(to_format);
55
use Bitcoin::Crypto::Transaction::Output;
@@ -55,14 +55,46 @@ my @fields = (
5555
);
5656

5757
subtest 'should sign and set final signatures when finalizing P2PKH input' => sub {
58+
my $utxo_tx = btc_transaction->new;
59+
60+
# utxo input is mandatory, so make a fake one
61+
$utxo_tx->add_input(
62+
utxo => [[hex => '1d350125e4839360ade708a32c64548b0ffde92421bc3348eaf07a55ad875651'], 0],
63+
);
64+
65+
# utxo output will be used for signing
66+
$utxo_tx->add_output(
67+
value => 10_000,
68+
locking_script => [address => $priv->get_public_key->get_legacy_address],
69+
);
70+
71+
my $psbt = build_psbt(
72+
@fields,
73+
{
74+
type => 'PSBT_IN_NON_WITNESS_UTXO',
75+
index => 0,
76+
value => $utxo_tx,
77+
}
78+
);
79+
80+
# adjust utxo transaction id (must match)
81+
$psbt->get_field('PSBT_IN_PREVIOUS_TXID', 0)->set_value($utxo_tx->get_hash);
82+
83+
is $psbt->sign($priv), 1, 'an input was signed';
84+
$psbt->finalize;
85+
86+
ok lives { $psbt->get_transaction->verify }, 'verification passed';
87+
};
88+
89+
subtest 'should sign and set final signatures when finalizing P2WPKH input' => sub {
5890
my $psbt = build_psbt(
5991
@fields,
6092
{
6193
type => 'PSBT_IN_WITNESS_UTXO',
6294
index => 0,
6395
value => Bitcoin::Crypto::Transaction::Output->new(
6496
value => 10_000,
65-
locking_script => [address => $priv->get_public_key->get_legacy_address],
97+
locking_script => [address => $priv->get_public_key->get_segwit_address],
6698
),
6799
}
68100
);

0 commit comments

Comments
 (0)