Class: BTC::Outpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/btcruby/outpoint.rb

Overview

Represents a reference to a previous transaction.

Constant Summary collapse

INVALID_INDEX =

aka "(unsigned int) -1" in BitcoinQT.

0xFFFFFFFF
ZERO_HASH256 =
("\x00".b*32).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transaction_hash: nil, transaction_id: nil, index: 0) ⇒ Outpoint

Returns a new instance of Outpoint.



11
12
13
14
15
16
17
18
# File 'lib/btcruby/outpoint.rb', line 11

def initialize(transaction_hash: nil, transaction_id: nil, index: 0)
  @transaction_hash = transaction_hash
  self.transaction_id = transaction_id if transaction_id
  while index < 0
    index += 2**32
  end
  @index = index
end

Instance Attribute Details

#index ⇒ Object

Returns the value of attribute index.



9
10
11
# File 'lib/btcruby/outpoint.rb', line 9

def index
  @index
end

#transaction_hash ⇒ Object

Returns the value of attribute transaction_hash.



7
8
9
# File 'lib/btcruby/outpoint.rb', line 7

def transaction_hash
  @transaction_hash
end

#transaction_id ⇒ Object

Returns the value of attribute transaction_id.



8
9
10
# File 'lib/btcruby/outpoint.rb', line 8

def transaction_id
  @transaction_id
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



43
44
45
46
# File 'lib/btcruby/outpoint.rb', line 43

def ==(other)
  index == other.index &&
  transaction_hash == other.transaction_hash
end

#data ⇒ Object



36
37
38
39
40
41
# File 'lib/btcruby/outpoint.rb', line 36

def data
  data = "".b
  data << BTC::Data.ensure_binary_encoding(transaction_hash)
  data << BTC::WireFormat.encode_uint32le(index)
  data
end

#hash ⇒ Object



49
50
51
# File 'lib/btcruby/outpoint.rb', line 49

def hash
  transaction_hash.hash ^ index
end

#null? ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/btcruby/outpoint.rb', line 32

def null?
  @index == INVALID_INDEX && @transaction_hash == ZERO_HASH256
end

#outpoint_id ⇒ Object



28
29
30
# File 'lib/btcruby/outpoint.rb', line 28

def outpoint_id
  %{#{transaction_id}:#{index}}
end

#to_s ⇒ Object



53
54
55
# File 'lib/btcruby/outpoint.rb', line 53

def to_s
  outpoint_id
end