From 12f709fb623ea3fcfb9a75cb70c5e769ab1a2ffc Mon Sep 17 00:00:00 2001 From: Jay Graber Date: Sun, 6 Aug 2017 23:19:21 -0700 Subject: [PATCH] Change tests, initial info collection --- xcat/cli.py | 11 +++++++++-- xcat/protocol.py | 30 ++++++++++++++++-------------- xcat/tests/test_cli.py | 35 +++++++++++++++++++++++++++++------ xcat/xcatconf.py | 40 +++++++++++++++++++++------------------- 4 files changed, 75 insertions(+), 41 deletions(-) diff --git a/xcat/cli.py b/xcat/cli.py index db29211..2087e47 100644 --- a/xcat/cli.py +++ b/xcat/cli.py @@ -155,9 +155,12 @@ def checktrade(tradeid): def newtrade(tradeid, **kwargs): print("Creating new XCAT trade...") erase_trade() - trade = seller_init(tradeid, **kwargs) + tradeid, trade= initialize_trade(tradeid, conf=kwargs['conf']) + print("Trade", trade) + trade = seller_init(tradeid, trade) print("\nUse 'xcat exporttrade [tradeid]' to export the trade and sent to the buyer.\n") save_state(trade, tradeid) + return trade def listtrades(): print("Trades") @@ -213,7 +216,11 @@ def main(): elif command == 'newtrade': if len(args.arguments) < 1: throw("Usage: newtrade [tradeid]") tradeid = args.arguments[0] - newtrade(tradeid, network=args.network, conf=args.conf) + print("Args.conf", args.conf) + if args.conf == None: + newtrade(tradeid, network=args.network, conf='cli') + else: + newtrade(tradeid, network=args.network, conf=args.conf) elif command == "daemon": #TODO: not implemented print("Run as daemon process") diff --git a/xcat/protocol.py b/xcat/protocol.py index 907f7bf..4d2fa75 100644 --- a/xcat/protocol.py +++ b/xcat/protocol.py @@ -162,21 +162,19 @@ def buyer_fulfill(trade): print("Please wait for the seller to remove your funds from escrow to complete the trade.") print_trade('buyer') -def seller_init(tradeid, **kwargs): - if kwargs['conf']: - conf = kwargs['conf'].upper() - if conf.upper() == 'REGTEST': - init_addrs = REGTEST['initiator'] - fulfill_addrs = REGTEST['fulfiller'] - elif conf.upper() == 'TESTNET': - init_addrs = TESTNET['initiator'] - fulfill_addrs = TESTNET['fulfiller'] - else: +def initialize_trade(tradeid, **kwargs): + trade = Trade() + print("kwargs", kwargs) + conf = kwargs['conf'] + if conf == 'cli': init_addrs = userInput.get_initiator_addresses() fulfill_addrs = userInput.get_fulfiller_addresses() - - trade = Trade() - amounts = userInput.get_trade_amounts() + amounts = userInput.get_trade_amounts() + print("AMOUNTS", amounts) + else: + init_addrs = ADDRS[conf]['initiator'] + fulfill_addrs = ADDRS[conf]['fulfiller'] + amounts = ADDRS[conf]['amounts'] sell = amounts['sell'] buy = amounts['buy'] @@ -192,7 +190,10 @@ def seller_init(tradeid, **kwargs): trade.buy = Contract(buy) print(trade.sell.__dict__) print(trade.buy.__dict__) + return tradeid, trade + +def seller_init(tradeid, trade): secret = generate_password() db.save_secret(tradeid, secret) print("\nGenerated a secret preimage to lock funds. This will only be stored locally: ", secret) @@ -202,8 +203,9 @@ def seller_init(tradeid, **kwargs): sell_locktime = 20 buy_locktime = 10 # Must be more than first tx print("Creating pay-to-script-hash for sell contract...") - create_sell_p2sh(trade, hash_of_secret, sell_locktime) + # create the p2sh addrs + create_sell_p2sh(trade, hash_of_secret, sell_locktime) create_buy_p2sh(trade, hash_of_secret, buy_locktime) trade.commitment = b2x(hash_of_secret) diff --git a/xcat/tests/test_cli.py b/xcat/tests/test_cli.py index 125345f..5a8cb77 100644 --- a/xcat/tests/test_cli.py +++ b/xcat/tests/test_cli.py @@ -1,6 +1,8 @@ import unittest import xcat.cli as cli +import xcat.db as db from xcat.tests.utils import mktrade +from xcat.trades import Trade, Contract class SimpleTestCase(unittest.TestCase): def setUp(self): @@ -11,18 +13,39 @@ class SimpleTestCase(unittest.TestCase): self.assertTrue(int(self.hexstr, 16)) def test_importtrade(self): - trade = cli.importtrade(self.__class__.hexstr, 'test') + trade = cli.importtrade('test', self.__class__.hexstr) class CliTest(SimpleTestCase): def test_findtrade(self): trade = cli.findtrade('test') def test_newtrade(self): - cli.newtrade('test2') - cli.newtrade('test2') - cli.checkBuyStatus('test2') - cli.checkSellStatus('test2') - cli.checkBuyStatus('test2') + trade = cli.newtrade('new', conf='regtest') + self.assertTrue(isinstance(trade, Trade)) + + def test_fundsell(self): + trade = db.get('new') + status = cli.seller_check_status(trade) + print("Trade status: {0}\n".format(status)) + self.assertEqual(status, 'init') + fund_tx = cli.fund_sell_contract(trade) + print("Sent fund_tx", fund_tx) + + # def test_fundbuy(self): + # trade = db.get('new') + # status = cli.buyer_check_status(trade) + # self.assertEqual(status, 'sellerFunded') + # fund_tx = cli.fund_contract(trade.buy) + # + # def test_seller_redeem(self): + # trade = db.get('new') + # status = cli.seller_check_status(trade) + # self.assertEqual(status, 'buyerFunded') + # + # def test_buyer_redeem(self): + # trade = db.get('new') + # status = cli.buyer_check_status(trade) + # self.assertEqual(status, 'sellerFunded') if __name__ == '__main__': unittest.main() diff --git a/xcat/xcatconf.py b/xcat/xcatconf.py index 4035d54..01e06ce 100644 --- a/xcat/xcatconf.py +++ b/xcat/xcatconf.py @@ -1,22 +1,24 @@ -# Replace with your own addresses -REGTEST = { - "initiator": { - "bitcoin": "mvc56qCEVj6p57xZ5URNC3v7qbatudHQ9b", - "zcash": "tmTF7LMLjvEsGdcepWPUsh4vgJNrKMWwEyc" +# Replace these with your own addresses +ADDRS = { + 'regtest': { + "initiator": { + "bitcoin": "mvc56qCEVj6p57xZ5URNC3v7qbatudHQ9b", + "zcash": "tmTF7LMLjvEsGdcepWPUsh4vgJNrKMWwEyc" + }, + "fulfiller": { + "bitcoin": "moRt56gJQGDNK46Y6fYy2HbooKnQXrTGDN", + "zcash": "tmK3rGzHDqa78MCwEicx9VcY9ZWX9gCF2nd" + }, + "amounts": {'buy': {'currency': 'zcash', 'amount': 0.02}, 'sell': {'currency': 'bitcoin', 'amount': 0.01}} }, - "fulfiller": { - "bitcoin": "moRt56gJQGDNK46Y6fYy2HbooKnQXrTGDN", - "zcash": "tmK3rGzHDqa78MCwEicx9VcY9ZWX9gCF2nd" - } -} - -TESTNET = { - "initiator": { - "bitcoin": "mvc56qCEVj6p57xZ5URNC3v7qbatudHQ9b", - "zcash": "tmTF7LMLjvEsGdcepWPUsh4vgJNrKMWwEyc" - }, - "fulfiller": { - "bitcoin": "mgRG44X4PQC1ZCA4V654UZjJGJ3pxbApj2", - "zcash": "tmLZu7MdjNdA6vbPTNTwdsZo91LnnrVTYB5" + 'testnet': { + "initiator": { + "bitcoin": "mvc56qCEVj6p57xZ5URNC3v7qbatudHQ9b", + "zcash": "tmTF7LMLjvEsGdcepWPUsh4vgJNrKMWwEyc" + }, + "fulfiller": { + "bitcoin": "mgRG44X4PQC1ZCA4V654UZjJGJ3pxbApj2", + "zcash": "tmLZu7MdjNdA6vbPTNTwdsZo91LnnrVTYB5" + } } }