|
1 | 1 | use Test2::V0; |
2 | | -use Bitcoin::Crypto qw(btc_prv btc_psbt); |
| 2 | +use Bitcoin::Crypto qw(btc_transaction btc_prv btc_psbt); |
3 | 3 | use Bitcoin::Crypto::Constants qw(:transaction); |
4 | 4 | use Bitcoin::Crypto::Util qw(to_format); |
5 | 5 | use Bitcoin::Crypto::Transaction::Output; |
@@ -55,14 +55,46 @@ my @fields = ( |
55 | 55 | ); |
56 | 56 |
|
57 | 57 | 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 { |
58 | 90 | my $psbt = build_psbt( |
59 | 91 | @fields, |
60 | 92 | { |
61 | 93 | type => 'PSBT_IN_WITNESS_UTXO', |
62 | 94 | index => 0, |
63 | 95 | value => Bitcoin::Crypto::Transaction::Output->new( |
64 | 96 | 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], |
66 | 98 | ), |
67 | 99 | } |
68 | 100 | ); |
|
0 commit comments