From 6312cd02b01db8acc110a8a67bf4d9388267e54d Mon Sep 17 00:00:00 2001 From: Jay Graber Date: Tue, 1 Aug 2017 13:03:10 -0700 Subject: [PATCH] Reduce dependency on json, down to buyer redeem step --- xcat/bitcoinRPC.py | 35 +++++++++++++++---- xcat/cli.py | 78 +++++++++++++++++++++++++++++++++++-------- xcat/protocol.py | 29 +++++++++++----- xcat/secret.json | 2 +- xcat/tests/test_db.py | 16 ++++----- xcat/userInput.py | 8 +++-- xcat/utils.py | 8 ++--- xcat/xcat.json | 2 +- xcat/zcashRPC.py | 55 +++++++++++++++++++----------- 9 files changed, 166 insertions(+), 67 deletions(-) diff --git a/xcat/bitcoinRPC.py b/xcat/bitcoinRPC.py index a219ac6..d222136 100644 --- a/xcat/bitcoinRPC.py +++ b/xcat/bitcoinRPC.py @@ -83,6 +83,7 @@ def fund_htlc(p2sh, amount): txid = b2x(lx(b2x(fund_txid))) return txid +# Following two functions are about the same def check_funds(p2sh): bitcoind.importaddress(p2sh, "", False) # Get amount in address @@ -90,6 +91,16 @@ def check_funds(p2sh): amount = amount/COIN return amount +def get_fund_status(p2sh): + bitcoind.importaddress(p2sh, "", False) + amount = bitcoind.getreceivedbyaddress(p2sh, 0) + amount = amount/COIN + print("Amount in bitcoin get_fund_status", amount, p2sh) + if amount > 0: + return 'funded' + else: + return 'empty' + ## TODO: FIX search for p2sh in block def search_p2sh(block, p2sh): print("Fetching block...") @@ -182,8 +193,12 @@ def auto_redeem(contract, secret): VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0, (SCRIPT_VERIFY_P2SH,)) print("script verified, sending raw tx") txid = bitcoind.sendrawtransaction(tx) - print("Txid of submitted redeem tx: ", b2x(lx(b2x(txid)))) - return b2x(lx(b2x(txid))) + fund_tx = str(fundtx['outpoint']) + redeem_tx = b2x(lx(b2x(txid))) + print("Returning fund_tx", fund_tx) + print("Txid of submitted redeem tx: ", redeem_tx) + print("TXID SUCCESSFULLY REDEEMED") + return {"redeem_tx": redeem_tx, "fund_tx": fund_tx} else: print("No contract for this p2sh found in database", p2sh) @@ -233,17 +248,23 @@ def redeem_contract(contract, secret): VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0, (SCRIPT_VERIFY_P2SH,)) print("script verified, sending raw tx") txid = bitcoind.sendrawtransaction(tx) - print("Txid of submitted redeem tx: ", b2x(lx(b2x(txid)))) + fund_tx = str(fundtx['outpoint']) + redeem_tx = b2x(lx(b2x(txid))) + print("Returning fund_tx", fund_tx) + print("Txid of submitted redeem tx: ", redeem_tx) print("TXID SUCCESSFULLY REDEEMED") - return 'redeem_tx', b2x(lx(b2x(txid))) + return {"redeem_tx": redeem_tx, "fund_tx": fund_tx} else: print("nLocktime exceeded, refunding") refundPubKey = find_refundAddr(contract) print('refundPubKey', refundPubKey) txid = bitcoind.sendtoaddress(refundPubKey, fundtx['amount'] - FEE) - print("Txid of refund tx:", b2x(lx(b2x(txid)))) - print("TXID SUCCESSFULLY REFUNDED") - return 'refund_tx', b2x(lx(b2x(txid))) + fund_tx = str(fundtx['outpoint']) + refund_tx = b2x(lx(b2x(txid))) + print("Returning fund_tx", fund_tx) + print("Txid of submitted refund tx: ", refund_tx) + print("TXID SUCCESSFULLY REDEEMED") + return {"refund_tx": refund_tx, "fund_tx": fund_tx} else: print("No contract for this p2sh found in database", p2sh) diff --git a/xcat/cli.py b/xcat/cli.py index 99cd034..c30532a 100644 --- a/xcat/cli.py +++ b/xcat/cli.py @@ -14,27 +14,71 @@ def save_state(trade, tradeid): def checkSellStatus(tradeid): trade = db.get(tradeid) - if trade.buy.get_status() == 'funded': + status = seller_check_status(trade) + print("In checkSellStatus", status) + # if trade.buy.get_status() == 'funded': + if status == 'initFund': + userInput.authorize_fund_sell(trade) + fund_tx = fund_sell_contract(trade) + print("Sent fund_tx", fund_tx) + trade.sell.fund_tx = fund_tx + save_state(trade, tradeid) + elif status == 'buyerFunded': secret = userInput.retrieve_password() print("SECRET found in checksellactions", secret) - trade = seller_redeem_p2sh(trade, secret) + txs = seller_redeem_p2sh(trade, secret) + print("TXS IN SELLER REDEEM BUYER TX", txs) + trade.buy.fund_tx = txs['fund_tx'] + trade.buy.redeem_tx = txs['redeem_tx'] print("TRADE SUCCESSFULLY REDEEMED", trade) save_state(trade, tradeid) - elif trade.buy.get_status() == 'empty': + # elif trade.buy.get_status() == 'empty': + elif status == 'sellerFunded': 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)) - elif trade.buy.get_status() == 'redeemed': + # elif trade.buy.get_status() == 'redeemed': + elif status == 'sellerRedeemed': print("You have already redeemed the p2sh on the second chain of this trade.") +def buyer_check_status(trade): + sellState = check_fund_status(trade.sell.currency, trade.sell.p2sh) + buyState = check_fund_status(trade.buy.currency, trade.buy.p2sh) + if sellState == 'funded' and buyState == 'empty': + return 'sellerFunded' # step1 + # TODO: Find funding txid. How does buyer get seller redeemed tx? + elif sellState == 'funded' and hasattr(trade.buy, 'fund_tx'): + return 'sellerRedeemed' # step3 + elif sellState == 'funded' and buyState == 'funded': + return 'buyerFunded' # step2 + elif sellState == 'empty' and buyState == 'empty': + return 'buyerRedeemed' # step4 + +def seller_check_status(trade): + sellState = check_fund_status(trade.sell.currency, trade.sell.p2sh) + buyState = check_fund_status(trade.buy.currency, trade.buy.p2sh) + if sellState == 'funded' and buyState == 'empty': + return 'sellerFunded' # step1 + elif sellState == 'funded' and hasattr(trade.buy, 'redeem_tx'): + return 'sellerRedeemed' # step3 + # TODO: How does seller get buyer funded tx? + elif sellState == 'funded' and buyState == 'funded': + return 'buyerFunded' # step2 + elif sellState == 'empty' and buyState == 'empty': + if hasattr(trade.buy, 'redeem_tx'): + return 'buyerRedeemed' # step4 + else: + return 'initFund' # step0 + # TODO: function to calculate appropriate locktimes between chains def checkBuyStatus(tradeid): trade = db.get(tradeid) - if trade.sell.get_status() == 'redeemed' and trade.buy.get_status() == 'redeemed': + status = buyer_check_status(trade) + print("In checkBuyStatus", status) + if status == 'buyerRedeemed': print("This trade is complete, both sides redeemed.") - elif trade.sell.get_status() == 'funded' and trade.buy.get_status() != 'redeemed': + # elif trade.sell.get_status() == 'funded' and trade.buy.get_status() != 'redeemed': + elif status == 'sellerFunded': print("One active trade available, fulfilling buyer contract...") - # they should calculate redeemScript for themselves print("Trade commitment", trade.commitment) - # TODO: which block to start computation from? # 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) @@ -46,12 +90,18 @@ def checkBuyStatus(tradeid): save_state(trade, tradeid) # else: # print("Compiled p2sh for htlc does not match what seller sent.") - elif trade.buy.get_status() == 'redeemed': - secret = parse_secret(trade.buy.currency, trade.buy.redeem_tx) + elif status == 'sellerRedeemed': + redeem_tx = find_redeem_tx(trade.buy.currency, trade.buy.p2sh) + trade.buy.redeem_tx = redeem_tx + secret = parse_secret(trade.buy.currency, redeem_tx) if secret != None: print("Found secret", secret) - txid = auto_redeem_p2sh(trade.sell, secret) - print("TXID after buyer redeem", txid) + txs = auto_redeem_p2sh(trade.sell, secret) + print("TXS IN SELLER REDEEMED", txs) + # trade.sell.fund_tx = txs['fund_tx'] + trade.sell.redeem_tx = txs['redeem_tx'] + print("TXID after buyer redeem", trade.sell.redeem_tx) + save_state(trade, tradeid) print("XCAT trade complete!") else: print("Secret not found in redeemtx") @@ -60,6 +110,7 @@ def checkBuyStatus(tradeid): def importtrade(hexstr, tradeid): trade = x2s(hexstr) trade = db.instantiate(trade) + import_addrs(trade) print(trade.toJSON()) save_state(trade, tradeid) @@ -98,8 +149,7 @@ def newtrade(tradeid): role = 'seller' print("Creating new XCAT trade...") trade = seller_init(Trade()) - # Save it to leveldb - # db.create(trade) + print("Use 'xcat exporttrade to export the trade and sent to the buyer.'") save_state(trade, tradeid) def main(): diff --git a/xcat/protocol.py b/xcat/protocol.py index d919931..00acbac 100644 --- a/xcat/protocol.py +++ b/xcat/protocol.py @@ -7,6 +7,16 @@ from xcat.utils import * from xcat.trades import Contract, Trade import xcat.userInput as userInput +def find_redeem_tx (currency, p2sh): + if currency == 'bitcoin': + bitcoinRPC.find_transaction_to_address(p2sh) + else: + zcashRPC.find_transaction_to_address(p2sh) + +def import_addrs(trade): + check_fund_status(trade.sell.currency, trade.sell.p2sh) + check_fund_status(trade.buy.currency, trade.buy.p2sh) + def check_p2sh(currency, address): if currency == 'bitcoin': print("Checking funds in Bitcoin p2sh") @@ -15,6 +25,14 @@ def check_p2sh(currency, address): print("Checking funds in Zcash p2sh") return zcashRPC.check_funds(address) +def check_fund_status(currency, address): + if currency == 'bitcoin': + print("Checking funds in Bitcoin p2sh") + return bitcoinRPC.get_fund_status(address) + else: + print("Checking funds in Zcash p2sh") + return zcashRPC.get_fund_status(address) + def create_htlc(currency, funder, redeemer, commitment, locktime): print("Commitment in create_htlc", commitment) if currency == 'bitcoin': @@ -129,11 +147,9 @@ def seller_redeem_p2sh(trade, secret): exit() else: # Seller redeems buyer's funded tx (contract in p2sh) - tx_type, txid = redeem_p2sh(trade.buy, secret) - print("Setting tx_type: txid", tx_type, txid) - setattr(trade.buy, tx_type, txid) + txs = redeem_p2sh(trade.buy, secret) print("You have redeemed {0} {1}!".format(buy.amount, buy.currency)) - return trade + return txs def buyer_fulfill(trade): buy = trade.buy @@ -181,11 +197,6 @@ def seller_init(trade): print("Creating pay-to-script-hash for sell contract...") create_sell_p2sh(trade, hash_of_secret, sell_locktime) - userInput.authorize_fund_sell(trade) - - txid = fund_sell_contract(trade) - print("Sent") - create_buy_p2sh(trade, hash_of_secret, buy_locktime) trade.commitment = b2x(hash_of_secret) diff --git a/xcat/secret.json b/xcat/secret.json index c7049a5..536aca3 100644 --- a/xcat/secret.json +++ b/xcat/secret.json @@ -1 +1 @@ -OBbU9kny \ No newline at end of file +secret \ No newline at end of file diff --git a/xcat/tests/test_db.py b/xcat/tests/test_db.py index a8bc2c5..88d138b 100644 --- a/xcat/tests/test_db.py +++ b/xcat/tests/test_db.py @@ -1,23 +1,21 @@ +import xcat.database as db import unittest, json -import xcat.db as db import xcat.trades as trades -from xcat.tests.utils import test_trade class DatabaseTest(unittest.TestCase): def setUp(self): - self.data = test_trade + self.data = {"sell": {"amount": 0.1, "redeemScript": "63a82003d58daab37238604b3e57d4a8bdcffa401dc497a9c1aa4f08ffac81616c22b68876a9147788b4511a25fba1092e67b307a6dcdb6da125d967022a04b17576a914c7043e62a7391596116f54f6a64c8548e97d3fd96888ac", "redeemblocknum": 1066, "currency": "bitcoin", "initiator": "myfFr5twPYNwgeXyjCmGcrzXtCmfmWXKYp", "p2sh": "2MuYSQ1uQ4CJg5Y5QL2vMmVPHNJ2KT5aJ6f", "fulfiller": "mrQzUGU1dwsWRx5gsKKSDPNtrsP65vCA3Z", "fund_tx": "5c5e91a89a08b2d6698f50c9fd9bb2fa22da6c74e226c3dd63d59511566a2fdb"}, "buy": {"amount": 0.2, "redeemScript": "63a82003d58daab37238604b3e57d4a8bdcffa401dc497a9c1aa4f08ffac81616c22b68876a9143ea29256c9d2888ca23de42a8b8e69ca2ec235b167023f0db17576a914c5acca6ef39c843c7a9c3ad01b2da95fe2edf5ba6888ac", "redeemblocknum": 3391, "currency": "zcash", "locktime": 10, "initiator": "tmFRXyju7ANM7A9mg75ZjyhFW1UJEhUPwfQ", "p2sh": "t2HP59RpfR34nBCWH4VVD497tkc2ikzgniP", "fulfiller": "tmTjZSg4pX2Us6V5HttiwFZwj464fD2ZgpY"}, "commitment": "03d58daab37238604b3e57d4a8bdcffa401dc497a9c1aa4f08ffac81616c22b6"} self.sell = trades.Contract(self.data['sell']) def test_create(self): - db.create(self.data, 'test') + sell = trades.Contract(self.data['sell']) + buy = trades.Contract(self.data['buy']) + trade = trades.Trade(sell, buy, commitment=self.data['commitment']) + db.create(trade, 'test') def test_get(self): trade = db.get('test') - tradejson = json.loads(trade.toJSON()) - datajson = json.loads(json.dumps(self.data)) - self.assertEqual(datajson['sell'], tradejson['sell']) - self.assertEqual(datajson['buy'], tradejson['buy']) - self.assertEqual(datajson['commitment'], tradejson['commitment']) + print("Trade") if __name__ == '__main__': unittest.main() diff --git a/xcat/userInput.py b/xcat/userInput.py index 6a7dff9..1a73d6b 100644 --- a/xcat/userInput.py +++ b/xcat/userInput.py @@ -62,11 +62,15 @@ def get_initiator_addresses(): def get_fulfiller_addresses(): btc_addr = input("Enter the bitcoin address of the party you want to trade with: ") # btc_addr = bXcat.new_bitcoin_addr() - btc_addr = 'mgRG44X4PQC1ZCA4V654UZjJGJ3pxbApj2' + # btc_addr = 'mgRG44X4PQC1ZCA4V654UZjJGJ3pxbApj2' # testnet + # btc_addr = "mvc56qCEVj6p57xZ5URNC3v7qbatudHQ9b" + btc_addr = "mpFD3Knp5znDKAHyiYdXMGEYvxmShjdwSS" print(btc_addr) zec_addr = input("Enter the zcash address of the party you want to trade with: ") # zec_addr = zXcat.new_zcash_addr() - zec_addr = 'tmLZu7MdjNdA6vbPTNTwdsZo91LnnrVTYB5' + # zec_addr = 'tmLZu7MdjNdA6vbPTNTwdsZo91LnnrVTYB5' #testnet + # zec_addr = "tmTF7LMLjvEsGdcepWPUsh4vgJNrKMWwEyc" + zec_addr = "tmEGtCab8BJWq3fUa7TK4qhWuY9Ab7SHRh2" print(zec_addr) addresses = {'bitcoin': btc_addr, 'zcash': zec_addr} return addresses diff --git a/xcat/utils.py b/xcat/utils.py index e569c3e..e7cda04 100644 --- a/xcat/utils.py +++ b/xcat/utils.py @@ -76,12 +76,12 @@ def generate_password(): # caching the secret locally for now... def get_secret(): - with open('secret.json') as data_file: + with open('xcat/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: + with open('xcat/secret.json', 'w+') as outfile: outfile.write(secret) ############################################# @@ -90,11 +90,11 @@ def save_secret(secret): def save_trade(trade): print("Trade in save_trade", trade) - with open('xcat.json', 'w+') as outfile: + with open('xcat/xcat.json', 'w+') as outfile: json.dump(trade, outfile) def get_trade(): - with open('xcat.json') as data_file: + with open('xcat/xcat.json') as data_file: xcatdb = json.load(data_file) sell = trades.Contract(xcatdb['sell']) buy = trades.Contract(xcatdb['buy']) diff --git a/xcat/xcat.json b/xcat/xcat.json index f4bc38a..74803ce 100644 --- a/xcat/xcat.json +++ b/xcat/xcat.json @@ -1 +1 @@ -{"sell": {"amount": 3.5, "redeemScript": "63a82003d58daab37238604b3e57d4a8bdcffa401dc497a9c1aa4f08ffac81616c22b68876a9147788b4511a25fba1092e67b307a6dcdb6da125d967022a04b17576a914c7043e62a7391596116f54f6a64c8548e97d3fd96888ac", "redeemblocknum": 1066, "currency": "bitcoin", "initiator": "myfFr5twPYNwgeXyjCmGcrzXtCmfmWXKYp", "p2sh": "2MuYSQ1uQ4CJg5Y5QL2vMmVPHNJ2KT5aJ6f", "fulfiller": "mrQzUGU1dwsWRx5gsKKSDPNtrsP65vCA3Z", "fund_tx": "5c5e91a89a08b2d6698f50c9fd9bb2fa22da6c74e226c3dd63d59511566a2fdb"}, "buy": {"amount": 1.2, "redeemScript": "63a82003d58daab37238604b3e57d4a8bdcffa401dc497a9c1aa4f08ffac81616c22b68876a9143ea29256c9d2888ca23de42a8b8e69ca2ec235b167023f0db17576a914c5acca6ef39c843c7a9c3ad01b2da95fe2edf5ba6888ac", "redeemblocknum": 3391, "currency": "zcash", "locktime": 10, "initiator": "tmFRXyju7ANM7A9mg75ZjyhFW1UJEhUPwfQ", "p2sh": "t2HP59RpfR34nBCWH4VVD497tkc2ikzgniP", "fulfiller": "tmTjZSg4pX2Us6V5HttiwFZwj464fD2ZgpY"}, "commitment": "03d58daab37238604b3e57d4a8bdcffa401dc497a9c1aa4f08ffac81616c22b6"} \ No newline at end of file +{"commitment": "2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b", "buy": {"redeemblocknum": 116161, "fulfiller": "tmEGtCab8BJWq3fUa7TK4qhWuY9Ab7SHRh2", "fund_tx": "e55390c7fa36725ea4458b9eb7a1b82850b9f2767362157508d1d8c39276b855:0", "redeem_tx": "9c3a1a7f2f83520f171329ce66272374818f5c0c8d933c227e652c48ded6f3ad", "currency": "zcash", "p2sh": "t27L54EpLd46CNx9heBhoWt7G2UozLRHhcH", "initiator": "tmMuMqAD83idRXeJtGjjRp2FrRujHaoywAv", "locktime": 10, "amount": 0.2, "redeemScript": "63a8202bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b8876a91485b6808f97ff87b256ad97303408efc782ddd4466703c1c501b17576a9143207950793180d8659ab15861b935217848ddc596888ac"}, "sell": {"redeemblocknum": 1155898, "fulfiller": "mpFD3Knp5znDKAHyiYdXMGEYvxmShjdwSS", "fund_tx": "867468520a2dc4297031966978964827958b921f0e9521f6d080a87362a5c173", "currency": "bitcoin", "p2sh": "2N5htfxW99WhoHu5kyGHbn5f1KryjPEjkq8", "initiator": "mmvpYgRLu6FmoQpLkea3Rcpwx74CXTqurU", "amount": 0.1, "redeemScript": "63a8202bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b8876a9145fbec93a4f3473966d4f93a6a50d7cb6febe8bbf67033aa311b17576a9144654452de033bf41e0adc837585bc94f7bec292c6888ac"}} \ No newline at end of file diff --git a/xcat/zcashRPC.py b/xcat/zcashRPC.py index 259ab58..e99cd81 100644 --- a/xcat/zcashRPC.py +++ b/xcat/zcashRPC.py @@ -76,6 +76,8 @@ def fund_htlc(p2sh, amount): txid = b2x(lx(b2x(fund_txid))) return txid + +# Following two functions are about the same def check_funds(p2sh): zcashd.importaddress(p2sh, "", False) print("Imported address", p2sh) @@ -85,6 +87,16 @@ def check_funds(p2sh): amount = amount/COIN return amount +def get_fund_status(p2sh): + zcashd.importaddress(p2sh, "", False) + amount = zcashd.getreceivedbyaddress(p2sh, 0) + amount = amount/COIN + print("Amount in zcash get_fund_status", amount, p2sh) + if amount > 0: + return 'funded' + else: + return 'empty' + def get_tx_details(txid): fund_txinfo = zcashd.gettransaction(txid) return fund_txinfo['details'][0] @@ -101,15 +113,6 @@ def find_transaction_to_address(p2sh): print(tx) return tx -# def get_tx_details(txid): -# # This method is problematic I haven't gotten the type conversions right -# print(bytearray.fromhex(txid)) -# print(b2x(bytearray.fromhex(txid))) -# fund_txinfo = zcashd.gettransaction(bytearray.fromhex(txid)) -# print(fund_txinfo) -# -# return fund_txinfo['details'][0] - def find_secret(p2sh): return parse_secret('4c25b5db9f3df48e48306891d8437c69308afa122f92416df1a3ba0d3604882f') zcashd.importaddress(p2sh, "", False) @@ -153,7 +156,6 @@ def auto_redeem(contract, secret): quit() fundtx = find_transaction_to_address(p2sh) amount = fundtx['amount'] / COIN - print("Found fundtx:", fundtx) p2sh = P2SHBitcoinAddress(p2sh) if fundtx['address'] == p2sh: print("Found {0} in p2sh {1}, redeeming...".format(amount, p2sh)) @@ -166,6 +168,7 @@ def auto_redeem(contract, secret): redeemPubKey = find_redeemAddr(contract) print('redeemPubKey', redeemPubKey) zec_redeemScript = CScript(x(contract.redeemScript)) + txin = CMutableTxIn(fundtx['outpoint']) txout = CMutableTxOut(fundtx['amount'] - FEE, redeemPubKey.to_scriptPubKey()) # Create the unsigned raw transaction. @@ -184,9 +187,12 @@ def auto_redeem(contract, secret): VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0, (SCRIPT_VERIFY_P2SH,)) print("script verified, sending raw tx") txid = zcashd.sendrawtransaction(tx) - print("Txid of submitted redeem tx: ", b2x(lx(b2x(txid)))) + redeem_tx = b2x(lx(b2x(txid))) + print("Txid of submitted redeem tx: ", redeem_tx) + fund_tx = str(fundtx['outpoint']) + print("Returning fund_tx", fund_tx) print("TXID SUCCESSFULLY REDEEMED") - return 'redeem_tx', b2x(lx(b2x(txid))) + return {"redeem_tx": redeem_tx, "fund_tx": fund_tx} else: # if blockcount >= redeemblocknum: # tx.nLockTime = redeemblocknum @@ -194,9 +200,12 @@ def auto_redeem(contract, secret): refundPubKey = find_refundAddr(contract) print('refundPubKey', refundPubKey) txid = zcashd.sendtoaddress(refundPubKey, fundtx['amount'] - FEE) - print("Txid of refund tx:", b2x(lx(b2x(txid)))) + refund_tx = b2x(lx(b2x(txid))) + print("Txid of submitted refund tx: ", refund_tx) + fund_tx = str(fundtx['outpoint']) + print("Returning fund_tx", fund_tx) print("TXID SUCCESSFULLY REFUNDED") - return 'refund_tx', b2x(lx(b2x(txid))) + return {"refund_tx": redeem_tx, "fund_tx": fund_tx} else: print("No contract for this p2sh found in database", p2sh) @@ -224,9 +233,9 @@ def redeem_contract(contract, secret): # TODO: parse the script once, up front. redeemPubKey = find_redeemAddr(contract) - print('redeemPubKey', redeemPubKey) zec_redeemScript = CScript(x(contract.redeemScript)) + txin = CMutableTxIn(fundtx['outpoint']) txout = CMutableTxOut(fundtx['amount'] - FEE, redeemPubKey.to_scriptPubKey()) # Create the unsigned raw transaction. @@ -245,17 +254,23 @@ def redeem_contract(contract, secret): VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0, (SCRIPT_VERIFY_P2SH,)) print("script verified, sending raw tx") txid = zcashd.sendrawtransaction(tx) - print("Txid of submitted redeem tx: ", b2x(lx(b2x(txid)))) + redeem_tx = b2x(lx(b2x(txid))) + fund_tx = str(fundtx['outpoint']) + print("Returning fund_tx", fund_tx) + print("Txid of submitted redeem tx: ", redeem_tx) print("TXID SUCCESSFULLY REDEEMED") - return 'redeem_tx', b2x(lx(b2x(txid))) + return {"redeem_tx": redeem_tx, "fund_tx": fund_tx} else: print("nLocktime exceeded, refunding") refundPubKey = find_refundAddr(contract) print('refundPubKey', refundPubKey) txid = zcashd.sendtoaddress(refundPubKey, fundtx['amount'] - FEE) - print("Txid of refund tx:", b2x(lx(b2x(txid)))) - print("TXID SUCCESSFULLY REFUNDED") - return 'refund_tx', b2x(lx(b2x(txid))) + refund_tx = b2x(lx(b2x(txid))) + fund_tx = str(fundtx['outpoint']) + print("Returning fund_tx", fund_tx) + print("Txid of submitted refund tx: ", refund_tx) + print("TXID SUCCESSFULLY REDEEMED") + return {"refund_tx": refund_tx, "fund_tx": fund_tx} else: print("No contract for this p2sh found in database", p2sh)