Class: Pochette::Backends::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/pochette/backends/base.rb

Overview

All Pochette backends must conform to this interface.

Direct Known Subclasses

BitcoinCore, BlockchainInfo, Trendy

Instance Method Summary collapse

Instance Method Details

#balances_for(addresses, confirmations) ⇒ Object

Gets the total received, spent and balance for a list of addresses. Confirmed balances are enforced to have a number of confirmation, appearing in a block is not enough. Returns a hash with: { address: [received, sent, total,

          unconfirmed_received, unconfirmed_sent, unconfirmed_total],
...}

Raises:

  • (NotImplementedError)


26
27
28
# File 'lib/pochette/backends/base.rb', line 26

def balances_for(addresses, confirmations)
  raise NotImplementedError
end

#block_heightObject

Raises:

  • (NotImplementedError)


72
73
74
# File 'lib/pochette/backends/base.rb', line 72

def block_height
  raise NotImplementedError
end

#incoming_for(addresses, min_date) ⇒ Object

Lists all bitcoins received by a list of addresses after a given date. Includes both confirmed and unconfirmed transactions, unconfirmed transactions have a nil block height. Returns a list of lists as following:

amount: Amount received (in satoshis)
address: Public address receiving the amount.
txid: The hash for the transaction that received it.
confirmations: Transaction confirmations
output position: To disambiguate in case address received more than once.
sender addresses: Comma separated list of input addresses,
  used to identify deposits from trusted parties.
  can be used to identify deposits from trusted parties.

Raises:

  • (NotImplementedError)


15
16
17
# File 'lib/pochette/backends/base.rb', line 15

def incoming_for(addresses, min_date)
  raise NotImplementedError
end

#list_transactions(txids) ⇒ Object

Gets information for the given transactions returns a list of objects, like so: [

{ hash: txid,
  version: 1,
  lock_time: 0,
  inputs: [
    { prev_hash: txid,
      prev_index: 0,
      sequence: 0,
      script_sig: hex_signature
    },
    ...
  ],
  bin_outputs: [
    { amount: amount (as satoshis),
      script_pubkey: hex_script
    },
    ...
  ]
}

Raises:

  • (NotImplementedError)


58
59
60
# File 'lib/pochette/backends/base.rb', line 58

def list_transactions(txids)
  raise NotImplementedError
end

#list_unspent(addresses) ⇒ Object

Get unspent utxos for the given addresses, returns a list of lists like so:

[address, txid, position (vout), amount (in satoshis)], …

Raises:

  • (NotImplementedError)


33
34
35
# File 'lib/pochette/backends/base.rb', line 33

def list_unspent(addresses)
  raise NotImplementedError
end

#propagate(hex) ⇒ Object

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/pochette/backends/base.rb', line 68

def propagate(hex)
  raise NotImplementedError
end

#pushtx(hex, options = { }) ⇒ Object



62
63
64
65
66
# File 'lib/pochette/backends/base.rb', line 62

def pushtx(hex, options = { })
  verify_signatures(hex, options) if options[:verify_signatures]
  propagate(hex)
  Bitcoin::Protocol::Tx.new(hex.htb).hash
end

#verify_signatures(hex, options = { }) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pochette/backends/base.rb', line 76

def verify_signatures(hex, options = { })
  tx = Bitcoin::P::Tx.new(hex.htb)
  tx.inputs.each_with_index do |input, idx|
    prev_tx = list_transactions([ input.previous_output ]).first
    outputs = prev_tx[:bin_outputs]
    script_pubkey = outputs[input.prev_out_index][:script_pubkey].htb
    unless tx.verify_input_signature(idx, script_pubkey, Time.now.to_i, options)
      raise Pochette::InvalidSignatureError, "Signature for input #{idx} is invalid."
    end
  end
  true
end