Class: Bitcoin::Protocol::TxOut
- Inherits:
-
Object
- Object
- Bitcoin::Protocol::TxOut
- Defined in:
- lib/bitcoin/protocol/txout.rb
Instance Attribute Summary collapse
-
#pk_script ⇒ Object
(also: #script)
pk_script output Script.
-
#pk_script_length ⇒ Object
pk_script output Script.
-
#redeem_script ⇒ Object
p2sh redeem script (optional, not included in the serialized binary format).
-
#value ⇒ Object
(also: #amount)
output value (in base units; “satoshi”).
Class Method Summary collapse
- .from_hash(output) ⇒ Object
- .from_io(buf) ⇒ Object
-
.value_to_address(value, address) ⇒ Object
create output spending
value
btc (base units) toaddress
.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(*args) ⇒ TxOut
constructor
A new instance of TxOut.
-
#parse_data(data) ⇒ Object
(also: #parse_payload)
parse raw binary data for transaction output.
-
#parse_data_from_io(buf) ⇒ Object
parse raw binary data for transaction output.
- #parsed_script ⇒ Object
- #to_hash(options = {}) ⇒ Object
- #to_null_payload ⇒ Object
- #to_payload ⇒ Object
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_script ⇒ Object 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_length ⇒ Object
pk_script output Script
12 13 14 |
# File 'lib/bitcoin/protocol/txout.rb', line 12 def pk_script_length @pk_script_length end |
#redeem_script ⇒ Object
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 |
#value ⇒ Object 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_script ⇒ Object
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( = {}) h = { 'value' => "%.8f" % (@value / 100000000.0), 'scriptPubKey' => parsed_script.to_string } h["address"] = parsed_script.get_address if parsed_script.is_hash160? && [:with_address] h end |
#to_null_payload ⇒ Object
57 58 59 |
# File 'lib/bitcoin/protocol/txout.rb', line 57 def to_null_payload self.class.new(-1, '').to_payload end |
#to_payload ⇒ Object
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 |