This commit is contained in:
Ariel Gabizon 2017-07-22 12:54:53 +02:00
parent df8f74cccb
commit cb607bd168
6 changed files with 49 additions and 64 deletions

32
api.py
View File

@ -1,7 +1,7 @@
import zXcat
import bXcat
from xcat import *
from zcash.core import b2x
print("Starting test of xcat...")
def get_initiator_addresses():
@ -22,7 +22,7 @@ def get_fulfiller_addresses():
def seller_init():
print("SELLER INITIATING CONTRACTS")
print("======+====================")
trade = get_buyer_trade()
trade = Trade()
# Get amounts
amounts = {"sell": {"currency": "bitcoin", "amount": "0.01"}, "buy": {"currency": "zcash", "amount": "1.12"}}
sell = amounts['sell']
@ -52,11 +52,14 @@ def seller_init():
buyer_lock_increment = 3
sell.redeemblocknum= compute_redeemblocknum(sell.currency, seller_lock_increment)
buy.redeemblocknum = compute_redeemblocknum(buy.currency, buyer_lock_increment)
sell.hash_of_secret = hash_of_secret
buy.hash_of_secret = hash_of_secret
sell.hash_of_secret = b2x(hash_of_secret)
buy.hash_of_secret = b2x(hash_of_secret)
sell = create_and_import_p2sh(sell)
buy = create_and_import_p2sh(buy)
sell.hash_of_secret = b2x(hash_of_secret)
buy.hash_of_secret = b2x(hash_of_secret)
trade.sellContract = sell
trade.buyContract = buy
@ -69,26 +72,31 @@ def buyer_init():
trade = get_init()
trade.sellContract = create_and_import_p2sh(trade.sellContract)
trade.buyContract = create_and_import_p2sh(trade.buyContract)
save_buyer_trade()
save_buyer_trade(trade)
def seller_fund():
print("SELLER FUNDING SELL CONTRACT")
PRINT("============================")
trade = get_seller_trade()
txid = fund_sell_contract(trade)
print("============================")
trade = get_init()
trade.sellContract = fund_contract(trade.sellContract)
print("fund txid on ", trade.sellContract.currency, " chain is ", trade.sellContract.fund_txid)
save_seller_trade(trade)
def buyer_fund():
print("BUYER FUNDING BUY CONTRACT")
print("==========================")
trade = get_buyer_trade()
txid = fund_buy_contract()
buy = trade.buyContract
trade = get_init()
sell = trade.sellContract
# buy_p2sh_balance = check_p2sh(buy.currency, buy.p2sh)
# first check that seller funded as they should
sell_p2sh_balance = check_p2sh(sell.currency, sell.p2sh)
if (sell_p2sh_balance < float(sell.amount)):
raise ValueError("Sell p2sh not funded, buyer cannot redeem")
trade.buyContract = fund_contract(trade.buyContract)
print("fund txid on ", trade.buyContract.currency, " chain is ", trade.buyContract.fund_txid)
save_buyer_trade(trade)
def seller_redeem():

View File

