Skip to content

Commit 5c85a5a

Browse files
committed
Resolve some TODO notes in PSBT Signer
1 parent 310acf4 commit 5c85a5a

1 file changed

Lines changed: 28 additions & 12 deletions

File tree

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

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ use Types::Common -sigs;
88

99
use Bitcoin::Crypto qw(btc_script_tree);
1010

11+
sub _input_has_witness_utxo
12+
{
13+
my ($self, $input_index) = @_;
14+
15+
my $witness_utxo = $self->get_all_fields('PSBT_IN_WITNESS_UTXO', $input_index);
16+
return !!$witness_utxo;
17+
}
18+
1119
sub _add_partial_signature
1220
{
1321
my ($self, $input_index, $key, $signature) = @_;
@@ -24,6 +32,8 @@ sub _do_sign_P2PKH
2432
{
2533
my ($self, $key, $tx, $input, $input_index) = @_;
2634

35+
return 0 if $self->_input_has_witness_utxo($input_index);
36+
2737
return 0 unless $key->get_public_key->get_hash eq $input->utxo->output->locking_script->get_raw_address;
2838
my $signer = $tx->sign(signing_index => $input_index);
2939
my $sighash = $self->get_all_fields('PSBT_IN_SIGHASH_TYPE', $input_index);
@@ -40,6 +50,11 @@ sub _do_sign_P2SH
4050
{
4151
my ($self, $key, $tx, $input, $input_index) = @_;
4252

53+
# TODO: If a redeemScript is provided, the scriptPubKey must be for that redeemScript
54+
55+
# TODO: only for normal P2SH (not nested segwit)
56+
return 0 if $self->_input_has_witness_utxo($input_index);
57+
4358
# TODO
4459
return 0;
4560
}
@@ -48,13 +63,24 @@ sub _do_sign_P2WPKH
4863
{
4964
my ($self, $key, $tx, $input, $input_index) = @_;
5065

51-
return $self->_do_sign_P2PKH($key, $tx, $input, $input_index);
66+
return 0 unless $key->get_public_key->get_hash eq $input->utxo->output->locking_script->get_raw_address;
67+
my $signer = $tx->sign(signing_index => $input_index);
68+
my $sighash = $self->get_all_fields('PSBT_IN_SIGHASH_TYPE', $input_index);
69+
70+
# use transaction signer's ability to give us the signature
71+
my $signature = $signer->add_signature($key, sighash => $sighash ? $sighash->value : undef)
72+
->signature->[-1];
73+
74+
$self->_add_partial_signature($input_index, $key, $signature);
75+
return 1;
5276
}
5377

5478
sub _do_sign_P2WSH
5579
{
5680
my ($self, $key, $tx, $input, $input_index) = @_;
5781

82+
# TODO: If a witnessScript is provided, the scriptPubKey or the redeemScript must be for that witnessScript
83+
5884
# TODO
5985
return 0;
6086
}
@@ -149,17 +175,7 @@ sub sign
149175

150176
return $signed;
151177

152-
# TODO: add PSBT_IN_PARTIAL_SIG to inputs
153-
# TODO: how to check if the input is ours?
154-
# TODO: use PSBT_IN_BIP32_DERIVATION to locate the key
155-
# TODO: create a signer and sign each type
156-
# TODO: BIP170 checks:
157-
# If a non-witness UTXO is provided, its hash must match the hash specified in the prevout
158-
# If a witness UTXO is provided, no non-witness signature may be created
159-
# If a redeemScript is provided, the scriptPubKey must be for that redeemScript
160-
# If a witnessScript is provided, the scriptPubKey or the redeemScript must be for that witnessScript
161-
# If a sighash type is provided, the signer must check that the sighash is acceptable. If unacceptable, they must fail.
162-
# If a sighash type is not provided, the signer should sign using SIGHASH_ALL, but may use any sighash type they wish.
178+
# TODO: If a sighash type is provided, the signer must check that the sighash is acceptable. If unacceptable, they must fail.
163179
# TODO: this note
164180
# For PSBTv2s, a signer must update the PSBT_GLOBAL_TX_MODIFIABLE field after signing inputs so that it accurately reflects the state of the PSBT. If the Signer added a signature that does not use SIGHASH_ANYONECANPAY, the Input Modifiable flag must be set to False. If the Signer added a signature that does not use SIGHASH_NONE, the Outputs Modifiable flag must be set to False. If the Signer added a signature that uses SIGHASH_SINGLE, the Has SIGHASH_SINGLE flag must be set to True.
165181
}

0 commit comments

Comments
 (0)