Class: Bitcoin::TxOut

Inherits:
Object show all
Includes:
OpenAssets::MarkerOutput
Defined in:
lib/bitcoin/tx_out.rb

Overview

transaction output

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OpenAssets::MarkerOutput

#open_assets_marker?

Constructor Details

#initialize(value: 0, script_pubkey: nil) ⇒ TxOut

Returns a new instance of TxOut.



14
15
16
17
# File 'lib/bitcoin/tx_out.rb', line 14

def initialize(value: 0, script_pubkey: nil)
  @value = value
  @script_pubkey = script_pubkey
end

Instance Attribute Details

#script_pubkeyObject

Returns the value of attribute script_pubkey.



12
13
14
# File 'lib/bitcoin/tx_out.rb', line 12

def script_pubkey
  @script_pubkey
end

#valueObject

Returns the value of attribute value.



11
12
13
# File 'lib/bitcoin/tx_out.rb', line 11

def value
  @value
end

Class Method Details

.parse_from_payload(payload) ⇒ Object



19
20
21
22
23
24
# File 'lib/bitcoin/tx_out.rb', line 19

def self.parse_from_payload(payload)
  buf = payload.is_a?(String) ? StringIO.new(payload) : payload
  value = buf.read(8).unpack('q').first
  script_size = Bitcoin.unpack_var_int_from_io(buf)
  new(value: value, script_pubkey: Script.parse_from_payload(buf.read(script_size)))
end

Instance Method Details

#==(other) ⇒ Object



44
45
46
# File 'lib/bitcoin/tx_out.rb', line 44

def ==(other)
  to_payload == other.to_payload
end

#to_empty_payloadObject



31
32
33
# File 'lib/bitcoin/tx_out.rb', line 31

def to_empty_payload
  'ffffffffffffffff00'.htb
end

#to_hObject



40
41
42
# File 'lib/bitcoin/tx_out.rb', line 40

def to_h
  {value: value_to_btc, script_pubkey: script_pubkey.to_h}
end

#to_payloadObject



26
27
28
29
# File 'lib/bitcoin/tx_out.rb', line 26

def to_payload
  s = script_pubkey.to_payload
  [value].pack('Q') << Bitcoin.pack_var_int(s.length) << s
end

#value_to_btcObject

convert satoshi to btc



36
37
38
# File 'lib/bitcoin/tx_out.rb', line 36

def value_to_btc
  value / 100000000.0
end