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.



298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/bc.rb', line 298

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.



290
291
292
# File 'lib/bc.rb', line 290

def bc
  @bc
end

#nameObject (readonly)

This (String) is our account designation.



293
294
295
# File 'lib/bc.rb', line 293

def name
  @name
end

Instance Method Details

#addressesObject

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



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

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.



335
336
337
# File 'lib/bc.rb', line 335

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

Donate amount to katmagic (bc’s author).



430
431
432
433
434
435
436
437
438
# File 'lib/bc.rb', line 430

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

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

	tx
end

#inspectObject

:nodoc:



444
445
446
# File 'lib/bc.rb', line 444

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

#new_addressObject

Get a new Address associated with this account.



359
360
361
362
363
364
365
366
367
368
369
# File 'lib/bc.rb', line 359

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.



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/bc.rb', line 374

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.



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/bc.rb', line 401

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:



440
441
442
# File 'lib/bc.rb', line 440

def to_s # :nodoc:
	@name
end

#transactionsObject

Get every Transaction associated with this account.



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/bc.rb', line 312

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.



346
347
348
349
350
351
352
353
354
355
356
# File 'lib/bc.rb', line 346

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