Class: Counterparty::Connection

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port = 4000, username = 'counterparty', password = '1234', host = 'xcp-dev.vennd.io') ⇒ Connection

Returns a new instance of Connection.



16
17
18
19
# File 'lib/counterparty/connection.rb', line 16

def initialize(port=4000, username='counterparty', password='1234', host='xcp-dev.vennd.io')
  @host,@port,@username,@password=host.to_s,port.to_i,username.to_s,password.to_s
  @timeout = DEFAULT_TIMEOUT
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



10
11
12
# File 'lib/counterparty/connection.rb', line 10

def host
  @host
end

#passwordObject

Returns the value of attribute password.



10
11
12
# File 'lib/counterparty/connection.rb', line 10

def password
  @password
end

#portObject

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

#usernameObject

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



49
50
51
52
# File 'lib/counterparty/connection.rb', line 49

def self.resource_request(klass) # :nodoc:
  define_method(klass.to_get_request){ |params| klass.find params }
  define_method(klass.to_create_request){ |params| klass.create(params).to_raw_tx }
end

Instance Method Details

#api_urlObject

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

#request(method, params) ⇒ Object

Issue a request to the counterpartyd server for the given method, with the given params.



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/counterparty/connection.rb', line 33

def request(method, params)
  client = RestClient::Resource.new api_url, :timeout => @timeout
  request = { method: method, params: params, jsonrpc: '2.0', id: '0' }.to_json
  response = JSON.parse client.post(request,
    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