# frozen_string_literal: true require 'addressable' require 'base64' require 'openssl' module Cryptum # This module is used to Interact with the APIs module API # Module specifically related to orders history retrieval. module Signature # Obtain latest order history public_class_method def self.generate(opts = {}) api_secret = opts[:api_secret] http_method = if opts[:http_method].nil? :GET else opts[:http_method].to_s.upcase.scrub.strip.chomp.to_sym end api_call = opts[:api_call].to_s.scrub.strip.chomp api_call = '/users/self/verify' if opts[:api_call].nil? if opts[:params].nil? path = api_call else uri = Addressable::URI.new uri.query_values = opts[:params] params = uri.query path = "#{api_call}?#{params}" end http_body = opts[:http_body].to_s.scrub.strip.chomp api_timestamp = Time.now.utc.to_i.to_s api_signature = Base64.strict_encode64( OpenSSL::HMAC.digest( 'sha256', Base64.strict_decode64(api_secret), "#{api_timestamp}#{http_method}#{path}#{http_body}" ) ) if http_body == '' api_signature = Base64.strict_encode64( OpenSSL::HMAC.digest( 'sha256', Base64.strict_decode64(api_secret), "#{api_timestamp}#{http_method}#{path}" ) ) end api_signature_response = {} api_signature_response[:api_timestamp] = api_timestamp api_signature_response[:api_signature] = api_signature api_signature_response rescue StandardError => e raise e end # Display Usage for this Module public_class_method def self.help puts "USAGE: signature = #{self}.generate_signature( api_secret: 'required - Coinbase Pro API Secret', http_method: 'optional - Defaults to :GET', api_call: 'optional - Defaults to /users/self/verify', params: 'optional - HTTP GET Parameters', http_body: 'optional HTTP POST Body' ) " end end end end