@ -16,8 +16,6 @@ from bitcoin.wallet import CBitcoinAddress, CBitcoinSecret, P2SHBitcoinAddress,
from utils import *
import zcash
import zcash.rpc
import pprint, json
# SelectParams('testnet')
@ -25,10 +23,9 @@ SelectParams('regtest')
bitcoind = bitcoin.rpc.Proxy()
FEE = 0.001*COIN
zcashd = zcash.rpc.Proxy()
def import_address(address):
zcashd.importaddress(address, "", False)
bitcoind.importaddress(address, "", False)
def parse_secret(txid):
decoded = bitcoind.getrawtransaction(lx(txid), 1)
@ -49,13 +46,13 @@ def privkey(address):
def hashtimelockcontract(contract):
funderAddr = CBitcoinAddress(contract.funder)
redeemerAddr = CBitcoinAddress(contract.redeemer)
h = contract.hash_of_secret
h = x(contract.hash_of_secret)
redeemblocknum = contract.redeemblocknum
print("REDEEMBLOCKNUM ZCASH", redeemblocknum)
print("REDEEMBLOCKNUM BITCOIN", redeemblocknum)
btc_redeemscript = CScript([OP_IF, OP_SHA256, h, OP_EQUALVERIFY,OP_DUP, OP_HASH160,
redeemerAddr, OP_ELSE, redeemblocknum, OP_CHECKLOCKTIMEVERIFY, OP_DROP, OP_DUP, OP_HASH160,
funderAddr, OP_ENDIF,OP_EQUALVERIFY, OP_CHECKSIG])
print("Redeem script for p2sh contract on Zcash blockchain:", b2x(btc_redeemscript))
print("Redeem script for p2sh contract on bitcoin blockchain:", b2x(btc_redeemscript))
txin_scriptPubKey = btc_redeemscript.to_p2sh_scriptPubKey()
# Convert the P2SH scriptPubKey to a base58 Bitcoin address
txin_p2sh_address = CBitcoinAddress.from_scriptPubKey(txin_scriptPubKey)
@ -73,6 +70,13 @@ def fund_htlc(p2sh, amount):
print("funding btc sell address:", txid)
return txid
def fund_contract(contract):
send_amount = float(contract.amount)*COIN
fund_txid = bitcoind.sendtoaddress(contract.p2sh, send_amount)
contract.fund_txid = b2x(lx(b2x(fund_txid)))
return contract
def check_funds(p2sh):
bitcoind.importaddress(p2sh, "", False)
# Get amount in address
@ -81,28 +85,6 @@ def check_funds(p2sh):
print("Amount in bitcoin address ", p2sh, ":",amount)
return amount
## TODO: FIX search for p2sh in block
def search_p2sh(block, p2sh):
print("Fetching block...")
blockdata = 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 get_tx_details(txid):
# must convert txid string to bytes x(txid)
fund_txinfo = bitcoind.gettransaction(lx(txid))
@ -223,7 +205,7 @@ def redeem_after_timelock(contract):
# takes hex and returns array of decoded script op codes
def parse_script(script_hex):
redeemscript = zcashd.decodescript(script_hex)
redeemscript = bitcoind.decodescript(script_hex)
scriptarray = redeemscript['asm'].split(' ')
return scriptarray

View File

@ -1 +1 @@
RteCNOgA
ix5h0rBj

View File

@ -25,11 +25,11 @@ def save_trade(trade):
def save_seller_trade(trade):
with open('sellertrade.json', 'w') as outfile:
json.dump(trade, outfile)
json.dump(jsonformat(trade), outfile)
def save_buyer_trade(trade):
with open('buyertrade.json', 'w') as outfile:
json.dump(trade, outfile)
json.dump(jsonformat(trade), outfile)
def save_init(trade):
with open('init.json', 'w') as outfile:
@ -55,7 +55,7 @@ def get_trade():
return trade
def get_seller_trade():
with open('sellertrade.json') as data_file:
with open('init.json') as data_file:
# try:
xcatdb = json.load(data_file)
sellContract = trades.Contract(xcatdb['sell'])

18
xcat.py
View File

@ -48,19 +48,13 @@ def fund_htlc(currency, p2sh, amount):
txid = zXcat.fund_htlc(p2sh, amount)
return txid
def fund_buy_contract(trade):
buy = trade.buyContract
txid = fund_htlc(buy.currency, buy.p2sh, buy.amount)
setattr(trade.buyContract, 'fund_tx', txid)
save(trade)
return txid
def fund_sell_contract(trade):
sell = trade.sellContract
txid = fund_htlc(sell.currency, sell.p2sh, sell.amount)
setattr(trade.sellContract, 'fund_tx', txid)
save(trade)
return txid
def fund_contract(contract):
if contract.currency == 'bitcoin':
contract = bXcat.fund_contract(contract)
else:
contract = zXcat.fund_contract(contract)
return contract
# updates the contract with the p2sh address and redeemscript generated according to the data in the contract

View File

@ -38,7 +38,7 @@ def privkey(address):
def hashtimelockcontract(contract):
funderAddr = CBitcoinAddress(contract.funder)
redeemerAddr = CBitcoinAddress(contract.redeemer)
h = contract.hash_of_secret
h = x(contract.hash_of_secret)
redeemblocknum = contract.redeemblocknum
print("REDEEMBLOCKNUM ZCASH", redeemblocknum)
zec_redeemscript = CScript([OP_IF, OP_SHA256, h, OP_EQUALVERIFY,OP_DUP, OP_HASH160,
@ -55,11 +55,12 @@ def hashtimelockcontract(contract):
# Returning all this to be saved locally in p2sh.json
return contract
def fund_htlc(p2sh, amount):
send_amount = float(amount)*COIN
fund_txid = zcashd.sendtoaddress(p2sh, send_amount)
txid = b2x(lx(b2x(fund_txid)))
return txid
def fund_contract(contract):
send_amount = float(contract.amount)*COIN
fund_txid = zcashd.sendtoaddress(contract.p2sh, send_amount)
# contract.fund_txid = ""
contract.fund_txid = b2x(lx(b2x(fund_txid)))
return contract
def check_funds(p2sh):
zcashd.importaddress(p2sh, "", False) #Ariel: changed this to true