Add saving of contracts to json obj

This commit is contained in:
Jay Graber 2017-05-22 21:18:44 -07:00
parent b1a575215f
commit befcc02a54
4 changed files with 24 additions and 21 deletions

View File

@ -38,19 +38,19 @@ def privkey(address):
bitcoind.dumpprivkey(address) bitcoind.dumpprivkey(address)
def hashtimelockcontract(funder, redeemer, secret, locktime): def hashtimelockcontract(funder, redeemer, secret, locktime):
funder = CBitcoinAddress(funder) funderAddr = CBitcoinAddress(funder)
redeemer = CBitcoinAddress(redeemer) redeemerAddr = CBitcoinAddress(redeemer)
h = sha256(secret) h = sha256(secret)
blocknum = bitcoind.getblockcount() blocknum = bitcoind.getblockcount()
redeemblocknum = blocknum + locktime redeemblocknum = blocknum + locktime
zec_redeemScript = CScript([OP_IF, OP_SHA256, h, OP_EQUALVERIFY,OP_DUP, OP_HASH160, 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, redeemerAddr, OP_ELSE, redeemblocknum, OP_CHECKLOCKTIMEVERIFY, OP_DROP, OP_DUP, OP_HASH160,
funder, OP_ENDIF,OP_EQUALVERIFY, OP_CHECKSIG]) funderAddr, OP_ENDIF,OP_EQUALVERIFY, OP_CHECKSIG])
txin_scriptPubKey = zec_redeemScript.to_p2sh_scriptPubKey() txin_scriptPubKey = zec_redeemScript.to_p2sh_scriptPubKey()
# Convert the P2SH scriptPubKey to a base58 Bitcoin address # Convert the P2SH scriptPubKey to a base58 Bitcoin address
txin_p2sh_address = CBitcoinAddress.from_scriptPubKey(txin_scriptPubKey) txin_p2sh_address = CBitcoinAddress.from_scriptPubKey(txin_scriptPubKey)
p2sh = str(txin_p2sh_address) 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): def fund_htlc(p2sh, amount):
send_amount = amount*COIN send_amount = amount*COIN

View File

@ -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} {"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}

23
xcat.py
View File

@ -26,9 +26,9 @@ def get_contract():
contractdb = json.load(data_file) contractdb = json.load(data_file)
return contractdb return contractdb
def save_contract(contract): def save_contract(contracts):
with open('contract.json', 'w') as outfile: with open('contract.json', 'w') as outfile:
json.dump(contract, outfile) json.dump(contracts, outfile)
def check_p2sh(currency, address): def check_p2sh(currency, address):
@ -47,8 +47,8 @@ def set_price():
buy = 'zcash' buy = 'zcash'
sell_amt = input("How much {0} do you want to sell?".format(sell)) 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)) buy_amt = input("How much {0} do you want to receive in exchange?".format(buy))
sell = {'currency': sell, 'amount': 1.2, 'status': 'empty'} sell = {'currency': sell, 'amount': 1.2}
buy = {'currency': buy, 'amount': 2.45, 'status': 'empty'} buy = {'currency': buy, 'amount': 2.45}
trade['sell'] = sell trade['sell'] = sell
trade['buy'] = buy trade['buy'] = buy
save_trade(trade) save_trade(trade)
@ -76,9 +76,11 @@ def initiate_trade():
locktime = 20 # Must be more than first tx locktime = 20 # Must be more than first tx
# Returns contract obj # Returns contract obj
contracts = [] contracts = {}
contract = create_htlc(currency, trade['sell']['initiator'], trade['sell']['fulfiller'], secret, locktime) 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'])) 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.") response = input("Type 'enter' to allow this program to send funds on your behalf.")
@ -200,14 +202,15 @@ if __name__ == '__main__':
trade = get_trade() trade = get_trade()
if role == "i": if role == "i":
if trade['sell']['status'] == 'empty': if 'status' not in trade['sell']:
set_price() set_price()
get_addresses() get_addresses()
initiate_trade() initiate_trade()
print("XCATDB Trade", trade) print("XCATDB Trade", trade)
elif trade['buy']['status'] == 'funded': elif 'status' in trade['sell']:
# Means buyer has already funded the currency the transaction initiator wants to exchange into if trade['sell']['status'] == 'funded':
seller_redeem() # Means buyer has already funded the currency the transaction initiator wants to exchange into
seller_redeem()
else: else:
if trade['sell']['status'] == 'funded': if trade['sell']['status'] == 'funded':
trade = get_trade() trade = get_trade()

View File

@ -35,21 +35,21 @@ def privkey(address):
zcashd.dumpprivkey(address) zcashd.dumpprivkey(address)
def hashtimelockcontract(funder, redeemer, secret, locktime): def hashtimelockcontract(funder, redeemer, secret, locktime):
funder = CBitcoinAddress(funder) funderAddr = CBitcoinAddress(funder)
redeemer = CBitcoinAddress(redeemer) redeemerAddr = CBitcoinAddress(redeemer)
h = sha256(secret) h = sha256(secret)
blocknum = zcashd.getblockcount() blocknum = zcashd.getblockcount()
redeemblocknum = blocknum + locktime redeemblocknum = blocknum + locktime
zec_redeemScript = CScript([OP_IF, OP_SHA256, h, OP_EQUALVERIFY,OP_DUP, OP_HASH160, 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, redeemerAddr, OP_ELSE, redeemblocknum, OP_CHECKLOCKTIMEVERIFY, OP_DROP, OP_DUP, OP_HASH160,
funder, OP_ENDIF,OP_EQUALVERIFY, OP_CHECKSIG]) funderAddr, OP_ENDIF,OP_EQUALVERIFY, OP_CHECKSIG])
print("TX2 Redeem script on Zcash blockchain:", b2x(zec_redeemScript)) print("TX2 Redeem script on Zcash blockchain:", b2x(zec_redeemScript))
txin_scriptPubKey = zec_redeemScript.to_p2sh_scriptPubKey() txin_scriptPubKey = zec_redeemScript.to_p2sh_scriptPubKey()
# Convert the P2SH scriptPubKey to a base58 Bitcoin address # Convert the P2SH scriptPubKey to a base58 Bitcoin address
txin_p2sh_address = CBitcoinAddress.from_scriptPubKey(txin_scriptPubKey) txin_p2sh_address = CBitcoinAddress.from_scriptPubKey(txin_scriptPubKey)
p2sh = str(txin_p2sh_address) p2sh = str(txin_p2sh_address)
# Returning all this to be saved locally in p2sh.json # 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): def fund_htlc(p2sh, amount):
send_amount = amount*COIN send_amount = amount*COIN