Parse secret for buyer

This commit is contained in:
Jay Graber 2017-07-31 19:13:46 -07:00
parent 0fcad69b06
commit d8fcd7e95b
7 changed files with 51 additions and 34 deletions

View File

@ -37,7 +37,9 @@ def parse_secret(txid):
print("Decoded", decoded)
# decoded = bitcoind.decoderawtransaction(raw)
asm = decoded['vin'][0]['scriptSig']['asm'].split(" ")
print(asm[2])
secret = asm[2]
print("Found secret: ", secret)
return secret
def get_keys(funder_address, redeemer_address):
fundpubkey = CBitcoinAddress(funder_address)
@ -68,14 +70,16 @@ def hashtimelockcontract(funder, redeemer, commitment, locktime):
# Convert the P2SH scriptPubKey to a base58 Bitcoin address
txin_p2sh_address = CBitcoinAddress.from_scriptPubKey(txin_scriptPubKey)
p2sh = str(txin_p2sh_address)
# Import address at same time you create
bitcoind.importaddress(p2sh, "", False)
print("p2sh computed", p2sh)
return {'p2sh': p2sh, 'redeemblocknum': redeemblocknum, 'redeemScript': b2x(redeemScript), 'redeemer': redeemer, 'funder': funder, 'locktime': locktime}
def fund_htlc(p2sh, amount):
send_amount = float(amount) * COIN
fund_txid = bitcoind.sendtoaddress(p2sh, send_amount)
# Import address at same time that you fund it
bitcoind.importaddress(p2sh, "", False)
fund_txid = bitcoind.sendtoaddress(p2sh, send_amount)
txid = b2x(lx(b2x(fund_txid)))
return txid
@ -291,8 +295,7 @@ def find_transaction_to_address(p2sh):
def new_bitcoin_addr():
addr = bitcoind.getnewaddress()
print('new btc addr', addr.to_scriptPubKey)
return addr.to_scriptPubKey()
return str(addr)
def generate(num):
blocks = bitcoind.generate(num)

View File

@ -15,8 +15,7 @@ def save_state(trade, tradeid):
def checkSellStatus(tradeid):
trade = db.get(tradeid)
if trade.buy.get_status() == 'funded':
input("Authorize retrieve secret:")
secret = get_secret()
secret = userInput.retrieve_password()
print("SECRET found in checksellactions", secret)
trade = seller_redeem_p2sh(trade, secret)
print("TRADE SUCCESSFULLY REDEEMED", trade)
@ -48,18 +47,20 @@ def checkBuyStatus(tradeid):
# else:
# print("Compiled p2sh for htlc does not match what seller sent.")
elif trade.buy.get_status() == 'redeemed':
# TODO: secret parsing
# secret = parse_secret(trade.buy.currency, trade.buy.redeem_tx)
secret = get_secret()
print("Found secret", secret)
txid = auto_redeem_p2sh(trade.sell, secret)
print("TXID after buyer redeem", txid)
print("XCAT trade complete!")
secret = parse_secret(trade.buy.currency, trade.buy.redeem_tx)
if secret != None:
print("Found secret", secret)
txid = auto_redeem_p2sh(trade.sell, secret)
print("TXID after buyer redeem", txid)
print("XCAT trade complete!")
else:
print("Secret not found in redeemtx")
# Import a trade in hex, and save to db
def importtrade(hexstr, tradeid):
trade = x2s(hexstr)
trade = db.instantiate(trade)
print(trade.toJSON())
save_state(trade, tradeid)
# Export a trade by its tradeid
@ -72,7 +73,7 @@ def exporttrade(tradeid):
def findtrade(tradeid):
trade = db.get(tradeid)
print(trade)
print(trade.toJSON())
return trade
def checktrade(tradeid):

View File

@ -6,6 +6,8 @@ import json
import ast
from xcat.trades import *
import xcat.bitcoinRPC as bitcoinRPC
db = plyvel.DB('/tmp/testdb', create_if_missing=True)
# Takes dict or obj, saves json str as bytes
@ -43,6 +45,10 @@ def instantiate(trade):
# testtrade = instantiate(testtrade)
# print(testtrade)
# addr = bitcoinRPC.new_bitcoin_addr()
# print(str(addr))
# print(b('thing'))
# hexstr = get(txid)
# print(x2s(hexstr))

View File

@ -97,6 +97,7 @@ def parse_secret(chain, txid):
secret = bitcoinRPC.parse_secret(txid)
else:
secret = zcashRPC.parse_secret(txid)
return secret
#### Main functions determining user flow from command line
def buyer_redeem(trade):
@ -161,6 +162,7 @@ def seller_init(trade):
init_addrs = userInput.get_initiator_addresses()
sell['initiator'] = init_addrs[sell_currency]
buy['initiator'] = init_addrs[buy_currency]
fulfill_addrs = userInput.get_fulfiller_addresses()
sell['fulfiller'] = fulfill_addrs[sell_currency]
buy['fulfiller'] = fulfill_addrs[buy_currency]

