Module: Bitcoin::Client

Defined in:
lib/bitcoin/client.rb,
lib/bitcoin/client/info.rb,
lib/bitcoin/client/configuration.rb

Overview

A singleton abstraction of the Bitcoin client. It accesses the client via the JSON-RPC interface which can be enabled on the official client. Once the Client is configured it can be reused for multiple calls.

Examples:

Bitcoin::Client.configure { |c| c.location = 'http://user:password@127.0.0.1:8332' }

Author:

Defined Under Namespace

Classes: Configuration, Info

Class Method Summary (collapse)

Class Method Details

+ (nil) back_up_wallet(path = nil) Also known as: backup_wallet, backupwallet

Back up the current state of the wallet to a specified file or folder. Be sure to use forward-slashes for path separators.

Examples

Bitcoin::Client.back_up_wallet
# => nil

Bitcoin::Client.back_up_wallet '/var/wallet/20111213000000000.dat'
# => nil

Parameters:

  • path (String) (defaults to: nil)

    The path where the wallet backup should be written. The target file should not exist and should be specified using Linux style (/var/wallet_backups/). If not specified a generated path such as /tmp/wallet.#{timestamp}.dat will be used.

Returns:

  • (nil)


79
80
81
82
# File 'lib/bitcoin/client.rb', line 79

def self.back_up_wallet(path = nil)
  path ||= "/tmp/wallet.#{Time.now.utc.to_i}.dat"
  call('backupwallet', path)
end

+ (Object) call(method, *args) (private)

Package the parameters provided into a JSON-RPC call and post them to the Bitcoin client.

Parameters:

  • method (String)

    The method that the Bitcoin client understands

  • args

    An indeterminate number of parameters that vary by method in number and type.

Returns:

  • (Object)

    Usually a Hash, String, Integer or Float containing the Bitcoin client's response unless an error occurs.

Raises:



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/bitcoin/client.rb', line 232

def self.call(method, *args)
  raise CommunicationError.new, 
        CommunicationError::UNKNOWN_HOST if @configuration.location.empty?

  post_json = {
    'method' => method, 
    'params' => args, 
    'id' => @configuration.identifier
  }.to_json

  begin
    response_json = RestClient.post(@configuration.location, post_json)
  rescue RestClient::ResourceNotFound
    return nil
  end
  response = JSON.parse response_json
  raise CommunicationError.new, response['error'] if response['error']
  
  response['result']
end

+ (nil) configure {|Bitcoin::Client::Configuration| ... }

Configure the connection to the Bitcoin client before you start interacting with it.

Examples:

Bitcoin::Client.configure { |config| config.location = 'http://user:password@127.0.0.1:8332' }

Yields:

  • (Bitcoin::Client::Configuration)

    The object yielded can be used to assign any of the following:

    `location`:: A String location where the client can be reached.
    `identifier`:: A String identifying your application.

Returns:

  • (nil)


44
45
46
47
# File 'lib/bitcoin/client.rb', line 44

def self.configure(&block)
  @configuration ||= Configuration.new
  yield(@configuration)
end

+ (Bitcoin::Account) get_account(address) Also known as: getaccount

Retrieve the Account that an Address belongs to.

Parameters:

  • address (String, Bitcoin::Address)

    The address you wish to find the account for.

Returns:



182
183
184
# File 'lib/bitcoin/client.rb', line 182

def self.(address)
  Account.new(call('getaccount', address.to_s))
end

+ (Float) get_balance(account = '', minimum_confirmations = 1) Also known as: getbalance

Get the current balance of either a specific account or the global account.

Examples

Bitcoin::Client.get_balance
# => 15.4

Bitcoin::Client.get_balance('Savings')
# => 12.0

Bitcoin::Client.get_balance('Non-existent')
# => 0.0

Bitcoin::Client.get_balance('', 0)
# => 23.6

Parameters:

  • account (String) (defaults to: '')

    The account name. An empty String implies the global account which is the default.

  • minimum_confirmations (Integer) (defaults to: 1)

    The minimum number of confirmations from the network for a transaction to be included in the balance. If 0 is provided here unconfirmed transactions will also be included. The default is 1.

