Class: Bitcoin::Protocol::TxOut
- Inherits:
-
Object
- Object
- Bitcoin::Protocol::TxOut
- Defined in:
- lib/bitcoin/protocol/txout.rb
Overview
TxOut section of en.bitcoin.it/wiki/Protocol_documentation#tx
Instance Attribute Summary collapse
-
#pk_script ⇒ Object
(also: #script)
pk_script output Script.
-
#pk_script_length ⇒ Object
readonly
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
- #clear_parsed_script_cache ⇒ 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.
16 17 18 19 20 21 22 23 24 |
# File 'lib/bitcoin/protocol/txout.rb', line 16 def initialize(*args) if args.size == 2 @value = args[0] @pk_script_length = args[1].bytesize @pk_script = 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
11 12 13 |
# File 'lib/bitcoin/protocol/txout.rb', line 11 def pk_script @pk_script end |
#pk_script_length ⇒ Object (readonly)
pk_script output Script
11 12 13 |
# File 'lib/bitcoin/protocol/txout.rb', line 11 def pk_script_length @pk_script_length end |
#redeem_script ⇒ Object
p2sh redeem script (optional, not included in the serialized binary format)
14 15 16 |
# File 'lib/bitcoin/protocol/txout.rb', line 14 def redeem_script @redeem_script end |
#value ⇒ Object Also known as: amount
output value (in base units; “satoshi”)
8 9 10 |
# File 'lib/bitcoin/protocol/txout.rb', line 8 def value @value end |
Class Method Details
.from_hash(output) ⇒ Object
81 82 83 84 85 |
# File 'lib/bitcoin/protocol/txout.rb', line 81 def self.from_hash(output) amount = output['value'] ? output['value'].delete('.').to_i : output['amount'] script = Script.binary_from_string(output['scriptPubKey'] || output['script']) new(amount, script) end |
.from_io(buf) ⇒ Object
39 40 41 42 43 |
# File 'lib/bitcoin/protocol/txout.rb', line 39 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
101 102 103 104 105 |
# File 'lib/bitcoin/protocol/txout.rb', line 101 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
26 27 28 29 30 |
# File 'lib/bitcoin/protocol/txout.rb', line 26 def ==(other) @value == other.value && @pk_script == other.pk_script rescue StandardError false end |
#clear_parsed_script_cache ⇒ Object
59 60 61 |
# File 'lib/bitcoin/protocol/txout.rb', line 59 def clear_parsed_script_cache remove_instance_variable(:@parsed_script) if defined?(@parsed_script) end |
#parse_data(data) ⇒ Object Also known as: parse_payload
parse raw binary data for transaction output
33 34 35 36 37 |
# File 'lib/bitcoin/protocol/txout.rb', line 33 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
46 47 48 49 50 51 |
# File 'lib/bitcoin/protocol/txout.rb', line 46 def parse_data_from_io(buf) clear_parsed_script_cache @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
55 56 57 |
# File 'lib/bitcoin/protocol/txout.rb', line 55 def parsed_script @parsed_script ||= Bitcoin::Script.new(pk_script) end |
#to_hash(options = {}) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/bitcoin/protocol/txout.rb', line 71 def to_hash( = {}) h = { 'value' => format('%.8f', (@value / 100_000_000.0)), 'scriptPubKey' => parsed_script.to_string } if [:with_address] addrs = parsed_script.get_addresses h['address'] = addrs.first if addrs.size == 1 end h end |
#to_null_payload ⇒ Object
67 68 69 |
# File 'lib/bitcoin/protocol/txout.rb', line 67 def to_null_payload self.class.new(-1, '').to_payload end |
#to_payload ⇒ Object
63 64 65 |
# File 'lib/bitcoin/protocol/txout.rb', line 63 def to_payload [@value].pack('Q') << Protocol.pack_var_int(@pk_script_length) << @pk_script end |