Merge pull request #21 from zcash/refactor-rpc

Refactor rpc
This commit is contained in:
arcalinea 2017-08-25 15:58:27 -07:00 committed by GitHub
commit 8a070461cd
7 changed files with 429 additions and 411 deletions

View File

@ -15,22 +15,25 @@ from bitcoin.core.scripteval import VerifyScript, SCRIPT_VERIFY_P2SH
from bitcoin.wallet import CBitcoinAddress, CBitcoinSecret, P2SHBitcoinAddress, P2PKHBitcoinAddress
from xcat.utils import *
from xcat.zcashRPC import parse_script
SelectParams('testnet')
# SelectParams('regtest')
# TODO: Accurately read user and pw info
bitcoind = bitcoin.rpc.Proxy(timeout=900)
FEE = 0.001*COIN
def validateaddress(addr):
return bitcoind.validateaddress(addr)
class bitcoinProxy():
def __init__(self, network='regtest', timeout=900):
self.network = network
self.timeout = timeout
def find_secret(p2sh, fundtx_input):
txs = bitcoind.call('listtransactions', "*", 20, 0, True)
SelectParams(self.network)
self.bitcoind = bitcoin.rpc.Proxy(timeout=self.timeout)
def validateaddress(self, addr):
return self.bitcoind.validateaddress(addr)
def find_secret(self, p2sh, fundtx_input):
txs = self.bitcoind.call('listtransactions', "*", 20, 0, True)
for tx in txs:
raw = bitcoind.gettransaction(lx(tx['txid']))['hex']
decoded = bitcoind.decoderawtransaction(raw)
raw = self.bitcoind.gettransaction(lx(tx['txid']))['hex']
decoded = self.bitcoind.decoderawtransaction(raw)
print("TXINFO", decoded['vin'][0])
if('txid' in decoded['vin'][0]):
sendid = decoded['vin'][0]['txid']
@ -40,7 +43,7 @@ def find_secret(p2sh, fundtx_input):
print("Redeem transaction with secret not found")
return
def parse_secret(txid):
def parse_secret(self, txid):
raw = zcashd.gettransaction(txid, True)['hex']
decoded = zcashd.call('decoderawtransaction', raw)
scriptSig = decoded['vin'][0]['scriptSig']
@ -50,23 +53,23 @@ def parse_secret(txid):
redeemPubkey = P2PKHBitcoinAddress.from_pubkey(x(pubkey))
return secret
def get_keys(funder_address, redeemer_address):
def get_keys(self, funder_address, redeemer_address):
fundpubkey = CBitcoinAddress(funder_address)
redeempubkey = CBitcoinAddress(redeemer_address)
# fundpubkey = bitcoind.getnewaddress()
# redeempubkey = bitcoind.getnewaddress()
# fundpubkey = self.bitcoind.getnewaddress()
# redeempubkey = self.bitcoind.getnewaddress()
return fundpubkey, redeempubkey
def privkey(address):
bitcoind.dumpprivkey(address)
def privkey(self, address):
self.bitcoind.dumpprivkey(address)
def hashtimelockcontract(funder, redeemer, commitment, locktime):
def hashtimelockcontract(self, funder, redeemer, commitment, locktime):
funderAddr = CBitcoinAddress(funder)
redeemerAddr = CBitcoinAddress(redeemer)
if type(commitment) == str:
commitment = x(commitment)
# h = sha256(secret)
blocknum = bitcoind.getblockcount()
blocknum = self.bitcoind.getblockcount()
print("Current blocknum on Bitcoin: ", blocknum)
redeemblocknum = blocknum + locktime
print("Redeemblocknum on Bitcoin: ", redeemblocknum)
@ -79,29 +82,29 @@ def hashtimelockcontract(funder, redeemer, commitment, locktime):
txin_p2sh_address = CBitcoinAddress.from_scriptPubKey(txin_scriptPubKey)
p2sh = str(txin_p2sh_address)
# Import address at same time you create
bitcoind.importaddress(p2sh, "", False)
self.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):
def fund_htlc(self, p2sh, amount):
send_amount = float(amount) * COIN
# Import address at same time that you fund it
bitcoind.importaddress(p2sh, "", False)
fund_txid = bitcoind.sendtoaddress(p2sh, send_amount)
self.bitcoind.importaddress(p2sh, "", False)
fund_txid = self.bitcoind.sendtoaddress(p2sh, send_amount)
txid = b2x(lx(b2x(fund_txid)))
return txid
# Following two functions are about the same
def check_funds(p2sh):
bitcoind.importaddress(p2sh, "", False)
# Following two functions are about the same
def check_funds(self, p2sh):
self.bitcoind.importaddress(p2sh, "", False)
# Get amount in address
amount = bitcoind.getreceivedbyaddress(p2sh, 0)
amount = self.bitcoind.getreceivedbyaddress(p2sh, 0)
amount = amount/COIN
return amount
def get_fund_status(p2sh):
bitcoind.importaddress(p2sh, "", False)
amount = bitcoind.getreceivedbyaddress(p2sh, 0)
def get_fund_status(self, p2sh):
self.bitcoind.importaddress(p2sh, "", False)
amount = self.bitcoind.getreceivedbyaddress(p2sh, 0)
amount = amount/COIN
print("Amount in bitcoin p2sh: ", amount, p2sh)
if amount > 0:
@ -109,10 +112,10 @@ def get_fund_status(p2sh):
else:
return 'empty'
## TODO: FIX search for p2sh in block
def search_p2sh(block, p2sh):
## TODO: FIX search for p2sh in block
def search_p2sh(self, block, p2sh):
print("Fetching block...")
blockdata = bitcoind.getblock(lx(block))
blockdata = self.bitcoind.getblock(lx(block))
print("done fetching block")
txs = blockdata.vtx
print("txs", txs)
@ -131,31 +134,31 @@ def search_p2sh(block, p2sh):
print("Address to p2sh found in transaction!", addr)
print("Returning from search_p2sh")
def get_tx_details(txid):
def get_tx_details(self, txid):
# must convert txid string to bytes x(txid)
fund_txinfo = bitcoind.gettransaction(lx(txid))
fund_txinfo = self.bitcoind.gettransaction(lx(txid))
return fund_txinfo['details'][0]
def redeem_contract(contract, secret):
def redeem_contract(self, contract, secret):
print("Parsing script for redeem_contract...")
scriptarray = parse_script(contract.redeemScript)
scriptarray = self.parse_script(contract.redeemScript)
redeemblocknum = scriptarray[8]
redeemPubKey = P2PKHBitcoinAddress.from_bytes(x(scriptarray[6]))
refundPubKey = P2PKHBitcoinAddress.from_bytes(x(scriptarray[13]))
p2sh = contract.p2sh
#checking there are funds in the address
amount = check_funds(p2sh)
amount = self.check_funds(p2sh)
if(amount == 0):
print("address ", p2sh, " not funded")
quit()
fundtx = find_transaction_to_address(p2sh)
fundtx = self.find_transaction_to_address(p2sh)
amount = fundtx['amount'] / COIN
# print("Found fund_tx: ", fundtx)
p2sh = P2SHBitcoinAddress(p2sh)
if fundtx['address'] == p2sh:
print("Found {0} in p2sh {1}, redeeming...".format(amount, p2sh))
blockcount = bitcoind.getblockcount()
blockcount = self.bitcoind.getblockcount()
print("\nCurrent blocknum at time of redeem on Zcash:", blockcount)
if blockcount < int(redeemblocknum):
print('redeemPubKey', redeemPubKey)
@ -166,7 +169,7 @@ def redeem_contract(contract, secret):
tx = CMutableTransaction([txin], [txout])
sighash = SignatureHash(zec_redeemScript, tx, 0, SIGHASH_ALL)
# TODO: protect privkey better, separate signing from rawtx creation
privkey = bitcoind.dumpprivkey(redeemPubKey)
privkey = self.bitcoind.dumpprivkey(redeemPubKey)
sig = privkey.sign(sighash) + bytes([SIGHASH_ALL])
preimage = secret.encode('utf-8')
txin.scriptSig = CScript([sig, privkey.pub, preimage, OP_TRUE, zec_redeemScript])
@ -176,49 +179,54 @@ def redeem_contract(contract, secret):
print('Raw redeem transaction hex: ', b2x(tx.serialize()))
VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0, (SCRIPT_VERIFY_P2SH,))
print("Script verified, sending raw transaction...")
txid = bitcoind.sendrawtransaction(tx)
txid = self.bitcoind.sendrawtransaction(tx)
fund_tx = str(fundtx['outpoint'])
redeem_tx = b2x(lx(b2x(txid)))
return {"redeem_tx": redeem_tx, "fund_tx": fund_tx}
else:
print("nLocktime exceeded, refunding")
print('refundPubKey', refundPubKey)
txid = bitcoind.sendtoaddress(refundPubKey, fundtx['amount'] - FEE)
txid = self.bitcoind.sendtoaddress(refundPubKey, fundtx['amount'] - FEE)
fund_tx = str(fundtx['outpoint'])
refund_tx = b2x(lx(b2x(txid)))
return {"refund_tx": refund_tx, "fund_tx": fund_tx}
else:
print("No contract for this p2sh found in database", p2sh)
def find_redeemblocknum(contract):
def parse_script(self, script_hex):
redeemScript = self.bitcoind.call('decodescript', script_hex)
scriptarray = redeemScript['asm'].split(' ')
return scriptarray
def find_redeemblocknum(self, contract):
scriptarray = parse_script(contract.redeemScript)
redeemblocknum = scriptarray[8]
return int(redeemblocknum)
def find_redeemAddr(contract):
def find_redeemAddr(self, contract):
scriptarray = parse_script(contract.redeemScript)
redeemer = scriptarray[6]
redeemAddr = P2PKHBitcoinAddress.from_bytes(x(redeemer))
return redeemAddr
def find_refundAddr(contract):
def find_refundAddr(self, contract):
scriptarray = parse_script(contract.redeemScript)
funder = scriptarray[13]
refundAddr = P2PKHBitcoinAddress.from_bytes(x(funder))
return refundAddr
def find_transaction_to_address(p2sh):
bitcoind.importaddress(p2sh, "", False)
txs = bitcoind.listunspent()
def find_transaction_to_address(self, p2sh):
self.bitcoind.importaddress(p2sh, "", False)
txs = self.bitcoind.listunspent()
for tx in txs:
if tx['address'] == CBitcoinAddress(p2sh):
print("Found tx to p2sh", p2sh)
return tx
def new_bitcoin_addr():
addr = bitcoind.getnewaddress()
def new_bitcoin_addr(self):
addr = self.bitcoind.getnewaddress()
return str(addr)
def generate(num):
blocks = bitcoind.generate(num)
def generate(self, num):
blocks = self.bitcoind.generate(num)
return blocks

