Class: Bitcoin::Account

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

Overview

This is a bitcoind account. (c.f. en.bitcoin.it/wiki/Accounts_explained)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bc, account_name) ⇒ Account

bc is a Bitcoin::Client instance. account_name is the (String) name of the account we’re associated with. account_name may be “”, in which case we represent the default account.



282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/bc.rb', line 282

def initialize(bc, )
  @bc = bc
  @name = .freeze

  unless @name.is_a?(String)
    raise TypeError, "account_name must be a String (#{@name.class} given)"
  end

  unless @bc.is_a?(Bitcoin::Client)
    raise TypeError, "bc must be a Bitcoin::Client (#{@bc.class} given)"
  end
end

Instance Attribute Details

#bcObject (readonly)

This is the Bitcoin::Client instance we are connected to.



274
275
276
# File 'lib/bc.rb', line 274

def bc
  @bc
end

#nameObject (readonly)

This (String) is our account designation.



277
278
279
# File 'lib/bc.rb', line 277

def name
  @name
end

Instance Method Details

#addressesObject

This is an Array of every Address associated with this account.



324
325
326
# File 'lib/bc.rb', line 324

def addresses
  @bc.jr.getaddressesbyaccount(@name).map(&@bc.method(:get_address))
end

#balance(minimum_confirmations = 1) ⇒ Object

Fetch the balance of this account. Only deposits with at least minimum_confirmations will be included in this total.



319
320
321
# File 'lib/bc.rb', line 319

def balance(minimum_confirmations=1)
  @bc.jr.getbalance(@name, minimum_confirmations)
end

Donate amount to katmagic (bc’s author).



414
415
416
417
418
419
420
421
422
# File 'lib/bc.rb', line 414

def donate(amount)
  tx = send('1LzDffumxiCSh8wEpxWE8fUozb2LUTcL8L', amount)

  if STDOUT.tty?
    puts('katmagic l♥ves y♥u ♥♥dles!')
  end

  tx
end

#inspectObject

:nodoc:



428
429
430
# File 'lib/bc.rb', line 428

def inspect # :nodoc:
  "#<Bitcoin::Account #{@name.inspect}>"
end

#new_addressObject

Get a new Address associated with this account.



343
344
345
346
347
348
349
350
351
352
353
# File 'lib/bc.rb', line 343

def new_address
  begin
    @bc.get_address(@bc.jr.getnewaddress(@name))
  rescue Jr::ServerError => ex
    if ex.code == -12
      raise EmptyKeypool
    else
      raise
    end
  end
end

#send(dest, amount) ⇒ Object

Send amount Bitcoin to dest. amount should be a positive real number; dest can either be a (String) bitcoin address, or an Address instance. We return a Transaction.



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/bc.rb', line 358

def send(dest, amount)
  dest = dest.to_s

  begin
    txid = @bc.jr.sendfrom(@name, dest, amount)
  rescue Jr::ServerError => ex
    case ex.code
      when -13
        raise LockedWallet

      when -6
        raise InsufficientFunds.new(amount, balance)

      when -5
        raise InvalidAddress, dest

      else
        raise
    end
  end

  @bc.get_transaction(txid)
end

#send_to_many(dests) ⇒ Object

dests is a Hash whose keys are Address or String instances and whose values are positive real numbers. Each key is sent the amount of Bitcoin specified by its value. We return a Transaction.



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/bc.rb', line 385

def send_to_many(dests)
  dests = Hash[
    dests.map do |dest, amount|
      [dest.to_s, amount]
    end
  ]

  begin
    txid = @bc.jr.sendmany(@name, dest, amount)
  rescue Jr::ServerError => ex
    case ex.code
      when -13
        raise LockedWallet

      when -6
        raise InsufficientFunds.new(amount.values.reduce(&:+), balance)

      when -5
        raise InvalidAddress, ex.split(':').fetch(1)

      else
        raise
    end
  end

  @bc.get_transaction(txid)
end

#to_sObject

:nodoc:



424
425
426
# File 'lib/bc.rb', line 424

def to_s # :nodoc:
  @name
end

#transactionsObject

Get every Transaction associated with this account.



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/bc.rb', line 296

def transactions
  grab = 20
  position = 0
  transactions = Array.new

  loop do
    new_transactions = @bc.jr.listtransactions(@name, grab, position)
    transactions += new_transactions.map{|tx| tx.fetch('txid')}

    if new_transactions.length < grab
      break
    else
      position += grab
    end
  end

  transactions.uniq.map do |tx|
    Transaction.new(@bc, tx)
  end
end

#unused_addressObject

Get an unused Address associated with this account, or create one if one doesn’t already exist.



330
331
332
333
334
335
336
337
338
339
340
# File 'lib/bc.rb', line 330

def unused_address
  begin
    @bc.get_address(@bc.jr.getaccountaddress(@name))
  rescue Jr::ServerError => ex
    if ex.code == -12
      raise EmptyKeypool
    else
      raise
    end
  end
end