Class: Counterparty::Connection
- Inherits:
-
Object
- Object
- Counterparty::Connection
- Defined in:
- lib/counterparty/connection.rb
Overview
This class connects to the Counterparty api. Mostly it’s not intended for use by library consumers, but there are some helper methods in here for those that prefer the Connection.get_burns syntax instead of the Counterparty::Burn.find syntax
Constant Summary collapse
- DEFAULT_TIMEOUT =
The default connection timeout, nil is “no timeout”
nil
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#password ⇒ Object
Returns the value of attribute password.
-
#port ⇒ Object
Returns the value of attribute port.
-
#timeout ⇒ Object
writeonly
A response timeout threshold By default, this initializes to -1, which means the library will wait indefinitely before timing out.
-
#username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
-
.resource_request(klass) ⇒ Object
This creates the legacy api-format request methods on the connection argument.
Instance Method Summary collapse
-
#api_url ⇒ Object
The url being connected to for the purpose of an api call.
-
#broadcast_tx(signed_tx) ⇒ Object
Broadcasts a signed transaction onto the bitcoin blockchain.
-
#initialize(port = 14000, username = 'rpc', password = '1234', host = 'localhost') ⇒ Connection
constructor
A new instance of Connection.
-
#request(method, params) ⇒ Object
Issue a request to the counterpartyd server for the given method, with the given params.
-
#sign_tx(raw_tx, private_key) ⇒ Object
Returns a signed raw transaction, given a private key.
Constructor Details
#initialize(port = 14000, username = 'rpc', password = '1234', host = 'localhost') ⇒ Connection
Returns a new instance of Connection.
16 17 18 19 |
# File 'lib/counterparty/connection.rb', line 16 def initialize(port=14000, username='rpc', password='1234', host='localhost') @host,@port,@username,@password=host.to_s,port.to_i,username.to_s,password.to_s @timeout = DEFAULT_TIMEOUT end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
10 11 12 |
# File 'lib/counterparty/connection.rb', line 10 def host @host end |
#password ⇒ Object
Returns the value of attribute password.
10 11 12 |
# File 'lib/counterparty/connection.rb', line 10 def password @password end |
#port ⇒ Object
Returns the value of attribute port.
10 11 12 |
# File 'lib/counterparty/connection.rb', line 10 def port @port end |
#timeout=(value) ⇒ Object (writeonly)
A response timeout threshold By default, this initializes to -1, which means the library will wait indefinitely before timing out
14 15 16 |
# File 'lib/counterparty/connection.rb', line 14 def timeout=(value) @timeout = value end |
#username ⇒ Object
Returns the value of attribute username.
10 11 12 |
# File 'lib/counterparty/connection.rb', line 10 def username @username end |
Class Method Details
.resource_request(klass) ⇒ Object
This creates the legacy api-format request methods on the connection argument. It lets us be relatively introspective about how we do this so as to be future-proof here going forward
54 55 56 57 58 |
# File 'lib/counterparty/connection.rb', line 54 def self.resource_request(klass) # :nodoc: define_method(klass.to_get_request){ |params| klass.find params } define_method(klass.to_do_request){ |params| klass.create(params).save! } define_method(klass.to_create_request){ |params| klass.create(params).to_raw_tx } end |
Instance Method Details
#api_url ⇒ Object
The url being connected to for the purpose of an api call
22 23 24 |
# File 'lib/counterparty/connection.rb', line 22 def api_url 'http://%s:%s@%s:%s/api/' % [@username,@password,@host,@port.to_s] end |
#broadcast_tx(signed_tx) ⇒ Object
Broadcasts a signed transaction onto the bitcoin blockchain
32 33 34 |
# File 'lib/counterparty/connection.rb', line 32 def broadcast_tx(signed_tx) request 'broadcast_tx', signed_tx_hex: signed_tx end |
#request(method, params) ⇒ Object
Issue a request to the counterpartyd server for the given method, with the given params.
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/counterparty/connection.rb', line 38 def request(method, params) client = RestClient::Resource.new api_url, :timeout => @timeout response = JSON.parse client.post({ method: method, params: params, jsonrpc: '2.0', id: '0' }.to_json, user: @username, password: @password, accept: 'json', content_type: 'json' ) raise JsonResponseError.new response if response.has_key? 'code' raise ResponseError.new response['error'] if response.has_key? 'error' response['result'] end |
#sign_tx(raw_tx, private_key) ⇒ Object
Returns a signed raw transaction, given a private key
27 28 29 |
# File 'lib/counterparty/connection.rb', line 27 def sign_tx(raw_tx, private_key) request 'sign_tx', unsigned_tx_hex: raw_tx, privkey: private_key end |