Remove dead code, rename things

This commit is contained in:
Jay Graber 2017-05-26 15:21:23 -07:00
parent 2699e76f32
commit 3ee8606dcf
9 changed files with 8 additions and 195 deletions

View File

@ -97,74 +97,11 @@ def search_p2sh(block, 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))
return fund_txinfo['details'][0]
def redeem(p2sh, action):
# ensure p2sh is imported
bitcoind.importaddress(p2sh, '', False)
contracts = get_contract()
trade = get_trade()
for key in contracts:
if key == p2sh:
contract = contracts[key]
if contract:
print("Redeeming tx in p2sh", p2sh)
# TODO: Have to get tx info from saved contract p2sh
redeemblocknum = contract['redeemblocknum']
zec_redeemScript = contract['zec_redeemScript']
txid = trade[action]['fund_tx']
details = get_tx_details(txid)
print("Txid for fund tx", txid)
# must be little endian hex
txin = CMutableTxIn(COutPoint(lx(txid), details['vout']))
redeemPubKey = CBitcoinAddress(contract['redeemer'])
amount = trade[action]['amount'] * COIN
print("amount: {0}, fee: {1}".format(amount, FEE))
txout = CMutableTxOut(amount - FEE, redeemPubKey.to_scriptPubKey())
# Create the unsigned raw transaction.
tx = CMutableTransaction([txin], [txout])
# nLockTime needs to be at least as large as parameter of CHECKLOCKTIMEVERIFY for script to verify
# TODO: these things like redeemblocknum should really be properties of a tx class...
# Need: redeemblocknum, zec_redeemScript, secret (for creator...), txid, redeemer...
# Is stored as hex, must convert to bytes
zec_redeemScript = CScript(x(zec_redeemScript))
tx.nLockTime = redeemblocknum
sighash = SignatureHash(zec_redeemScript, tx, 0, SIGHASH_ALL)
# TODO: figure out how to better protect privkey?
privkey = bitcoind.dumpprivkey(redeemPubKey)
sig = privkey.sign(sighash) + bytes([SIGHASH_ALL])
# TODO: Figure out where to store secret preimage securely. Parse from scriptsig of redeemtx
secret = trade['sell']['secret']
preimage = secret.encode('utf-8')
print('preimage', preimage)
# print('zec_redeemScript', zec_redeemScript)
txin.scriptSig = CScript([sig, privkey.pub, preimage, OP_TRUE, zec_redeemScript])
# print("Redeem tx hex:", b2x(tx.serialize()))
# Can only call to_p2sh_scriptPubKey on CScript obj
txin_scriptPubKey = zec_redeemScript.to_p2sh_scriptPubKey()
# print("txin.scriptSig", b2x(txin.scriptSig))
# print("txin_scriptPubKey", b2x(txin_scriptPubKey))
# print('tx', tx)
VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0, (SCRIPT_VERIFY_P2SH,))
print("Script verified, sending raw tx...")
print("Raw tx of prepared redeem tx: ", b2x(tx.serialize()))
txid = bitcoind.sendrawtransaction(tx)
txhex = b2x(lx(b2x(txid)))
print("Txid of submitted redeem tx: ", txhex)
return txhex
else:
print("No contract for this p2sh found in database", p2sh)
# redeems automatically after buyer has funded tx, by scanning for transaction to the p2sh
# i.e., doesn't require buyer telling us fund txid
def auto_redeem(contract, secret):

View File

@ -1,15 +0,0 @@
#!/usr/bin/env python3
import sys
import subprocess
subprocess.Popen("source /home/jay/Zcash/python3-xcat/venv/bin/activate", shell=True)
import bitcoin
import bitcoin.rpc
SelectParams('regtest')
bitcoind = bitcoin.rpc.Proxy()
txid = sys.argv[1]
print("Incoming txid:", txid)
tx = bitcoind.gettransaction(txid, 0)
print(tx)

View File

@ -1 +1 @@
Gm3fgTRH
1nXlLORk

View File

