hexsafe 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/hexsafe/api.rb +108 -110
  3. metadata +2 -16
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 67c619cf134e56baf443d7ce5589e9b3aea49bd6b99761245a7935cd60ff4a41
4
- data.tar.gz: 7046c524cb47ee23551bd5f99f0401963ed49d0de0b6776fe7be4122d3d94863
3
+ metadata.gz: 3ec8ce1e707e3567efe4143851d16fb4493633a0a797eed4141f30ff0b4dbb75
4
+ data.tar.gz: 5aab011d9c06c0c0cea2c8e834c5f8e2f3add402e0e8ae6e78b66d807992da1e
5
5
  SHA512:
6
- metadata.gz: e382ee7a548b1f77ae48d1ad229815cf7a88e79c22bcd82a5a9b97619edd3a48962d6db81f5470dfdd6494822668c794a0d2fc88b69385e4909ab40cf2ef6bdf
7
- data.tar.gz: 509c7baaf43e422689b4b5d8255f519dbeb3efcc32ffe30f10c28360f60edcc98ee7d687125502a89bb9fea9fe3f27154d3977ecefd8ccebf214c251a048ea26
6
+ metadata.gz: cf1cb87f60b179ad143c68fd0b36662854baffcb73dfe403ecc5e335c4d8b85a082f336b8d40cc5f0b8a376739fc493ee0b15e9af76c6e84cd6f7d93f7017c55
7
+ data.tar.gz: f48ec28c83e7e62d5630927e11388ee6ac52147963f6c53a5386f06bf1cc1bb849f819eff13d8303770b0fe113863e6c74528ce8a693202523d32489d2b55b89
data/lib/hexsafe/api.rb CHANGED
@@ -1,116 +1,112 @@
1
1
  module Hexsafe
2
2
  class Api
3
-
4
- class Error < RuntimeError; end
5
-
6
- def initialize options = {}
7
- @mode = options[:env].upcase rescue nil
8
- @key = options[:key] rescue nil
9
- @secret = options[:secret] rescue nil
10
- end
11
-
12
- def get_accounts
13
- response = rest_api("GET", "account", {})
14
- end
15
-
16
- def get_account account_id
17
- response = rest_api("GET", "account/#{account_id}", {})
18
- end
19
-
20
- def get_balance account_id
21
- response = rest_api("GET", "balance/#{account_id}", {})
22
- end
23
-
24
- def get_txn ticker, tx_hash
25
- response = rest_api("GET", "transaction/asset_ticker/#{ticker}/hash/#{tx_hash}", {})
26
- end
27
-
28
- def get_txn_ac account_id, start_time, end_time
29
- response = rest_api("GET", "transaction/account_id/#{account_id}/start_time/#{start_time}/end_time/#{end_time}", {})
30
- end
31
-
32
- def create_deposit body
33
- response = rest_api("post", "deposit", body)
34
- end
35
-
36
- def cancel_deposit request_id
37
- response = rest_api("DELETE", "deposit/#{request_id}", {})
38
- end
39
-
40
- def get_deposit request_id
41
- response = rest_api("GET", "deposit/#{request_id}", {})
42
- end
43
-
44
- def create_withdraw body
45
- response = rest_api("post", "withdraw", body)
46
- end
47
-
48
- def cancel_withdraw request_id
49
- response = rest_api("DELETE", "withdraw/#{request_id}", {})
50
- end
51
-
52
- def get_withdraw request_id
53
- response = rest_api("GET", "withdraw/#{request_id}", {})
54
- end
55
-
56
- def subscribe_webhook
57
- response = rest_api("post", "webhook", body)
58
- end
59
-
60
- def get_subscriptions account_id
61
- response = rest_api("GET", "webhook/#{account_id}", {})
62
- end
63
-
64
- def delete_subscription uuid
65
- response = rest_api("DELETE", "webhook/#{uuid}", {})
66
- end
67
-
68
- protected
69
-
70
- def rest_api(verb, path, body = nil)
71
- verb = verb.upcase
72
- body_digest = nil
73
- puts "\n\n\n#{new_timestamp}\n\n"
74
- timestamp = new_timestamp.to_s + "0000"
75
- request_line = verb + ' ' + base_url+path + ' HTTP/1.1'
76
- if body
77
- if non_get_verb? verb
78
- body_digest = Base64.strict_encode64(digest(JSON.generate(body)))
79
- end
3
+ class Error < RuntimeError; end
4
+
5
+ def initialize options = {}
6
+ @mode = options[:env].upcase rescue nil
7
+ @key = options[:key] rescue nil
8
+ @secret = options[:secret] rescue nil
9
+ end
10
+
11
+ def get_accounts
12
+ response = rest_api("GET", "account", {})
13
+ end
14
+
15
+ def get_account account_id
16
+ response = rest_api("GET", "account/#{account_id}", {})
17
+ end
18
+
19
+ def get_balance account_id
20
+ response = rest_api("GET", "balance/#{account_id}", {})
21
+ end
22
+
23
+ def get_txn ticker, tx_hash
24
+ response = rest_api("GET", "transaction/asset_ticker/#{ticker.upcase}/hash/#{tx_hash}", {})
25
+ end
26
+
27
+ def get_txn_ac account_id, start_time, end_time
28
+ response = rest_api("GET", "transaction/account_id/#{account_id}/start_time/#{start_time}/end_time/#{end_time}", {})
29
+ end
30
+
31
+ def create_deposit body
32
+ response = rest_api("post", "deposit", body)
33
+ end
34
+
35
+ def cancel_deposit request_id
36
+ response = rest_api("DELETE", "deposit/#{request_id}", {})
37
+ end
38
+
39
+ def get_deposit request_id
40
+ response = rest_api("GET", "deposit/#{request_id}", {})
80
41
  end
