Class: BSV::Transaction::P2PKH
- Inherits:
-
UnlockingScriptTemplate
- Object
- UnlockingScriptTemplate
- BSV::Transaction::P2PKH
- Defined in:
- lib/bsv/transaction/p2pkh.rb
Overview
P2PKH unlocking script template for deferred transaction signing.
Holds a private key and sighash type, generating the unlocking script (signature + public key) when #sign is called with the full transaction.
Constant Summary collapse
- ESTIMATED_SCRIPT_LENGTH =
Estimated unlocking script length: 1 + ~72 (DER sig + hashtype) + 1 + 33 (compressed pubkey).
107
Instance Method Summary collapse
-
#estimated_length(_tx, _input_index) ⇒ Integer
Estimated script length for fee calculation.
-
#initialize(private_key, sighash_type: Sighash::ALL_FORK_ID) ⇒ P2PKH
constructor
A new instance of P2PKH.
-
#sign(tx, input_index) ⇒ Script::Script
Generate the P2PKH unlocking script for the given input.
Constructor Details
#initialize(private_key, sighash_type: Sighash::ALL_FORK_ID) ⇒ P2PKH
Returns a new instance of P2PKH.
19 20 21 22 23 |
# File 'lib/bsv/transaction/p2pkh.rb', line 19 def initialize(private_key, sighash_type: Sighash::ALL_FORK_ID) super() @private_key = private_key @sighash_type = sighash_type end |
Instance Method Details
#estimated_length(_tx, _input_index) ⇒ Integer
Returns estimated script length for fee calculation.
39 40 41 |
# File 'lib/bsv/transaction/p2pkh.rb', line 39 def estimated_length(_tx, _input_index) ESTIMATED_SCRIPT_LENGTH end |
#sign(tx, input_index) ⇒ Script::Script
Generate the P2PKH unlocking script for the given input.
30 31 32 33 34 35 36 |
# File 'lib/bsv/transaction/p2pkh.rb', line 30 def sign(tx, input_index) hash = tx.sighash(input_index, @sighash_type) signature = @private_key.sign(hash) sig_with_hashtype = signature.to_der + [@sighash_type].pack('C') pubkey_bytes = @private_key.public_key.compressed BSV::Script::Script.p2pkh_unlock(sig_with_hashtype, pubkey_bytes) end |