Class: BlockCypher::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/blockcypher/api.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version: V1, currency: BTC, network: MAIN_NET, api_token: nil) ⇒ Api

Returns a new instance of Api.



21
22
23
24
25
26
# 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

Instance Attribute Details

#api_tokenObject (readonly)

Returns the value of attribute api_token.



19
20
21
# File 'lib/blockcypher/api.rb', line 19

def api_token
  @api_token
end

Instance Method Details

#address_balance(address, omit_wallet_addresses: false) ⇒ Object



194
195
196
197
# 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) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
# 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) ⇒ Object



199
200
201
202
203
# 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, txlimit: 20) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# 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, txlimit: 20)
  query = {
    limit: limit,
    includeHex: include_hex,
    omitWalletAddresses: omit_wallet_addresses,
    includeConfidence: include_confidence,
    txlimit: txlimit
  }
  query[:before] = before if before
  query[:after] = after if after

  api_http_get("/addrs/#{address}/full", query: query)
end

#address_generateObject

Address APIs



170
171
172
# File 'lib/blockcypher/api.rb', line 170

def address_generate
  api_http_post('/addrs')
end

#address_generate_multi(pubkeys, script_type) ⇒ Object



174
175
176
177
# 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) ⇒ Object



340
341
342
# File 'lib/blockcypher/api.rb', line 340

def asset_address(asset_id, oap_address)
  api_http_get("/oap/#{asset_id}/addrs/#{oap_address}")
end

#asset_txs(asset_id) ⇒ Object



336
337
338
# File 'lib/blockcypher/api.rb', line 336

def asset_txs(asset_id)
  api_http_get("/oap/#{asset_id}/txs")
end

#blockchainObject



44
45
46
# File 'lib/blockcypher/api.rb', line 44

def blockchain
  api_http_get('')
end

#blockchain_block(block_index, params) ⇒ Object



40
41
42
# 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) ⇒ Object



36
37
38
# File 'lib/blockcypher/api.rb', line 36

def blockchain_transaction(transaction_hash, **params)
  api_http_get('/txs/' + transaction_hash, query: params)
end

#blockchain_unconfirmed_txObject

Blockchain API



32
33
34
# 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) ⇒ Object Also known as: create_payments_forwarding

Payments and Forwarding API



287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/blockcypher/api.rb', line 287

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

#decode_hex(hex) ⇒ Object

Transaction API



61
62
63
64
# 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) ⇒ Object



308
309
310
# File 'lib/blockcypher/api.rb', line 308

def delete_forwarding_address(id)
  api_http_delete('/payments/' + id)
end

#event_webhook_delete(id) ⇒ Object



279
280
281
# File 'lib/blockcypher/api.rb', line 279

def event_webhook_delete(id)
  api_http_delete('/hooks/' + id)
end

#event_webhook_get(id) ⇒ Object



275
276
277
# File 'lib/blockcypher/api.rb', line 275

def event_webhook_get(id)
  api_http_get('/hooks/' + id)
end

#event_webhook_listallObject



271
272
273
# File 'lib/blockcypher/api.rb', line 271

def event_webhook_listall
  api_http_get('/hooks')
end

#event_webhook_subscribe(url, event, options = {}) ⇒ Object

Events API



262
263
264
265
266
267
268
269
# File 'lib/blockcypher/api.rb', line 262

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) ⇒ Object

Faucet API



52
53
54
55
# 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_addressObject

Asset API #



316
317
318
# File 'lib/blockcypher/api.rb', line 316

def generate_asset_address
  api_http_post('/oap/addrs')
end

#issue_asset(from_private, to_address, amount) ⇒ Object



320
321
322
323
324
325
326
# File 'lib/blockcypher/api.rb', line 320

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_addressesObject



304
305
306
# File 'lib/blockcypher/api.rb', line 304

def list_forwarding_addresses
  api_http_get('/payments')
end

#microtx_from_priv(private_key, to_address, value_satoshis) ⇒ Object

This method sends private key to server



144
145
146
147
148
149
150
151
# 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) ⇒ Object

This method uses public key, signs with private key locally



154
155
156
157
158
159
160
161
162
163
164
# 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) ⇒ Object



105
106
107
108
# 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) ⇒ Object



66
67
68
69
# 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) ⇒ Object



71
72
73
74
75
76
77
# 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) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# 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) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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) ⇒ Object



124
125
126
127
# 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) ⇒ Object



129
130
131
132
133
# 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) ⇒ Object



96
97
98
99
100
101
102
103
# 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) ⇒ Object



328
329
330
331
332
333
334
# File 'lib/blockcypher/api.rb', line 328

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) ⇒ Object



135
136
137
# 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) ⇒ Object



234
235
236
237
238
239
# File 'lib/blockcypher/api.rb', line 234

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) ⇒ Object

Wallet API



225
226
227
228
# File 'lib/blockcypher/api.rb', line 225

def wallet_create(name, addresses)
  payload = { 'name' => name, 'addresses' => Array(addresses) }
  api_http_post('/wallets', json_payload: payload)
end

#wallet_delete(name) ⇒ Object



254
255
256
# File 'lib/blockcypher/api.rb', line 254

def wallet_delete(name)
  api_http_delete('/wallets/' + name)
end

#wallet_delete_addr(name, addresses) ⇒ Object



245
246
247
248
# File 'lib/blockcypher/api.rb', line 245

def wallet_delete_addr(name, addresses)
  addrjoin = addresses.join(';')
  api_http_delete('/wallets/' + name + '/addresses', query: { address: addrjoin })
end

#wallet_gen_addr(name) ⇒ Object



250
251
252
# File 'lib/blockcypher/api.rb', line 250

def wallet_gen_addr(name)
  api_http_post('/wallets/' + name + '/addresses/generate')
end

#wallet_get(name) ⇒ Object



230
231
232
# File 'lib/blockcypher/api.rb', line 230

def wallet_get(name)
  api_http_get('/wallets/' + name)
end

#wallet_get_addr(name) ⇒ Object



241
242
243
# File 'lib/blockcypher/api.rb', line 241

def wallet_get_addr(name)
  api_http_get('/wallets/' + name + '/addresses')
end