class BlockCypher::Api

Attributes

api_token[R]

Public Class Methods

new(version: V1, currency: BTC, network: MAIN_NET, api_token: nil) click to toggle source
# File lib/blockcypher/api.rb, line 21
def initialize(version: V1, currency: BTC, network: MAIN_NET, api_token: nil)
  @version = version
  @currency = currency
  @network = network
  @api_token = api_token
end

Public Instance Methods

address_balance(address, omit_wallet_addresses: false) click to toggle source
# File lib/blockcypher/api.rb, line 194
def address_balance(address, omit_wallet_addresses: false)
  query = { omitWalletAddresses: omit_wallet_addresses }
  api_http_get('/addrs/' + address + '/balance', query: query)
end
address_details(address, unspent_only: false, limit: 50, before: nil, after: nil, confirmations: nil, omit_wallet_addresses: false, include_confidence: false) click to toggle source
# File lib/blockcypher/api.rb, line 179
def address_details(address, unspent_only: false, limit: 50,
                    before: nil, after: nil, confirmations: nil,
                    omit_wallet_addresses: false, include_confidence: false)
  query = {
    unspentOnly: unspent_only,
    limit: limit,
    omitWalletAddresses: omit_wallet_addresses,
    includeConfidence: include_confidence
  }
  query[:before] = before if before
  query[:after] = after if after

  api_http_get('/addrs/' + address, query: query)
end
address_final_balance(address, omit_wallet_addresses: false) click to toggle source
# File lib/blockcypher/api.rb, line 199
def address_final_balance(address, omit_wallet_addresses: false)
  details = address_balance(address,
                            omit_wallet_addresses: omit_wallet_addresses)
  details['final_balance']
end
address_full_txs(address, limit: 10, before: nil, after: nil, include_hex: false, omit_wallet_addresses: false, include_confidence: false) click to toggle source
# File lib/blockcypher/api.rb, line 205
def address_full_txs(address, limit: 10, before: nil, after: nil,
                     include_hex: false, omit_wallet_addresses: false, include_confidence: false)
  query = {
    limit: limit,
    includeHex: include_hex,
    omitWalletAddresses: omit_wallet_addresses,
    includeConfidence: include_confidence
  }
  query[:before] = before if before
  query[:after] = after if after

  api_http_get("/addrs/#{address}/full", query: query)
end
address_generate() click to toggle source

Address APIs

# File lib/blockcypher/api.rb, line 170
def address_generate
  api_http_post('/addrs')
end
address_generate_multi(pubkeys, script_type) click to toggle source
# File lib/blockcypher/api.rb, line 174
def address_generate_multi(pubkeys, script_type)
  payload = { 'pubkeys' => pubkeys, 'script_type' => script_type }
  api_http_post('/addrs', json_payload: payload)
end
asset_address(asset_id, oap_address) click to toggle source
# File lib/blockcypher/api.rb, line 338
def asset_address(asset_id, oap_address)
  api_http_get("/oap/#{asset_id}/addrs/#{oap_address}")
end
asset_txs(asset_id) click to toggle source
# File lib/blockcypher/api.rb, line 334
def asset_txs(asset_id)
  api_http_get("/oap/#{asset_id}/txs")
end
blockchain() click to toggle source
# File lib/blockcypher/api.rb, line 44
def blockchain
  api_http_get('')
end
blockchain_block(block_index, params) click to toggle source
# File lib/blockcypher/api.rb, line 40
def blockchain_block(block_index, params)
  api_http_get('/blocks/' + block_index, query: params)
end
blockchain_transaction(transaction_hash, **params) click to toggle source
# File lib/blockcypher/api.rb, line 36
def blockchain_transaction(transaction_hash, **params)
  api_http_get('/txs/' + transaction_hash, query: params)
end
blockchain_unconfirmed_tx() click to toggle source

Blockchain API

# File lib/blockcypher/api.rb, line 32
def blockchain_unconfirmed_tx
  api_http_get('/txs')
end
create_forwarding_address( destination, callback_url: nil, enable_confirmations: false, mining_fees_satoshis: nil ) click to toggle source

Payments and Forwarding API

# File lib/blockcypher/api.rb, line 285
def create_forwarding_address(
  destination,
  callback_url: nil,
  enable_confirmations: false,
  mining_fees_satoshis: nil
)
  payload = {
    destination: destination,
    callback_url: callback_url,
    enable_confirmations: enable_confirmations,
    mining_fees_satoshis: mining_fees_satoshis
  }
  api_http_post('/payments', json_payload: payload)
end
Also aliased as: create_payments_forwarding
create_payments_forwarding( destination, callback_url: nil, enable_confirmations: false, mining_fees_satoshis: nil )
decode_hex(hex) click to toggle source

Transaction API

# File lib/blockcypher/api.rb, line 61
def decode_hex(hex)
  payload = { 'tx' => hex }
  api_http_post('/txs/decode', json_payload: payload)
end
delete_forwarding_address(id) click to toggle source
# File lib/blockcypher/api.rb, line 306
def delete_forwarding_address(id)
  api_http_delete('/payments/' + id)
