Class: BlockCypher::Api
- Inherits:
-
Object
- Object
- BlockCypher::Api
- Defined in:
- lib/blockcypher/api.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#api_token ⇒ Object
readonly
Returns the value of attribute api_token.
Instance Method Summary collapse
- #address_balance(address) ⇒ Object
- #address_details(address, unspent_only: false) ⇒ Object
- #address_final_balance(address) ⇒ Object
- #address_full_txs(address, limit: 10) ⇒ Object
-
#address_generate ⇒ Object
Address APIs.
- #address_generate_multi(pubkeys, script_type) ⇒ Object
- #blockchain ⇒ Object
- #blockchain_block(block_index, params) ⇒ Object
- #blockchain_transaction(transaction_hash) ⇒ Object
-
#blockchain_unconfirmed_tx ⇒ Object
Blockchain API.
-
#create_forwarding_address(destination, callback_url: nil, enable_confirmations: false) ⇒ Object
(also: #create_payments_forwarding)
Payments and Forwarding API.
-
#decode_hex(hex) ⇒ Object
Transaction API.
- #delete_forwarding_address(id) ⇒ Object
- #event_webhook_delete(id) ⇒ Object
- #event_webhook_get(id) ⇒ Object
- #event_webhook_listall ⇒ Object
-
#event_webhook_subscribe(url, event, confirmations: nil, hash: nil, address: nil, script: nil) ⇒ Object
Events API.
-
#faucet(address, amount) ⇒ Object
Faucet API.
-
#initialize(version: V1, currency: BTC, network: MAIN_NET, api_token: nil) ⇒ Api
constructor
A new instance of Api.
- #list_forwarding_addresses ⇒ Object
-
#microtx_from_priv(private_key, to_address, value_satoshis) ⇒ Object
This method sends private key to server.
-
#microtx_from_pub(private_key, to_address, value_satoshis) ⇒ Object
This method uses public key, signs with private key locally.
- #pubkey_from_priv(private_key) ⇒ Object
- #push_hex(hex) ⇒ Object
- #send_money(from_address, to_address, satoshi_amount, private_key) ⇒ Object
- #signer(private_key, tosign) ⇒ Object
- #transaction_new(input_addreses, output_addresses, satoshi_amount) ⇒ Object
- #transaction_new_custom(payload) ⇒ Object
- #transaction_send_custom(payload) ⇒ Object
- #transaction_sign_and_send(new_tx, private_key) ⇒ Object
- #tx_confidence(tx_hash) ⇒ Object
- #wallet_add_addr(name, addresses) ⇒ Object
-
#wallet_create(name, addresses) ⇒ Object
Wallet API.
- #wallet_delete(name) ⇒ Object
- #wallet_delete_addr(name, addresses) ⇒ Object
- #wallet_gen_addr(name) ⇒ Object
- #wallet_get(name) ⇒ Object
- #wallet_get_addr(name) ⇒ Object
Constructor Details
#initialize(version: V1, currency: BTC, network: MAIN_NET, api_token: nil) ⇒ Api
Returns a new instance of Api.
20 21 22 23 24 25 |
# File 'lib/blockcypher/api.rb', line 20 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_token ⇒ Object (readonly)
Returns the value of attribute api_token.
18 19 20 |
# File 'lib/blockcypher/api.rb', line 18 def api_token @api_token end |
Instance Method Details
#address_balance(address) ⇒ Object
185 186 187 |
# File 'lib/blockcypher/api.rb', line 185 def address_balance(address) api_http_get('/addrs/' + address + '/balance') end |
#address_details(address, unspent_only: false) ⇒ Object
181 182 183 |
# File 'lib/blockcypher/api.rb', line 181 def address_details(address, unspent_only: false) api_http_get('/addrs/' + address, query: { unspentOnly: unspent_only } ) end |
#address_final_balance(address) ⇒ Object
189 190 191 192 |
# File 'lib/blockcypher/api.rb', line 189 def address_final_balance(address) details = address_balance(address) details['final_balance'] end |
#address_full_txs(address, limit: 10) ⇒ Object
194 195 196 |
# File 'lib/blockcypher/api.rb', line 194 def address_full_txs(address, limit: 10) api_http_get("/addrs/#{address}/full", query: { limit: limit }) end |
#address_generate ⇒ Object
Address APIs
172 173 174 |
# File 'lib/blockcypher/api.rb', line 172 def address_generate api_http_post('/addrs') end |
#address_generate_multi(pubkeys, script_type) ⇒ Object
176 177 178 179 |
# File 'lib/blockcypher/api.rb', line 176 def address_generate_multi(pubkeys, script_type) payload = { 'pubkeys' => pubkeys, 'script_type' => script_type} api_http_post('/addrs', json_payload: payload) end |
#blockchain ⇒ Object
43 44 45 |
# File 'lib/blockcypher/api.rb', line 43 def blockchain api_http_get('') end |
#blockchain_block(block_index, params) ⇒ Object
39 40 41 |
# File 'lib/blockcypher/api.rb', line 39 def blockchain_block(block_index, params) api_http_get('/blocks/' + block_index, query: params) end |
#blockchain_transaction(transaction_hash) ⇒ Object
35 36 37 |
# File 'lib/blockcypher/api.rb', line 35 def blockchain_transaction(transaction_hash) api_http_get('/txs/' + transaction_hash) end |
#blockchain_unconfirmed_tx ⇒ Object
Blockchain API
31 32 33 |
# File 'lib/blockcypher/api.rb', line 31 def blockchain_unconfirmed_tx api_http_get('/txs') end |
#create_forwarding_address(destination, callback_url: nil, enable_confirmations: false) ⇒ Object Also known as: create_payments_forwarding
Payments and Forwarding API
265 266 267 268 269 270 271 272 |
# File 'lib/blockcypher/api.rb', line 265 def create_forwarding_address(destination, callback_url: nil, enable_confirmations: false) payload = { destination: destination, callback_url: callback_url, enable_confirmations: enable_confirmations } api_http_post('/payments', json_payload: payload) end |
#decode_hex(hex) ⇒ Object
Transaction API
60 61 62 63 |
# File 'lib/blockcypher/api.rb', line 60 def decode_hex(hex) payload = { 'tx' => hex} api_http_post('/txs/decode', json_payload: payload) end |
#delete_forwarding_address(id) ⇒ Object
280 281 282 |
# File 'lib/blockcypher/api.rb', line 280 def delete_forwarding_address(id) api_http_delete("/payments/" + id) end |
#event_webhook_delete(id) ⇒ Object
257 258 259 |
# File 'lib/blockcypher/api.rb', line 257 def event_webhook_delete(id) api_http_delete('/hooks/' + id) end |
#event_webhook_get(id) ⇒ Object
253 254 255 |
# File 'lib/blockcypher/api.rb', line 253 def event_webhook_get(id) api_http_get('/hooks/' + id) end |
#event_webhook_listall ⇒ Object
249 250 251 |
# File 'lib/blockcypher/api.rb', line 249 def event_webhook_listall api_http_get('/hooks') end |
#event_webhook_subscribe(url, event, confirmations: nil, hash: nil, address: nil, script: nil) ⇒ Object
Events API
237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/blockcypher/api.rb', line 237 def event_webhook_subscribe(url, event, confirmations: nil, hash:nil, address:nil, script:nil) payload = { url: url, event: event, } payload[:address] = address if address payload[:hash] = hash if hash payload[:script] = script if script payload[:confirmations] = confirmations if confirmations api_http_post('/hooks', json_payload: payload) end |
#faucet(address, amount) ⇒ Object
Faucet API
51 52 53 54 |
# File 'lib/blockcypher/api.rb', line 51 def faucet(address, amount) payload = { 'address' => address, 'amount' => amount } api_http_post('/faucet', json_payload: payload) end |
#list_forwarding_addresses ⇒ Object
276 277 278 |
# File 'lib/blockcypher/api.rb', line 276 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
146 147 148 149 150 151 152 153 |
# File 'lib/blockcypher/api.rb', line 146 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
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/blockcypher/api.rb', line 156 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
107 108 109 110 |
# File 'lib/blockcypher/api.rb', line 107 def pubkey_from_priv(private_key) key = Bitcoin::Key.new(private_key, nil, compressed = true) key.pub end |
#push_hex(hex) ⇒ Object
65 66 67 68 |
# File 'lib/blockcypher/api.rb', line 65 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
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/blockcypher/api.rb', line 70 def send_money(from_address, to_address, satoshi_amount, private_key) unless to_address.kind_of? Array to_address = [to_address] end tx_new = transaction_new([from_address], to_address, satoshi_amount) transaction_sign_and_send(tx_new, private_key) end |
#signer(private_key, tosign) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/blockcypher/api.rb', line 112 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.unpack("H*").first signatures << sig_hex end return signatures end |
#transaction_new(input_addreses, output_addresses, satoshi_amount) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/blockcypher/api.rb', line 81 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
126 127 128 129 |
# File 'lib/blockcypher/api.rb', line 126 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
131 132 133 134 135 |
# File 'lib/blockcypher/api.rb', line 131 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
98 99 100 101 102 103 104 105 |
# File 'lib/blockcypher/api.rb', line 98 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 |
#tx_confidence(tx_hash) ⇒ Object
137 138 139 |
# File 'lib/blockcypher/api.rb', line 137 def tx_confidence(tx_hash) api_http_get('/txs/' + tx_hash + '/confidence') end |
#wallet_add_addr(name, addresses) ⇒ Object
211 212 213 214 |
# File 'lib/blockcypher/api.rb', line 211 def wallet_add_addr(name, addresses) payload = { 'addresses' => Array(addresses)} api_http_post('/wallets/' + name + '/addresses', json_payload: payload) end |
#wallet_create(name, addresses) ⇒ Object
Wallet API
202 203 204 205 |
# File 'lib/blockcypher/api.rb', line 202 def wallet_create(name, addresses) payload = { 'name' => name, 'addresses' => Array(addresses)} api_http_post('/wallets', json_payload: payload) end |
#wallet_delete(name) ⇒ Object
229 230 231 |
# File 'lib/blockcypher/api.rb', line 229 def wallet_delete(name) api_http_delete('/wallets/' + name) end |
#wallet_delete_addr(name, addresses) ⇒ Object
220 221 222 223 |
# File 'lib/blockcypher/api.rb', line 220 def wallet_delete_addr(name, addresses) addrjoin = addresses.join(";") api_http_delete('/wallets/' + name + '/addresses', query: { address: addrjoin}) end |
#wallet_gen_addr(name) ⇒ Object
225 226 227 |
# File 'lib/blockcypher/api.rb', line 225 def wallet_gen_addr(name) api_http_post('/wallets/' + name + '/addresses/generate') end |
#wallet_get(name) ⇒ Object
207 208 209 |
# File 'lib/blockcypher/api.rb', line 207 def wallet_get(name) api_http_get('/wallets/' + name) end |
#wallet_get_addr(name) ⇒ Object
216 217 218 |
# File 'lib/blockcypher/api.rb', line 216 def wallet_get_addr(name) api_http_get('/wallets/' + name + '/addresses') end |