Class: BSV::Transaction::TransactionOutput

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(satoshis:, locking_script:, change: false) ⇒ TransactionOutput

Returns a new instance of TransactionOutput.

Parameters:

  • satoshis (Integer)

    output value in satoshis

  • locking_script (Script::Script)

    the locking script

  • change (Boolean) (defaults to: false)

    whether this is a change output (default: false)



23
24
25
26
27
# File 'lib/bsv/transaction/transaction_output.rb', line 23

def initialize(satoshis:, locking_script:, change: false)
  @satoshis = satoshis
  @locking_script = locking_script
  @change = change
end

Instance Attribute Details

#changeBoolean

Returns whether this output receives change.

Returns:

  • (Boolean)

    whether this output receives change



18
19
20
# File 'lib/bsv/transaction/transaction_output.rb', line 18

def change
  @change
end

#locking_scriptScript::Script

Returns the locking script (spending conditions).

Returns:



15
16
17
# File 'lib/bsv/transaction/transaction_output.rb', line 15

def locking_script
  @locking_script
end

#satoshisInteger

Returns the output value in satoshis.

Returns:

  • (Integer)

    the output value in satoshis



12
13
14
# File 'lib/bsv/transaction/transaction_output.rb', line 12

def satoshis
  @satoshis
end

Class Method Details

.from_binary(data, offset = 0) ⇒ Array(TransactionOutput, Integer)

Deserialise a transaction output from binary data.

Parameters:

  • data (String)

    binary data

  • offset (Integer) (defaults to: 0)

    byte offset to start reading from

Returns:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bsv/transaction/transaction_output.rb', line 42

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

#to_binaryString

Serialise the output to its binary wire format.

Returns:

  • (String)

    binary output (8-byte LE satoshis + varint + script)



32
33
34
35
# File 'lib/bsv/transaction/transaction_output.rb', line 32

def to_binary
  script_bytes = @locking_script.to_binary
  [satoshis].pack('Q<') + VarInt.encode(script_bytes.bytesize) + script_bytes
end