zbxcat/xcat/protocol.py

200 lines
7.3 KiB
Python
Raw Normal View History

2017-05-22 18:00:34 -07:00
import json
2017-05-23 13:34:48 -07:00
import os, sys
2017-05-22 18:00:34 -07:00
from pprint import pprint
2017-07-31 13:51:54 -07:00
import xcat.zcashRPC as zcashRPC
import xcat.bitcoinRPC as bitcoinRPC
2017-07-28 17:23:32 -07:00
from xcat.utils import *
from xcat.trades import Contract, Trade
2017-07-31 13:51:54 -07:00
import xcat.userInput as userInput
2017-05-22 18:00:34 -07:00
def find_secret_from_fundtx(currency, p2sh, fundtx):
print("Fund tx in protocol.py", fundtx)
if currency == 'bitcoin':
secret = bitcoinRPC.find_secret(p2sh, fundtx)
else:
secret = zcashRPC.find_secret(p2sh, fundtx)
return secret
def import_addrs(trade):
check_fund_status(trade.sell.currency, trade.sell.p2sh)
check_fund_status(trade.buy.currency, trade.buy.p2sh)
2017-05-22 18:00:34 -07:00
def check_p2sh(currency, address):
if currency == 'bitcoin':
2017-05-23 14:12:01 -07:00
print("Checking funds in Bitcoin p2sh")
2017-07-28 17:23:32 -07:00
return bitcoinRPC.check_funds(address)
2017-05-22 18:00:34 -07:00
else:
2017-05-23 14:12:01 -07:00
print("Checking funds in Zcash p2sh")
2017-07-28 17:23:32 -07:00
return zcashRPC.check_funds(address)
2017-05-22 18:00:34 -07:00
def check_fund_status(currency, address):
if currency == 'bitcoin':
print("Checking funds in Bitcoin p2sh")
return bitcoinRPC.get_fund_status(address)
else:
print("Checking funds in Zcash p2sh")
return zcashRPC.get_fund_status(address)
2017-07-26 13:23:12 -07:00
def create_htlc(currency, funder, redeemer, commitment, locktime):
2017-07-28 13:57:44 -07:00
print("Commitment in create_htlc", commitment)
2017-05-22 18:00:34 -07:00
if currency == 'bitcoin':
2017-07-28 17:23:32 -07:00
sell_p2sh = bitcoinRPC.hashtimelockcontract(funder, redeemer, commitment, locktime)
2017-05-22 18:00:34 -07:00
else:
2017-07-28 17:23:32 -07:00
sell_p2sh = zcashRPC.hashtimelockcontract(funder, redeemer, commitment, locktime)
2017-05-22 18:00:34 -07:00
return sell_p2sh
def fund_htlc(currency, p2sh, amount):
if currency == 'bitcoin':
2017-07-28 17:23:32 -07:00
txid = bitcoinRPC.fund_htlc(p2sh, amount)
2017-05-22 18:00:34 -07:00
else:
2017-07-28 17:23:32 -07:00
txid = zcashRPC.fund_htlc(p2sh, amount)
print("fund_htlc txid", txid )
2017-05-22 18:00:34 -07:00
return txid
2017-07-26 13:23:12 -07:00
#
# def fund_buy_contract(trade):
# buy = trade.buy
# txid = fund_htlc(buy.currency, buy.p2sh, buy.amount)
# setattr(trade.buy, 'fund_tx', txid)
# save(trade)
# return txid
def fund_contract(contract):
txid = fund_htlc(contract.currency, contract.p2sh, contract.amount)
print("TXID coming back from fund_contract", txid)
return txid
2017-05-22 18:00:34 -07:00
def fund_sell_contract(trade):
sell = trade.sell
txid = fund_htlc(sell.currency, sell.p2sh, sell.amount)
setattr(trade.sell, 'fund_tx', txid)
save(trade)
return txid
2017-05-26 13:15:52 -07:00
2017-07-26 13:23:12 -07:00
def create_sell_p2sh(trade, commitment, locktime):
# CREATE SELL CONTRACT
sell = trade.sell
2017-07-26 13:23:12 -07:00
contract = create_htlc(sell.currency, sell.initiator, sell.fulfiller, commitment, locktime)
2017-05-26 13:15:52 -07:00
print("sell contract", contract)
setattr(trade.sell, 'p2sh', contract['p2sh'])
setattr(trade.sell, 'redeemScript', contract['redeemScript'])
setattr(trade.sell, 'redeemblocknum', contract['redeemblocknum'])
2017-07-28 13:57:44 -07:00
setattr(trade.buy, 'locktime', contract['locktime'])
save(trade)
2017-05-22 18:00:34 -07:00
2017-07-26 13:23:12 -07:00
def create_buy_p2sh(trade, commitment, locktime):
2017-05-26 13:15:52 -07:00
## CREATE BUY CONTRACT
buy = trade.buy
print("Now creating buy contract on the {0} blockchain where you will wait for the buyer to send funds...".format(buy.currency))
2017-07-26 13:23:12 -07:00
print("HTLC DETAILS", buy.currency, buy.fulfiller, buy.initiator, commitment, locktime)
buy_contract = create_htlc(buy.currency, buy.fulfiller, buy.initiator, commitment, locktime)
2017-05-26 13:15:52 -07:00
print("Buy contract", buy_contract)
setattr(trade.buy, 'p2sh', buy_contract['p2sh'])
setattr(trade.buy, 'redeemScript', buy_contract['redeemScript'])
setattr(trade.buy, 'redeemblocknum', buy_contract['redeemblocknum'])
2017-07-28 13:57:44 -07:00
setattr(trade.buy, 'locktime', buy_contract['locktime'])
print("Now contact the buyer and tell them to send funds to this p2sh: ", trade.buy.p2sh)
2017-05-26 13:15:52 -07:00
save(trade)
2017-05-22 18:00:34 -07:00
2017-07-26 13:23:12 -07:00
def redeem_p2sh(contract, secret):
currency = contract.currency
if currency == 'bitcoin':
2017-07-28 17:23:32 -07:00
res = bitcoinRPC.redeem_contract(contract, secret)
2017-07-26 13:23:12 -07:00
else:
2017-07-28 17:23:32 -07:00
res = zcashRPC.redeem_contract(contract, secret)
2017-07-26 13:23:12 -07:00
return res
def parse_secret(chain, txid):
if chain == 'bitcoin':
2017-07-28 17:23:32 -07:00
secret = bitcoinRPC.parse_secret(txid)
2017-07-26 13:23:12 -07:00
else:
2017-07-28 17:23:32 -07:00
secret = zcashRPC.parse_secret(txid)
2017-07-31 19:13:46 -07:00
return secret
2017-05-26 13:15:52 -07:00
#### 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))
2017-05-26 13:15:52 -07:00
exit()
else:
2017-05-26 13:15:52 -07:00
# Buyer redeems seller's funded tx
p2sh = trade.sell.p2sh
currency = trade.sell.currency
2017-05-26 13:15:52 -07:00
# Buy contract is where seller disclosed secret in redeeming
if trade.buy.currency == 'bitcoin':
2017-07-28 17:23:32 -07:00
secret = bitcoinRPC.parse_secret(trade.buy.redeem_tx)
2017-05-26 13:15:52 -07:00
else:
2017-07-28 17:23:32 -07:00
secret = zcashRPC.parse_secret(trade.buy.redeem_tx)
2017-05-26 13:15:52 -07:00
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)
2017-05-26 13:15:52 -07:00
exit()
2017-07-26 13:23:12 -07:00
def seller_redeem_p2sh(trade, secret):
buy = trade.buy
userInput.authorize_seller_redeem(buy)
if trade.sell.get_status() == 'redeemed':
print("You already redeemed the funds and acquired {0} {1}".format(buy.amount, buy.currency))
exit()
else:
# Seller redeems buyer's funded tx (contract in p2sh)
txs = redeem_p2sh(trade.buy, 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.")
print_trade('buyer')
2017-07-28 13:57:44 -07:00
def seller_init(trade):
2017-07-31 17:06:58 -07:00
# TODO: pass in amounts, or get from cli. {"amounts": {"buy": {}, "sell": {}}}
amounts = userInput.get_trade_amounts()
2017-05-26 13:15:52 -07:00
sell = amounts['sell']
buy = amounts['buy']
sell_currency = sell['currency']
buy_currency = buy['currency']
# Get addresses
init_addrs = userInput.get_initiator_addresses()
2017-05-26 13:15:52 -07:00
sell['initiator'] = init_addrs[sell_currency]
buy['initiator'] = init_addrs[buy_currency]
2017-07-31 19:13:46 -07:00
fulfill_addrs = userInput.get_fulfiller_addresses()
2017-05-26 13:15:52 -07:00
sell['fulfiller'] = fulfill_addrs[sell_currency]
buy['fulfiller'] = fulfill_addrs[buy_currency]
# initializing contract classes with addresses, currencies, and amounts
trade.sell = Contract(sell)
trade.buy = Contract(buy)
print(trade.sell.__dict__)
print(trade.buy.__dict__)
2017-05-26 13:15:52 -07:00
secret = userInput.create_password()
2017-07-26 13:23:12 -07:00
save_secret(secret)
hash_of_secret = sha256(secret)
# TODO: Implement locktimes and mock block passage of time
2017-08-01 15:33:49 -07:00
sell_locktime = 20
buy_locktime = 10 # Must be more than first tx
print("Creating pay-to-script-hash for sell contract...")
2017-07-26 13:23:12 -07:00
create_sell_p2sh(trade, hash_of_secret, sell_locktime)
2017-07-28 13:57:44 -07:00
create_buy_p2sh(trade, hash_of_secret, buy_locktime)
2017-07-26 13:23:12 -07:00
trade.commitment = b2x(hash_of_secret)
2017-07-28 13:57:44 -07:00
print("TRADE after seller init", trade.toJSON())
return trade