Class: Bitcoin::OutPoint

Inherits:
Object
  • Object
show all
Includes:
HexConverter
Defined in:
lib/bitcoin/out_point.rb

Overview

outpoint class

Constant Summary collapse

COINBASE_HASH =
'0000000000000000000000000000000000000000000000000000000000000000'
COINBASE_INDEX =
4294967295

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HexConverter

#to_hex

Constructor Details

#initialize(tx_hash, index = -1)) ⇒ OutPoint

Returns a new instance of OutPoint.



14
15
16
17
# File 'lib/bitcoin/out_point.rb', line 14

def initialize(tx_hash, index = -1)
  @tx_hash = tx_hash
  @index = index
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



12
13
14
# File 'lib/bitcoin/out_point.rb', line 12

def index
  @index
end

#tx_hashObject (readonly)

Returns the value of attribute tx_hash.



11
12
13
# File 'lib/bitcoin/out_point.rb', line 11

def tx_hash
  @tx_hash
end

Class Method Details

.create_coinbase_outpointObject



40
41
42
# File 'lib/bitcoin/out_point.rb', line 40

def self.create_coinbase_outpoint
  new(COINBASE_HASH, COINBASE_INDEX)
end

.from_txid(txid, index) ⇒ Object



19
20
21
# File 'lib/bitcoin/out_point.rb', line 19

def self.from_txid(txid, index)
  self.new(txid.rhex, index)
end

.parse_from_payload(payload) ⇒ Bitcoin::OutPoint

Parse from payload

Parameters:

  • payload (String|StringIO)

    prvout payload with binary format

Returns:



26
27
28
29
30
# File 'lib/bitcoin/out_point.rb', line 26

def self.parse_from_payload(payload)
  buf = payload.is_a?(String) ? StringIO.new(payload) : payload
  hash, index = buf.read(36).unpack('a32V')
  OutPoint.new(hash.bth, index)
end

Instance Method Details

#coinbase?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/bitcoin/out_point.rb', line 32

def coinbase?
  tx_hash == COINBASE_HASH && index == COINBASE_INDEX
end

#to_payloadObject



36
37
38
# File 'lib/bitcoin/out_point.rb', line 36

def to_payload
  [tx_hash.htb, index].pack('a32V')
end

#to_sObject



53
54
55
56
# File 'lib/bitcoin/out_point.rb', line 53

def to_s
  return "[#{index}]" unless tx_hash
  "#{txid}[#{index}]"
end

#txidObject

convert hash to txid



49
50
51
# File 'lib/bitcoin/out_point.rb', line 49

def txid
  tx_hash.rhex
end

#valid?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/bitcoin/out_point.rb', line 44

def valid?
  index >= 0 && (!coinbase? && tx_hash != COINBASE_HASH)
end