@ -1,3 +1,8 @@
class Trade(object):
def __init__(self, sellContract=None, buyContract=None):
'''Create a new trade with a sell contract and buy contract across two chains'''
self.sellContract = sellContract
self.buyContract = buyContract
class Contract(object):
def __init__(self, data):
@ -19,38 +24,4 @@ class Contract(object):
else:
return 'empty'
# def __init__(self, **kwargs):
# # initiator=None, fulfiller=None, currency=None, p2sh=None, amount=None, preimage=None
# print('kwargs in init', kwargs)
# # print(type(*data))
# '''Create a new hash time-locked contract'''
# self.initiator = kwargs['initiator']
# self.fulfiller = kwargs['fulfiller']
# self.currency = kwargs['currency']
# self.p2sh = kwargs['p2sh']
# self.amount = kwargs['amount']
# if 'preimage' in kwargs:
# self.preimage = kwargs['preimage']
# # have a 'status' property, for empty, funded, refunded, or redeemed
# def __str__(self):
# return self
# how to distinguish buy and sell contracts? use syntax new Contract()? Remember self's pupbkey?
# def createBuy():
# Intitialize a trade and then create the contracts?
class Trade(object):
def __init__(self, sellContract=None, buyContract=None):
'''Create a new trade with a sell contract and buy contract across two chains'''
self.sellContract = sellContract
self.buyContract = buyContract
# other classes; transactions? users?
class Participant(object):
def __init__(self, zcashAddr=None, bitcoinAddr=None):
self.zcashAddr=None
self.bitcoinAddr=None

View File

@ -1,2 +0,0 @@
7af1b1d333431217958032adc11b84278939d400a7368d33bab4e8455b4203f0
7997c2eafeda0815b840bfbd60e5ad70d8879aac252bd594c732a04d6a7b85e9

View File

@ -1,4 +0,0 @@
#!/bin/bash
# /home/jay/Zcash/python3-xcat/protocol/checktx.py $@
echo "$@" >> "/home/jay/Zcash/python3-xcat/protocol/watchdata"

View File

@ -1 +1 @@
{"buy": {"amount": "1.12", "redeemScript": "63a820b21103bc7877943604d301576d57041ed42f81742e68b3afa8fdaf5bec6dfdf58876a9143ea29256c9d2888ca23de42a8b8e69ca2ec235b16702b600b17576a914c5acca6ef39c843c7a9c3ad01b2da95fe2edf5ba6888ac", "p2sh": "t2APNfUCdbD96pxXr4VD6C9fmvFX2JNbezp", "initiator": "tmFRXyju7ANM7A9mg75ZjyhFW1UJEhUPwfQ", "fund_tx": "ab3f3aab5070e890e1136e205668ee348c558c70c3ac2dac7d3d307e956ce776", "fulfiller": "tmTjZSg4pX2Us6V5HttiwFZwj464fD2ZgpY", "redeemblocknum": 182, "currency": "zcash", "redeem_tx": "2054369bc2a4efba472843a3f379416265f274952c3bdce0623415378c93511c"}, "sell": {"amount": "0.5", "redeemScript": "63a820b21103bc7877943604d301576d57041ed42f81742e68b3afa8fdaf5bec6dfdf58876a9147788b4511a25fba1092e67b307a6dcdb6da125d967029200b17576a914c7043e62a7391596116f54f6a64c8548e97d3fd96888ac", "p2sh": "2N6jDZzUcTicfsXM4ogWSMvYQXqwzMmT1d6", "initiator": "myfFr5twPYNwgeXyjCmGcrzXtCmfmWXKYp", "fund_tx": "97f4a5f61d4569e65dc84303734708f5c383f3f347bbf8d95ed00b4c5ac9b5c4", "fulfiller": "mrQzUGU1dwsWRx5gsKKSDPNtrsP65vCA3Z", "redeemblocknum": 146, "currency": "bitcoin", "redeem_tx": "b9ecc56289489b29b41d3277d1478ed9271eeecf140ac1a14c8b6bd5893d168f"}}
{"sell": {"redeemScript": "63a8208002f14ffc3869269ba5b233d3c934f37115c8821c590da9a28e4df6b44e773d8876a9147788b4511a25fba1092e67b307a6dcdb6da125d967029200b17576a914c7043e62a7391596116f54f6a64c8548e97d3fd96888ac", "redeem_tx": "ce654c459da4dae859aaf0e88ff8c427ad06874b56f0b332ae158914c67011f0", "p2sh": "2NDymuzi7wFP2SEcwicq3qBXqz2qVAyp9jJ", "currency": "bitcoin", "fund_tx": "ef4cb435c7d7e5fcf128538ae652d5f9920384f63aa39b5f29f6bc2f0fb1b0ea", "redeemblocknum": 146, "amount": "0.5", "fulfiller": "mrQzUGU1dwsWRx5gsKKSDPNtrsP65vCA3Z", "initiator": "myfFr5twPYNwgeXyjCmGcrzXtCmfmWXKYp"}, "buy": {"redeemScript": "63a8208002f14ffc3869269ba5b233d3c934f37115c8821c590da9a28e4df6b44e773d8876a9143ea29256c9d2888ca23de42a8b8e69ca2ec235b16702b600b17576a914c5acca6ef39c843c7a9c3ad01b2da95fe2edf5ba6888ac", "redeem_tx": "71fb28287ac315ad890f342b0b154f13895c836c6c7bff511c8d58bb6da0a752", "p2sh": "t26pfk2JKC4XzNLdcmXwrpPMTyb13SDWDjV", "currency": "zcash", "fund_tx": "828afa314f8ba0f75e1f3a808fbf39d57b0b2c71a445b90f6ac3b24bd8eddc3e", "redeemblocknum": 182, "amount": "1.12", "fulfiller": "tmTjZSg4pX2Us6V5HttiwFZwj464fD2ZgpY", "initiator": "tmFRXyju7ANM7A9mg75ZjyhFW1UJEhUPwfQ"}}

