Class: Bitcoin::Address
- Inherits:
-
Object
- Object
- Bitcoin::Address
- Defined in:
- lib/bc.rb
Overview
This class represents a Bitcoin address.
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
This is our actual (String) Bitcoin address.
-
#bc ⇒ Object
readonly
This is the Bitcoin::Client instance we are connected to.
Instance Method Summary collapse
-
#account ⇒ Object
Get the Account we’re associated with.
-
#account=(account) ⇒ Object
Associate us with
account
, which can either be a String or an Account. -
#initialize(bc, address) ⇒ Address
constructor
bc
is a Bitcoin::Client instance. -
#inspect ⇒ Object
:nodoc:.
-
#private_key ⇒ Object
This is the (String) private key associated with this address.
-
#sign(msg) ⇒ Object
Sign the (String) message
msg
. -
#to_s ⇒ Object
:nodoc:.
-
#transactions ⇒ Object
Return an Array of every Transaction associated with us.
-
#valid? ⇒ Boolean
Are we a valid Bitcoin address?.
-
#verify(msg, sig) ⇒ Object
Verify the signature
sig
of a messagemsg
signed by our private key.
Constructor Details
#initialize(bc, address) ⇒ Address
bc
is a Bitcoin::Client instance. address
is a (String) Bitcoin address that we have the private key for.
465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/bc.rb', line 465 def initialize(bc, address) @bc = bc @address = address unless @bc.is_a?(Bitcoin::Client) raise TypeError, "bc must be a Bitcoin::Client (#{@bc.class} given)" end unless valid? raise InvalidAddress, @address end end |
Instance Attribute Details
#address ⇒ Object (readonly)
This is our actual (String) Bitcoin address.
461 462 463 |
# File 'lib/bc.rb', line 461 def address @address end |
#bc ⇒ Object (readonly)
This is the Bitcoin::Client instance we are connected to.
458 459 460 |
# File 'lib/bc.rb', line 458 def bc @bc end |
Instance Method Details
#account ⇒ Object
Get the Account we’re associated with.
511 512 513 |
# File 'lib/bc.rb', line 511 def account @bc.get_account(@bc.jr.getaccount(@address)) end |
#account=(account) ⇒ Object
Associate us with account
, which can either be a String or an Account. Either way, we will return an Account.
517 518 519 520 |
# File 'lib/bc.rb', line 517 def account=(account) @bc.jr.setaccount(@address, account.to_s) @bc.get_account(account.to_s) end |
#inspect ⇒ Object
:nodoc:
553 554 555 |
# File 'lib/bc.rb', line 553 def inspect # :nodoc: "#<Bitcoin::Address #{@address}>" end |
#private_key ⇒ Object
This is the (String) private key associated with this address. If this account’s address is invalid (or from the wrong bitcoin network), we raise InvalidAddress. If we don’t have the private key associated with this address (or our wallet is locked), we return nil
.
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 |
# File 'lib/bc.rb', line 494 def private_key begin @private_key ||= @bc.jr.dumpprivkey(@address) rescue Jr::ServerError => ex $ex = ex.code case ex.code when -5 raise(InvalidAddress, @address) when -4 nil else raise end end end |
#sign(msg) ⇒ Object
Sign the (String) message msg
. We return a detached base-64 encoded signature (String). In order to verify the message, you will need both the signature and msg
. If we don’t know private key, we raise UnknownPrivateKey. (c.f. Client.verify())
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
# File 'lib/bc.rb', line 526 def sign(msg) begin @bc.jr.(@address, msg) rescue Jr::ServerError => ex case ex.code when -13 raise LockedWallet when -4 raise UnknownPrivateKey, @address else raise end end end |
#to_s ⇒ Object
:nodoc:
549 550 551 |
# File 'lib/bc.rb', line 549 def to_s # :nodoc: @address end |
#transactions ⇒ Object
Return an Array of every Transaction associated with us.
479 480 481 482 483 |
# File 'lib/bc.rb', line 479 def transactions account.transactions.find_all do |tx| tx.include?(self) end end |
#valid? ⇒ Boolean
Are we a valid Bitcoin address?
486 487 488 |
# File 'lib/bc.rb', line 486 def valid? @bc.is_valid?(@address) end |
#verify(msg, sig) ⇒ Object
Verify the signature sig
of a message msg
signed by our private key. We return true
if the signature is valid, or false
if it is not.
545 546 547 |
# File 'lib/bc.rb', line 545 def verify(msg, sig) @bc.jr.(@address, msg, sig) end |