0

Playing with this script, I'd like to understand how to construct a bip199 htlc and redeem it, however this script fails on the OP_EQUALVERIFY and I can't seem to understand why. I'd like an explanation as to what this script is doing wrong, and how to overcome it so I can apply this knowledge to other bitcoin scripts.

import bitcoin
import bitcoin.rpc
from bitcoin import SelectParams
from bitcoin.core import b2x, lx, b2lx, COIN, COutPoint, CMutableTxOut, CMutableTxIn, CMutableTransaction, Hash160
from bitcoin.core.script import CScript, OP_DUP, OP_IF, OP_ELSE, OP_ENDIF, OP_HASH160, OP_EQUALVERIFY, OP_CHECKSIG, SignatureHash, SIGHASH_ALL
from bitcoin.core.script import OP_DROP, OP_CHECKLOCKTIMEVERIFY, OP_SHA256, OP_TRUE
from bitcoin.core.scripteval import VerifyScript, SCRIPT_VERIFY_P2SH
from bitcoin.wallet import CBitcoinAddress, CBitcoinSecret

import hashlib
from hashlib import sha256

SelectParams('regtest')
proxy = bitcoin.rpc.Proxy()

preimage = bytes(b'preimage'.hex(), 'utf8')
h = sha256(preimage).digest()

recipientpubkey = proxy.getnewaddress()
senderpubkey = proxy.getnewaddress()
seckey = proxy.dumpprivkey(recipientpubkey)

lockduration = 10
blocknum = proxy.getblockcount()
redeemblocknum = blocknum + lockduration

txin_redeemScript = CScript([
            OP_IF,
                OP_SHA256, h, OP_EQUALVERIFY,OP_DUP, OP_HASH160, recipientpubkey,
            OP_ELSE,
                redeemblocknum, OP_CHECKLOCKTIMEVERIFY, OP_DROP, OP_DUP, OP_HASH160, bytes(Hash160(seckey.pub)),
            OP_ENDIF,
                OP_EQUALVERIFY, OP_CHECKSIG
          ])


txin_scriptPubKey = txin_redeemScript.to_p2sh_scriptPubKey()

txin_p2sh_address = CBitcoinAddress.from_scriptPubKey(txin_scriptPubKey)
p2sh = str(txin_p2sh_address)

amount = 1.0*COIN

fund_tx = proxy.sendtoaddress(txin_p2sh_address, amount)

txinfo = proxy.gettransaction(fund_tx)
details = txinfo['details'][0]
vout = details['vout']

txin = CMutableTxIn(COutPoint(fund_tx, vout))

default_fee = 0.001*COIN
txout = CMutableTxOut(amount - default_fee, recipientpubkey.to_scriptPubKey())

tx = CMutableTransaction([txin], [txout])

sighash = SignatureHash(txin_redeemScript, tx, 0, SIGHASH_ALL)

sig = seckey.sign(sighash) + bytes([SIGHASH_ALL])

txin.scriptSig = CScript([sig, seckey.pub, preimage, preimage, txin_redeemScript])

VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0, (SCRIPT_VERIFY_P2SH,))

txid = proxy.sendrawtransaction(tx)

  • It could help if you posted the actual script (hex) that failed. – Coding Enthusiast Apr 2 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.