Add updated utils
This commit is contained in:
parent
6861e73472
commit
2b7d00b73e
122
utils.py
122
utils.py
|
@ -1,8 +1,9 @@
|
|||
import hashlib
|
||||
import json
|
||||
import random
|
||||
import binascii
|
||||
import hashlib, json, random, binascii
|
||||
import trades
|
||||
|
||||
############################################
|
||||
########### Data conversion utils ##########
|
||||
############################################
|
||||
def b(string):
|
||||
"""Convert a string to bytes"""
|
||||
return str.encode(string)
|
||||
|
@ -29,8 +30,34 @@ def hex2dict(hexstr):
|
|||
print(jsonstr)
|
||||
return json.loads(jsonstr)
|
||||
|
||||
######################
|
||||
def jsonformat(trade):
|
||||
return {
|
||||
'sell': trade.sell.__dict__,
|
||||
'buy': trade.buyContract.__dict__
|
||||
}
|
||||
|
||||
############################################
|
||||
#### Role detection utils ####
|
||||
############################################
|
||||
def find_role(contract):
|
||||
# Obviously when regtest created both addrs on same machine, role is both.
|
||||
if parse_addrs(contract.initiator):
|
||||
return 'initiator'
|
||||
else:
|
||||
return 'fulfiller'
|
||||
|
||||
def parse_addrs(address):
|
||||
if address[:1] == 'm':
|
||||
status = bXcat.validateaddress(address)
|
||||
else:
|
||||
status = zXcat.validateaddress(address)
|
||||
status = status['ismine']
|
||||
print("Address {0} is mine: {1}".format(address, status))
|
||||
return status
|
||||
|
||||
############################################
|
||||
########### Preimage utils #################
|
||||
############################################
|
||||
|
||||
def sha256(secret):
|
||||
preimage = secret.encode('utf8')
|
||||
|
@ -43,23 +70,6 @@ def generate_password():
|
|||
p = "".join(random.sample(s,passlen))
|
||||
return p
|
||||
|
||||
# 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:
|
||||
try:
|
||||
xcatdb = json.load(data_file)
|
||||
return xcatdb
|
||||
except:
|
||||
return None
|
||||
|
||||
def erase_trade():
|
||||
with open('xcat.json', 'w') as outfile:
|
||||
outfile.write('')
|
||||
|
||||
# caching the secret locally for now...
|
||||
def get_secret():
|
||||
with open('secret.json') as data_file:
|
||||
|
@ -67,12 +77,32 @@ def get_secret():
|
|||
return line.strip('\n')
|
||||
|
||||
def save_secret(secret):
|
||||
with open('secret.json', 'w+') as outfile:
|
||||
outfile.write(secret)
|
||||
|
||||
#############################################
|
||||
######### xcat.json temp file #############
|
||||
#############################################
|
||||
|
||||
def save_trade(trade):
|
||||
print("Trade in save_trade", trade)
|
||||
with open('xcat.json', 'w+') as outfile:
|
||||
json.dump(trade, outfile)
|
||||
|
||||
def get_trade():
|
||||
try:
|
||||
with open('secret.json', 'w') as outfile:
|
||||
outfile.write(secret)
|
||||
except IOError:
|
||||
with open('secret.json', 'w+') as outfile:
|
||||
outfile.write(secret)
|
||||
with open('xcat.json') as data_file:
|
||||
xcatdb = json.load(data_file)
|
||||
sell = trades.Contract(xcatdb['sell'])
|
||||
buy = trades.Contract(xcatdb['buy'])
|
||||
trade = trades.Trade(sell, buy)
|
||||
return trade
|
||||
except:
|
||||
return None
|
||||
|
||||
def erase_trade():
|
||||
with open('xcat.json', 'w') as outfile:
|
||||
outfile.write('')
|
||||
|
||||
def save(trade):
|
||||
print("Saving trade")
|
||||
|
@ -81,3 +111,41 @@ def save(trade):
|
|||
'buy': trade.buy.__dict__
|
||||
}
|
||||
save_trade(trade)
|
||||
|
||||
|
||||
#############################################
|
||||
######### Ariel's changes ###############
|
||||
#############################################
|
||||
|
||||
|
||||
def save_seller_trade(trade):
|
||||
with open('sellertrade.json', 'w') as outfile:
|
||||
json.dump(jsonformat(trade), outfile)
|
||||
|
||||
def save_buyer_trade(trade):
|
||||
with open('buyertrade.json', 'w') as outfile:
|
||||
json.dump(jsonformat(trade), outfile)
|
||||
|
||||
def save_init(trade):
|
||||
with open('init.json', 'w') as outfile:
|
||||
json.dump(jsonformat(trade), outfile)
|
||||
|
||||
def get_seller_trade():
|
||||
data_file = open('init.json', 'w+')
|
||||
# try:
|
||||
xcatdb = json.load(data_file)
|
||||
sell = trades.Contract(xcatdb['sell'])
|
||||
buyContract = trades.Contract(xcatdb['buy'])
|
||||
trade = trades.Trade(sell,buyContract)
|
||||
|
||||
return trade
|
||||
|
||||
def get_buyer_trade():
|
||||
with open('buyertrade.json') as data_file:
|
||||
# try:
|
||||
xcatdb = json.load(data_file)
|
||||
sell = trades.Contract(xcatdb['sell'])
|
||||
buyContract = trades.Contract(xcatdb['buy'])
|
||||
trade = trades.Trade(sell,buyContract)
|
||||
|
||||
return trade
|
||||
|
|
Loading…
Reference in New Issue