View File

@ -13,16 +13,15 @@ class SimpleTestCase(unittest.TestCase):
def test_importtrade(self):
trade = cli.importtrade(self.__class__.hexstr, 'test')
class CliTest(SimpleTestCase):
def test_findtrade(self):
trade = cli.findtrade('test')
# class CliTest(SimpleTestCase):
# def test_findtrade(self):
# trade = cli.findtrade('test')
# def test_newtrade(self):
# cli.newtrade('test2')
# cli.checkBuyStatus('test2')
# cli.checkSellStatus('test2')
# cli.checkBuyStatus('test2')
def test_newtrade(self):
cli.newtrade('test2')
cli.checkBuyStatus('test2')
cli.checkSellStatus('test2')
cli.checkBuyStatus('test2')
if __name__ == '__main__':
unittest.main()

View File

@ -48,13 +48,13 @@ def authorize_fund_sell(htlcTrade):
response = input("Type 'enter' to allow this program to send funds on your behalf.")
def get_initiator_addresses():
btc_addr = input("Enter your bitcoin address: ")
# btc_addr = bXcat.new_bitcoin_addr()
btc_addr = 'mihDbm4QGzDUqYtjAaq71RSjXeA1ptDpkY'
btc_addr = input("Enter your bitcoin address or press enter to generate one: ")
btc_addr = bitcoinRPC.new_bitcoin_addr()
# btc_addr = 'mgRG44X4PQC1ZCA4V654UZjJGJ3pxbApj2'
print(btc_addr)
zec_addr = input("Enter your zcash address: ")
# zec_addr = zXcat.new_zcash_addr()
zec_addr = 'tmCDNXibkr2QoG7QxtwqeyfYcXBBZoqUwQm'
zec_addr = input("Enter your zcash address or press enter to generate one: ")
zec_addr = zcashRPC.new_zcash_addr()
# zec_addr = 'tmLZu7MdjNdA6vbPTNTwdsZo91LnnrVTYB5'
print(zec_addr)
addresses = {'bitcoin': btc_addr, 'zcash': zec_addr}
return addresses
@ -62,11 +62,11 @@ def get_initiator_addresses():
def get_fulfiller_addresses():
btc_addr = input("Enter the bitcoin address of the party you want to trade with: ")
# btc_addr = bXcat.new_bitcoin_addr()
btc_addr = 'mk8k2dpYEGpffbr6JtDg1797prTM9UTTaw'
btc_addr = 'mgRG44X4PQC1ZCA4V654UZjJGJ3pxbApj2'
print(btc_addr)
zec_addr = input("Enter the zcash address of the party you want to trade with: ")
# zec_addr = zXcat.new_zcash_addr()
zec_addr = 'tmAz1aYqBzqvRnhCdp38e8q45rkJZTERGh9'
zec_addr = 'tmLZu7MdjNdA6vbPTNTwdsZo91LnnrVTYB5'
print(zec_addr)
addresses = {'bitcoin': btc_addr, 'zcash': zec_addr}
return addresses

View File

@ -24,6 +24,11 @@ SelectParams('regtest')
zcashd = zcash.rpc.Proxy(service_url="http://user:password@127.0.0.1:18232")
FEE = 0.001*COIN
def x2s(hexstring):
"""Convert hex to a utf-8 string"""
return binascii.unhexlify(hexstring).decode('utf-8')
def validateaddress(addr):
return zcashd.validateaddress(addr)
@ -58,14 +63,16 @@ def hashtimelockcontract(funder, redeemer, commitment, locktime):
txin_p2sh_address = CBitcoinAddress.from_scriptPubKey(txin_scriptPubKey)
p2sh = str(txin_p2sh_address)
print("p2sh computed", p2sh)
# Import address as soon as you create it
zcashd.importaddress(p2sh, "", False)
# Returning all this to be saved locally in p2sh.json
return {'p2sh': p2sh, 'redeemblocknum': redeemblocknum, 'redeemScript': b2x(zec_redeemScript), 'redeemer': redeemer, 'funder': funder, 'locktime': locktime}
def fund_htlc(p2sh, amount):
send_amount = float(amount)*COIN
fund_txid = zcashd.sendtoaddress(p2sh, send_amount)
# Import addr at same time as you fund
zcashd.importaddress(p2sh, "", False)
fund_txid = zcashd.sendtoaddress(p2sh, send_amount)
txid = b2x(lx(b2x(fund_txid)))
return txid
@ -301,8 +308,7 @@ def find_recipient(contract):
def new_zcash_addr():
addr = zcashd.getnewaddress()
print('new ZEC addr', addr.to_p2sh_scriptPubKey)
return addr.to_scriptPubKey()
return str(addr)
def generate(num):
blocks = zcashd.generate(num)