View File

@ -24,7 +24,12 @@ def checkSellStatus(tradeid):
secret = db.get_secret(tradeid)
print("Retrieved secret to redeem funds for {0}: {1}".format(tradeid, secret))
txs = seller_redeem_p2sh(trade, secret)
if 'redeem_tx' in txs:
trade.buy.redeem_tx = txs['redeem_tx']
print("Redeem tx: ", txs['redeem_tx'])
elif 'refund_tx' in txs:
trade.buy.redeem_tx = txs['refund_tx']
print("Refund tx: ", txs['refund_tx'])
save_state(trade, tradeid)
# Remove from db? Or just from temporary file storage
cleanup(tradeid)
@ -87,8 +92,12 @@ def checkBuyStatus(tradeid):
if secret != None:
print("Found secret on blockchain in seller's redeem tx: ", secret)
txs = redeem_p2sh(trade.sell, secret)
if 'redeem_tx' in txs:
trade.sell.redeem_tx = txs['redeem_tx']
print("Redeem txid: ", trade.sell.redeem_tx)
elif 'refund_tx' in txs:
trade.sell.redeem_tx = txs['refund_tx']
print("Refund tx: ", txs['refund_tx'])
save_state(trade, tradeid)
print("XCAT trade complete!")
else:
@ -134,6 +143,15 @@ def findtrade(tradeid):
print(trade.toJSON())
return trade
def find_role(contract):
# When regtest created both addrs on same machine, role is both.
if is_myaddr(contract.initiator) and is_myaddr(contract.fulfiller):
return 'test'
elif is_myaddr(contract.initiator):
return 'initiator'
else:
return 'fulfiller'
def checktrade(tradeid):
print("In checktrade")
trade = db.get(tradeid)
@ -183,7 +201,7 @@ def main():
parser.add_argument("arguments", action="store", nargs="*", help="add arguments")
parser.add_argument("-w", "--wormhole", action="store_true", help="Transfer trade data through magic-wormhole")
parser.add_argument("-c", "--conf", action="store", help="Use default trade data in conf file.")
parser.add_argument("-n", "--network", action="store", help="Set network to regtest or mainnet. Defaults to testnet while in beta.")
parser.add_argument("-n", "--network", action="store", help="Set network to regtest or mainnet. Defaults to testnet while in alpha.")
# parser.add_argument("--daemon", "-d", action="store_true", help="Run as daemon process")
args = parser.parse_args()