81
- signature = encode_hmac_512(timestamp, host, request_line, body_digest)
82
- url = host+base_url+path
83
-
84
- if ['POST', 'PUT', 'PATCH', 'DELETE'].include?(verb)
85
- authorization = "hmac username=\"" + key + "\", algorithm=\"hmac-sha512\", headers=\"nonce host digest request-line\", signature=\"" + signature + "\""
86
- else
87
- authorization = "hmac username=\"" + key + "\", algorithm=\"hmac-sha512\", headers=\"nonce host request-line\", signature=\"" + signature + "\"";
88
- end
89
- if is_get? verb
90
- response = Faraday.get(url) do |req|
91
- req.headers['nonce'] = timestamp.to_s
92
- req.headers['authorization'] = authorization
93
- req.headers['Date'] = http_format_date
94
- req.headers['Content-Type'] = 'application/json'
95
- req.headers['Accept'] = 'application/json'
42
+
43
+ def create_withdraw body
44
+ response = rest_api("post", "withdraw", body)
45
+ end
46
+
47
+ def cancel_withdraw request_id
48
+ response = rest_api("DELETE", "withdraw/#{request_id}", {})
49
+ end
50
+
51
+ def get_withdraw request_id
52
+ response = rest_api("GET", "withdraw/#{request_id}", {})
53
+ end
54
+
55
+ def subscribe_webhook
56
+ response = rest_api("post", "webhook", body)
57
+ end
58
+
59
+ def get_subscriptions account_id
60
+ response = rest_api("GET", "webhook/#{account_id}", {})
61
+ end
62
+
63
+ def delete_subscription uuid
64
+ response = rest_api("DELETE", "webhook/#{uuid}", {})
65
+ end
66
+
67
+ protected
68
+
69
+ def rest_api(verb, path, body = nil)
70
+ verb = verb.upcase
71
+ body_digest = nil
72
+ timestamp = new_timestamp.to_s + "0000"
73
+ request_line = verb + ' ' + base_url+path + ' HTTP/1.1'
74
+ if body
75
+ if non_get_verb? verb
76
+ body_digest = Base64.strict_encode64(digest(JSON.generate(body)))
77
+ end
78
+ end
79
+ signature = encode_hmac_512(timestamp, host, request_line, body_digest)
80
+ url = host+base_url+path
81
+
82
+ if ['POST', 'PUT', 'PATCH', 'DELETE'].include?(verb)
83
+ authorization = "hmac username=\"" + key + "\", algorithm=\"hmac-sha512\", headers=\"nonce host digest request-line\", signature=\"" + signature + "\""
84
+ else
85
+ authorization = "hmac username=\"" + key + "\", algorithm=\"hmac-sha512\", headers=\"nonce host request-line\", signature=\"" + signature + "\"";
96
86
  end
97
- elsif non_get_verb? verb
98
- response = Faraday.post(url) do |req|
99
- req.body = body.compact.to_json
100
- req.headers['nonce'] = timestamp.to_s
101
- req.headers['digest'] = "SHA-256="+body_digest
102
- req.headers['authorization'] = authorization
103
- req.headers['Date'] = http_format_date
104
- req.headers['Content-Type'] = 'application/json'
87
+ if is_get? verb
88
+ response = Faraday.get(url) do |req|
89
+ req.headers['nonce'] = timestamp.to_s
90
+ req.headers['authorization'] = authorization
91
+ req.headers['Date'] = http_format_date
92
+ req.headers['Content-Type'] = 'application/json'
93
+ req.headers['Accept'] = 'application/json'
94
+ end
95
+ elsif non_get_verb? verb
96
+ response = Faraday.post(url) do |req|
97
+ req.body = body.compact.to_json
98
+ req.headers['nonce'] = timestamp.to_s
99
+ req.headers['digest'] = "SHA-256="+body_digest
100
+ req.headers['authorization'] = authorization
101
+ req.headers['Date'] = http_format_date
102
+ req.headers['Content-Type'] = 'application/json'
103
+ end
105
104
  end
106
- end
107
- # Rails.logger { response.describe }
108
- # response.assert_success!
109
- JSON.parse(response.body)
110
- end
111
-
112
- private
113
-
105
+ JSON.parse(response.body)
106
+ end
107
+
108
+ private
109
+
114
110
  def base_url
115
111
  return "/hexsafe/api/v4/"
116
112
  end
@@ -121,10 +117,12 @@ module Hexsafe
121
117
  end
122
118
 
123
119
  def key
120
+ return "Empty key" if @key.nil?
124
121
  @key ||= @key
125
122
  end
126
123
 
127
124
  def secret
125
+ return "Empty secret" if @secret.nil?
128
126
  @secret ||= @secret
129
127
  end
130
128
 
@@ -133,7 +131,7 @@ module Hexsafe
133
131
  sha256.digest(data.encode('utf-8'))
134
132
  end
135
133
 
136
- def hmac_512 (secret, data)
134
+ def hmac_512(secret, data)
137
135
  OpenSSL::HMAC.digest('sha512',secret, data)
138
136
  end
139
137
 
@@ -163,5 +161,5 @@ module Hexsafe
163
161
  def non_get_verb? verb
164
162
  return ['POST', 'PUT', 'PATCH', 'DELETE'].include?(verb)
165
163
  end
166
- end
164
+ end
167
165
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hexsafe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - imrahulprajapat
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-05 00:00:00.000000000 Z
11
+ date: 2021-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -94,20 +94,6 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '1.6'
97
- - !ruby/object:Gem::Dependency
98
- name: pry
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
97
  - !ruby/object:Gem::Dependency
112
98
  name: rake
113
99
  requirement: !ruby/object:Gem::Requirement