Class: Bitcoin::Protocol::TxOut

Inherits:
Object
  • Object
show all
Defined in:
lib/bitcoin/protocol/txout.rb

Direct Known Subclasses

Storage::Models::TxOut

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TxOut

Returns a new instance of TxOut.



14
15
16
17
18
19
20
# File 'lib/bitcoin/protocol/txout.rb', line 14

def initialize *args
  if args.size == 2
    @value, @pk_script_length, @pk_script = args[0], args[1].bytesize, args[1]
  else
    @value, @pk_script_length, @pk_script = *args
  end
end

Instance Attribute Details

#pk_scriptObject Also known as: script

pk_script output Script



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

def pk_script
  @pk_script
end

#pk_script_lengthObject

pk_script output Script



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

def pk_script_length
  @pk_script_length
end

#valueObject Also known as: amount

output value (in base units; “satoshi”)



9
10
11
# File 'lib/bitcoin/protocol/txout.rb', line 9

def value
  @value
end

Class Method Details

.from_hash(output) ⇒ Object



66
67
68
69
70
# File 'lib/bitcoin/protocol/txout.rb', line 66

def self.from_hash(output)
  amount = output['value'].gsub('.','').to_i
  script = Script.binary_from_string(output['scriptPubKey'])
  new(amount, script)
end

.from_io(buf) ⇒ Object



33
34
35
# File 'lib/bitcoin/protocol/txout.rb', line 33

def self.from_io(buf)
  txout = new; txout.parse_data_from_io(buf); txout
end

.value_to_address(value, address) ⇒ Object

create output spending value btc (base units) to address



83
84
85
86
87
# File 'lib/bitcoin/protocol/txout.rb', line 83

def self.value_to_address(value, address)
  pk_script = Bitcoin::Script.to_address_script(address)
  raise "Script#pk_script nil with address #{address}" unless pk_script
  new(value, pk_script)
end

Instance Method Details

#==(other) ⇒ Object



22
23
24
# File 'lib/bitcoin/protocol/txout.rb', line 22

def ==(other)
  @value == other.value && @pk_script == other.pk_script
end

#get_scriptObject



46
47
48
# File 'lib/bitcoin/protocol/txout.rb', line 46

def get_script
  @script_cache || Bitcoin::Script.new(@pk_script)
end

#parse_data(data) ⇒ Object Also known as: parse_payload

parse raw binary data for transaction output



27
28
29
30
31
# File 'lib/bitcoin/protocol/txout.rb', line 27

def parse_data(data)
  buf = data.is_a?(String) ? StringIO.new(data) : data
  parse_data_from_io(buf)
  buf.pos
end

#parse_data_from_io(buf) ⇒ Object

parse raw binary data for transaction output



38
39
40
41
42
# File 'lib/bitcoin/protocol/txout.rb', line 38

def parse_data_from_io(buf)
  @value = buf.read(8).unpack("Q")[0]
  @pk_script_length = Protocol.unpack_var_int_from_io(buf)
  @pk_script = buf.read(@pk_script_length)
end

#to_hash(options = {}) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/bitcoin/protocol/txout.rb', line 58

def to_hash(options = {})
  script = get_script
  h = { 'value' => "%.8f" % (@value / 100000000.0),
    'scriptPubKey' => script.to_string }
  h["address"] = script.get_address  if script.is_hash160? && options[:with_address]
  h
end

#to_null_payloadObject



54
55
56
# File 'lib/bitcoin/protocol/txout.rb', line 54

def to_null_payload
  self.class.new(-1, '').to_payload
end

#to_payloadObject



50
51
52
# File 'lib/bitcoin/protocol/txout.rb', line 50

def to_payload
  [@value].pack("Q") << Protocol.pack_var_int(@pk_script_length) << @pk_script
end