Module: CoinOp::Bit::Spendable

Defined in:
lib/coin-op/bit/spendable.rb

Overview

A mixin to provide simple transaction preparation. Not currently used in any production code, so needs vetting.

Requires the including class to define these methods:

  • network

  • balance

  • select_unspent

  • authorize

Defined Under Namespace

Classes: InsufficientFunds

Instance Method Summary collapse

Instance Method Details

#authorize(transaction) ⇒ Object

Authorize the supplied transaction by setting its inputs’ script_sigs to whatever values are appropriate.



35
36
37
# File 'lib/coin-op/bit/spendable.rb', line 35

def authorize(transaction)
  raise "implement #authorize in your class"
end

#balanceObject



23
24
25
# File 'lib/coin-op/bit/spendable.rb', line 23

def balance
  raise "implement #balance in your class"
end

#create_transaction(outputs, change_address, fee_amount = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/coin-op/bit/spendable.rb', line 39

def create_transaction(outputs, change_address, fee_amount=nil)

  transaction = CoinOp::Bit::Transaction.from_data(
    :fee => fee_amount,
    :outputs => outputs
  )

  if self.balance < transaction.output_value
    raise InsufficientFunds
  end

  unspent = self.select_unspent(transaction.output_value)

  unspent.each do |output|
    transaction.add_input :output => output
  end

  input_amount = unspent.inject(0) {|sum, output| sum += output.value }

  # FIXME: there's likely another unspent output we can add, but the present
  # implementation of all this can't easily help us.  Possibly stop
  # using select_unspent(value) and start using a while loop that shifts
  # outputs off the array.  Then we can start the process over.
  unless transaction.funded?
    raise InsufficientFunds
  end

  transaction.add_change change_address

  self.authorize(transaction)
  transaction
end

#networkObject

Return the network name (must be one of the keys from Bitcoin.network)



19
20
21
# File 'lib/coin-op/bit/spendable.rb', line 19

def network
  raise "implement #network in your class"
end

#select_unspent(value) ⇒ Object

Takes a value in satoshis. Returns an array of spendable Outputs



29
30
31
# File 'lib/coin-op/bit/spendable.rb', line 29

def select_unspent(value)
  raise "implement #select_unspent in your class"
end