2017-07-31 14:01:20 -07:00
|
|
|
import hashlib, json, random, binascii
|
2017-07-28 19:44:24 -07:00
|
|
|
import xcat.trades as trades
|
2017-07-31 16:25:49 -07:00
|
|
|
import xcat.bitcoinRPC as bitcoinRPC
|
|
|
|
import xcat.zcashRPC as zcashRPC
|
2017-05-26 09:23:49 -07:00
|
|
|
|
2017-07-26 08:06:39 -07:00
|
|
|
############################################
|
|
|
|
########### Data conversion utils ##########
|
|
|
|
############################################
|
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-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-07-26 08:06:39 -07:00
|
|
|
def jsonformat(trade):
|
|
|
|
return {
|
|
|
|
'sell': trade.sell.__dict__,
|
|
|
|
'buy': trade.buyContract.__dict__
|
|
|
|
}
|
2017-07-03 09:48:05 -07:00
|
|
|
|
2017-07-26 08:06:39 -07:00
|
|
|
############################################
|
|
|
|
#### Role detection utils ####
|
|
|
|
############################################
|
|
|
|
def find_role(contract):
|
|
|
|
# Obviously when regtest created both addrs on same machine, role is both.
|
2017-07-31 16:25:49 -07:00
|
|
|
if is_myaddr(contract.initiator) and is_myaddr(contract.fulfiller):
|
|
|
|
return 'test'
|
|
|
|
elif is_myaddr(contract.initiator):
|
2017-07-26 08:06:39 -07:00
|
|
|
return 'initiator'
|
|
|
|
else:
|
|
|
|
return 'fulfiller'
|
|
|
|
|
2017-07-31 16:25:49 -07:00
|
|
|
def is_myaddr(address):
|
2017-07-26 08:06:39 -07:00
|
|
|
if address[:1] == 'm':
|
2017-07-31 16:25:49 -07:00
|
|
|
status = bitcoinRPC.validateaddress(address)
|
2017-07-26 08:06:39 -07:00
|
|
|
else:
|
2017-07-31 16:25:49 -07:00
|
|
|
status = zcashRPC.validateaddress(address)
|
2017-07-26 08:06:39 -07:00
|
|
|
status = status['ismine']
|
|
|
|
print("Address {0} is mine: {1}".format(address, status))
|
|
|
|
return status
|
|
|
|
|
|
|
|
############################################
|
|
|
|
########### Preimage utils #################
|
|
|
|
############################################
|
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-07-26 08:06:39 -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):
|
|
|
|
with open('secret.json', 'w+') as outfile:
|
|
|
|
outfile.write(secret)
|
|
|
|
|
|
|
|
#############################################
|
|
|
|
######### xcat.json temp file #############
|
|
|
|
#############################################
|
|
|
|
|
2017-05-22 21:57:06 -07:00
|
|
|
def save_trade(trade):
|
2017-07-26 08:06:39 -07:00
|
|
|
print("Trade in save_trade", trade)
|
|
|
|
with open('xcat.json', 'w+') as outfile:
|
2017-05-22 21:57:06 -07:00
|
|
|
json.dump(trade, outfile)
|
|
|
|
|
|
|
|
def get_trade():
|
2017-07-31 14:01:20 -07:00
|
|
|
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, commitment=xcatdb['commitment'])
|
|
|
|
return trade
|
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
|
|
|
def save(trade):
|
|
|
|
print("Saving trade")
|
|
|
|
trade = {
|
2017-07-24 13:23:40 -07:00
|
|
|
'sell': trade.sell.__dict__,
|
2017-07-28 13:57:44 -07:00
|
|
|
'buy': trade.buy.__dict__,
|
|
|
|
'commitment': trade.commitment
|
2017-05-26 09:23:49 -07:00
|
|
|
}
|
|
|
|
save_trade(trade)
|