From e6cb6764ae1ed601d4731e0dd0d32def403c3a38 Mon Sep 17 00:00:00 2001 From: Jay Graber Date: Fri, 15 Sep 2017 11:51:40 -0700 Subject: [PATCH 1/4] Fix refund bug --- xcat/bitcoinRPC.py | 3 +++ xcat/zcashRPC.py | 6 ++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/xcat/bitcoinRPC.py b/xcat/bitcoinRPC.py index 338d8e3..0a3c73f 100644 --- a/xcat/bitcoinRPC.py +++ b/xcat/bitcoinRPC.py @@ -208,6 +208,9 @@ class bitcoinProxy(): txout = CMutableTxOut(fundtx['amount'] - FEE, refundPubKey.to_scriptPubKey()) # Create the unsigned raw transaction. tx = CMutableTransaction([txin], [txout]) + # Set nSequence and nLockTime + txin.nSequence = 0 + tx.nLockTime = contract.redeemblocknum sighash = SignatureHash(redeemScript, tx, 0, SIGHASH_ALL) privkey = self.bitcoind.dumpprivkey(refundPubKey) sig = privkey.sign(sighash) + bytes([SIGHASH_ALL]) diff --git a/xcat/zcashRPC.py b/xcat/zcashRPC.py index b2cafd0..0aa38fd 100644 --- a/xcat/zcashRPC.py +++ b/xcat/zcashRPC.py @@ -212,14 +212,16 @@ class zcashProxy(): redeemScript = CScript(x(contract.redeemScript)) txin = CMutableTxIn(fundtx['outpoint']) txout = CMutableTxOut(fundtx['amount'] - FEE, refundPubKey.to_scriptPubKey()) - # Create the unsigned raw transaction. tx = CMutableTransaction([txin], [txout]) + # Set nSequence and nLockTime + txin.nSequence = 0 + tx.nLockTime = contract.redeemblocknum + # Create the unsigned raw transaction. sighash = SignatureHash(redeemScript, tx, 0, SIGHASH_ALL) privkey = self.zcashd.dumpprivkey(refundPubKey) sig = privkey.sign(sighash) + bytes([SIGHASH_ALL]) # Sign without secret txin.scriptSig = CScript([sig, privkey.pub, OP_FALSE, redeemScript]) - # txin.nSequence = 2185 txin_scriptPubKey = redeemScript.to_p2sh_scriptPubKey() print('Raw redeem transaction hex: {0}'.format(b2x(tx.serialize()))) res = VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0, (SCRIPT_VERIFY_P2SH,)) From 7e39d8a48b2eab47be0d5b20bdaa046401cfabd6 Mon Sep 17 00:00:00 2001 From: Jay Graber Date: Fri, 15 Sep 2017 13:15:17 -0700 Subject: [PATCH 2/4] Remove calls to zcashd --- xcat/bitcoinRPC.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/xcat/bitcoinRPC.py b/xcat/bitcoinRPC.py index 0a3c73f..cc1ce1c 100644 --- a/xcat/bitcoinRPC.py +++ b/xcat/bitcoinRPC.py @@ -48,8 +48,8 @@ class bitcoinProxy(): return def parse_secret(self, txid): - raw = zcashd.gettransaction(txid, True)['hex'] - decoded = zcashd.call('decoderawtransaction', raw) + raw = self.bitcoind.call('gettransaction', txid, True)['hex'] + decoded = self.bitcoind.call('decoderawtransaction', raw) scriptSig = decoded['vin'][0]['scriptSig'] asm = scriptSig['asm'].split(" ") pubkey = asm[1] @@ -125,11 +125,8 @@ class bitcoinProxy(): print("txs", txs) for tx in txs: txhex = b2x(tx.serialize()) - # Using my fork of python-zcashlib to get result of decoderawtransaction txhex = txhex + '00' - rawtx = zcashd.decoderawtransaction(txhex) - # print('rawtx', rawtx) - print(rawtx) + rawtx = self.bitcoind.call('decoderawtransaction', txhex) for vout in rawtx['vout']: if 'addresses' in vout['scriptPubKey']: for addr in vout['scriptPubKey']['addresses']: @@ -163,7 +160,7 @@ class bitcoinProxy(): print("Found {0} in p2sh {1}, redeeming...".format(amount, p2sh)) blockcount = self.bitcoind.getblockcount() - print("\nCurrent blocknum at time of redeem on Zcash:", blockcount) + print("\nCurrent blocknum at time of redeem on Bitcoin:", blockcount) if blockcount < int(redeemblocknum): return self.redeem(contract, fundtx, secret) else: From 5b118354e79aa4ce6bed975f3520e7f6d222242d Mon Sep 17 00:00:00 2001 From: Jay Graber Date: Fri, 15 Sep 2017 13:18:17 -0700 Subject: [PATCH 3/4] Rm print_trade --- xcat/protocol.py | 1 - 1 file changed, 1 deletion(-) diff --git a/xcat/protocol.py b/xcat/protocol.py index 7f0281c..6d13e06 100644 --- a/xcat/protocol.py +++ b/xcat/protocol.py @@ -204,7 +204,6 @@ def buyer_fulfill(trade): else: print("It looks like you've already funded the contract to buy {1}, the amount in escrow in the p2sh is {0}.".format(buy_p2sh_balance, buy.currency)) print("Please wait for the seller to remove your funds from escrow to complete the trade.") - print_trade('buyer') def initialize_trade(tradeid, **kwargs): trade = Trade() From 83277eb63001f506f9e40c8de7fe0010d01c5a19 Mon Sep 17 00:00:00 2001 From: Jay Graber Date: Fri, 15 Sep 2017 13:26:02 -0700 Subject: [PATCH 4/4] Rm unnecessary functions in protocol.py --- xcat/cli.py | 2 +- xcat/protocol.py | 37 +------------------------------------ 2 files changed, 2 insertions(+), 37 deletions(-) diff --git a/xcat/cli.py b/xcat/cli.py index ea2b440..722ba96 100644 --- a/xcat/cli.py +++ b/xcat/cli.py @@ -262,7 +262,7 @@ def main(): tradeid = args.arguments[0] checkBuyStatus(tradeid) elif command == "step3": - generate(31) + # generate(31) tradeid = args.arguments[0] checkSellStatus(tradeid) elif command == "step4": diff --git a/xcat/protocol.py b/xcat/protocol.py index 6d13e06..10135ab 100644 --- a/xcat/protocol.py +++ b/xcat/protocol.py @@ -157,27 +157,7 @@ def create_buy_p2sh(trade, commitment, locktime): save(trade) -#### Main functions determining user flow from command line -def buyer_redeem(trade): - userInput.authorize_buyer_redeem(trade) - if trade.sell.get_status() == 'redeemed': - print("You already redeemed the funds and acquired {0} {1}".format(trade.sell.amount, trade.sell.currency)) - exit() - else: - # Buyer redeems seller's funded tx - p2sh = trade.sell.p2sh - currency = trade.sell.currency - # Buy contract is where seller disclosed secret in redeeming - if trade.buy.currency == 'bitcoin': - secret = bitcoinRPC.parse_secret(trade.buy.redeem_tx) - else: - secret = zcashRPC.parse_secret(trade.buy.redeem_tx) - print("Found secret in seller's redeem tx", secret) - redeem_tx = redeem_p2sh(trade.sell, secret) - setattr(trade.sell, 'redeem_tx', redeem_tx) - save(trade) - exit() - +#### Main functions related to user flow from command line def seller_redeem_p2sh(trade, secret): buy = trade.buy userInput.authorize_seller_redeem(buy) @@ -190,21 +170,6 @@ def seller_redeem_p2sh(trade, secret): print("You have redeemed {0} {1}!".format(buy.amount, buy.currency)) return txs -def buyer_fulfill(trade): - buy = trade.buy - sell = trade.sell - buy_p2sh_balance = check_p2sh(buy.currency, buy.p2sh) - sell_p2sh_balance = check_p2sh(sell.currency, sell.p2sh) - - if buy_p2sh_balance == 0: - userInput.authorize_buyer_fulfill(sell_p2sh_balance, sell.currency, buy_p2sh_balance, buy.currency) - print("Buy amt:", buy.amount) - txid = fund_buy_contract(trade) - print("Fund tx txid:", txid) - else: - print("It looks like you've already funded the contract to buy {1}, the amount in escrow in the p2sh is {0}.".format(buy_p2sh_balance, buy.currency)) - print("Please wait for the seller to remove your funds from escrow to complete the trade.") - def initialize_trade(tradeid, **kwargs): trade = Trade() conf = kwargs['conf']