Class: CoinOp::Bit::Output

Inherits:
Object
  • Object
show all
Includes:
Encodings
Defined in:
lib/coin-op/bit/output.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Encodings

#base58, #decode_base58, #decode_hex, #hex

Constructor Details

#initialize(options, network: nil) ⇒ Output

Takes a Hash with required keys:

  • either :transaction (instance of Transaction) or :transaction_hash (hex-encoded hash of a Bitcoin transaction)

  • :index

  • :value

optional keys:

  • either :script (a value usable in Script.new) or :address (a valid Bitcoin address)

  • :metadata (a Hash with arbitrary contents)

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/coin-op/bit/output.rb', line 23

def initialize(options, network: nil)
  network_name = options[:network] || network
  raise(ArgumentError, 'Network cannot be nil!') unless network_name
  if options[:transaction]
    @transaction = options[:transaction]
  elsif options[:transaction_hash]
    @transaction_hash = options[:transaction_hash]
  end

  # FIXME: be aware of string bitcoin values versus
  # integer satoshi values
  @index, @value, @address, confirmations =
    options.values_at :index, :value, :address, :confirmations

  @metadata = options[:metadata] || {}
  @metadata[:confirmations] ||= confirmations

  if options[:script]
    @script = Script.new(options[:script], network: network_name)
  elsif @address
    @script = Script.new(address: @address, network: network_name)
  end


  @native = Bitcoin::Protocol::TxOut.from_hash(
    "value" => @value.to_s,
    "scriptPubKey" => @script.to_s
  )
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



8
9
10
# File 'lib/coin-op/bit/output.rb', line 8

def index
  @index
end

#metadataObject

Returns the value of attribute metadata.



7
8
9
# File 'lib/coin-op/bit/output.rb', line 7

def 
  @metadata
end

#nativeObject (readonly)

Returns the value of attribute native.



8
9
10
# File 'lib/coin-op/bit/output.rb', line 8

def native
  @native
end

#scriptObject (readonly)

Returns the value of attribute script.



8
9
10
# File 'lib/coin-op/bit/output.rb', line 8

def script
  @script
end

#transactionObject (readonly)

Returns the value of attribute transaction.



8
9
10
# File 'lib/coin-op/bit/output.rb', line 8

def transaction
  @transaction
end

#valueObject (readonly)

Returns the value of attribute value.



8
9
10
# File 'lib/coin-op/bit/output.rb', line 8

def value
  @value
end

Instance Method Details

#addressObject

The bitcoin address generated from the associated Script.



54
55
56
57
58
# File 'lib/coin-op/bit/output.rb', line 54

def address
  if @script
    @script.address
  end
end

#confirmationsObject



60
61
62
# File 'lib/coin-op/bit/output.rb', line 60

def confirmations
  @metadata[:confirmations]
end

#set_transaction(transaction, index) ⇒ Object

DEPRECATED



65
66
67
68
# File 'lib/coin-op/bit/output.rb', line 65

def set_transaction(transaction, index)
  @transaction_hash = nil
  @transaction, @index = transaction, index
end

#to_hashObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/coin-op/bit/output.rb', line 82

def to_hash
  {
    :transaction_hash => self.transaction_hash,
    :index => self.index,
    :value => self.value,
    :script => self.script,
    :address => self.address,
    :metadata => self.
  }
end

#to_json(*a) ⇒ Object



93
94
95
# File 'lib/coin-op/bit/output.rb', line 93

def to_json(*a)
  self.to_hash.to_json(*a)
end

#transaction_hashObject

Returns the transaction hash for this output.



71
72
73
74
75
76
77
78
79
# File 'lib/coin-op/bit/output.rb', line 71

def transaction_hash
  if @transaction
    @transaction.hex_hash
  elsif @transaction_hash
    @transaction_hash
  else
    ""
  end
end