zbxcat/xcat/cli.py

160 lines
5.8 KiB
Python
Raw Normal View History

import argparse, textwrap
2017-07-28 17:23:32 -07:00
from xcat.utils import *
2017-07-28 20:45:46 -07:00
import xcat.db as db
2017-07-28 17:23:32 -07:00
import xcat.bitcoinRPC
import xcat.zcashRPC
2017-07-31 13:51:54 -07:00
import xcat.userInput as userInput
2017-07-28 17:23:32 -07:00
from xcat.trades import *
from xcat.protocol import *
2017-07-31 13:32:51 -07:00
import ast
2017-07-28 17:23:32 -07:00
def save_state(trade, tradeid):
2017-07-26 13:23:12 -07:00
save(trade)
2017-07-28 17:23:32 -07:00
db.create(trade, tradeid)
2017-07-31 15:15:22 -07:00
def checkSellStatus(tradeid):
trade = db.get(tradeid)
if trade.buy.get_status() == 'funded':
2017-07-31 16:25:49 -07:00
input("Authorize retrieve secret:")
2017-07-26 13:23:12 -07:00
secret = get_secret()
print("SECRET found in checksellactions", secret)
trade = seller_redeem_p2sh(trade, secret)
print("TRADE SUCCESSFULLY REDEEMED", trade)
2017-07-31 15:15:22 -07:00
save_state(trade, tradeid)
elif trade.buy.get_status() == 'empty':
print("Buyer has not yet funded the contract where you offered to buy {0}, please wait for them to complete their part.".format(trade.buy.currency))
2017-07-26 13:23:12 -07:00
elif trade.buy.get_status() == 'redeemed':
print("You have already redeemed the p2sh on the second chain of this trade.")
2017-07-28 13:57:44 -07:00
# TODO: function to calculate appropriate locktimes between chains
2017-07-31 15:15:22 -07:00
def checkBuyStatus(tradeid):
trade = db.get(tradeid)
2017-07-31 16:25:49 -07:00
if trade.sell.get_status() == 'redeemed' and trade.buy.get_status() == 'redeemed':
print("This trade is complete, both sides redeemed.")
elif trade.sell.get_status() == 'funded' and trade.buy.get_status() != 'redeemed':
print("One active trade available, fulfilling buyer contract...")
2017-07-26 13:23:12 -07:00
# they should calculate redeemScript for themselves
2017-07-28 13:57:44 -07:00
print("Trade commitment", trade.commitment)
# TODO: which block to start computation from?
2017-07-31 16:25:49 -07:00
# htlc = create_htlc(trade.buy.currency, trade.buy.fulfiller, trade.buy.initiator, trade.commitment, trade.buy.locktime)
# buyer_p2sh = htlc['p2sh']
# print("Buyer p2sh:", buyer_p2sh)
2017-07-26 13:23:12 -07:00
# If the two p2sh match...
2017-07-31 16:25:49 -07:00
# if buyer_p2sh == trade.buy.p2sh:
fund_tx = fund_contract(trade.buy)
trade.buy.fund_tx = fund_tx
print("trade buy with redeemscript?", trade.buy.__dict__)
save_state(trade, tradeid)
# else:
# print("Compiled p2sh for htlc does not match what seller sent.")
elif trade.buy.get_status() == 'redeemed':
2017-07-26 13:23:12 -07:00
# TODO: secret parsing
# secret = parse_secret(trade.buy.currency, trade.buy.redeem_tx)
secret = get_secret()
print("Found secret", secret)
txid = auto_redeem_p2sh(trade.sell, secret)
print("TXID after buyer redeem", txid)
print("XCAT trade complete!")
2017-07-28 17:23:32 -07:00
# Import a trade in hex, and save to db
2017-07-31 17:06:58 -07:00
def importtrade(hexstr, tradeid):
2017-07-28 20:45:46 -07:00
trade = x2s(hexstr)
2017-07-31 16:25:49 -07:00
trade = db.instantiate(trade)
2017-07-31 17:06:58 -07:00
save_state(trade, tradeid)
2017-07-28 17:23:32 -07:00
# Export a trade by its tradeid
def exporttrade(tradeid):
# trade = get_trade()
trade = db.get(tradeid)
2017-07-31 17:06:58 -07:00
hexstr = s2x(trade.toJSON())
2017-07-28 17:23:32 -07:00
print(hexstr)
2017-07-31 17:06:58 -07:00
return hexstr
2017-07-31 13:32:51 -07:00
2017-07-31 16:25:49 -07:00
def findtrade(tradeid):
trade = db.get(tradeid)
2017-07-31 15:27:43 -07:00
print(trade)
2017-07-31 17:06:58 -07:00
return trade
2017-07-28 17:23:32 -07:00
2017-07-31 16:25:49 -07:00
def checktrade(tradeid):
print("In checktrade")
trade = db.get(tradeid)
if find_role(trade.sell) == 'test':
input("Is this a test? Both buyer and seller addresses are yours, press 'enter' to test.")
checkBuyStatus(tradeid)
checkSellStatus(tradeid)
checkBuyStatus(tradeid)
elif find_role(trade.sell) == 'initiator':
print("You are the seller in this trade.")
role = 'seller'
checkSellStatus(tradeid)
else:
print("You are the buyer in this trade.")
role = 'buyer'
checkBuyStatus(tradeid)
2017-07-28 17:23:32 -07:00
def newtrade(tradeid):
erase_trade()
role = 'seller'
print("Creating new XCAT trade...")
trade = seller_init(Trade())
# Save it to leveldb
# db.create(trade)
save_state(trade, tradeid)
def main():
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
description=textwrap.dedent('''\
== Trades ==
2017-07-31 16:25:49 -07:00
newtrade "tradeid" - create a new trade
checktrade "tradeid"- check for actions to be taken on an existing trade
2017-07-31 13:32:51 -07:00
importtrade "hexstr" - import an existing trade from a hex string
2017-07-31 16:25:49 -07:00
exporttrade "tradeid" - export the data of an existing trade as a hex string. Takes the tradeid as an argument
findtrade "tradeid" - find a trade by the tradeid
'''))
parser.add_argument("command", action="store", help="list commands")
2017-07-12 20:33:56 -07:00
parser.add_argument("argument", action="store", nargs="*", help="add an argument")
# parser.add_argument("--daemon", "-d", action="store_true", help="Run as daemon process")
2017-07-28 17:23:32 -07:00
# TODO: function to view available trades
# TODO: function to tell if tradeid already exists for newtrade
args = parser.parse_args()
# how to hold state of role
command = args.command
if command == 'importtrade':
2017-07-12 20:33:56 -07:00
hexstr = args.argument[0]
2017-07-31 17:06:58 -07:00
tradeid = args.argument[1]
importtrade(hexstr, tradeid)
elif command == 'exporttrade':
2017-07-28 17:23:32 -07:00
tradeid = args.argument[0]
exporttrade(tradeid)
2017-07-31 13:32:51 -07:00
elif command == "findtrade":
print("Finding trade")
key = args.argument[0]
2017-07-31 15:27:43 -07:00
findtrade(key)
2017-07-31 16:25:49 -07:00
elif command == 'checktrade':
tradeid = args.argument[0]
checktrade(tradeid)
elif command == 'newtrade':
2017-07-31 13:51:54 -07:00
print("in new trade")
2017-07-28 17:23:32 -07:00
try:
tradeid = args.argument[0]
newtrade(tradeid)
except:
tradeid = userInput.enter_trade_id()
newtrade(tradeid)
elif command == "daemon":
#TODO: implement
print("Run as daemon process")
2017-07-31 15:27:43 -07:00
# Ad hoc testing of workflow starts here
2017-07-26 13:23:12 -07:00
elif command == "step2":
2017-07-31 14:01:20 -07:00
# trade = get_trade()
tradeid = args.argument[0]
2017-07-31 15:15:22 -07:00
checkBuyStatus(tradeid)
2017-07-26 13:23:12 -07:00
elif command == "step3":
2017-07-31 15:15:22 -07:00
tradeid = args.argument[0]
checkSellStatus(tradeid)
2017-07-26 13:23:12 -07:00
elif command == "step4":
2017-07-31 15:15:22 -07:00
tradeid = args.argument[0]
checkBuyStatus(tradeid)