12
xcat.py
View File

@ -6,7 +6,7 @@ from time import sleep
import json
import os, sys
from pprint import pprint
from contract import Contract, Trade, Participant
from trades import Contract, Trade
import userInput
def check_p2sh(currency, address):
@ -78,16 +78,6 @@ def redeem_p2sh(contract, secret):
res = zXcat.auto_redeem(contract, secret)
return res
# def check_blocks(p2sh):
# # blocks = []
# with open('watchdata', 'r') as infile:
# for line in infile:
# res = bXcat.search_p2sh(line.strip('\n'), p2sh)
# # blocks.append(line.strip('\n'))
# # print(blocks)
# # for block in blocks:
# # res = bXcat.search_p2sh(block, p2sh)
def print_trade(role):
print("\nTrade status for {0}:".format(role))
trade = get_trade()

View File

@ -123,70 +123,6 @@ def parse_secret(txid):
print(secret)
return secret
def redeem(p2sh, action):
contracts = get_contract()
trade = get_trade()
print("p2sh", p2sh)
for key in contracts:
print(key)
if key == p2sh:
contract = contracts[key]
if contract:
print("Redeeming tx in p2sh", p2sh)
# TODO: Have to get tx info from saved contract p2sh
redeemblocknum = contract['redeemblocknum']
zec_redeemScript = contract['redeemScript']
txid = trade[action]['fund_tx']
details = get_tx_details(txid)
print("Txid for fund tx", txid)
# must be little endian hex
txin = CMutableTxIn(COutPoint(lx(txid), details['vout']))
redeemPubKey = CBitcoinAddress(contract['redeemer'])
amount = trade[action]['amount'] * COIN
print("amount: {0}, fee: {1}".format(amount, FEE))
txout = CMutableTxOut(amount - FEE, redeemPubKey.to_scriptPubKey())
# Create the unsigned raw transaction.
tx = CMutableTransaction([txin], [txout])
# nLockTime needs to be at least as large as parameter of CHECKLOCKTIMEVERIFY for script to verify
# TODO: these things like redeemblocknum should really be properties of a tx class...
# Need: redeemblocknum, zec_redeemScript, secret (for creator...), txid, redeemer...
# Is stored as hex, must convert to bytes
zec_redeemScript = CScript(x(zec_redeemScript))
tx.nLockTime = redeemblocknum
sighash = SignatureHash(zec_redeemScript, tx, 0, SIGHASH_ALL)
# TODO: figure out how to better protect privkey?
privkey = zcashd.dumpprivkey(redeemPubKey)
sig = privkey.sign(sighash) + bytes([SIGHASH_ALL])
# TODO: Figure out where to store secret preimage securely. Parse from scriptsig of redeemtx
secret = trade['sell']['secret']
preimage = secret.encode('utf-8')
print('preimage', preimage)
# print('zec_redeemScript', zec_redeemScript)
txin.scriptSig = CScript([sig, privkey.pub, preimage, OP_TRUE, zec_redeemScript])
# print("Redeem tx hex:", b2x(tx.serialize()))
# Can only call to_p2sh_scriptPubKey on CScript obj
txin_scriptPubKey = zec_redeemScript.to_p2sh_scriptPubKey()
# print("txin.scriptSig", b2x(txin.scriptSig))
# print("txin_scriptPubKey", b2x(txin_scriptPubKey))
# print('tx', tx)
VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0, (SCRIPT_VERIFY_P2SH,))
print("Script verified, sending raw tx...")
print("Raw tx of prepared redeem tx: ", b2x(tx.serialize()))
txid = zcashd.sendrawtransaction(tx)
txhex = b2x(lx(b2x(txid)))
print("Txid of submitted redeem tx: ", txhex)
return txhex
else:
print("No contract for this p2sh found in database", p2sh)
# redeems automatically after buyer has funded tx, by scanning for transaction to the p2sh
# i.e., doesn't require buyer telling us fund txid