lint utils.py

This commit is contained in:
James Prestwich 2017-09-04 18:00:55 -06:00
parent 5198a2b765
commit 5fc8cf0d13
No known key found for this signature in database
GPG Key ID: 519E010A79028CCC
1 changed files with 34 additions and 9 deletions

View File

@ -1,80 +1,102 @@
import hashlib, json, random, binascii import hashlib
import xcat.trades as trades import json
import random
import binascii
import os import os
import xcat.trades as trades
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
############################################ ############################################
########### Data conversion utils ########## ########### Data conversion utils ##########
############################################ ############################################
def b(string): def b(string):
"""Convert a string to bytes""" """Convert a string to bytes"""
return str.encode(string) return str.encode(string)
def x(h): def x(h):
"""Convert a hex string to bytes""" """Convert a hex string to bytes"""
return binascii.unhexlify(h.encode('utf8')) return binascii.unhexlify(h.encode('utf8'))
def b2x(b): def b2x(b):
"""Convert bytes to a hex string""" """Convert bytes to a hex string"""
return binascii.hexlify(b).decode('utf8') return binascii.hexlify(b).decode('utf8')
def x2s(hexstring): def x2s(hexstring):
"""Convert hex to a utf-8 string""" """Convert hex to a utf-8 string"""
return binascii.unhexlify(hexstring).decode('utf-8') return binascii.unhexlify(hexstring).decode('utf-8')
def s2x(string): def s2x(string):
"""Convert a utf-8 string to hex""" """Convert a utf-8 string to hex"""
return b2x(b(string)) return b2x(b(string))
def hex2dict(hexstr): def hex2dict(hexstr):
jsonstr = x2s(hexstr) jsonstr = x2s(hexstr)
print(hexstr['fund_tx']) print(hexstr['fund_tx'])
print(jsonstr) print(jsonstr)
return json.loads(jsonstr) return json.loads(jsonstr)
def jsonformat(trade): def jsonformat(trade):
return { return {
'sell': trade.sell.__dict__, 'sell': trade.sell.__dict__,
'buy': trade.buyContract.__dict__ 'buy': trade.buyContract.__dict__
} }
############################################ ############################################
########### Preimage utils ################# ########### Preimage utils #################
############################################ ############################################
def generate_password(): def generate_password():
s = "1234567890abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" s = ("1234567890abcdefghijklmnopqrstuvwxyz"
"01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ")
passlen = 32 passlen = 32
p = "".join(random.sample(s,passlen)) p = "".join(random.sample(s, passlen))
return p return p
def sha256(secret): def sha256(secret):
preimage = secret.encode('utf8') preimage = secret.encode('utf8')
h = hashlib.sha256(preimage).digest() h = hashlib.sha256(preimage).digest()
return h return h
############################################ ############################################
######## Error handling for CLI ############ ######## Error handling for CLI ############
############################################ ############################################
def throw(err): def throw(err):
print(err) print(err)
exit() exit()
############################################# #############################################
######### xcat.json temp file ############# ######### xcat.json temp file #############
############################################# #############################################
tmp_dir = os.path.join(ROOT_DIR, '.tmp') tmp_dir = os.path.join(ROOT_DIR, '.tmp')
if not os.path.exists(tmp_dir): if not os.path.exists(tmp_dir):
os.makedirs(tmp_dir) os.makedirs(tmp_dir)
xcatjson = os.path.join(tmp_dir, 'xcat.json') xcatjson = os.path.join(tmp_dir, 'xcat.json')
def save_trade(trade): def save_trade(trade):
with open(xcatjson, 'w+') as outfile: with open(xcatjson, 'w+') as outfile:
json.dump(trade, outfile) json.dump(trade, outfile)
def get_trade(): def get_trade():
with open(xcatjson) as data_file: with open(xcatjson) as data_file:
xcatdb = json.load(data_file) xcatdb = json.load(data_file)
@ -83,6 +105,7 @@ def get_trade():
trade = trades.Trade(sell, buy, commitment=xcatdb['commitment']) trade = trades.Trade(sell, buy, commitment=xcatdb['commitment'])
return trade return trade
def erase_trade(): def erase_trade():
try: try:
with open(xcatjson, 'w') as outfile: with open(xcatjson, 'w') as outfile:
@ -90,15 +113,17 @@ def erase_trade():
except: except:
pass pass
def save(trade): def save(trade):
# print("Saving trade") # print("Saving trade")
trade = { trade = {
'sell': trade.sell.__dict__, 'sell': trade.sell.__dict__,
'buy': trade.buy.__dict__, 'buy': trade.buy.__dict__,
'commitment': trade.commitment 'commitment': trade.commitment
} }
save_trade(trade) save_trade(trade)
# Remove tmp files when trade is complete # Remove tmp files when trade is complete
def cleanup(tradeid): def cleanup(tradeid):
try: try: