Class: Bitcoin::Protocol::TxIn
- Inherits:
-
Object
- Object
- Bitcoin::Protocol::TxIn
- Defined in:
- lib/bitcoin/protocol/txin.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_SEQUENCE =
"\xff\xff\xff\xff"- NULL_HASH =
"\x00"*32
- COINBASE_INDEX =
0xffffffff
Instance Attribute Summary collapse
-
#prev_out ⇒ Object
previous output hash.
-
#prev_out_index ⇒ Object
previous output index.
-
#script_sig ⇒ Object
(also: #script)
script_sig input Script (signature).
-
#script_sig_length ⇒ Object
(also: #script_length)
script_sig input Script (signature).
-
#sequence ⇒ Object
sequence.
-
#sig_address ⇒ Object
signature hash and the address of the key that needs to sign it (used when dealing with unsigned or partly signed tx).
-
#sig_hash ⇒ Object
signature hash and the address of the key that needs to sign it (used when dealing with unsigned or partly signed tx).
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other) ⇒ Object
compare to another txout.
- #add_signature_pubkey_script(sig, pubkey_hex) ⇒ Object
-
#coinbase? ⇒ Boolean
check if input is coinbase.
-
#initialize(*args) ⇒ TxIn
constructor
A new instance of TxIn.
-
#is_final? ⇒ Boolean
returns true if the sequence number is final (DEFAULT_SEQUENCE).
-
#parse_data(data) ⇒ Object
parse raw binary data for transaction input.
- #parse_data_from_io(buf) ⇒ Object
-
#previous_output ⇒ Object
previous output in hex.
- #to_hash(options = {}) ⇒ Object
- #to_payload(script = @script_sig, sequence = @sequence) ⇒ Object
Constructor Details
#initialize(*args) ⇒ TxIn
Returns a new instance of TxIn.
31 32 33 34 35 36 37 |
# File 'lib/bitcoin/protocol/txin.rb', line 31 def initialize *args @prev_out, @prev_out_index, @script_sig_length, @script_sig, @sequence = *args @script_sig_length ||= 0 @script_sig ||= '' @sequence ||= DEFAULT_SEQUENCE end |
Instance Attribute Details
#prev_out ⇒ Object
previous output hash
9 10 11 |
# File 'lib/bitcoin/protocol/txin.rb', line 9 def prev_out @prev_out end |
#prev_out_index ⇒ Object
previous output index
12 13 14 |
# File 'lib/bitcoin/protocol/txin.rb', line 12 def prev_out_index @prev_out_index end |
#script_sig ⇒ Object Also known as: script
script_sig input Script (signature)
15 16 17 |
# File 'lib/bitcoin/protocol/txin.rb', line 15 def script_sig @script_sig end |
#script_sig_length ⇒ Object Also known as: script_length
script_sig input Script (signature)
15 16 17 |
# File 'lib/bitcoin/protocol/txin.rb', line 15 def script_sig_length @script_sig_length end |
#sequence ⇒ Object
sequence
25 26 27 |
# File 'lib/bitcoin/protocol/txin.rb', line 25 def sequence @sequence end |
#sig_address ⇒ Object
signature hash and the address of the key that needs to sign it (used when dealing with unsigned or partly signed tx)
19 20 21 |
# File 'lib/bitcoin/protocol/txin.rb', line 19 def sig_address @sig_address end |
#sig_hash ⇒ Object
signature hash and the address of the key that needs to sign it (used when dealing with unsigned or partly signed tx)
19 20 21 |
# File 'lib/bitcoin/protocol/txin.rb', line 19 def sig_hash @sig_hash end |
Class Method Details
.from_hash(input) ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/bitcoin/protocol/txin.rb', line 87 def self.from_hash(input) txin = TxIn.new([ input['prev_out']['hash'] ].pack('H*').reverse, input['prev_out']['n']) if input['coinbase'] txin.script_sig = [ input['coinbase'] ].pack("H*") else txin.script_sig = Script.binary_from_string(input['scriptSig']) end txin.sequence = [ input['sequence'] || 0xffffffff ].pack("V") txin end |
.from_hex_hash(hash, index) ⇒ Object
98 99 100 |
# File 'lib/bitcoin/protocol/txin.rb', line 98 def self.from_hex_hash(hash, index) TxIn.new([hash].pack("H*").reverse, index, 0) end |
.from_io(buf) ⇒ Object
61 62 63 |
# File 'lib/bitcoin/protocol/txin.rb', line 61 def self.from_io(buf) txin = new; txin.parse_data_from_io(buf); txin end |
Instance Method Details
#==(other) ⇒ Object
compare to another txout
40 41 42 43 44 45 46 47 |
# File 'lib/bitcoin/protocol/txin.rb', line 40 def ==(other) @prev_out == other.prev_out && @prev_out_index == other.prev_out_index && @script_sig == other.script_sig && @sequence == other.sequence rescue false end |
#add_signature_pubkey_script(sig, pubkey_hex) ⇒ Object
119 120 121 |
# File 'lib/bitcoin/protocol/txin.rb', line 119 def add_signature_pubkey_script(sig, pubkey_hex) self.script = Bitcoin::Script.to_signature_pubkey_script(sig, [pubkey_hex].pack("H*")) end |
#coinbase? ⇒ Boolean
check if input is coinbase
108 109 110 |
# File 'lib/bitcoin/protocol/txin.rb', line 108 def coinbase? (@prev_out_index == COINBASE_INDEX) && (@prev_out == NULL_HASH) end |
#is_final? ⇒ Boolean
returns true if the sequence number is final (DEFAULT_SEQUENCE)
50 51 52 |
# File 'lib/bitcoin/protocol/txin.rb', line 50 def is_final? self.sequence == DEFAULT_SEQUENCE end |
#parse_data(data) ⇒ Object
parse raw binary data for transaction input
55 56 57 58 59 |
# File 'lib/bitcoin/protocol/txin.rb', line 55 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
65 66 67 68 69 70 |
# File 'lib/bitcoin/protocol/txin.rb', line 65 def parse_data_from_io(buf) @prev_out, @prev_out_index = buf.read(36).unpack("a32V") @script_sig_length = Protocol.unpack_var_int_from_io(buf) @script_sig = buf.read(@script_sig_length) @sequence = buf.read(4) end |
#previous_output ⇒ Object
previous output in hex
103 104 105 |
# File 'lib/bitcoin/protocol/txin.rb', line 103 def previous_output @prev_out.reverse_hth end |
#to_hash(options = {}) ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/bitcoin/protocol/txin.rb', line 76 def to_hash( = {}) t = { 'prev_out' => { 'hash' => @prev_out.reverse_hth, 'n' => @prev_out_index } } if coinbase? t['coinbase'] = @script_sig.unpack("H*")[0] else # coinbase tx t['scriptSig'] = Bitcoin::Script.new(@script_sig).to_string end t['sequence'] = @sequence.unpack("V")[0] unless @sequence == "\xff\xff\xff\xff" t end |
#to_payload(script = @script_sig, sequence = @sequence) ⇒ Object
72 73 74 |
# File 'lib/bitcoin/protocol/txin.rb', line 72 def to_payload(script=@script_sig, sequence=@sequence) [@prev_out, @prev_out_index].pack("a32V") << Protocol.pack_var_int(script.bytesize) << script << (sequence || DEFAULT_SEQUENCE) end |