2017-05-22 18:00:34 -07:00
|
|
|
import hashlib
|
2017-05-23 11:22:21 -07:00
|
|
|
import json
|
2017-05-26 09:23:49 -07:00
|
|
|
import random
|
|
|
|
import binascii
|
|
|
|
|
2017-07-03 09:48:05 -07:00
|
|
|
def b(string):
|
|
|
|
"""Convert a string to bytes"""
|
|
|
|
return str.encode(string)
|
|
|
|
|
|
|
|
def x(h):
|
|
|
|
"""Convert a hex string to bytes"""
|
|
|
|
return binascii.unhexlify(h.encode('utf8'))
|
|
|
|
|
|
|
|
def b2x(b):
|
|
|
|
"""Convert bytes to a hex string"""
|
|
|
|
return binascii.hexlify(b).decode('utf8')
|
|
|
|
|
|
|
|
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-07-03 09:48:05 -07:00
|
|
|
def s2x(string):
|
|
|
|
"""Convert a utf-8 string to hex"""
|
|
|
|
return b2x(b(string))
|
|
|
|
|
2017-07-12 20:33:56 -07:00
|
|
|
def hex2dict(hexstr):
|
|
|
|
jsonstr = x2s(hexstr)
|
2017-07-24 13:23:40 -07:00
|
|
|
print(hexstr['fund_tx'])
|
2017-07-12 20:33:56 -07:00
|
|
|
print(jsonstr)
|
|
|
|
return json.loads(jsonstr)
|
2017-07-03 09:48:05 -07:00
|
|
|
|
|
|
|
######################
|
|
|
|
|
|
|
|
|
2017-05-22 18:00:34 -07:00
|
|
|
def sha256(secret):
|
|
|
|
preimage = secret.encode('utf8')
|
|
|
|
h = hashlib.sha256(preimage).digest()
|
|
|
|
return h
|
2017-05-22 21:57:06 -07:00
|
|
|
|
2017-05-26 09:23:49 -07:00
|
|
|
def generate_password():
|
|
|
|
s = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
passlen = 8
|
|
|
|
p = "".join(random.sample(s,passlen))
|
|
|
|
return p
|
|
|
|
|
2017-05-22 21:57:06 -07:00
|
|
|
# TODO: Port these over to leveldb or some other database
|
|
|
|
def save_trade(trade):
|
|
|
|
with open('xcat.json', 'w') as outfile:
|
|
|
|
json.dump(trade, outfile)
|
|
|
|
|
|
|
|
def get_trade():
|
|
|
|
with open('xcat.json') as data_file:
|
2017-05-23 13:34:48 -07:00
|
|
|
try:
|
|
|
|
xcatdb = json.load(data_file)
|
|
|
|
return xcatdb
|
|
|
|
except:
|
|
|
|
return None
|
2017-05-22 21:57:06 -07:00
|
|
|
|
2017-05-23 14:12:01 -07:00
|
|
|
def erase_trade():
|
|
|
|
with open('xcat.json', 'w') as outfile:
|
|
|
|
outfile.write('')
|
|
|
|
|
2017-05-26 09:23:49 -07:00
|
|
|
# caching the secret locally for now...
|
|
|
|
def get_secret():
|
|
|
|
with open('secret.json') as data_file:
|
|
|
|
for line in data_file:
|
|
|
|
return line.strip('\n')
|
|
|
|
|
|
|
|
def save_secret(secret):
|
2017-07-24 07:41:03 -07:00
|
|
|
try:
|
|
|
|
with open('secret.json', 'w') as outfile:
|
|
|
|
outfile.write(secret)
|
|
|
|
except IOError:
|
|
|
|
with open('secret.json', 'w+') as outfile:
|
|
|
|
outfile.write(secret)
|
2017-05-26 09:23:49 -07:00
|
|
|
|
|
|
|
def save(trade):
|
|
|
|
print("Saving trade")
|
|
|
|
trade = {
|
2017-07-24 13:23:40 -07:00
|
|
|
'sell': trade.sell.__dict__,
|
|
|
|
'buy': trade.buy.__dict__
|
2017-05-26 09:23:49 -07:00
|
|
|
}
|
|
|
|
save_trade(trade)
|