Export xcat.json to hex string on cli
This commit is contained in:
parent
d9ee4d5b99
commit
ee49e77279
32
cli.py
32
cli.py
|
@ -4,6 +4,7 @@ import database as db
|
||||||
import bXcat, zXcat
|
import bXcat, zXcat
|
||||||
from trades import *
|
from trades import *
|
||||||
from xcat import *
|
from xcat import *
|
||||||
|
import ast
|
||||||
|
|
||||||
def find_role(contract):
|
def find_role(contract):
|
||||||
# Obviously when regtest created both addrs on same machine, role is both.
|
# Obviously when regtest created both addrs on same machine, role is both.
|
||||||
|
@ -35,6 +36,9 @@ def checkBuyActions(trade):
|
||||||
buyer_redeem(trade)
|
buyer_redeem(trade)
|
||||||
print("XCAT trade complete!")
|
print("XCAT trade complete!")
|
||||||
|
|
||||||
|
def instantiateTrade(trade):
|
||||||
|
return Trade(buyContract=Contract(trade['buy']), sellContract=Contract(trade['sell']))
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
|
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
|
||||||
description=textwrap.dedent('''\
|
description=textwrap.dedent('''\
|
||||||
|
@ -42,10 +46,11 @@ if __name__ == '__main__':
|
||||||
newtrade - create a new trade
|
newtrade - create a new trade
|
||||||
checktrades - check for actions to be taken on existing trades
|
checktrades - check for actions to be taken on existing trades
|
||||||
importcontract "hexstr" - import an existing trade from a hex string
|
importcontract "hexstr" - import an existing trade from a hex string
|
||||||
exportcontract - (not implemented) export the data of an existing xcat trade as a hex string
|
exportcontract - export the data of an existing xcat trade as a hex string
|
||||||
|
|
||||||
'''))
|
'''))
|
||||||
parser.add_argument("command", action="store", help="list commands")
|
parser.add_argument("command", action="store", help="list commands")
|
||||||
|
parser.add_argument("argument", action="store", nargs="*", help="add an argument")
|
||||||
# parser.add_argument("-importcontract", type=str, action="store", help="import an existing trade from a hex string.")
|
# parser.add_argument("-importcontract", type=str, action="store", help="import an existing trade from a hex string.")
|
||||||
# parser.add_argument("-newtrade", action="store", help="create a new trade.")
|
# parser.add_argument("-newtrade", action="store", help="create a new trade.")
|
||||||
# parser.add_argument("-checktrades", action="store", help="check status of existing trades")
|
# parser.add_argument("-checktrades", action="store", help="check status of existing trades")
|
||||||
|
@ -54,21 +59,26 @@ if __name__ == '__main__':
|
||||||
# how to hold state of role
|
# how to hold state of role
|
||||||
command = args.command
|
command = args.command
|
||||||
if command == 'importcontract':
|
if command == 'importcontract':
|
||||||
erase_trade()
|
hexstr = args.argument[0]
|
||||||
role = 'seller'
|
trade = x2s(hexstr)
|
||||||
htlcTrade = Trade()
|
trade = instantiateTrade(ast.literal_eval(trade))
|
||||||
print("Creating new XCAT transaction...")
|
print(trade)
|
||||||
|
elif command == 'exportcontract':
|
||||||
|
trade = get_trade()
|
||||||
|
hexstr = s2x(str(trade))
|
||||||
|
print(trade)
|
||||||
|
print(hexstr)
|
||||||
elif command == 'checktrades':
|
elif command == 'checktrades':
|
||||||
trade = get_trade()
|
trade = get_trade()
|
||||||
buyContract = Contract(trade['buy'])
|
trade = instantiateTrade(trade)
|
||||||
sellContract = Contract(trade['sell'])
|
if find_role(trade.sellContract) == 'initiator':
|
||||||
trade = Trade(buyContract=buyContract, sellContract=sellContract)
|
|
||||||
if find_role(sellContract) == 'initiator':
|
|
||||||
role = 'seller'
|
role = 'seller'
|
||||||
checkSellActions(trade)
|
checkSellActions(trade)
|
||||||
else:
|
else:
|
||||||
role = 'buyer'
|
role = 'buyer'
|
||||||
checkBuyActions(trade)
|
checkBuyActions(trade)
|
||||||
elif command == 'newtrade':
|
elif command == 'newtrade':
|
||||||
hexstr = args.importcontract
|
erase_trade()
|
||||||
db.create(hexstr)
|
role = 'seller'
|
||||||
|
htlcTrade = Trade()
|
||||||
|
print("Creating new XCAT transaction...")
|
||||||
|
|
|
@ -9,10 +9,6 @@ trade = get_trade()
|
||||||
## txid we retrieve by
|
## txid we retrieve by
|
||||||
txid = trade['sell']['fund_tx']
|
txid = trade['sell']['fund_tx']
|
||||||
|
|
||||||
def hex2dict(hexstr):
|
|
||||||
jsonstr = x2s(hexstr)
|
|
||||||
return json.loads(jsonstr)
|
|
||||||
|
|
||||||
def create(hexstr):
|
def create(hexstr):
|
||||||
trade = hex2dict(hexstr)
|
trade = hex2dict(hexstr)
|
||||||
txid = trade['sell']['fund_tx']
|
txid = trade['sell']['fund_tx']
|
||||||
|
|
4
utils.py
4
utils.py
|
@ -23,6 +23,10 @@ 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):
|
||||||
|
jsonstr = x2s(hexstr)
|
||||||
|
print(jsonstr)
|
||||||
|
return json.loads(jsonstr)
|
||||||
|
|
||||||
######################
|
######################
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue