Пример #1
0
    def refund(self, contract):
        fundtx = self.find_transaction_to_address(contract.p2sh)
        print("Fund tx found in refund: ", fundtx)
        refundPubKey = self.find_refundAddr(contract)
        print('refundPubKey: {0}'.format(refundPubKey))

        redeemScript = CScript(x(contract.redeemScript))
        txin = CMutableTxIn(fundtx['outpoint'])
        txout = CMutableTxOut(fundtx['amount'] - FEE,
                              refundPubKey.to_scriptPubKey())
        # Create the unsigned raw transaction.
        tx = CMutableTransaction([txin], [txout])
        # Set nSequence and nLockTime
        txin.nSequence = 0
        tx.nLockTime = contract.redeemblocknum
        # Create the unsigned raw transaction.
        sighash = SignatureHash(redeemScript, tx, 0, SIGHASH_ALL)
        privkey = self.zcashd.dumpprivkey(refundPubKey)
        sig = privkey.sign(sighash) + bytes([SIGHASH_ALL])
        # Sign without secret
        txin.scriptSig = CScript([sig, privkey.pub, OP_FALSE, redeemScript])
        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, ))
        print("Script verified, sending raw transaction... (NOT)", res)
        txid = self.zcashd.sendrawtransaction(tx)
        refund_tx = b2x(lx(b2x(txid)))
        fund_tx = str(fundtx['outpoint'])
        return {"refund_tx": refund_tx, "fund_tx": fund_tx}
Пример #2
0
 def redeem(self, contract, fundtx, secret):
     # TODO: parse the script once, up front.
     redeemPubKey = self.find_redeemAddr(contract)
     print('redeemPubKey', redeemPubKey)
     zec_redeemScript = CScript(x(contract.redeemScript))
     txin = CMutableTxIn(fundtx['outpoint'])
     txout = CMutableTxOut(fundtx['amount'] - FEE,
                           redeemPubKey.to_scriptPubKey())
     # Create the unsigned raw transaction.
     tx = CMutableTransaction([txin], [txout])
     sighash = SignatureHash(zec_redeemScript, tx, 0, SIGHASH_ALL)
     # TODO: figure out how to better protect privkey
     privkey = self.zcashd.dumpprivkey(redeemPubKey)
     sig = privkey.sign(sighash) + bytes([SIGHASH_ALL])
     print("SECRET", secret)
     preimage = secret.encode('utf-8')
     txin.scriptSig = CScript(
         [sig, privkey.pub, preimage, OP_TRUE, zec_redeemScript])
     txin_scriptPubKey = zec_redeemScript.to_p2sh_scriptPubKey()
     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 = self.zcashd.sendrawtransaction(tx)
     redeem_tx = b2x(lx(b2x(txid)))
     fund_tx = str(fundtx['outpoint'])
     return {"redeem_tx": redeem_tx, "fund_tx": fund_tx}
Пример #3
0
    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 = self.check_funds(p2sh)
        if (amount == 0):
            print("Address ", p2sh, " not funded")
            quit()
        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))

            # Where can you find redeemblocknum in the transaction?
            # redeemblocknum = find_redeemblocknum(contract)
            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 = self.find_redeemAddr(contract)

                print('redeemPubKey', redeemPubKey)
                zec_redeemScript = CScript(x(contract.redeemScript))

                txin = CMutableTxIn(fundtx['outpoint'])
                txout = CMutableTxOut(fundtx['amount'] - FEE,
                                      redeemPubKey.to_scriptPubKey())
                # Create the unsigned raw transaction.
                tx = CMutableTransaction([txin], [txout])
                sighash = SignatureHash(zec_redeemScript, tx, 0, SIGHASH_ALL)
                # TODO: figure out how to better protect privkey
                privkey = self.zcashd.dumpprivkey(redeemPubKey)
                sig = privkey.sign(sighash) + bytes([SIGHASH_ALL])
                print("SECRET", secret)
                preimage = secret.encode('utf-8')
                txin.scriptSig = CScript(
                    [sig, privkey.pub, preimage, OP_TRUE, zec_redeemScript])
                txin_scriptPubKey = zec_redeemScript.to_p2sh_scriptPubKey()
                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 = 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 = self.find_refundAddr(contract)
                print('refundPubKey', refundPubKey)
                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)
Пример #4
0
def redeem_after_timelock(contract):
    print(contract)
    p2sh = contract.p2sh
    fundtx = find_transaction_to_address(p2sh)
    amount = fundtx['amount'] / COIN

    if (fundtx['address'].__str__() != p2sh):
        print("no fund transaction found to the contract p2sh address ", p2sh)
        quit()
    # print("Found fundtx:", fundtx)
    # Parsing redeemblocknum from the redeemscript of the p2sh
    redeemblocknum = find_redeemblocknum(contract)
    blockcount = zcashd.getblockcount()
    print("Current block:", blockcount, "Can redeem from block:",
          redeemblocknum)
    if (still_locked(contract)):
        print("too early for redeeming with timelock try again at block",
              redeemblocknum, "or later")
        return

    print("Found {0} in p2sh {1}, redeeming...".format(amount, p2sh))

    redeemPubKey = find_refundAddr(contract)
    print('refundPubKey', redeemPubKey)

    redeemScript = CScript(x(contract.redeemScript))
    txin = CMutableTxIn(fundtx['outpoint'])
    txout = CMutableTxOut(fundtx['amount'] - FEE,
                          redeemPubKey.to_scriptPubKey())
    # Create the unsigned raw transaction.
    txin.nSequence = 0
    tx = CMutableTransaction([txin], [txout])
    tx.nLockTime = redeemblocknum

    sighash = SignatureHash(redeemScript, tx, 0, SIGHASH_ALL)
    # TODO: figure out how to better protect privkey
    privkey = zcashd.dumpprivkey(redeemPubKey)
    sig = privkey.sign(sighash) + bytes([SIGHASH_ALL])
    txin.scriptSig = CScript([sig, privkey.pub, OP_FALSE, redeemScript])

    # exit()

    # print("txin.scriptSig", b2x(txin.scriptSig))
    txin_scriptPubKey = redeemScript.to_p2sh_scriptPubKey()
    # print('Redeem txhex', b2x(tx.serialize()))
    VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0,
                 (SCRIPT_VERIFY_P2SH, ))
    print("script verified, sending raw tx")
    txid = zcashd.sendrawtransaction(tx)
    print("Txid of submitted redeem tx: ", b2x(lx(b2x(txid))))
    return b2x(lx(b2x(txid)))
Пример #5
0
def redeem_with_secret(contract, secret):

    # How to find redeemScript and redeemblocknum from blockchain?
    # print("Redeeming contract using secret", contract.__dict__)

    p2sh = contract['p2sh']
    minamount = float(contract['amount'])

    # checking there are funds in the address

    amount = check_funds(p2sh)
    if amount < minamount:
        print ('address ', p2sh, ' not sufficiently funded')
        return False
    fundtx = find_transaction_to_address(p2sh)
    amount = fundtx['amount'] / COIN
    p2sh = P2SHBitcoinAddress(p2sh)
    if fundtx['address'] == p2sh:
        print 'Found {0} in p2sh {1}, redeeming...'.format(amount, p2sh)

        redeemPubKey = find_redeemAddr(contract)
        print ('redeemPubKey', redeemPubKey)

        redeemScript = CScript(x(contract['redeemScript']))
        txin = CMutableTxIn(fundtx['outpoint'])
        txout = CMutableTxOut(fundtx['amount'] - FEE,
                              redeemPubKey.to_scriptPubKey())

        # Create the unsigned raw transaction.

        tx = CMutableTransaction([txin], [txout])
        sighash = SignatureHash(redeemScript, tx, 0, SIGHASH_ALL)

        # TODO: figure out how to better protect privkey

        privkey = zcashd.dumpprivkey(redeemPubKey)
        sig = privkey.sign(sighash) + bytes([SIGHASH_ALL])
        print ('SECRET', secret)

        # secret = get_secret()

        preimage = secret.encode('utf-8')
        txin.scriptSig = CScript([sig, privkey.pub, preimage, OP_TRUE,
                                 redeemScript])

        # print("txin.scriptSig", b2x(txin.scriptSig))

        txin_scriptPubKey = redeemScript.to_p2sh_scriptPubKey()

        # print('Redeem txhex', b2x(tx.serialize()))

        VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0,
                     (SCRIPT_VERIFY_P2SH, ))
        print 'script verified, sending raw tx'
        txid = zcashd.sendrawtransaction(tx)
        print ('Txid of submitted redeem tx: ', b2x(lx(b2x(txid))))
        return b2x(lx(b2x(txid)))
    else:
        print ('No contract for this p2sh found in database', p2sh)