end
event_webhook_delete(id) click to toggle source
# File lib/blockcypher/api.rb, line 277
def event_webhook_delete(id)
  api_http_delete('/hooks/' + id)
end
event_webhook_get(id) click to toggle source
# File lib/blockcypher/api.rb, line 273
def event_webhook_get(id)
  api_http_get('/hooks/' + id)
end
event_webhook_listall() click to toggle source
# File lib/blockcypher/api.rb, line 269
def event_webhook_listall
  api_http_get('/hooks')
end
event_webhook_subscribe(url, event, options = {}) click to toggle source

Events API

# File lib/blockcypher/api.rb, line 260
def event_webhook_subscribe(url, event, options = {})
  payload = {
    url: url,
    event: event
  }.merge(options)

  api_http_post('/hooks', json_payload: payload)
end
faucet(address, amount) click to toggle source

Faucet API

# File lib/blockcypher/api.rb, line 52
def faucet(address, amount)
  payload = { 'address' => address, 'amount' => amount }
  api_http_post('/faucet', json_payload: payload)
end
generate_asset_address() click to toggle source

Asset API #

# File lib/blockcypher/api.rb, line 314
def generate_asset_address
  api_http_post('/oap/addrs')
end
issue_asset(from_private, to_address, amount) click to toggle source
# File lib/blockcypher/api.rb, line 318
def issue_asset(from_private, to_address, amount)
  api_http_post('/oap/issue', json_payload: {
                  from_private: from_private,
                  to_address: to_address,
                  amount: amount
                })
end
list_forwarding_addresses() click to toggle source
# File lib/blockcypher/api.rb, line 302
def list_forwarding_addresses
  api_http_get('/payments')
end
microtx_from_priv(private_key, to_address, value_satoshis) click to toggle source

This method sends private key to server

# File lib/blockcypher/api.rb, line 144
def microtx_from_priv(private_key, to_address, value_satoshis)
  payload = {
    from_private: private_key,
    to_address: to_address,
    value_satoshis: value_satoshis
  }
  api_http_post('/txs/micro', json_payload: payload)
end
microtx_from_pub(private_key, to_address, value_satoshis) click to toggle source

This method uses public key, signs with private key locally

# File lib/blockcypher/api.rb, line 154
def microtx_from_pub(private_key, to_address, value_satoshis)
  pubkey = pubkey_from_priv(private_key)
  payload = {
    from_pubkey: pubkey,
    to_address: to_address,
    value_satoshis: value_satoshis
  }
  micro_skel = api_http_post('/txs/micro', json_payload: payload)
  micro_skel['signatures'] = signer(private_key, micro_skel['tosign'])
  api_http_post('/txs/micro', json_payload: micro_skel)
end
pubkey_from_priv(private_key) click to toggle source
# File lib/blockcypher/api.rb, line 105
def pubkey_from_priv(private_key)
  key = Bitcoin::Key.new(private_key, nil, compressed = true)
  key.pub
end
push_hex(hex) click to toggle source
# File lib/blockcypher/api.rb, line 66
def push_hex(hex)
  payload = { 'tx' => hex }
  api_http_post('/txs/push', json_payload: payload)
end
send_money(from_address, to_address, satoshi_amount, private_key) click to toggle source
# File lib/blockcypher/api.rb, line 71
def send_money(from_address, to_address, satoshi_amount, private_key)
  to_address = [to_address] unless to_address.is_a? Array

  tx_new = transaction_new([from_address], to_address, satoshi_amount)

  transaction_sign_and_send(tx_new, private_key)
end
signer(private_key, tosign) click to toggle source
# File lib/blockcypher/api.rb, line 110
def signer(private_key, tosign)
  key = Bitcoin::Key.new(private_key, nil, compressed = true)
  signatures = []

  tosign.each do |to_sign_hex|
    to_sign_binary = [to_sign_hex].pack('H*')
    sig_binary = key.sign(to_sign_binary)
    sig_hex = sig_binary.unpack1('H*')
    signatures << sig_hex
  end

  signatures
end
transaction_new(input_addreses, output_addresses, satoshi_amount) click to toggle source
# File lib/blockcypher/api.rb, line 79
def transaction_new(input_addreses, output_addresses, satoshi_amount)
  payload = {
    'inputs' => [
      {
        addresses: input_addreses
      }
    ],
    'outputs' => [
      {
        addresses: output_addresses,
        value: satoshi_amount
      }
    ]
  }
  api_http_post('/txs/new', json_payload: payload)
end
transaction_new_custom(payload) click to toggle source
# File lib/blockcypher/api.rb, line 124
def transaction_new_custom(payload)
  # Build payload yourself, for custom transactions
  api_http_post('/txs/new', json_payload: payload)
end
transaction_send_custom(payload) click to toggle source
# File lib/blockcypher/api.rb, line 129
def transaction_send_custom(payload)
  # Send TXSkeleton payload yourself, for custom transactions
  # You may need to sign your data using another library, like Bitcoin
  api_http_post('/txs/send', json_payload: payload)