View File

@ -6,8 +6,6 @@ import json
import ast
from xcat.trades import *
import xcat.bitcoinRPC as bitcoinRPC
db = plyvel.DB('/tmp/xcatDB', create_if_missing=True)
preimageDB = plyvel.DB('/tmp/preimageDB', create_if_missing=True)

View File

@ -1,13 +1,25 @@
import json
import os, sys
from pprint import pprint
import xcat.zcashRPC as zcashRPC
import xcat.bitcoinRPC as bitcoinRPC
from xcat.utils import *
from xcat.trades import Contract, Trade
import xcat.userInput as userInput
import xcat.db as db
from xcat.xcatconf import *
from xcat.bitcoinRPC import bitcoinProxy
from xcat.zcashRPC import zcashProxy
bitcoinRPC = bitcoinProxy()
zcashRPC = zcashProxy()
def is_myaddr(address):
if address[:1] == 'm':
status = bitcoinRPC.validateaddress(address)
else:
status = zcashRPC.validateaddress(address)
status = status['ismine']
# print("Address {0} is mine: {1}".format(address, status))
return status
def find_secret_from_fundtx(currency, p2sh, fundtx):
if currency == 'bitcoin':

View File

@ -1,5 +1,7 @@
from xcat.utils import *
from xcat.db import *
from xcat.bitcoinRPC import bitcoinProxy
from xcat.zcashRPC import zcashProxy
def enter_trade_id():
tradeid = input("Enter a unique identifier for this trade: ")
@ -34,6 +36,8 @@ def authorize_fund_sell(htlcTrade):
response = input("Type 'enter' to allow this program to send funds on your behalf.")
def get_initiator_addresses():
bitcoinRPC = bitcoinProxy()
zcashRPC = zcashProxy()
btc_addr = input("Enter your bitcoin address or press enter to generate one: ")
btc_addr = bitcoinRPC.new_bitcoin_addr()
print(btc_addr)

