diff --git a/xcat/bitcoinRPC.py b/xcat/bitcoinRPC.py index cfa650d..b8d240c 100644 --- a/xcat/bitcoinRPC.py +++ b/xcat/bitcoinRPC.py @@ -53,13 +53,13 @@ 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] + # pubkey = asm[1] # secret = utils.x2s(asm[2]) - # # redeemPubkey = P2PKHBitcoinAddress.from_pubkey(x(pubkey)) + # redeemPubkey = P2PKHBitcoinAddress.from_pubkey(x(pubkey)) # return secret def get_keys(self, funder_address, redeemer_address): @@ -131,27 +131,23 @@ class bitcoinProxy(): return 'empty' # TODO: FIX search for p2sh in block - # def search_p2sh(self, block, p2sh): - # print("Fetching block...") - # blockdata = self.bitcoind.getblock(lx(block)) - # print("done fetching block") - # txs = blockdata.vtx - # 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) - # for vout in rawtx['vout']: - # if 'addresses' in vout['scriptPubKey']: - # for addr in vout['scriptPubKey']['addresses']: - # print("Sent to address:", addr) - # if addr == p2sh: - # print("Address to p2sh found in transaction! ", - # addr) - # print("Returning from search_p2sh") + def search_p2sh(self, block, p2sh): + print("Fetching block...") + blockdata = self.bitcoind.getblock(lx(block)) + print("done fetching block") + txs = blockdata.vtx + print("txs", txs) + for tx in txs: + txhex = b2x(tx.serialize()) + txhex = txhex + '00' + rawtx = self.bitcoind.call('decoderawtransaction', txhex) + for vout in rawtx['vout']: + if 'addresses' in vout['scriptPubKey']: + for addr in vout['scriptPubKey']['addresses']: + print("Sent to address:", addr) + if addr == p2sh: + print("Address to p2sh found in transaction!", addr) + print("Returning from search_p2sh") def get_tx_details(self, txid): # must convert txid string to bytes x(txid) @@ -178,7 +174,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: @@ -229,6 +225,9 @@ class bitcoinProxy(): # 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/cli.py b/xcat/cli.py index 389ca69..5190b57 100644 --- a/xcat/cli.py +++ b/xcat/cli.py @@ -360,8 +360,8 @@ def main(): checkBuyStatus(tradeid) elif command == "step3": - protocol = Protocol() - protocol.generate(31) + # protocol = Protocol() + # protocol.generate(31) tradeid = args.arguments[0] checkSellStatus(tradeid) diff --git a/xcat/protocol.py b/xcat/protocol.py index db6cb62..dae278b 100644 --- a/xcat/protocol.py +++ b/xcat/protocol.py @@ -171,28 +171,6 @@ class Protocol(): raise ValueError('Currency not recognized: %s', currency) return secret - # Main functions determining user flow from command line - def buyer_redeem(self, 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 = self.bitcoinRPC.parse_secret(trade.buy.redeem_tx) - else: - secret = self.zcashRPC.parse_secret(trade.buy.redeem_tx) - print("Found secret in seller's redeem tx", secret) - redeem_tx = self.redeem_p2sh(trade.sell, secret) - setattr(trade.sell, 'redeem_tx', redeem_tx) - utils.save(trade) - exit() - def seller_redeem_p2sh(self, trade, secret): buy = trade.buy userInput.authorize_seller_redeem(buy) @@ -208,29 +186,6 @@ class Protocol(): "{0} {1}!".format(buy.amount, buy.currency)) return txs - def buyer_fulfill(self, trade): - buy = trade.buy - sell = trade.sell - buy_p2sh_balance = self.check_p2sh(buy.currency, buy.p2sh) - sell_p2sh_balance = self.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 = self.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.") - self.print_trade('buyer') - def initialize_trade(self, tradeid, **kwargs): trade = Trade() conf = kwargs['conf'] diff --git a/xcat/zcashRPC.py b/xcat/zcashRPC.py index 6a49b96..aa03f99 100644 --- a/xcat/zcashRPC.py +++ b/xcat/zcashRPC.py @@ -235,12 +235,15 @@ class zcashProxy(): 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,