2017-09-04 17:00:55 -07:00
|
|
|
import hashlib
|
|
|
|
import json
|
|
|
|
import random
|
|
|
|
import binascii
|
2017-08-02 18:06:09 -07:00
|
|
|
import os
|
2017-09-04 17:00:55 -07:00
|
|
|
import xcat.trades as trades
|
2017-08-02 18:06:09 -07:00
|
|
|
|
|
|
|
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
|
2017-05-26 09:23:49 -07:00
|
|
|
|
2017-09-04 17:00:55 -07:00
|
|
|
|
2017-07-26 08:06:39 -07:00
|
|
|
############################################
|
|
|
|
########### Data conversion utils ##########
|
|
|
|
############################################
|
2017-09-04 17:00:55 -07:00
|
|
|
|
|
|
|
|
2017-07-03 09:48:05 -07:00
|
|
|
def b(string):
|
|
|
|
"""Convert a string to bytes"""
|
|
|
|
return str.encode(string)
|
|
|
|
|
2017-09-04 17:00:55 -07:00
|
|
|
|
2017-07-03 09:48:05 -07:00
|
|
|
def x(h):
|
|
|
|
"""Convert a hex string to bytes"""
|
|
|
|
return binascii.unhexlify(h.encode('utf8'))
|
|
|
|
|
2017-09-04 17:00:55 -07:00
|
|
|
|
2017-07-03 09:48:05 -07:00
|
|
|
def b2x(b):
|
|
|
|
"""Convert bytes to a hex string"""
|
|
|
|
return binascii.hexlify(b).decode('utf8')
|
|
|
|
|
2017-09-04 17:00:55 -07:00
|
|
|
|
2017-07-03 09:48:05 -07:00
|
|
|
def x2s(hexstring):
|
|
|
|
"""Convert hex to a utf-8 string"""
|
2017-05-26 09:23:49 -07:00
|
|
|
return binascii.unhexlify(hexstring).decode('utf-8')
|
2017-05-22 18:00:34 -07:00
|
|
|
|
2017-09-04 17:00:55 -07:00
|
|
|
|
2017-07-03 09:48:05 -07:00
|
|
|
def s2x(string):
|
|
|
|
"""Convert a utf-8 string to hex"""
|
|
|
|
return b2x(b(string))
|
|
|
|
|
2017-09-04 17:00:55 -07:00
|
|
|
|
2017-07-12 20:33:56 -07:00
|
|
|
def hex2dict(hexstr):
|
|
|
|
jsonstr = x2s(hexstr)
|
2017-07-31 14:01:20 -07:00
|
|
|
print(hexstr['fund_tx'])
|
|
|
|
print(jsonstr)
|
2017-07-12 20:33:56 -07:00
|
|
|
return json.loads(jsonstr)
|
2017-07-03 09:48:05 -07:00
|
|
|
|
2017-09-04 17:00:55 -07:00
|
|
|
|
2017-07-26 08:06:39 -07:00
|
|
|
def jsonformat(trade):
|
|
|
|
return {
|
2017-09-04 17:00:55 -07:00
|
|
|
'sell': trade.sell.__dict__,
|
|
|
|
'buy': trade.buyContract.__dict__
|
2017-07-26 08:06:39 -07:00
|
|
|
}
|
2017-07-03 09:48:05 -07:00
|
|
|
|
2017-09-04 17:00:55 -07:00
|
|
|
|
2017-07-26 08:06:39 -07:00
|
|
|
############################################
|
|
|
|
########### Preimage utils #################
|
|
|
|
############################################
|
2017-07-03 09:48:05 -07:00
|
|
|
|
2017-09-04 17:00:55 -07:00
|
|
|
|
2017-05-26 09:23:49 -07:00
|
|
|
def generate_password():
|
2017-09-04 17:00:55 -07:00
|
|
|
s = ("1234567890abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
2017-08-02 18:06:09 -07:00
|
|
|
passlen = 32
|
2017-09-04 17:00:55 -07:00
|
|
|
p = "".join(random.sample(s, passlen))
|
2017-05-26 09:23:49 -07:00
|
|
|
return p
|
|
|
|
|
2017-09-04 17:00:55 -07:00
|
|
|
|
2017-08-02 18:06:09 -07:00
|
|
|
def sha256(secret):
|
|
|
|
preimage = secret.encode('utf8')
|
|
|
|
h = hashlib.sha256(preimage).digest()
|
|
|
|
return h
|
2017-07-26 08:06:39 -07:00
|
|
|
|
2017-09-04 17:00:55 -07:00
|
|
|
|
2017-08-02 19:36:07 -07:00
|
|
|
############################################
|
|
|
|
######## Error handling for CLI ############
|
|
|
|
############################################
|
|
|
|
|
2017-09-04 17:00:55 -07:00
|
|
|
|
2017-08-02 19:36:07 -07:00
|
|
|
def throw(err):
|
|
|
|
print(err)
|
|
|
|
exit()
|
|
|
|
|
2017-09-04 17:00:55 -07:00
|
|
|
|
2017-07-26 08:06:39 -07:00
|
|
|
#############################################
|
|
|
|
######### xcat.json temp file #############
|
|
|
|
#############################################
|
|
|
|
|
2017-09-04 17:00:55 -07:00
|
|
|
|
2017-08-02 22:34:46 -07:00
|
|
|
tmp_dir = os.path.join(ROOT_DIR, '.tmp')
|
|
|
|
if not os.path.exists(tmp_dir):
|
|
|
|
os.makedirs(tmp_dir)
|
|
|
|
xcatjson = os.path.join(tmp_dir, 'xcat.json')
|
2017-08-02 18:06:09 -07:00
|
|
|
|
2017-09-04 17:00:55 -07:00
|
|
|
|
2017-05-22 21:57:06 -07:00
|
|
|
def save_trade(trade):
|
2017-08-02 18:06:09 -07:00
|
|
|
with open(xcatjson, 'w+') as outfile:
|
2017-05-22 21:57:06 -07:00
|
|
|
json.dump(trade, outfile)
|
|
|
|
|
2017-09-04 17:00:55 -07:00
|
|
|
|
2017-05-22 21:57:06 -07:00
|
|
|
def get_trade():
|
2017-08-02 18:06:09 -07:00
|
|
|
with open(xcatjson) as data_file:
|
2017-07-31 14:01:20 -07:00
|
|
|
xcatdb = json.load(data_file)
|
|
|
|
sell = trades.Contract(xcatdb['sell'])
|
|
|
|
buy = trades.Contract(xcatdb['buy'])
|
|
|
|
trade = trades.Trade(sell, buy, commitment=xcatdb['commitment'])
|
|
|
|
return trade
|
2017-05-22 21:57:06 -07:00
|
|
|
|
2017-09-04 17:00:55 -07:00
|
|
|
|
2017-05-23 14:12:01 -07:00
|
|
|
def erase_trade():
|
2017-08-02 22:34:46 -07:00
|
|
|
try:
|
|
|
|
with open(xcatjson, 'w') as outfile:
|
|
|
|
outfile.write('')
|
|
|
|
except:
|
|
|
|
pass
|
2017-05-23 14:12:01 -07:00
|
|
|
|
2017-09-04 17:00:55 -07:00
|
|
|
|
2017-05-26 09:23:49 -07:00
|
|
|
def save(trade):
|
2017-08-25 12:12:29 -07:00
|
|
|
# print("Saving trade")
|
2017-05-26 09:23:49 -07:00
|
|
|
trade = {
|
2017-09-04 17:00:55 -07:00
|
|
|
'sell': trade.sell.__dict__,
|
|
|
|
'buy': trade.buy.__dict__,
|
|
|
|
'commitment': trade.commitment
|
2017-05-26 09:23:49 -07:00
|
|
|
}
|
|
|
|
save_trade(trade)
|
2017-08-02 18:45:36 -07:00
|
|
|
|
2017-09-04 17:00:55 -07:00
|
|
|
|
2017-08-02 18:45:36 -07:00
|
|
|
# Remove tmp files when trade is complete
|
|
|
|
def cleanup(tradeid):
|
|
|
|
try:
|
|
|
|
os.remove(os.path.join(ROOT_DIR, '.tmp/{0}'.format(tradeid)))
|
|
|
|
except:
|
|
|
|
pass
|