Add updated utils
This commit is contained in:
parent
6861e73472
commit
2b7d00b73e
120
utils.py
120
utils.py
|
@ -1,8 +1,9 @@
|
||||||
import hashlib
|
import hashlib, json, random, binascii
|
||||||
import json
|
import trades
|
||||||
import random
|
|
||||||
import binascii
|
|
||||||
|
|
||||||
|
############################################
|
||||||
|
########### 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)
|
||||||
|
@ -29,8 +30,34 @@ def hex2dict(hexstr):
|
||||||
print(jsonstr)
|
print(jsonstr)
|
||||||
return json.loads(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):
|
def sha256(secret):
|
||||||
preimage = secret.encode('utf8')
|
preimage = secret.encode('utf8')
|
||||||
|
@ -43,23 +70,6 @@ def generate_password():
|
||||||
p = "".join(random.sample(s,passlen))
|
p = "".join(random.sample(s,passlen))
|
||||||
return p
|
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...
|
# caching the secret locally for now...
|
||||||
def get_secret():
|
def get_secret():
|
||||||
with open('secret.json') as data_file:
|
with open('secret.json') as data_file:
|
||||||
|
@ -67,13 +77,33 @@ def get_secret():
|
||||||
return line.strip('\n')
|
return line.strip('\n')
|
||||||
|
|
||||||
def save_secret(secret):
|
def save_secret(secret):
|
||||||
try:
|
|
||||||
with open('secret.json', 'w') as outfile:
|
|
||||||
outfile.write(secret)
|
|
||||||
except IOError:
|
|
||||||
with open('secret.json', 'w+') as outfile:
|
with open('secret.json', 'w+') as outfile:
|
||||||
outfile.write(secret)
|
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('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):
|
def save(trade):
|
||||||
print("Saving trade")
|
print("Saving trade")
|
||||||
trade = {
|
trade = {
|
||||||
|
@ -81,3 +111,41 @@ def save(trade):
|
||||||
'buy': trade.buy.__dict__
|
'buy': trade.buy.__dict__
|
||||||
}
|
}
|
||||||
save_trade(trade)
|
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