Returns:

  • (Float)

    The balance of the account or 0.0 if it does not exist.



109
110
111
# File 'lib/bitcoin/client.rb', line 109

def self.get_balance( = '', minimum_confirmations = 1)
  call('getbalance', .to_s, minimum_confirmations)
end

+ (Bitcoin::Client::Info) get_info Also known as: getinfo

Gets the current state of the Bitcoin client. See Bitcoin::Client::Info for documentation on what information it provides.

Example

Bitcoin::Client.get_info.mining?
# => false

Returns:



58
59
60
# File 'lib/bitcoin/client.rb', line 58

def self.get_info
  Info.new(call('getinfo'))
end

+ (Bitcoin::Address) get_new_address(account = nil) Also known as: getnewaddress

Generate a new address for a specific account or the global account

Parameters:

  • account (String, Bitcoin::Account) (defaults to: nil)

    The account the new address should belong to. Use this if you want to track payments to this address.

Returns:



193
194
195
196
197
198
199
# File 'lib/bitcoin/client.rb', line 193

def self.get_new_address( = nil)
  if 
    Address.new(call('getnewaddress', .to_s), true, true, .to_s)
  else
    Address.new(call('getnewaddress'), true, true, .to_s)
  end
end

+ (Hash) list_accounts(minimum_confirmations = 1) Also known as: listaccounts

Fetch all accounts with their current balances

Parameters:

  • minimum_confirmations (Integer) (defaults to: 1)

    The number of confirmations that are required on a Transaction in order for it to contribute to the total balance.

Returns:

  • (Hash)

    The Hash returned has the account name as a String for keys and the Bitcoin::Account object as the value. The balance can be retrieved from these objects.



167
168
169
170
171
172
173
174
# File 'lib/bitcoin/client.rb', line 167

def self.list_accounts(minimum_confirmations = 1)
  return_hash = {}
  call('listaccounts', minimum_confirmations).each do |name, balance|
    return_hash[name] = Account.new(name, balance)
  end

  return_hash
end

+ (Hash) list_received_by_address(minimum_confirmations = 1, include_empty_addresses = false) Also known as: listreceivedbyaddress

Fetch total amounts received by the Bitcoin client per Address.

Parameters:

  • minimum_confirmations (Integer) (defaults to: 1)

    The number of confirmations required for the transaction to count toward the total.

  • include_empty_addresses (true, false) (defaults to: false)

    A flag you can set if you want a list of all addresses, otherwise empty addresses are excluded.

Returns:

  • (Hash)

    A Hash with String addresses as keys and a Bitcoin::Address in :address, a Float for the total amount received in :amount and a Integer number of confirmations for the most recent transaction in :confirmations.



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/bitcoin/client.rb', line 144

def self.list_received_by_address(minimum_confirmations = 1, include_empty_addresses = false)
  raw_hash = call('listreceivedbyaddress', minimum_confirmations, include_empty_addresses)
  return_hash = {}
  raw_hash.each do |raw_object|
    return_hash[raw_object['address']] = {
      :address => Address.new(raw_object['address'], true, true, Bitcoin::Account.new(raw_object['account'])),
      :amount => raw_object['amount'],
      :confirmations => raw_object['confirmations']
    }
  end

  return_hash
end

+ (Object) method_missing(name, *args, &block) (private)

Allows methods that have not been implemented to be used. Over time this will be relied upon less and less and changes to the return types of method calls captured by method_missing will not be considered backwards-incompatible for semantic versioning considerations.



219
220
221
# File 'lib/bitcoin/client.rb', line 219

def self.method_missing(name, *args, &block)
  return call(name.to_s.gsub('_', ''), *args)
end

+ (Bitcoin::Address) validate_address(address) Also known as: validateaddress

Validates a Bitcoin address and instantiates an appropriate Address object.

Parameters:

  • address (String)

    the cryptographic hash of the address

Returns:



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/bitcoin/client.rb', line 119

def self.validate_address(address)
  result_hash = call('validateaddress', address)

  if result_hash
    Address.new(result_hash['address'], 
                result_hash['isvalid'], 
                result_hash['ismine'], 
                result_hash['account'])
  else
    Address.new(address, false)
  end
end