Пример #6
0
# ========================= PART 2: ZCASH P2SH FUNDED, REDEEM OR REFUND =========================

# ================= AFTER 48 HRS: ALICE REFUNDS AFTER BOB TIMES OUT =========================
# Mock passage of time -- comment out to test normal redeem condition
# zcashd.generate(25)

if (zcashd.getblockcount() >= redeemblocknum):
    print(
        "Alice -- Bob did not redeem the Zcash you put in escrow within the timeout period, so refunding you..... "
    )
    txin = CMutableTxIn(COutPoint(fund_tx, fund_vout))
    # The default nSequence of FFFFFFFF won't let you redeem when there's a CHECKTIMELOCKVERIFY
    txin.nSequence = 0
    txout = CMutableTxOut(send_amount - FEE, alicepubkey.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
    tx.nLockTime = redeemblocknum
    # Calculate the signature hash for that transaction. Note how the script we use
    # is the redeemScript, not the scriptPubKey. EvalScript() will be evaluating the redeemScript
    sighash = SignatureHash(zec_redeemScript, tx, 0, SIGHASH_ALL)
    sig = alice_seckey.sign(sighash) + bytes([SIGHASH_ALL])
    txin.scriptSig = CScript(
        [sig, alice_seckey.pub, OP_FALSE, zec_redeemScript])
    print("Time lock has passed, Alice redeeming her own tx:")
    print("Refund tx hex:", b2x(tx.serialize()))
    VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0,
                 (SCRIPT_VERIFY_P2SH, ))
    txid = zcashd.sendrawtransaction(tx)
    print("Txid of submitted refund tx: ", b2x(lx(b2x(txid))))
    quit()
Пример #7
0
vout = details['vout']
print('vout', vout)

# Create the txin structure, which includes the outpoint. The scriptSig
# defaults to being empty.
txin = CMutableTxIn(COutPoint(txid, vout))

# Create the txout. This time we create the scriptPubKey from a Bitcoin
# address.
regtest_addr = 'tmMDAio4NC4Y3xXgDMGpco8rWh13dPJt1A6'
output_val = amount - 0.001
txout = CMutableTxOut(output_val,
                      CBitcoinAddress(regtest_addr).to_scriptPubKey())

# Create the unsigned transaction.
tx = CMutableTransaction([txin], [txout])

# Calculate the signature hash for that transaction. Note how the script we use
# is the redeemScript, not the scriptPubKey. That's because when the CHECKSIG
# operation happens EvalScript() will be evaluating the redeemScript, so the
# corresponding SignatureHash() function will use that same script when it
# replaces the scriptSig in the transaction being hashed with the script being
# executed.
sighash = SignatureHash(txin_redeemScript, tx, 0, SIGHASH_ANYONECANPAY)
print('sighash', b2x(sighash))