end
transaction_sign_and_send(new_tx, private_key) click to toggle source
# File lib/blockcypher/api.rb, line 96
def transaction_sign_and_send(new_tx, private_key)
  pubkey = pubkey_from_priv(private_key)
  # Make array of pubkeys matching length of 'tosign'
  new_tx['pubkeys'] = Array.new(new_tx['tosign'].length) { pubkey }
  # Sign the 'tosign' array
  new_tx['signatures'] = signer(private_key, new_tx['tosign'])
  api_http_post('/txs/send', json_payload: new_tx)
end
transfer_asset(asset_id, from_private, to_address, amount) click to toggle source
# File lib/blockcypher/api.rb, line 326
def transfer_asset(asset_id, from_private, to_address, amount)
  api_http_post("/oap/#{asset_id}/transfer", json_payload: {
                  from_private: from_private,
                  to_address: to_address,
                  amount: amount
                })
end
tx_confidence(tx_hash) click to toggle source
# File lib/blockcypher/api.rb, line 135
def tx_confidence(tx_hash)
  api_http_get('/txs/' + tx_hash + '/confidence')
end
wallet_add_addr(name, addresses, omit_wallet_addresses: false) click to toggle source
# File lib/blockcypher/api.rb, line 232
def wallet_add_addr(name, addresses, omit_wallet_addresses: false)
  payload = { 'addresses' => Array(addresses) }
  query = { omitWalletAddresses: omit_wallet_addresses }
  api_http_post('/wallets/' + name + '/addresses',
                json_payload: payload, query: query)
end
wallet_create(name, addresses) click to toggle source

Wallet API

# File lib/blockcypher/api.rb, line 223
def wallet_create(name, addresses)
  payload = { 'name' => name, 'addresses' => Array(addresses) }
  api_http_post('/wallets', json_payload: payload)
end
wallet_delete(name) click to toggle source
# File lib/blockcypher/api.rb, line 252
def wallet_delete(name)
  api_http_delete('/wallets/' + name)
end
wallet_delete_addr(name, addresses) click to toggle source
# File lib/blockcypher/api.rb, line 243
def wallet_delete_addr(name, addresses)
  addrjoin = addresses.join(';')
  api_http_delete('/wallets/' + name + '/addresses', query: { address: addrjoin })
end
wallet_gen_addr(name) click to toggle source
# File lib/blockcypher/api.rb, line 248
def wallet_gen_addr(name)
  api_http_post('/wallets/' + name + '/addresses/generate')
end
wallet_get(name) click to toggle source
# File lib/blockcypher/api.rb, line 228
def wallet_get(name)
  api_http_get('/wallets/' + name)
end
wallet_get_addr(name) click to toggle source
# File lib/blockcypher/api.rb, line 239
def wallet_get_addr(name)
  api_http_get('/wallets/' + name + '/addresses')
end

Private Instance Methods

api_http_call(http_method, api_path, query, json_payload: nil) click to toggle source
# File lib/blockcypher/api.rb, line 344
def api_http_call(http_method, api_path, query, json_payload: nil)
  uri = endpoint_uri(api_path, query)

  # Build the connection
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  # Build the Request
  if http_method == :post
    request = Net::HTTP::Post.new(uri.request_uri)
  elsif http_method == :get
    request = Net::HTTP::Get.new(uri.request_uri)
  elsif http_method == :delete
    request = Net::HTTP::Delete.new(uri.request_uri)
  else
    raise 'Invalid HTTP method'
  end

  unless json_payload.nil?
    request.content_type = 'application/json'
    request.body = json_payload.to_json
  end

  response = http.request(request)
  response_code = response.code.to_i

  # Detect errors/return 204 empty body
  if response_code >= 400
    raise Error, uri.to_s + ' Response:' + response.body
  elsif response_code == 204
    return nil
  end

  # Process the response
  begin
    json_response = JSON.parse(response.body)
    json_response
  rescue StandardError => e
    raise "Unable to parse JSON response #{e.inspect}, #{response.body}"
  end
end
api_http_delete(api_path, query: {}) click to toggle source
# File lib/blockcypher/api.rb, line 394
def api_http_delete(api_path, query: {})
  api_http_call(:delete, api_path, query)
end
api_http_get(api_path, query: {}) click to toggle source
# File lib/blockcypher/api.rb, line 386
def api_http_get(api_path, query: {})
  api_http_call(:get, api_path, query)
end
api_http_post(api_path, json_payload: nil, query: {}) click to toggle source
# File lib/blockcypher/api.rb, line 390
def api_http_post(api_path, json_payload: nil, query: {})
  api_http_call(:post, api_path, query, json_payload: json_payload)
end
endpoint_uri(api_path, query) click to toggle source
# File lib/blockcypher/api.rb, line 398
def endpoint_uri(api_path, query)
  uri = URI("https://api.blockcypher.com/#{@version}/#{@currency}/#{@network}#{api_path}")
  query[:token] = api_token if api_token
  uri.query = URI.encode_www_form(query) unless query.empty?
  uri
end