diff --git a/api.py b/api.py new file mode 100644 index 0000000..aa0bb01 --- /dev/null +++ b/api.py @@ -0,0 +1,125 @@ +import zXcat +import bXcat +from xcat import * + +print("Starting test of xcat...") + +def get_initiator_addresses(): + baddr = bXcat.new_bitcoin_addr() + zaddr = zXcat.new_zcash_addr() + # print("type baddr", type(baddr)) + # print("type baddr", type(baddr.to_scriptPubKey())) + return {'bitcoin': baddr.__str__(), 'zcash': zaddr.__str__()} + # return {'bitcoin': 'myfFr5twPYNwgeXyjCmGcrzXtCmfmWXKYp', 'zcash': 'tmFRXyju7ANM7A9mg75ZjyhFW1UJEhUPwfQ'} + +def get_fulfiller_addresses(): + baddr = bXcat.new_bitcoin_addr() + zaddr = zXcat.new_zcash_addr() + return {'bitcoin': baddr.__str__(), 'zcash': zaddr.__str__()} + # return {'bitcoin': 'myfFr5twPYNwgeXyjCmGcrzXtCmfmWXKYp', 'zcash': 'tmFRXyju7ANM7A9mg75ZjyhFW1UJEhUPwfQ'} + # return {'bitcoin': 'mrQzUGU1dwsWRx5gsKKSDPNtrsP65vCA3Z', 'zcash': 'tmTjZSg4pX2Us6V5HttiwFZwj464fD2ZgpY'} + +def initiate(): + print("SELLER FUNDING SELL CONTRACT") + print("============================") + trade = Trade() + # Get amounts + amounts = {"sell": {"currency": "zcash", "amount": "0.01"}, "buy": {"currency": "bitcoin", "amount": "1.12"}} + sell = amounts['sell'] + buy = amounts['buy'] + sell_currency = sell['currency'] + buy_currency = buy['currency'] + # Get addresses + init_addrs = get_initiator_addresses() + sell['initiator'] = init_addrs[sell_currency] + buy['initiator'] = init_addrs[buy_currency] + fulfill_addrs = get_fulfiller_addresses() + sell['fulfiller'] = fulfill_addrs[sell_currency] + buy['fulfiller'] = fulfill_addrs[buy_currency] + # initializing contract classes with addresses, currencies, and amounts + trade.sellContract = Contract(sell) + trade.buyContract = Contract(buy) + print(trade.sellContract.__dict__) + print(trade.buyContract.__dict__) + + secret = generate_password() + print("Generating secret to lock funds:", secret) + save_secret(secret) + # TODO: Implement locktimes and mock block passage of time + seller_locktime = 6 # Must be more than buyer_locktime, so that seller reveal secret before their own locktime + buyer_locktime = 3 + + create_sell_p2sh(trade, secret, seller_locktime) + txid = fund_sell_contract(trade) + print("Sent fund tx of sell contract:", txid) + create_buy_p2sh(trade, secret, buyer_locktime) + save(trade) + return trade + +# buyer checks that seller funded the sell contract, and if so funds the buy contract +def fund_buyer(): + print("BUYER FUNDING BUY CONTRACT") + print("==========================") + trade = get_trade() + buy = trade.buyContract + sell = trade.sellContract + # buy_p2sh_balance = check_p2sh(buy.currency, buy.p2sh) + sell_p2sh_balance = check_p2sh(sell.currency, sell.p2sh) + if (sell_p2sh_balance < float(sell.amount)): + raise ValueError("Sell p2sh not funded, buyer cannot redeem") + print("Seller has deposited funds, so funding the buy contract:") + txid = fund_buy_contract(trade) + print("Buyer Fund tx txid:", txid) + +def redeem_seller(): + trade = get_trade() + print(trade) + print("SELLER REDEEMING BUY CONTRACT") + print("=============================") + buy = trade.buyContract + print(buy) + # if trade.sellContract.get_status() == 'redeemed': + # raise RuntimeError("Sell contract status was already redeemed before seller could redeem buyer's tx") + #else: + secret = get_secret() # Just the seller getting his local copy of the secret + print("SELLER SECRET IN TEST:", secret) + txid = redeem_p2sh(trade.buyContract, secret, trade.sellContract) + setattr(trade.buyContract, 'redeem_tx', txid) + save(trade) + + +def redeem_buyer(): + print("BUYER REDEEMING SELL CONTRACT") + print("=============================") + trade = get_trade() + buyContract = trade.buyContract + sellContract = trade.sellContract + secret = "" + # if sellContract.get_status() == 'redeemed': + # raise RuntimeError("Sell contract was redeemed before buyer could retrieve funds") + # elif buyContract.get_status() == 'refunded': + # print("buyContract was refunded to buyer") + # else: + # Buy contract is where seller disclosed secret in redeeming + if buyContract.currency == 'bitcoin': + if (bXcat.still_locked(buyContract)): + if(not hasattr(buyContract,'fund_tx')): + print("Seems address has not been funded yet. Aborting.") + quit() + secret = bXcat.find_secret(buyContract.p2sh,buyContract.fund_tx) + if(secret != ""): + print("Found secret in seller's redeem tx on bitcoin chain:", secret) + else: + if zXcat.still_locked(buyContract): + secret = zXcat.find_secret(buyContract.p2sh,buyContract.fund_tx) + if(secret != ""): + print("Found secret in seller's redeem tx on zcash chain:", secret) + redeem_tx = redeem_p2sh(sellContract, secret, buyContract) + setattr(trade.sellContract, 'redeem_tx', redeem_tx) + save(trade) + + +def generate_blocks(num): + bXcat.generate(num) + zXcat.generate(num) + diff --git a/secret.json b/secret.json index d31e730..0fc274e 100644 --- a/secret.json +++ b/secret.json @@ -1 +1 @@ -tOfh1GKN \ No newline at end of file +N9BkxaG0 \ No newline at end of file diff --git a/test.py b/test.py index 6e680fb..2556b44 100644 --- a/test.py +++ b/test.py @@ -9,7 +9,7 @@ fund_buyer() zXcat.generate(6) redeem_seller() zXcat.generate(2) - +bXcat.generate(20) redeem_buyer() diff --git a/xcat.json b/xcat.json index 14c7937..efa3cdf 100644 --- a/xcat.json +++ b/xcat.json @@ -1 +1 @@ -{"sell": {"initiator": "tmVKxirCairPPjCZwxXo4zBEisxADWvzkWB", "fund_tx": "1717da92ff8d2099a63c0a54f397b34f082515d06665a5b3099d9d92122335e4", "fulfiller": "tmAo2UmEvPMnB34JZNjaV3yboYktBgFbnBG", "amount": "0.01", "redeemScript": "63a820d762c010f57e979c77e450acb050e29629e2f240c5fc6131730216b06144e01e8876a9140bda780692cb0c8de760a027b749e158ef45905367022b08b17576a914d7267dd0b627f7749d533d4546da62cfec0c4f666888ac", "redeemblocknum": 2091, "redeem_tx": false, "currency": "zcash", "p2sh": "t2R7AdHyWsuJyt85VeLp3c9Pi1EAJiZcZu2"}, "buy": {"initiator": "mx9NBQR93Ldmw3pzHvvcLiKfb3z19BNESe", "fund_tx": "ad486abc70d2d78ec5485c416e244afa9c37a70a14fd3b3013a264678acb8d31", "fulfiller": "mwAwF2ASL1qu7Yw4Liqz7gB3SWSaqTGw8F", "amount": "1.12", "redeemScript": "63a820d762c010f57e979c77e450acb050e29629e2f240c5fc6131730216b06144e01e8876a914b664ecf7ab99237ccb56a3f90eb0326d973ad1d76702001fb17576a914abb8d09cc8ca93f60d145cfdbaad21abf264b75b6888ac", "redeemblocknum": 7936, "redeem_tx": "370a5975500900500362f1c30a6e5ef7285466abd136c8acc9281398b72b161a", "currency": "bitcoin", "p2sh": "2NEerpQfmykRjh1L9GBTo43MpkXNpwoD1iV"}} \ No newline at end of file +{"sell": {"currency": "zcash", "initiator": "tmYU7UVFGVK2cmNqQ9dLoNFZMWSvZWCeKtD", "redeemblocknum": 2109, "amount": "0.01", "fulfiller": "tmVx4ckwZAZFPwxMGgrpZ6N7wp3VRgDuXfy", "fund_tx": "ebaa6f3c99ed282701eb8ed926ca822cd9b52458f72e9170988920ae1cffd7ff", "redeem_tx": false, "p2sh": "t2VK8Pgu4GEfnBrh8bLTND1ns3bfQUw7wP9", "redeemScript": "63a8202bf467e67c060bc84c7cc1a47791d8678d8eac11a9e80af993b767661131e7f38876a914ddfa63110b8569bc80c0e5cbe3ea9df142ddea6e67023d08b17576a914f99980ad0a78f8f742d88d97726cc9c91df172f86888ac"}, "buy": {"currency": "bitcoin", "initiator": "mrjxeVKPWCEQYEU9du3CnGT9NKpVNUzSKz", "redeemblocknum": 7936, "amount": "1.12", "fulfiller": "miN9ygMUHEipaobPFuoRwmsNN3ovwzPMDS", "fund_tx": "c7dd7447cbc8717d4574011bcf75c727425ac0585f2aac7f3a87365bbbfedc09", "redeem_tx": "31f691e284f9ea59eb76d691a9442f8dda4897c69772fa4b986f3e4eb1d63b90", "p2sh": "2MtDXWhoi4PwA66pCbusQpGTdGeN6UYTQEu", "redeemScript": "63a8202bf467e67c060bc84c7cc1a47791d8678d8eac11a9e80af993b767661131e7f38876a9147b1f15d1da93ba0414fe7ac36297a2bb7749a3ef6702001fb17576a9141f3e62e6daa403faea8bc8a30259f0888a1a7cb06888ac"}} \ No newline at end of file