def make_htlc(data):
    contract = {}
    print('making htlc')
    funderAddr = CBitcoinAddress(data['initiator'])
    redeemerAddr = CBitcoinAddress(data['fulfiller'])
    blocknum = zcashd.getblockcount()
    print("Current blocknum", blocknum)
    redeemblocknum = blocknum + int(data['timeLock'])
    print("redeemblocknum", redeemblocknum)
    # secret = get_secret()
    # print(secret)
    secret = data['secret']
    hash_of_secret = sha256(secret)
    print(b2x(hash_of_secret))
    print(type(hash_of_secret))
    print("REDEEMBLOCKNUM ZCASH", redeemblocknum)
    zec_redeemScript = CScript([
        OP_IF, OP_SHA256, hash_of_secret, 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 Zcash blockchain:",
          b2x(zec_redeemScript))
    txin_scriptPubKey = zec_redeemScript.to_p2sh_scriptPubKey()
    # Convert the P2SH scriptPubKey to a base58 Bitcoin address
    txin_p2sh_address = CBitcoinAddress.from_scriptPubKey(txin_scriptPubKey)
    p2sh = str(txin_p2sh_address)
    # Returning all this to be saved locally in contract.json
    contract['hash_of_secret'] = b2x(hash_of_secret)
    contract['redeemblocknum'] = redeemblocknum
    contract['redeemScript'] = b2x(zec_redeemScript)
    contract['p2sh'] = p2sh
    save_contract(contract)
    return contract
Exemple #2
0
def hashtimelockcontract(funder, redeemer, secret, locktime):
    funderAddr = CBitcoinAddress(funder)
    redeemerAddr = CBitcoinAddress(redeemer)
    h = sha256(secret)
    blocknum = zcashd.getblockcount()
    print("Current blocknum", blocknum)
    redeemblocknum = blocknum + locktime
    print("REDEEMBLOCKNUM ZCASH", redeemblocknum)
    zec_redeemScript = CScript([
        OP_IF, OP_SHA256, h, 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 Zcash blockchain:",
          b2x(zec_redeemScript))
    txin_scriptPubKey = zec_redeemScript.to_p2sh_scriptPubKey()
    # Convert the P2SH scriptPubKey to a base58 Bitcoin address
    txin_p2sh_address = CBitcoinAddress.from_scriptPubKey(txin_scriptPubKey)
    p2sh = str(txin_p2sh_address)
    # Returning all this to be saved locally in p2sh.json
    return {
        'p2sh': p2sh,
        'redeemblocknum': redeemblocknum,
        'redeemScript': b2x(zec_redeemScript),
        'redeemer': redeemer,
        'funder': funder
    }
Exemple #3
0
 def hashtimelockcontract(self, funder, redeemer, commitment, locktime):
     funderAddr = CBitcoinAddress(funder)
     redeemerAddr = CBitcoinAddress(redeemer)
     if type(commitment) == str:
         commitment = x(commitment)
     # h = sha256(secret)
     blocknum = self.zcashd.getblockcount()
     print("Current blocknum on Zcash: ", blocknum)
     redeemblocknum = blocknum + locktime
     print("Redeemblocknum on Zcash: ", redeemblocknum)
     # can rm op_dup and op_hash160 if you replace addrs with pubkeys (as raw hex/bin data?), and can rm last op_equalverify (for direct pubkey comparison)
     zec_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 Zcash blockchain: ", b2x(zec_redeemScript))
     txin_scriptPubKey = zec_redeemScript.to_p2sh_scriptPubKey()
     # Convert the P2SH scriptPubKey to a base58 Bitcoin address
     txin_p2sh_address = CBitcoinAddress.from_scriptPubKey(
         txin_scriptPubKey)
     p2sh = str(txin_p2sh_address)
     print("p2sh computed: ", p2sh)
     # Import address as soon as you create it
     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 payment_ack(serialized_Payment_message):
    """Generates a PaymentACK object, captures client refund address and returns a message"""

    pao = o.PaymentACK()
    pao.payment.ParseFromString(serialized_Payment_message)
    pao.memo = 'String shown to user after payment confirmation'

    refund_address = CBitcoinAddress.from_scriptPubKey(
        CScript(pao.payment.refund_to[0].script))

    sds_pa = pao.SerializeToString()

    open('sds_pa_blob', 'wb').write(sds_pa)
    headers = {
        'Content-Type': 'application/bitcoin-payment',
        'Accept': 'application/bitcoin-paymentack'
    }
    http_response_object = urllib2.Request('file:sds_pa_blob', None, headers)

    return http_response_object
Exemple #5
0
# ========================= LOCKTIME SCRIPT CREATION =========================
lockduration = 20  # Must be more than first tx
blocknum = zcashd.getblockcount()
redeemblocknum = blocknum + lockduration
zec_redeemScript = CScript([
    OP_IF, OP_SHA256, h, OP_EQUALVERIFY, OP_DUP, OP_HASH160, bobpubkey,
    OP_ELSE, redeemblocknum, OP_CHECKLOCKTIMEVERIFY, OP_DROP, OP_DUP,
    OP_HASH160, alicepubkey, OP_ENDIF, OP_EQUALVERIFY, OP_CHECKSIG
])
print("TX2 Redeem script on Zcash blockchain:", b2x(zec_redeemScript))

# ========================= TX1: CREATE BITCOIN P2SH FROM SCRIPT =========================
txin_scriptPubKey = zec_redeemScript.to_p2sh_scriptPubKey()
# Convert the P2SH scriptPubKey to a base58 Bitcoin address
txin_p2sh_address = CBitcoinAddress.from_scriptPubKey(txin_scriptPubKey)
p2sh = str(txin_p2sh_address)
print('Alice -- send funds to this p2sh address to initiate atomic swap:',
      p2sh)

response = input(
    "Alice -- Type 'enter' to allow zbxcat to fund the Zcash p2sh on your behalf:"
)
send_amount = 10.0 * COIN
fund_tx = zcashd.sendtoaddress(txin_p2sh_address, send_amount)
print(
    'Bob -- Alice send fund tx to the Zcash p2sh. Please wait for confirmation. Txid:',
    b2x(lx(b2x(fund_tx))))

# ========================= CONFIRM ZCASH FUNDING TX TO P2SH =========================
zcashd.importaddress(p2sh)