View File

@ -1,7 +1,5 @@
import hashlib, json, random, binascii
import xcat.trades as trades
import xcat.bitcoinRPC as bitcoinRPC
import xcat.zcashRPC as zcashRPC
import os
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
@ -41,27 +39,6 @@ def jsonformat(trade):
'buy': trade.buyContract.__dict__
}
############################################
#### Role detection utils ####
############################################
def find_role(contract):
# Obviously when regtest created both addrs on same machine, role is both.
if is_myaddr(contract.initiator) and is_myaddr(contract.fulfiller):
return 'test'
elif is_myaddr(contract.initiator):
return 'initiator'
else:
return 'fulfiller'
def is_myaddr(address):
if address[:1] == 'm':
status = bitcoinRPC.validateaddress(address)
else:
status = zcashRPC.validateaddress(address)
status = status['ismine']
# print("Address {0} is mine: {1}".format(address, status))
return status
############################################
########### Preimage utils #################
############################################

View File

@ -16,35 +16,36 @@ from zcash.core.script import CScript, OP_DUP, OP_IF, OP_ELSE, OP_ENDIF, OP_HASH
from zcash.core.scripteval import VerifyScript, SCRIPT_VERIFY_P2SH
from zcash.wallet import CBitcoinAddress, CBitcoinSecret, P2SHBitcoinAddress, P2PKHBitcoinAddress
from xcat.utils import *
from xcat.utils import x2s
SelectParams('testnet')
# SelectParams('regtest')
zcashd = zcash.rpc.Proxy(timeout=900)
FEE = 0.001*COIN
def x2s(hexstring):
"""Convert hex to a utf-8 string"""
return binascii.unhexlify(hexstring).decode('utf-8')
class zcashProxy():
def __init__(self, network='regtest', timeout=900):
self.network = network
self.timeout = timeout
def validateaddress(addr):
return zcashd.validateaddress(addr)
SelectParams(self.network)
self.zcashd = zcash.rpc.Proxy(timeout=self.timeout)
def get_keys(funder_address, redeemer_address):
def validateaddress(self, addr):
return self.zcashd.validateaddress(addr)
def get_keys(self, funder_address, redeemer_address):
fundpubkey = CBitcoinAddress(funder_address)
redeempubkey = CBitcoinAddress(redeemer_address)
return fundpubkey, redeempubkey
def privkey(address):
zcashd.dumpprivkey(address)
def privkey(self, address):
self.zcashd.dumpprivkey(address)
def hashtimelockcontract(funder, redeemer, commitment, locktime):
def hashtimelockcontract(self, funder, redeemer, commitment, locktime):
funderAddr = CBitcoinAddress(funder)
redeemerAddr = CBitcoinAddress(redeemer)
if type(commitment) == str:
commitment = x(commitment)
# h = sha256(secret)
blocknum = zcashd.getblockcount()
blocknum = self.zcashd.getblockcount()
print("Current blocknum on Zcash: ", blocknum)
redeemblocknum = blocknum + locktime
print("Redeemblocknum on Zcash: ", redeemblocknum)
@ -59,29 +60,29 @@ def hashtimelockcontract(funder, redeemer, commitment, locktime):
p2sh = str(txin_p2sh_address)
print("p2sh computed: ", p2sh)
# Import address as soon as you create it
zcashd.importaddress(p2sh, "", False)
self.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):
def fund_htlc(self, p2sh, amount):
send_amount = float(amount)*COIN
# Import addr at same time as you fund
zcashd.importaddress(p2sh, "", False)
fund_txid = zcashd.sendtoaddress(p2sh, send_amount)
self.zcashd.importaddress(p2sh, "", False)
fund_txid = self.zcashd.sendtoaddress(p2sh, send_amount)
txid = b2x(lx(b2x(fund_txid)))
return txid
# Following two functions are about the same
def check_funds(p2sh):
zcashd.importaddress(p2sh, "", False)
# Following two functions are about the same
def check_funds(self, p2sh):
self.zcashd.importaddress(p2sh, "", False)
# Get amount in address
amount = zcashd.getreceivedbyaddress(p2sh, 0)
amount = self.zcashd.getreceivedbyaddress(p2sh, 0)
amount = amount/COIN
return amount
def get_fund_status(p2sh):
zcashd.importaddress(p2sh, "", False)
amount = zcashd.getreceivedbyaddress(p2sh, 0)
def get_fund_status(self, p2sh):
self.zcashd.importaddress(p2sh, "", False)
amount = self.zcashd.getreceivedbyaddress(p2sh, 0)
amount = amount/COIN
print("Amount in zcash p2sh: ", amount, p2sh)
if amount > 0:
@ -89,34 +90,34 @@ def get_fund_status(p2sh):
else:
return 'empty'
def get_tx_details(txid):
fund_txinfo = zcashd.gettransaction(txid)
def get_tx_details(self, txid):
fund_txinfo = self.zcashd.gettransaction(txid)
return fund_txinfo['details'][0]
def find_transaction_to_address(p2sh):
zcashd.importaddress(p2sh, "", False)
txs = zcashd.listunspent(0, 100)
def find_transaction_to_address(self, p2sh):
self.zcashd.importaddress(p2sh, "", False)
txs = self.zcashd.listunspent(0, 100)
for tx in txs:
if tx['address'] == CBitcoinAddress(p2sh):
print("Found tx to p2sh", p2sh)
return tx
def find_secret(p2sh, fundtx_input):
txs = zcashd.call('listtransactions', "*", 20, 0, True)
def find_secret(self, p2sh, fundtx_input):
txs = self.zcashd.call('listtransactions', "*", 20, 0, True)
for tx in txs:
raw = zcashd.gettransaction(lx(tx['txid']))['hex']
decoded = zcashd.decoderawtransaction(raw)
raw = self.zcashd.gettransaction(lx(tx['txid']))['hex']
decoded = self.zcashd.decoderawtransaction(raw)
if('txid' in decoded['vin'][0]):
sendid = decoded['vin'][0]['txid']
if (sendid == fundtx_input ):
print("Found funding tx: ", sendid)
return parse_secret(lx(tx['txid']))
return self.parse_secret(lx(tx['txid']))
print("Redeem transaction with secret not found")
return
def parse_secret(txid):
raw = zcashd.gettransaction(txid, True)['hex']
decoded = zcashd.decoderawtransaction(raw)
def parse_secret(self, txid):
raw = self.zcashd.gettransaction(txid, True)['hex']
decoded = self.zcashd.decoderawtransaction(raw)
scriptSig = decoded['vin'][0]['scriptSig']
asm = scriptSig['asm'].split(" ")
pubkey = asm[1]
@ -124,15 +125,15 @@ def parse_secret(txid):
redeemPubkey = P2PKHBitcoinAddress.from_pubkey(x(pubkey))
return secret
def redeem_contract(contract, secret):
def redeem_contract(self, contract, secret):
# How to find redeemScript and redeemblocknum from blockchain?
p2sh = contract.p2sh
#checking there are funds in the address
amount = check_funds(p2sh)
amount = self.check_funds(p2sh)
if(amount == 0):
print("Address ", p2sh, " not funded")
quit()
fundtx = find_transaction_to_address(p2sh)
fundtx = self.find_transaction_to_address(p2sh)
amount = fundtx['amount'] / COIN
# print("Found fund_tx: ", fundtx)
p2sh = P2SHBitcoinAddress(p2sh)
@ -141,11 +142,11 @@ def redeem_contract(contract, secret):
# Where can you find redeemblocknum in the transaction?
# redeemblocknum = find_redeemblocknum(contract)
blockcount = zcashd.getblockcount()
blockcount = self.zcashd.getblockcount()
print("\nCurrent blocknum at time of redeem on Zcash:", blockcount)
if blockcount < contract.redeemblocknum:
# TODO: parse the script once, up front.
redeemPubKey = find_redeemAddr(contract)
redeemPubKey = self.find_redeemAddr(contract)
print('redeemPubKey', redeemPubKey)
zec_redeemScript = CScript(x(contract.redeemScript))
@ -156,7 +157,7 @@ def redeem_contract(contract, secret):
tx = CMutableTransaction([txin], [txout])
sighash = SignatureHash(zec_redeemScript, tx, 0, SIGHASH_ALL)
# TODO: figure out how to better protect privkey
privkey = zcashd.dumpprivkey(redeemPubKey)
privkey = self.zcashd.dumpprivkey(redeemPubKey)
sig = privkey.sign(sighash) + bytes([SIGHASH_ALL])
print("SECRET", secret)
preimage = secret.encode('utf-8')
@ -165,50 +166,50 @@ def redeem_contract(contract, secret):
print('Raw redeem transaction hex: ', b2x(tx.serialize()))
VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0, (SCRIPT_VERIFY_P2SH,))
print("Script verified, sending raw redeem transaction...")
txid = zcashd.sendrawtransaction(tx)
txid = self.zcashd.sendrawtransaction(tx)
redeem_tx = b2x(lx(b2x(txid)))
fund_tx = str(fundtx['outpoint'])
return {"redeem_tx": redeem_tx, "fund_tx": fund_tx}
else:
print("nLocktime exceeded, refunding")
refundPubKey = find_refundAddr(contract)
refundPubKey = self.find_refundAddr(contract)
print('refundPubKey', refundPubKey)
txid = zcashd.sendtoaddress(refundPubKey, fundtx['amount'] - FEE)
txid = self.zcashd.sendtoaddress(refundPubKey, fundtx['amount'] - FEE)
refund_tx = b2x(lx(b2x(txid)))
fund_tx = str(fundtx['outpoint'])
return {"refund_tx": refund_tx, "fund_tx": fund_tx}
else:
print("No contract for this p2sh found in database", p2sh)
def parse_script(script_hex):
redeemScript = zcashd.decodescript(script_hex)
def parse_script(self, script_hex):
redeemScript = self.zcashd.decodescript(script_hex)
scriptarray = redeemScript['asm'].split(' ')
return scriptarray
def find_redeemblocknum(contract):
def find_redeemblocknum(self, contract):
print("In find_redeemblocknum")
scriptarray = parse_script(contract.redeemScript)
scriptarray = self.parse_script(contract.redeemScript)
print("Returning scriptarray", scriptarray)
redeemblocknum = scriptarray[8]
return int(redeemblocknum)
def find_redeemAddr(contract):
scriptarray = parse_script(contract.redeemScript)
def find_redeemAddr(self, contract):
scriptarray = self.parse_script(contract.redeemScript)
redeemer = scriptarray[6]
redeemAddr = P2PKHBitcoinAddress.from_bytes(x(redeemer))
return redeemAddr
def find_refundAddr(contract):
scriptarray = parse_script(contract.redeemScript)
def find_refundAddr(self, contract):
scriptarray = self.parse_script(contract.redeemScript)
funder = scriptarray[13]
refundAddr = P2PKHBitcoinAddress.from_bytes(x(funder))
return refundAddr
def find_recipient(contract):
def find_recipient(self, contract):
# make this dependent on actual fund tx to p2sh, not contract
txid = contract.fund_tx
raw = zcashd.gettransaction(lx(txid), True)['hex']
decoded = zcashd.decoderawtransaction(raw)
raw = self.zcashd.gettransaction(lx(txid), True)['hex']
decoded = self.zcashd.decoderawtransaction(raw)
scriptSig = decoded['vin'][0]['scriptSig']
print("Decoded", scriptSig)
asm = scriptSig['asm'].split(" ")
@ -221,10 +222,10 @@ def find_recipient(contract):
redeemPubkey = P2PKHBitcoinAddress.from_pubkey(x(pubkey))
print('redeemPubkey', redeemPubkey)
def new_zcash_addr():
addr = zcashd.getnewaddress()
def new_zcash_addr(self):
addr = self.zcashd.getnewaddress()
return str(addr)
def generate(num):
blocks = zcashd.generate(num)
def generate(self, num):
blocks = self.zcashd.generate(num)
return blocks