Class: Straight::Blockchain::MyceliumAdapter

Inherits:
Adapter
  • Object
show all
Defined in:
lib/straight/blockchain_adapters/mycelium_adapter.rb

Constant Summary collapse

MAINNET_SERVERS =
%w{
  https://mws2.mycelium.com/wapi/wapi
  https://mws6.mycelium.com/wapi/wapi
  https://mws7.mycelium.com/wapi/wapi
}
TESTNET_SERVERS =
%w{
  https://node3.mycelium.com/wapitestnet/wapi
}
PINNED_CERTIFICATES =
%w{
  6afe0e9b6806fa4a49fc6818512014332953f30101dad7b91e76c14e073c3134
}.to_set

Constants inherited from Adapter

Adapter::MAX_TRIES

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(testnet: false) ⇒ MyceliumAdapter



26
27
28
29
30
31
# File 'lib/straight/blockchain_adapters/mycelium_adapter.rb', line 26

def initialize(testnet: false)
  @latest_block = { cache_timestamp: nil, block: nil }
  @testnet = testnet
  @api_servers = @testnet ? TESTNET_SERVERS : MAINNET_SERVERS
  set_base_url
end

Class Method Details

.mainnet_adapterObject



18
19
20
# File 'lib/straight/blockchain_adapters/mycelium_adapter.rb', line 18

def self.mainnet_adapter
  new(testnet: false)
end

.testnet_adapterObject



22
23
24
# File 'lib/straight/blockchain_adapters/mycelium_adapter.rb', line 22

def self.testnet_adapter
  new(testnet: true)
end

Instance Method Details

#fetch_balance_for(address) ⇒ Object

Returns the current balance of the address



69
70
71
72
73
74
75
# File 'lib/straight/blockchain_adapters/mycelium_adapter.rb', line 69

def fetch_balance_for(address)
  unspent = 0
  api_request('queryUnspentOutputs', { addresses: [address]})['unspent'].each do |out|
    unspent += out['value']
  end
  unspent
end

#fetch_transaction(tid, address: nil) ⇒ Object

Returns transaction info for the tid



49
50
51
52
# File 'lib/straight/blockchain_adapters/mycelium_adapter.rb', line 49

def fetch_transaction(tid, address: nil)
  transaction = api_request('getTransactions', { txIds: [tid] })['transactions'].first
  straighten_transaction transaction, address: address
end

#fetch_transactions_for(address) ⇒ Object

Supposed to returns all transactions for the address, but currently actually returns the first one, since we only need one.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/straight/blockchain_adapters/mycelium_adapter.rb', line 56

def fetch_transactions_for(address)
  # API may return nil instead of an empty array if address turns out to be invalid
  # (for example when trying to supply a testnet address instead of mainnet while using
  # mainnet adapter.
  if api_response = api_request('queryTransactionInventory', { addresses: [address], limit: 1 })
    tid = api_response["txIds"].first
    tid ? [fetch_transaction(tid, address: address)] : []
  else
    raise BitcoinAddressInvalid, message: "address in question: #{address}"
  end
end

#latest_block(force_reload: false) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/straight/blockchain_adapters/mycelium_adapter.rb', line 77

def latest_block(force_reload: false)
  # If we checked Blockchain.info latest block data
  # more than a minute ago, check again. Otherwise, use cached version.
  if @latest_block[:cache_timestamp].nil?              ||
     @latest_block[:cache_timestamp] < (Time.now - 60) ||
     force_reload
    @latest_block = {
      cache_timestamp: Time.now,
      block: api_request('queryUnspentOutputs', { addresses: []} )
    }
  else
    @latest_block
  end
end

#next_serverObject



44
45
46
# File 'lib/straight/blockchain_adapters/mycelium_adapter.rb', line 44

def next_server
  set_base_url(@api_servers.index(@base_url) + 1)
end

#set_base_url(num = 0) ⇒ Object

Set url for API request.



39
40
41
42
# File 'lib/straight/blockchain_adapters/mycelium_adapter.rb', line 39

def set_base_url(num = 0)
  return nil if num >= @api_servers.size
  @base_url = @api_servers[num]
end

#testnet?Boolean



33
34
35
# File 'lib/straight/blockchain_adapters/mycelium_adapter.rb', line 33

def testnet?
  @testnet
end