-
Notifications
You must be signed in to change notification settings - Fork 0
/
btcchina.rb
67 lines (48 loc) · 1.37 KB
/
btcchina.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require './log'
require 'json'
require 'rest-open-uri'
require 'digest/sha2'
require 'digest/hmac'
require 'debugger'
require "base64"
class Btcchina
def initialize key, secret
@key = key
@secret = secret
@trade_url = 'api.btcchina.com/api_trade_v1.php'
end
def get_account_info
request 'getAccountInfo', ""
end
def buy
end
def sell
end
def btc_price
end
private
def sign params
Digest::HMAC.hexdigest(params, @secret, Digest::SHA1)
end
def request method, params
debugger
tonce = millisecond
body = URI.encode_www_form tonce: tonce, accesskey: @key, requestmethod: "post", id: tonce, method: method.to_s, params: params.to_s
hash = sign body
auth = Base64.strict_encode64("#{@key}:#{hash}")
# url = "https://" + @key + ":" + hash + "@" + @trade_url
url = "https://api.btcchina.com/api_trade_v1.php"
open url, method: :post, 'Content-Type' => 'application/json-rpc',
'Authorization' => "Basic #{auth}",
'Json-Rpc-Tonce' => "#{tonce}"
#"Json-Rpc-Tonce" => tonce, "Authorization" => "Basic " + encode
#, http_basic_authentication: [@key, hash]
# rescue Exception => e
# puts e and Log.error e
end
def millisecond
((Time.now.to_f) * 1000000).to_i.to_s
end
end
site = Btcchina.new "035d1452-2ce3-48ea-8676-6971ac7d4dd1", "d8b408a9-8cad-4bcd-a928-131303c684a7"
site.get_account_info