-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMtGoxHMAC.py
318 lines (282 loc) · 11 KB
/
MtGoxHMAC.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
"""
MtGoxHMAC v0.01
Copyright 2011 Brian Monkaba
This file is part of ga-bitbot.
ga-bitbot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ga-bitbot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ga-bitbot. If not, see <http://www.gnu.org/licenses/>.
"""
from contextlib import closing
from Crypto.Cipher import AES
import getpass
import base64
import hmac
import hashlib
import time
import json
import urllib
import urllib2
import urlparse
class ServerError(Exception):
def __init__(self, ret):
self.ret = ret
def __str__(self):
return "Server error: %s" % self.ret
class UserError(Exception):
def __init__(self, errmsg):
self.errmsg = errmsg
def __str__(self):
return self.errmsg
class Client:
def __init__(self, enc_password=""):
if enc_password == "":
print "MtGoxHMAC: Enter your API key file encryption password."
enc_password = getpass.getpass()#raw_input()
try:
f = open('./config/salt.txt','r')
salt = f.read()
f.close()
hash_pass = hashlib.sha256(enc_password + salt).digest()
f = open('./config/api_key.txt')
ciphertext = f.read()
f.close()
decryptor = AES.new(hash_pass, AES.MODE_CBC,ciphertext[:AES.block_size])
plaintext = decryptor.decrypt(ciphertext[AES.block_size:])
d = json.loads(plaintext)
self.key = d['key']
self.secret = d['secret']
except:
print "\n\n\nError: you may have entered an invalid password or the encrypted api key file doesn't exist"
print "If you haven't yet generated the encrypted key file, run the encrypt_api_key.py script."
while 1:
pass
self.buff = ""
self.timeout = 15
self.__url_parts = urlparse.urlsplit("https://mtgox.com/api/0/")
self.__url_parts_1 = urlparse.urlsplit("https://mtgox.com/api/1/")
self.clock_window = time.time()
self.clock = time.time()
self.query_count = 0
self.query_limit_per_time_slice = 5
self.query_time_slice = 10
def throttle(self):
self.clock = time.time()
tdelta = self.clock - self.clock_window
if tdelta > self.query_time_slice:
self.query_count = 0
self.clock_window = time.time()
self.query_count += 1
if self.query_count > self.query_limit_per_time_slice:
#throttle the connection
#print "### Throttled ###"
time.sleep(self.query_time_slice - tdelta)
return
def perform(self, path, params,JSON=True,API_VERSION=0):
self.throttle()
nonce = str(int(time.time()*1000))
if params != None:
params = params.items()
else:
params = []
params += [(u'nonce',nonce)]
post_data = urllib.urlencode(params)
ahmac = base64.b64encode(str(hmac.new(base64.b64decode(self.secret),post_data,hashlib.sha512).digest()))
# Create header for auth-requiring operations
header = {
"User-Agent": 'ga-bitbot',
"Rest-Key": self.key,
"Rest-Sign": ahmac
}
if API_VERSION == 0:
url = urlparse.urlunsplit((
self.__url_parts.scheme,
self.__url_parts.netloc,
self.__url_parts.path + path,
self.__url_parts.query,
self.__url_parts.fragment
))
else: #assuming API_VERSION 1
url = urlparse.urlunsplit((
self.__url_parts_1.scheme,
self.__url_parts_1.netloc,
self.__url_parts_1.path + path,
self.__url_parts_1.query,
self.__url_parts_1.fragment
))
req = urllib2.Request(url, post_data, header)
with closing(urllib2.urlopen(req, post_data)) as res:
if JSON == True:
data = json.load(res)
else:
data = res.read()
if JSON == True:
if u"error" in data:
if data[u"error"] == u"Not logged in.":
raise UserError()
else:
raise ServerError(data[u"error"])
else:
return data
return data
def request(self, path, params,JSON=True,API_VERSION=0):
ret = self.perform(path, params,JSON,API_VERSION)
if JSON == True:
if "error" in ret:
raise UserError(ret["error"])
else:
return ret
return ret
#public api
def get_bid_history(self,OID):
params = {"type":'bid',"order":OID}
return self.request('generic/private/order/result',params,API_VERSION=1)
def get_ask_history(self,OID):
params = {"type":'ask',"order":OID}
return self.request('generic/private/order/result',params,API_VERSION=1)
def get_bid_tids(self,OID):
#used to link an OID from an API order to a list of TIDs reported in the account history log
try:
history = self.get_bid_history(OID)
except:
#OID not found, return an empty list
return []
else:
trade_ids = []
if history['result'] == 'success':
for trade in history['return']['trades']:
trade_ids.append(trade['trade_id'])
#return the list of trade ids
return trade_ids
else:
return []
def get_ask_tids(self,OID):
#used to link an OID from an API order to a list of TIDs reported in the account history log
try:
history = self.get_ask_history(OID)
except:
#OID not found, return an empty list
return []
else:
trade_ids = []
if history['result'] == 'success':
for trade in history['return']['trades']:
trade_ids.append(trade['trade_id'])
#return the list of trade ids
return trade_ids
else:
return []
def get_history_btc(self):
return self.request('history_BTC.csv',None,JSON=False)
def get_history_usd(self):
return self.request('history_USD.csv',None,JSON=False)
def get_info(self):
return self.request('info.php', None)
def get_ticker(self):
return self.request("ticker.php", None)["ticker"]
def get_depth(self):
return self.request("data/getDepth.php", {"Currency":"USD"})
def get_trades(self):
return self.request("data/getTrades.php", None)
def get_balance(self):
return self.request("getFunds.php", None)
def get_orders(self):
return self.request("getOrders.php", None)
def buy_btc(self, amount, price):
#new mtgox market orders begin in a pending state
#so we have to make a second delayed call to verify the order was actually accepted
#there is a risk here that the mtgox system will not be able to verify the order before
#the second call so it could reported as still pending.
#In this case, the host script will need to verify the order at a later time.
#to do: check how the system responds to instant orders and partialy filled orders.
if amount < 0.01:
print "minimun amount is 0.1btc"
return 0
params = {"amount":str(amount), "price":str(price)}
buy = self.request("buyBTC.php", params)
oid = buy['oid']
#check the response for the order
for order in buy['orders']:
if order['oid'] == oid:
return order
#if it wasn't reported yet...check again
time.sleep(1)
orders = self.get_orders()['orders']
for order in orders:
if order['oid'] == oid:
return order
else:
return None
def sell_btc(self, amount, price):
if amount < 0.01:
print "minimun amount is 0.1btc"
return 0
params = {"amount":str(amount), "price":str(price)}
sell = self.request("sellBTC.php", params)
oid = sell['oid']
#check the response for the order
for order in sell['orders']:
if order['oid'] == oid:
return order
#if it wasn't reported yet...check again
time.sleep(2)
orders = self.get_orders()['orders']
for order in orders:
if order['oid'] == oid:
return order
else:
return None
def cancel_buy_order(self, oid):
params = {"oid":str(oid), "type":str(2)}
return self.request("cancelOrder.php", params)
def cancel_sell_order(self, oid):
params = {"oid":str(oid), "type":str(1)}
return self.request("cancelOrder.php", params)
if __name__ == "__main__":
def ppdict(d):
#pretty print a dict
print "-"*40
try:
for key in d.keys():
print key,':',d[key]
except:
print d
return d
def pwdict(d,filename):
#pretty write a dict
f = open(filename,'w')
try:
for key in d.keys():
f.write(key + " : " + str(d[key]) + "\n")
except:
pass
f.write('\n' + '-'*80 + '\n')
f.write(str(d))
f.close()
return d
print "\nMTGoxHMAC module test"
print "**warning: running this script will initiate then cancel an order on the MtGox exchange.**"
print "\ndownloaded examples of the MtGox json format will be stored in the test_data folder."
c = Client()
"""
b = ppdict(pwdict(c.buy_btc(1.5,0.25),'./test_data/mg_buy.txt'))
s = ppdict(pwdict(c.sell_btc(1.0,100.00),'./test_data/mg_sell.txt'))
ppdict(pwdict(c.get_info(),'./test_data/mg_info.txt'))
ppdict(pwdict(c.get_ticker(),'./test_data/mg_ticker.txt'))
ppdict(pwdict(c.get_depth(),'./test_data/mg_depth.txt'))
ppdict(pwdict(c.get_balance(),'./test_data/mg_balance.txt'))
ppdict(pwdict(c.get_orders(),'./test_data/mg_orders.txt'))
ppdict(pwdict(c.cancel_buy_order(b['oid']),'./test_data/mg_cancel_buy.txt'))
ppdict(pwdict(c.cancel_sell_order(s['oid']),'./test_data/mg_cancel_sell.txt'))
ppdict(pwdict(c.get_history_btc(),'./test_data/mg_history_btc.txt'))
ppdict(pwdict(c.get_history_usd(),'./test_data/mg_history_usd.txt'))
ppdict(pwdict(c.get_bid_history(b['oid']),'./test_data/mg_bid_history.txt'))
ppdict(pwdict(c.get_ask_history(s['oid']),'./test_data/mg_ask_history.txt'))
print "done."
"""