Class: BSV::Transaction::TransactionInput
- Inherits:
-
Object
- Object
- BSV::Transaction::TransactionInput
- Defined in:
- lib/bsv/transaction/transaction_input.rb
Overview
A transaction input referencing a previous output to spend.
Inputs identify the output being spent by its transaction ID and output index (the “outpoint”), and provide an unlocking script to satisfy the locking script conditions.
Instance Attribute Summary collapse
-
#prev_tx_id ⇒ String
readonly
32-byte transaction ID of the output being spent (internal byte order).
-
#prev_tx_out_index ⇒ Integer
readonly
Index of the output within the previous transaction.
-
#sequence ⇒ Integer
Sequence number (default: 0xFFFFFFFF).
-
#source_locking_script ⇒ Script::Script?
Locking script of the source output (needed for sighash).
-
#source_satoshis ⇒ Integer?
Satoshi value of the source output (needed for sighash).
-
#source_transaction ⇒ Transaction?
The full source transaction (for BEEF wiring).
-
#unlocking_script ⇒ Script::Script?
The unlocking script (set after signing).
-
#unlocking_script_template ⇒ UnlockingScriptTemplate?
Template for deferred signing.
Class Method Summary collapse
-
.from_binary(data, offset = 0) ⇒ Array(TransactionInput, Integer)
Deserialise a transaction input from binary data.
-
.txid_from_hex(hex) ⇒ String
Convert a hex transaction ID to internal byte order (reversed).
Instance Method Summary collapse
-
#initialize(prev_tx_id:, prev_tx_out_index:, unlocking_script: nil, sequence: 0xFFFFFFFF) ⇒ TransactionInput
constructor
A new instance of TransactionInput.
-
#outpoint_binary ⇒ String
Serialise the outpoint (prev_tx_id + output index) as binary.
-
#to_binary ⇒ String
Serialise the input to its binary wire format.
-
#txid_hex ⇒ String
The previous transaction ID in hex (display order).
Constructor Details
#initialize(prev_tx_id:, prev_tx_out_index:, unlocking_script: nil, sequence: 0xFFFFFFFF) ⇒ TransactionInput
Returns a new instance of TransactionInput.
39 40 41 42 43 44 |
# File 'lib/bsv/transaction/transaction_input.rb', line 39 def initialize(prev_tx_id:, prev_tx_out_index:, unlocking_script: nil, sequence: 0xFFFFFFFF) @prev_tx_id = prev_tx_id.b @prev_tx_out_index = prev_tx_out_index @unlocking_script = unlocking_script @sequence = sequence end |
Instance Attribute Details
#prev_tx_id ⇒ String (readonly)
Returns 32-byte transaction ID of the output being spent (internal byte order).
12 13 14 |
# File 'lib/bsv/transaction/transaction_input.rb', line 12 def prev_tx_id @prev_tx_id end |
#prev_tx_out_index ⇒ Integer (readonly)
Returns index of the output within the previous transaction.
15 16 17 |
# File 'lib/bsv/transaction/transaction_input.rb', line 15 def prev_tx_out_index @prev_tx_out_index end |
#sequence ⇒ Integer
Returns sequence number (default: 0xFFFFFFFF).
18 19 20 |
# File 'lib/bsv/transaction/transaction_input.rb', line 18 def sequence @sequence end |
#source_locking_script ⇒ Script::Script?
Returns locking script of the source output (needed for sighash).
27 28 29 |
# File 'lib/bsv/transaction/transaction_input.rb', line 27 def source_locking_script @source_locking_script end |
#source_satoshis ⇒ Integer?
Returns satoshi value of the source output (needed for sighash).
24 25 26 |
# File 'lib/bsv/transaction/transaction_input.rb', line 24 def source_satoshis @source_satoshis end |
#source_transaction ⇒ Transaction?
Returns the full source transaction (for BEEF wiring).
30 31 32 |
# File 'lib/bsv/transaction/transaction_input.rb', line 30 def source_transaction @source_transaction end |
#unlocking_script ⇒ Script::Script?
Returns the unlocking script (set after signing).
21 22 23 |
# File 'lib/bsv/transaction/transaction_input.rb', line 21 def unlocking_script @unlocking_script end |
#unlocking_script_template ⇒ UnlockingScriptTemplate?
Returns template for deferred signing.
33 34 35 |
# File 'lib/bsv/transaction/transaction_input.rb', line 33 def unlocking_script_template @unlocking_script_template end |
Class Method Details
.from_binary(data, offset = 0) ⇒ Array(TransactionInput, Integer)
Deserialise a transaction input from binary data.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/bsv/transaction/transaction_input.rb', line 63 def self.from_binary(data, offset = 0) if data.bytesize < offset + 36 raise ArgumentError, "truncated input: need 36 bytes for outpoint at offset #{offset}, got #{data.bytesize - offset}" end prev_tx_id = data.byteslice(offset, 32) prev_tx_out_index = data.byteslice(offset + 32, 4).unpack1('V') offset += 36 script_len, vi_size = VarInt.decode(data, offset) offset += vi_size if data.bytesize < offset + script_len raise ArgumentError, "truncated input: need #{script_len} bytes for script at offset #{offset}, got #{data.bytesize - offset}" end unlocking_script = (BSV::Script::Script.from_binary(data.byteslice(offset, script_len)) if script_len.positive?) offset += script_len if data.bytesize < offset + 4 raise ArgumentError, "truncated input: need 4 bytes for sequence at offset #{offset}, got #{data.bytesize - offset}" end sequence = data.byteslice(offset, 4).unpack1('V') total = 36 + vi_size + script_len + 4 input = new( prev_tx_id: prev_tx_id, prev_tx_out_index: prev_tx_out_index, unlocking_script: unlocking_script, sequence: sequence ) [input, total] end |
.txid_from_hex(hex) ⇒ String
Convert a hex transaction ID to internal byte order (reversed).
105 106 107 |
# File 'lib/bsv/transaction/transaction_input.rb', line 105 def self.txid_from_hex(hex) [hex].pack('H*').reverse end |
Instance Method Details
#outpoint_binary ⇒ String
Serialise the outpoint (prev_tx_id + output index) as binary.
112 113 114 |
# File 'lib/bsv/transaction/transaction_input.rb', line 112 def outpoint_binary @prev_tx_id + [@prev_tx_out_index].pack('V') end |
#to_binary ⇒ String
Serialise the input to its binary wire format.
49 50 51 52 53 54 55 56 |
# File 'lib/bsv/transaction/transaction_input.rb', line 49 def to_binary script_bytes = @unlocking_script ? @unlocking_script.to_binary : ''.b @prev_tx_id + [@prev_tx_out_index].pack('V') + VarInt.encode(script_bytes.bytesize) + script_bytes + [@sequence].pack('V') end |
#txid_hex ⇒ String
The previous transaction ID in hex (display order).
119 120 121 |
# File 'lib/bsv/transaction/transaction_input.rb', line 119 def txid_hex @prev_tx_id.reverse.unpack1('H*') end |