Class: Tapyrus::TxIn

Inherits:
Object
  • Object
show all
Defined in:
lib/tapyrus/tx_in.rb

Overview

transaction input

Constant Summary collapse

SEQUENCE_FINAL =

Setting nSequence to this value for every input in a transaction disables nLockTime.

0xffffffff
SEQUENCE_LOCKTIME_DISABLE_FLAG =

If this flag set, TxIn#sequence is NOT interpreted as a relative lock-time.

(1 << 31)
SEQUENCE_LOCKTIME_TYPE_FLAG =

If TxIn#sequence encodes a relative lock-time and this flag is set, the relative lock-time has units of 512 seconds, otherwise it specifies blocks with a granularity of 1.

(1 << 22)
SEQUENCE_LOCKTIME_MASK =

If TxIn#sequence encodes a relative lock-time, this mask is applied to extract that lock-time from the sequence field.

0x0000ffff

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out_point: nil, script_sig: Tapyrus::Script.new, sequence: SEQUENCE_FINAL) ⇒ TxIn

Returns a new instance of TxIn.



24
25
26
27
28
# File 'lib/tapyrus/tx_in.rb', line 24

def initialize(out_point: nil, script_sig: Tapyrus::Script.new, sequence: SEQUENCE_FINAL)
  @out_point = out_point
  @script_sig = script_sig
  @sequence = sequence
end

Instance Attribute Details

#out_pointObject

Returns the value of attribute out_point.



7
8
9
# File 'lib/tapyrus/tx_in.rb', line 7

def out_point
  @out_point
end

#script_sigObject

Returns the value of attribute script_sig.



8
9
10
# File 'lib/tapyrus/tx_in.rb', line 8

def script_sig
  @script_sig
end

#sequenceObject

Returns the value of attribute sequence.



9
10
11
# File 'lib/tapyrus/tx_in.rb', line 9

def sequence
  @sequence
end

Class Method Details

.parse_from_payload(payload) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tapyrus/tx_in.rb', line 30

def self.parse_from_payload(payload)
  buf = payload.is_a?(String) ? StringIO.new(payload) : payload
  i = new
  hash, index = buf.read(36).unpack("a32V")
  i.out_point = OutPoint.new(hash.bth, index)
  sig_length = Tapyrus.unpack_var_int_from_io(buf)
  if sig_length == 0
    i.script_sig = Script.new
  else
    i.script_sig = Script.parse_from_payload(buf.read(sig_length))
  end
  i.sequence = buf.read(4).unpack("V").first
  i
end

Instance Method Details

#==(other) ⇒ Object



67
68
69
# File 'lib/tapyrus/tx_in.rb', line 67

def ==(other)
  to_payload == other.to_payload
end

#coinbase?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/tapyrus/tx_in.rb', line 45

def coinbase?
  out_point.coinbase?
end

#prev_hashObject

return previous output hash (not txid)



72
73
74
75
# File 'lib/tapyrus/tx_in.rb', line 72

def prev_hash
  return nil unless out_point
  out_point.tx_hash
end

#to_hObject



59
60
61
62
63
64
65
# File 'lib/tapyrus/tx_in.rb', line 59

def to_h
  sig = script_sig.to_h
  sig.delete(:type)
  h = { txid: out_point.txid, vout: out_point.index, script_sig: sig }
  h[:sequence] = sequence
  h
end

#to_payload(script_sig = @script_sig, sequence = @sequence, use_malfix: false) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/tapyrus/tx_in.rb', line 49

def to_payload(script_sig = @script_sig, sequence = @sequence, use_malfix: false)
  p = out_point.to_payload
  unless use_malfix
    p << Tapyrus.pack_var_int(script_sig.to_payload.bytesize)
    p << script_sig.to_payload
  end
  p << [sequence].pack("V")
  p
end