lint bitcoinRPC

This commit is contained in:
James Prestwich 2017-09-12 23:44:25 -06:00
parent 2745b46692
commit cdda932dde
No known key found for this signature in database
GPG Key ID: 519E010A79028CCC
1 changed files with 51 additions and 28 deletions

View File

@ -4,13 +4,17 @@ import sys
import bitcoin
import bitcoin.rpc
# from bitcoin import SelectParams
from bitcoin.core import b2x, lx, b2lx, x, COIN, COutPoint, CMutableTxOut, CMutableTxIn, CMutableTransaction, Hash160, CTransaction
from bitcoin.base58 import decode
from bitcoin.core.script import CScript, OP_DUP, OP_IF, OP_ELSE, OP_ENDIF, OP_HASH160, OP_EQUALVERIFY, OP_CHECKSIG, SignatureHash, SIGHASH_ALL, OP_FALSE, OP_DROP, OP_CHECKLOCKTIMEVERIFY, OP_SHA256, OP_TRUE, OP_FALSE
from bitcoin.core import b2x, lx, x, COIN, CMutableTxOut
from bitcoin.core import CMutableTxIn, CMutableTransaction
from bitcoin.core.script import CScript, OP_DUP, OP_IF, OP_ELSE, OP_ENDIF
from bitcoin.core.script import OP_HASH160, OP_EQUALVERIFY, OP_CHECKSIG
from bitcoin.core.script import SignatureHash, SIGHASH_ALL, OP_FALSE, OP_DROP
from bitcoin.core.script import OP_CHECKLOCKTIMEVERIFY, OP_SHA256, OP_TRUE
from bitcoin.core.scripteval import VerifyScript, SCRIPT_VERIFY_P2SH
from bitcoin.wallet import CBitcoinAddress, CBitcoinSecret, P2SHBitcoinAddress, P2PKHBitcoinAddress
from bitcoin.wallet import CBitcoinAddress, P2SHBitcoinAddress
from bitcoin.wallet import P2PKHBitcoinAddress
from xcat.utils import *
import xcat.utils as utils
import logging
if sys.version_info.major < 3:
@ -53,9 +57,9 @@ class bitcoinProxy():
decoded = zcashd.call('decoderawtransaction', raw)
scriptSig = decoded['vin'][0]['scriptSig']
asm = scriptSig['asm'].split(" ")
pubkey = asm[1]
secret = x2s(asm[2])
redeemPubkey = P2PKHBitcoinAddress.from_pubkey(x(pubkey))
# pubkey = asm[1]
secret = utils.x2s(asm[2])
# redeemPubkey = P2PKHBitcoinAddress.from_pubkey(x(pubkey))
return secret
def get_keys(self, funder_address, redeemer_address):
@ -78,18 +82,27 @@ class bitcoinProxy():
print("Current blocknum on Bitcoin: ", blocknum)
redeemblocknum = blocknum + locktime
print("Redeemblocknum on Bitcoin: ", redeemblocknum)
redeemScript = CScript([OP_IF, OP_SHA256, commitment, 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 Bitcoin blockchain: {0}".format(b2x(redeemScript)))
redeemScript = CScript(
[OP_IF, OP_SHA256, commitment, 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 Bitcoin blockchain: "
# "{0}".format(b2x(redeemScript)))
txin_scriptPubKey = redeemScript.to_p2sh_scriptPubKey()
# Convert the P2SH scriptPubKey to a base58 Bitcoin address
txin_p2sh_address = CBitcoinAddress.from_scriptPubKey(txin_scriptPubKey)
txin_p2sh_address = CBitcoinAddress.from_scriptPubKey(
txin_scriptPubKey)
p2sh = str(txin_p2sh_address)
# Import address at same time you create
self.bitcoind.importaddress(p2sh, "", False)
print("p2sh computed", p2sh)
return {'p2sh': p2sh, 'redeemblocknum': redeemblocknum, 'redeemScript': b2x(redeemScript), 'redeemer': redeemer, 'funder': funder, 'locktime': locktime}
return {'p2sh': p2sh,
'redeemblocknum': redeemblocknum,
'redeemScript': b2x(redeemScript),
'redeemer': redeemer,
'funder': funder,
'locktime': locktime}
def fund_htlc(self, p2sh, amount):
send_amount = float(amount) * COIN
@ -117,7 +130,7 @@ class bitcoinProxy():
else:
return 'empty'
## TODO: FIX search for p2sh in block
# TODO: FIX search for p2sh in block
def search_p2sh(self, block, p2sh):
print("Fetching block...")
blockdata = self.bitcoind.getblock(lx(block))
@ -136,7 +149,8 @@ class bitcoinProxy():
for addr in vout['scriptPubKey']['addresses']:
print("Sent to address:", addr)
if addr == p2sh:
print("Address to p2sh found in transaction!", addr)
print("Address to p2sh found in transaction! ",
addr)
print("Returning from search_p2sh")
def get_tx_details(self, txid):
@ -149,9 +163,9 @@ class bitcoinProxy():
scriptarray = self.parse_script(contract.redeemScript)
redeemblocknum = scriptarray[8]
self.redeemPubKey = P2PKHBitcoinAddress.from_bytes(x(scriptarray[6]))
refundPubKey = P2PKHBitcoinAddress.from_bytes(x(scriptarray[13]))
# refundPubKey = P2PKHBitcoinAddress.from_bytes(x(scriptarray[13]))
p2sh = contract.p2sh
#checking there are funds in the address
# checking there are funds in the address
amount = self.check_funds(p2sh)
if(amount == 0):
print("address ", p2sh, " not funded")
@ -178,7 +192,9 @@ class bitcoinProxy():
# TODO: Compare with script on blockchain?
redeemScript = CScript(x(contract.redeemScript))
txin = CMutableTxIn(fundtx['outpoint'])
txout = CMutableTxOut(fundtx['amount'] - FEE, self.redeemPubKey.to_scriptPubKey())
txout = CMutableTxOut(fundtx['amount'] - FEE,
self.redeemPubKey.to_scriptPubKey())
# Create the unsigned raw transaction.
tx = CMutableTransaction([txin], [txout])
sighash = SignatureHash(redeemScript, tx, 0, SIGHASH_ALL)
@ -186,17 +202,19 @@ class bitcoinProxy():
privkey = self.bitcoind.dumpprivkey(self.redeemPubKey)
sig = privkey.sign(sighash) + bytes([SIGHASH_ALL])
preimage = secret.encode('utf-8')
txin.scriptSig = CScript([sig, privkey.pub, preimage, OP_TRUE, redeemScript])
txin.scriptSig = CScript([sig, privkey.pub, preimage,
OP_TRUE, redeemScript])
# print("txin.scriptSig", b2x(txin.scriptSig))
txin_scriptPubKey = redeemScript.to_p2sh_scriptPubKey()
print('Raw redeem transaction hex: ', b2x(tx.serialize()))
VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0, (SCRIPT_VERIFY_P2SH,))
VerifyScript(txin.scriptSig, txin_scriptPubKey,
tx, 0, (SCRIPT_VERIFY_P2SH,))
print("Script verified, sending raw transaction...")
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}
redeem_tx = b2x(lx(b2x(txid)))
return {"redeem_tx": redeem_tx, "fund_tx": fund_tx}
def refund(self, contract):
fundtx = self.find_transaction_to_address(contract.p2sh)
@ -206,21 +224,26 @@ class bitcoinProxy():
redeemScript = CScript(x(contract.redeemScript))
txin = CMutableTxIn(fundtx['outpoint'])
txout = CMutableTxOut(fundtx['amount'] - FEE, refundPubKey.to_scriptPubKey())
txout = CMutableTxOut(fundtx['amount'] - FEE,
refundPubKey.to_scriptPubKey())
# Create the unsigned raw transaction.
tx = CMutableTransaction([txin], [txout])
sighash = SignatureHash(redeemScript, tx, 0, SIGHASH_ALL)
privkey = self.bitcoind.dumpprivkey(refundPubKey)
sig = privkey.sign(sighash) + bytes([SIGHASH_ALL])
# Sign without secret
txin.scriptSig = CScript([sig, privkey.pub, OP_FALSE, redeemScript])
# txin.nSequence = 2185
txin_scriptPubKey = redeemScript.to_p2sh_scriptPubKey()
print('Raw redeem transaction hex: {0}'.format(b2x(tx.serialize())))
res = VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0, (SCRIPT_VERIFY_P2SH,))
res = VerifyScript(txin.scriptSig, txin_scriptPubKey,
tx, 0, (SCRIPT_VERIFY_P2SH,))
print("Script verified, sending raw transaction... (NOT)", res)
txid = self.bitcoind.sendrawtransaction(tx)
refund_tx = b2x(lx(b2x(txid)))
refund_tx = b2x(lx(b2x(txid)))
fund_tx = str(fundtx['outpoint'])
return {"refund_tx": refund_tx, "fund_tx": fund_tx}
@ -230,12 +253,12 @@ class bitcoinProxy():
return scriptarray
def find_redeemblocknum(self, contract):
scriptarray = parse_script(contract.redeemScript)
scriptarray = self.parse_script(contract.redeemScript)
redeemblocknum = scriptarray[8]
return int(redeemblocknum)
def find_redeemAddr(self, contract):
scriptarray = parse_script(contract.redeemScript)
scriptarray = self.parse_script(contract.redeemScript)
redeemer = scriptarray[6]
redeemAddr = P2PKHBitcoinAddress.from_bytes(x(redeemer))
return redeemAddr