# privkey = CBitcoinSecret('cNUMK7zPWDq8YLFoARbLiFSgJnZ4jDuJzXxdSebNupzHB8ZSbQ4n')
# Now sign it. We have to append the type of signature we want to the end, in
# this case the usual SIGHASH_ALL.
# sig = privkey.sign(sighash) + bytes([SIGHASH_ANYONECANPAY])
Пример #8
0
def auto_redeem(contract, secret):
    # How to find redeemScript and redeemblocknum from blockchain?
    print("Contract in auto redeem", contract.__dict__)
    p2sh = contract.p2sh
    #checking there are funds in the address
    amount = check_funds(p2sh)
    if (amount == 0):
        print("address ", p2sh, " not funded")
        quit()
    fundtx = find_transaction_to_address(p2sh)
    amount = fundtx['amount'] / COIN
    p2sh = P2SHBitcoinAddress(p2sh)
    if fundtx['address'] == p2sh:
        print("Found {0} in p2sh {1}, redeeming...".format(amount, p2sh))

        # Parsing redeemblocknum from the redeemscript of the p2sh
        redeemblocknum = find_redeemblocknum(contract)
        blockcount = zcashd.getblockcount()
        print("\nCurrent blocknum at time of redeem on Zcash chain:",
              blockcount)
        if blockcount < redeemblocknum:
            redeemPubKey = find_redeemAddr(contract)
            print('redeemPubKey', redeemPubKey)
        else:
            print("nLocktime exceeded, refunding")
            redeemPubKey = find_refundAddr(contract)
            print('refundPubKey', redeemPubKey)
        # redeemPubKey = CBitcoinAddress.from_scriptPubKey(redeemPubKey)
        # exit()

        zec_redeemScript = CScript(x(contract.redeemScript))
        txin = CMutableTxIn(fundtx['outpoint'])
        txout = CMutableTxOut(fundtx['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...
        if blockcount >= redeemblocknum:
            print("\nLocktime exceeded")
            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])
        print("SECRET", secret)
        preimage = secret.encode('utf-8')
        txin.scriptSig = CScript(
            [sig, privkey.pub, preimage, OP_TRUE, zec_redeemScript])

        # exit()

        # print("txin.scriptSig", b2x(txin.scriptSig))
        txin_scriptPubKey = zec_redeemScript.to_p2sh_scriptPubKey()
        # print('Redeem txhex', b2x(tx.serialize()))
        VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0,
                     (SCRIPT_VERIFY_P2SH, ))
        print("script verified, sending raw tx")
        txid = bitcoind.sendrawtransaction(tx)
        print("Txid of submitted redeem tx: ", b2x(lx(b2x(txid))))
        return b2x(lx(b2x(txid)))
    else:
        print("No contract for this p2sh found in database", p2sh)
Пример #9
0
# hex and converts it to bytes.
txid = lx('bff785da9f8169f49be92fa95e31f0890c385bfb1bd24d6b94d7900057c617ae')
vout = 0

# Create the txin structure, which includes the outpoint. The scriptSig
# defaults to being empty.
txin = CMutableTxIn(COutPoint(txid, vout))

# Create the txout. This time we create the scriptPubKey from a Bitcoin
# address.
txout = CMutableTxOut(
    0.0005 * COIN,
    CBitcoinAddress('tmKBPqa8qqKA7vrGq1AaXHSAr9vqa3GczzK').to_scriptPubKey())

# Create the unsigned transaction.
tx = CMutableTransaction([txin], [txout])

# Calculate the signature hash for that transaction. Note how the script we use
# is the redeemScript, not the scriptPubKey. That's because when the CHECKSIG
# operation happens EvalScript() will be evaluating the redeemScript, so the
# corresponding SignatureHash() function will use that same script when it
# replaces the scriptSig in the transaction being hashed with the script being
# executed.
sighash = SignatureHash(txin_redeemScript, tx, 0, SIGHASH_ALL)

# Now sign it. We have to append the type of signature we want to the end, in
# this case the usual SIGHASH_ALL.
sig = seckey.sign(sighash) + bytes([SIGHASH_ALL])

# Set the scriptSig of our transaction input appropriately.
txin.scriptSig = CScript([sig, txin_redeemScript])