From f0b63896488f872a94a33ac7e06d8e84176d4fe6 Mon Sep 17 00:00:00 2001 From: Jay Graber Date: Mon, 22 May 2017 23:59:32 -0700 Subject: [PATCH] Add bitcoin redeem func --- bXcat.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ xcat.py | 13 ++----------- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/bXcat.py b/bXcat.py index 0341af3..9f0fdb0 100644 --- a/bXcat.py +++ b/bXcat.py @@ -65,6 +65,7 @@ def check_funds(p2sh): amount = amount/COIN return amount +## TODO: FIX search for p2sh in block def search_p2sh(block, p2sh): print("Fetching block...") blockdata = bitcoind.getblock(lx(block)) @@ -85,3 +86,48 @@ def search_p2sh(block, p2sh): if addr == p2sh: print("Address to p2sh found in transaction!", addr) print("Returning from search_p2sh") + + +def get_tx_details(txid): + fund_txinfo = bitcoind.gettransaction(fund_tx) + return fund_txinfo['details'][0] + +def redeem(p2sh, action): + contracts = get_contract() + trade = get_trade() + for key in contracts: + if key == p2sh: + contract = contracts[key] + if contract: + print("Redeeming tx in p2sh", p2sh) + # TODO: Have to get tx info from saved contract p2sh + redeemblocknum = contract['redeemblocknum'] + zec_redeemScript = contract['zec_redeemScript'] + + txid = trade['action']['fund_tx'] + details = get_tx_details(txid) + txin = CMutableTxIn(COutPoint(txid, details['vout'])) + redeemPubKey = CBitcoinAddress(contract['redeemer']) + txout = CMutableTxOut(details['amount'] - FEE, redeemPubKey.to_scriptPubKey()) + # Create the unsigned raw transaction. + tx = CMutableTransaction([txin], [txout]) + # nLockTime needs to be at least as large as parameter of CHECKLOCKTIMEVERIFY for script to verify + # TODO: these things like redeemblocknum should really be properties of a tx class... + # Need: redeemblocknum, zec_redeemScript, secret (for creator...), txid, redeemer... + tx.nLockTime = redeemblocknum + sighash = SignatureHash(zec_redeemScript, tx, 0, SIGHASH_ALL) + # TODO: figure out how to better protect privkey? + privkey = bitcoind.dumpprivkey(redeemPubKey) + sig = privkey.sign(sighash) + bytes([SIGHASH_ALL]) + txin.scriptSig = CScript([sig, privkey.pub, contract['secret'], OP_TRUE, zec_redeemScript]) + print("Redeem tx hex:", b2x(tx.serialize())) + + print("txin.scriptSig", b2x(txin.scriptSig)) + print("txin_scriptPubKey", b2x(txin_scriptPubKey)) + print('tx', tx) + 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)))) + else: + print("No contract for this p2sh found in database", p2sh) diff --git a/xcat.py b/xcat.py index 71b8bad..db03791 100644 --- a/xcat.py +++ b/xcat.py @@ -7,11 +7,6 @@ import json import os from pprint import pprint -def delay(): - sleep(1) - return "hi" - - def check_p2sh(currency, address): if currency == 'bitcoin': print("Checking funds in btc p2sh") @@ -190,7 +185,8 @@ if __name__ == '__main__': # Means buyer has already funded the currency the transaction initiator wants to exchange into seller_redeem() else: - if trade['sell']['status'] == 'funded': + # if 'status' not in trade['buy']: + elif trade['sell']['status'] == 'funded': trade = get_trade() buyer_fulfill() # How to monitor if txs are included in blocks -- should use blocknotify and a monitor daemon? @@ -202,8 +198,3 @@ if __name__ == '__main__': buyer_redeem() pprint(get_trade()) - - - # result = delay() - # wait(lambda: result) is result - # print(result)