Class: Bitcoin::Protocol::TxOut

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TxOut

Returns a new instance of TxOut.



17
18
19
20
21
22
23
# File 'lib/bitcoin/protocol/txout.rb', line 17

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

#redeem_scriptObject

p2sh redeem script (optional, not included in the serialized binary format)



15
16
17
# File 'lib/bitcoin/protocol/txout.rb', line 15

def redeem_script
  @redeem_script
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



68
69
70
71
72
# File 'lib/bitcoin/protocol/txout.rb', line 68

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

.from_io(buf) ⇒ Object



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

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



86
87
88
89
90
# File 'lib/bitcoin/protocol/txout.rb', line 86

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



25
26
27
# File 'lib/bitcoin/protocol/txout.rb', line 25

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

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

parse raw binary data for transaction output



30
31
32
33
34
# File 'lib/bitcoin/protocol/txout.rb', line 30

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



41
42
43
44
45
# File 'lib/bitcoin/protocol/txout.rb', line 41

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

#parsed_scriptObject



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

def parsed_script
  @parsed_script ||= Bitcoin::Script.new(pk_script)
end

#to_hash(options = {}) ⇒ Object



61
62
63
64
65
66
# File 'lib/bitcoin/protocol/txout.rb', line 61

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

#to_null_payloadObject



57
58
59
# File 'lib/bitcoin/protocol/txout.rb', line 57

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

#to_payloadObject



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

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