class Cryptsy::WebClient

Unsafe client that allows you to do things that the Cryptsy API does not permit, including withdrawals to untrusted addresses, as well as pre-approving addresses for withdrawals

Constants

DEFAULT_OPTIONS

Attributes

Public Class Methods

new(username, password, tfa_secret, options = {}) click to toggle source

@param [String] username @param [String] password @param [String] tfa_secret @param [Hash] options

# File lib/cryptsy/web_client.rb, line 21
def initialize(username, password, tfa_secret, options = {})
  @username = username
  @password = password
  @tfa = ROTP::TOTP.new(tfa_secret)

  @cookie_jar = HTTP::CookieJar.new

  @connection = Faraday.new(DEFAULT_OPTIONS.merge(options)) do |builder|
    builder.use :cookie_jar, jar: @cookie_jar
    builder.request :url_encoded
    builder.adapter Faraday.default_adapter
  end
end

Public Instance Methods

add_trusted_address(currency_id, address) click to toggle source

Submits a trusted address for pre-approved withdrawals

@param [Integer] currency_id @param [String] address @return [Faraday::Response]

# File lib/cryptsy/web_client.rb, line 61
def add_trusted_address(currency_id, address)
  request = {
    'data[Withdrawal][currency_id]' => currency_id,
    'data[Withdrawal][address]' => address,
    'data[Withdrawal][existing_password]' => @password,
    'data[Withdrawal][pincode]' => @tfa.now,
  }

  post_with_token('/users/addtrustedaddress', request)
end
get(url) click to toggle source

@param [String] url @return [Faraday::Response]

# File lib/cryptsy/web_client.rb, line 95
def get(url)
  @connection.get(url)
end
login() click to toggle source

Performs login operation using the configured username and password @return [Faraday::Response]

# File lib/cryptsy/web_client.rb, line 37
def login
  request = {
    'data[User][username]' => @username,
    'data[User][password]' => @password,
  }

  post_with_token('/users/login', request)
end
make_withdrawal(currency_id, address, amount) click to toggle source

Submits a request for withdrawal to an untrusted address

Note that this will only work for addresses that have not been trusted. Use the JSON API for withdrawing to trusted addresses.

@param [Integer] currency_id @param [String] address @param [Float] amount @return [Faraday::Response]

# File lib/cryptsy/web_client.rb, line 81
def make_withdrawal(currency_id, address, amount)
  request = {
    'data[Withdrawal][fee]' => '1.00000000',
    'data[Withdrawal][wdamount]' => amount,
    'data[Withdrawal][address]' => address,
    'data[Withdrawal][existing_password]' => @password,
    'data[Withdrawal][pincode]' => @tfa.now,
  }

  post_with_token("/users/makewithdrawal/#{currency_id}", request)
end
pincode() click to toggle source

Finishes login operation using TOTP and TFA secret @return [Faraday::Response]

# File lib/cryptsy/web_client.rb, line 48
def pincode
  request = {
    'data[User][pincode]' => @tfa.now
  }

  post_with_token('/users/pincode', request)
end
post(url, body) click to toggle source

@param [String] url @param [Hash] body @return [Faraday::Response]

# File lib/cryptsy/web_client.rb, line 102
def post(url, body)
  @connection.post(url, body)
end
post_with_token(url, request) click to toggle source

Performs an initial GET request to the given URL to obtain any CSRF tokens, injects them into the given request, then performs a POST request to the given URL

@param [String] url @param [Hash] request @return [Faraday::Request]

# File lib/cryptsy/web_client.rb, line 112
def post_with_token(url, request)
  prepare_request(get(url), request)
  post(url, request)
end

Private Instance Methods

prepare_request(initial_response, new_request) click to toggle source

@param [Faraday::Response] initial_response @param [Hash] new_request @return [void]

# File lib/cryptsy/web_client.rb, line 122
def prepare_request(initial_response, new_request)
  doc = Nokogiri::HTML(initial_response.body)

  # Inject CSRF token into new request
  doc.xpath('//input').each do |input|
    if input[:name] =~ /_Token/
      new_request[input[:name]] = input[:value]
    end
  end

  # Set the request method
  new_request['_method'] = 'POST'
end