From befcc02a54693b59be86c97f659a8df19811b6c9 Mon Sep 17 00:00:00 2001 From: Jay Graber Date: Mon, 22 May 2017 21:18:44 -0700 Subject: [PATCH] Add saving of contracts to json obj --- bXcat.py | 10 +++++----- xcat.json | 2 +- xcat.py | 23 +++++++++++++---------- zXcat.py | 10 +++++----- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/bXcat.py b/bXcat.py index 47c2a13..0341af3 100644 --- a/bXcat.py +++ b/bXcat.py @@ -38,19 +38,19 @@ def privkey(address): bitcoind.dumpprivkey(address) def hashtimelockcontract(funder, redeemer, secret, locktime): - funder = CBitcoinAddress(funder) - redeemer = CBitcoinAddress(redeemer) + funderAddr = CBitcoinAddress(funder) + redeemerAddr = CBitcoinAddress(redeemer) h = sha256(secret) blocknum = bitcoind.getblockcount() redeemblocknum = blocknum + locktime zec_redeemScript = CScript([OP_IF, OP_SHA256, h, OP_EQUALVERIFY,OP_DUP, OP_HASH160, - redeemer, OP_ELSE, redeemblocknum, OP_CHECKLOCKTIMEVERIFY, OP_DROP, OP_DUP, OP_HASH160, - funder, OP_ENDIF,OP_EQUALVERIFY, OP_CHECKSIG]) + redeemerAddr, OP_ELSE, redeemblocknum, OP_CHECKLOCKTIMEVERIFY, OP_DROP, OP_DUP, OP_HASH160, + funderAddr, OP_ENDIF,OP_EQUALVERIFY, OP_CHECKSIG]) txin_scriptPubKey = zec_redeemScript.to_p2sh_scriptPubKey() # Convert the P2SH scriptPubKey to a base58 Bitcoin address txin_p2sh_address = CBitcoinAddress.from_scriptPubKey(txin_scriptPubKey) p2sh = str(txin_p2sh_address) - return p2sh + return {'p2sh': p2sh, 'redeemblocknum': redeemblocknum, 'zec_redeemScript': b2x(zec_redeemScript), 'redeemer': redeemer, 'funder': funder} def fund_htlc(p2sh, amount): send_amount = amount*COIN diff --git a/xcat.json b/xcat.json index bb57fa0..be02f9d 100644 --- a/xcat.json +++ b/xcat.json @@ -1 +1 @@ -{"buy": {"currency": "zcash", "amount": 2.45, "p2sh": "t2HGf3VPaDvacd94UZiYipwxQ1E5BuVwyFT", "initiator": "tmWnA7ypaCtpG7KhEWfr5XA1Rpm8521yMfX", "fulfiller": "tmTqTsBFkeKXyawHfZfcAZQY47xEhpEbo1E"}, "sell": {"currency": "bitcoin", "amount": 1.2, "initiator": "mpxpkAUatZR45rdrWQSjkUK7z9LyeSMoEr", "p2sh": "2N1Up8hp196ZXknnxGAUYHXdnA6eHMLg3xM", "fulfiller": "mg1EHcpWyErmGhMvpZ9ch2qzFE7ZTKuaEy", "fund_tx": "a5d3b35eb57effda9ce359e10ec10a6320d4b0e5fca2712105a65f0541f474e4"}, "id": 1} \ No newline at end of file +{"sell": {"p2sh": "2N1Up8hp196ZXknnxGAUYHXdnA6eHMLg3xM", "secret": "test", "initiator": "mpxpkAUatZR45rdrWQSjkUK7z9LyeSMoEr", "currency": "bitcoin", "status": "funded", "fulfiller": "mg1EHcpWyErmGhMvpZ9ch2qzFE7ZTKuaEy", "fund_tx": "5c21b70b22d8c33c7706233c1319689a17ed6346d6b1f7f9f44cb442fdeca014", "amount": 1.2}, "buy": {"initiator": "tmWnA7ypaCtpG7KhEWfr5XA1Rpm8521yMfX", "currency": "zcash", "amount": 2.45, "fulfiller": "tmTqTsBFkeKXyawHfZfcAZQY47xEhpEbo1E"}, "id": 1} \ No newline at end of file diff --git a/xcat.py b/xcat.py index c67eea8..06c851c 100644 --- a/xcat.py +++ b/xcat.py @@ -26,9 +26,9 @@ def get_contract(): contractdb = json.load(data_file) return contractdb -def save_contract(contract): +def save_contract(contracts): with open('contract.json', 'w') as outfile: - json.dump(contract, outfile) + json.dump(contracts, outfile) def check_p2sh(currency, address): @@ -47,8 +47,8 @@ def set_price(): buy = 'zcash' sell_amt = input("How much {0} do you want to sell?".format(sell)) buy_amt = input("How much {0} do you want to receive in exchange?".format(buy)) - sell = {'currency': sell, 'amount': 1.2, 'status': 'empty'} - buy = {'currency': buy, 'amount': 2.45, 'status': 'empty'} + sell = {'currency': sell, 'amount': 1.2} + buy = {'currency': buy, 'amount': 2.45} trade['sell'] = sell trade['buy'] = buy save_trade(trade) @@ -76,9 +76,11 @@ def initiate_trade(): locktime = 20 # Must be more than first tx # Returns contract obj - contracts = [] + contracts = {} contract = create_htlc(currency, trade['sell']['initiator'], trade['sell']['fulfiller'], secret, locktime) - contracts.append(contract) + sell_p2sh = contract['p2sh'] + contracts[contract['p2sh']] = contract + save_contract(contracts) print('To complete your sell, send {0} {1} to this p2sh: {2}'.format(trade['sell']['amount'], currency, contract['p2sh'])) response = input("Type 'enter' to allow this program to send funds on your behalf.") @@ -200,14 +202,15 @@ if __name__ == '__main__': trade = get_trade() if role == "i": - if trade['sell']['status'] == 'empty': + if 'status' not in trade['sell']: set_price() get_addresses() initiate_trade() print("XCATDB Trade", trade) - elif trade['buy']['status'] == 'funded': - # Means buyer has already funded the currency the transaction initiator wants to exchange into - seller_redeem() + elif 'status' in trade['sell']: + if trade['sell']['status'] == 'funded': + # Means buyer has already funded the currency the transaction initiator wants to exchange into + seller_redeem() else: if trade['sell']['status'] == 'funded': trade = get_trade() diff --git a/zXcat.py b/zXcat.py index 4b3090d..4b8dcd8 100644 --- a/zXcat.py +++ b/zXcat.py @@ -35,21 +35,21 @@ def privkey(address): zcashd.dumpprivkey(address) def hashtimelockcontract(funder, redeemer, secret, locktime): - funder = CBitcoinAddress(funder) - redeemer = CBitcoinAddress(redeemer) + funderAddr = CBitcoinAddress(funder) + redeemerAddr = CBitcoinAddress(redeemer) h = sha256(secret) blocknum = zcashd.getblockcount() redeemblocknum = blocknum + locktime zec_redeemScript = CScript([OP_IF, OP_SHA256, h, OP_EQUALVERIFY,OP_DUP, OP_HASH160, - redeemer, OP_ELSE, redeemblocknum, OP_CHECKLOCKTIMEVERIFY, OP_DROP, OP_DUP, OP_HASH160, - funder, OP_ENDIF,OP_EQUALVERIFY, OP_CHECKSIG]) + redeemerAddr, OP_ELSE, redeemblocknum, OP_CHECKLOCKTIMEVERIFY, OP_DROP, OP_DUP, OP_HASH160, + funderAddr, OP_ENDIF,OP_EQUALVERIFY, OP_CHECKSIG]) print("TX2 Redeem script on Zcash blockchain:", b2x(zec_redeemScript)) txin_scriptPubKey = zec_redeemScript.to_p2sh_scriptPubKey() # Convert the P2SH scriptPubKey to a base58 Bitcoin address txin_p2sh_address = CBitcoinAddress.from_scriptPubKey(txin_scriptPubKey) p2sh = str(txin_p2sh_address) # Returning all this to be saved locally in p2sh.json - return {'p2sh': p2sh, 'redeemblocknum': redeemblocknum, 'zec_redeemScript': zec_redeemScript, 'redeemer': redeemer, 'funder': funder} + return {'p2sh': p2sh, 'redeemblocknum': redeemblocknum, 'zec_redeemScript': b2x(zec_redeemScript), 'redeemer': redeemer, 'funder': funder} def fund_htlc(p2sh, amount): send_amount = amount*COIN