Class: BSV::Transaction::TransactionOutput
- Inherits:
-
Object
- Object
- BSV::Transaction::TransactionOutput
- Defined in:
- lib/bsv/transaction/transaction_output.rb
Overview
A transaction output specifying an amount and spending conditions.
Each output locks a number of satoshis behind a locking script. Outputs are consumed by transaction inputs that provide matching unlocking scripts.
Instance Attribute Summary collapse
-
#change ⇒ Boolean
Whether this output receives change.
-
#locking_script ⇒ Object
Returns the value of attribute locking_script.
-
#satoshis ⇒ Object
Returns the value of attribute satoshis.
Class Method Summary collapse
-
.from_binary(data, offset = 0) ⇒ Array(TransactionOutput, Integer)
Deserialise a transaction output from binary data.
Instance Method Summary collapse
-
#initialize(satoshis:, locking_script:, change: false) ⇒ TransactionOutput
constructor
A new instance of TransactionOutput.
-
#initialize_copy(other) ⇒ Object
Called by
#dupand#clone. -
#to_binary ⇒ String
Serialise the output to its binary wire format.
Constructor Details
#initialize(satoshis:, locking_script:, change: false) ⇒ TransactionOutput
Returns a new instance of TransactionOutput.
29 30 31 32 33 34 |
# File 'lib/bsv/transaction/transaction_output.rb', line 29 def initialize(satoshis:, locking_script:, change: false) @satoshis = satoshis @locking_script = locking_script @change = change @owning_tx = nil end |
Instance Attribute Details
#change ⇒ Boolean
Returns whether this output receives change.
24 25 26 |
# File 'lib/bsv/transaction/transaction_output.rb', line 24 def change @change end |
#locking_script ⇒ Object
Returns the value of attribute locking_script.
21 22 23 |
# File 'lib/bsv/transaction/transaction_output.rb', line 21 def locking_script @locking_script end |
#satoshis ⇒ Object
Returns the value of attribute satoshis.
15 16 17 |
# File 'lib/bsv/transaction/transaction_output.rb', line 15 def satoshis @satoshis end |
Class Method Details
.from_binary(data, offset = 0) ⇒ Array(TransactionOutput, Integer)
Deserialise a transaction output from binary data.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/bsv/transaction/transaction_output.rb', line 82 def self.from_binary(data, offset = 0) if data.bytesize < offset + 8 raise ArgumentError, "truncated output: need 8 bytes for satoshis at offset #{offset}, got #{data.bytesize - offset}" end satoshis = data.byteslice(offset, 8).unpack1('Q<') offset += 8 script_len, vi_size = VarInt.decode(data, offset) offset += vi_size if data.bytesize < offset + script_len raise ArgumentError, "truncated output: need #{script_len} bytes for script at offset #{offset}, got #{data.bytesize - offset}" end script_bytes = data.byteslice(offset, script_len) locking_script = BSV::Script::Script.from_binary(script_bytes) [new(satoshis: satoshis, locking_script: locking_script), 8 + vi_size + script_len] end |
Instance Method Details
#initialize_copy(other) ⇒ Object
Called by #dup and #clone. Clears the owning-Tx backref so that the
cloned output does not belong to any transaction until it is explicitly
added via Tx#add_output.
39 40 41 42 |
# File 'lib/bsv/transaction/transaction_output.rb', line 39 def initialize_copy(other) super @owning_tx = nil end |
#to_binary ⇒ String
Memoised; see sighash-cache for the invalidation contract.
Serialise the output to its binary wire format.
70 71 72 73 74 75 |
# File 'lib/bsv/transaction/transaction_output.rb', line 70 def to_binary @to_binary ||= begin script_bytes = @locking_script.to_binary ([@satoshis].pack('Q<') + VarInt.encode(script_bytes.bytesize) + script_bytes).freeze end end |