class Blockchain::Wallet
Attributes
client[R]
Public Class Methods
new(identifier, password, url = 'http://localhost:3000/', second_password = nil, api_code = nil)
click to toggle source
# File lib/blockchain/wallet.rb, line 10 def initialize(identifier, password, url = 'http://localhost:3000/', second_password = nil, api_code = nil) @client = Client.new(url, api_code) @identifier = identifier @password = password @second_password = second_password end
Public Instance Methods
archive_address(address)
click to toggle source
# File lib/blockchain/wallet.rb, line 98 def archive_address(address) params = build_basic_request() params['address'] = address response = @client.call_api("merchant/#{@identifier}/archive_address", method: 'post', data: params) json_response = parse_json(response) return json_response['archived'] end
build_basic_request()
click to toggle source
# File lib/blockchain/wallet.rb, line 114 def build_basic_request() params = { 'password' => @password } if !@second_password.nil? params['second_password'] = @second_password end return params end
get_address(address)
click to toggle source
# File lib/blockchain/wallet.rb, line 76 def get_address(address) params = build_basic_request() params['address'] = address response = @client.call_api("merchant/#{@identifier}/address_balance", method: 'get', data: params) json_response = parse_json(response) return WalletAddress.new(json_response['balance'], json_response['address'], nil, json_response['total_received']) end
get_balance()
click to toggle source
# File lib/blockchain/wallet.rb, line 54 def get_balance() response = @client.call_api("merchant/#{@identifier}/balance", method: 'get', data: build_basic_request()) json_response = parse_json(response) return json_response['balance'] end
list_addresses()
click to toggle source
# File lib/blockchain/wallet.rb, line 60 def list_addresses() params = build_basic_request() response = @client.call_api("merchant/#{@identifier}/list", method: 'get', data: params) json_response = parse_json(response) addresses = [] json_response['addresses'].each do |a| addr = WalletAddress.new(a['balance'], a['address'], a['label'], a['total_received']) addresses.push(addr) end return addresses end
new_address(label = nil)
click to toggle source
# File lib/blockchain/wallet.rb, line 87 def new_address(label = nil) params = build_basic_request() if !label.nil? then params['label'] = label end response = @client.call_api("merchant/#{@identifier}/new_address", method: 'post', data: params) json_response = parse_json(response) return WalletAddress.new(0, json_response['address'], json_response['label'], 0) end
parse_json(response)
click to toggle source
convenience method that parses a response into json AND makes sure there are no errors
# File lib/blockchain/wallet.rb, line 123 def parse_json(response) json_response = JSON.parse(response) error = json_response['error'] if !error.nil? then raise APIException.new(error) end return json_response end
send(to, amount, from_address: nil, fee: nil)
click to toggle source
# File lib/blockchain/wallet.rb, line 17 def send(to, amount, from_address: nil, fee: nil) recipient = { to => amount } return send_many(recipient, from_address: from_address, fee: fee) end
send_many(recipients, from_address: nil, fee: nil)
click to toggle source
# File lib/blockchain/wallet.rb, line 22 def send_many(recipients, from_address: nil, fee: nil) params = build_basic_request() method = '' if recipients.nil? || recipients.size == 0 raise ArgumentError, 'Sending bitcoin from your wallet requires at least one receipient' end if recipients.size == 1 params['to'] = recipients.keys[0] params['amount'] = recipients.values[0] method = 'payment' else params['recipients'] = JSON.dump(recipients) method = 'sendmany' end if !from_address.nil? params['from'] = from_address end if !fee.nil? params['fee'] = fee end response = @client.call_api("merchant/#{@identifier}/#{method}", method: 'post', data: params) json_response = parse_json(response) return PaymentResponse.new( json_response['message'], json_response['tx_hash'], json_response['notice']) end
unarchive_address(address)
click to toggle source
# File lib/blockchain/wallet.rb, line 106 def unarchive_address(address) params = build_basic_request() params['address'] = address response = @client.call_api("merchant/#{@identifier}/unarchive_address", method: 'post', data: params) json_response = parse_json(response) return json_response['active'] end