src/btc/utils.js
function buildHashTimeLockContract(network, xHash, destH160Addr, revokerH160Addr, lockTime) {
  const bitcoinNetwork = bitcoin.networks[network];

  const redeemScript = bitcoin.script.compile([
    bitcoin.opcodes.OP_IF,
    bitcoin.opcodes.OP_SHA256,
    Buffer.from(xHash, 'hex'),
    bitcoin.opcodes.OP_EQUALVERIFY,
    bitcoin.opcodes.OP_DUP,
    bitcoin.opcodes.OP_HASH160,
    Buffer.from(hex.stripPrefix(destH160Addr), 'hex'),

    bitcoin.opcodes.OP_ELSE,
    bitcoin.script.number.encode(lockTime),
    bitcoin.opcodes.OP_CHECKLOCKTIMEVERIFY,
    bitcoin.opcodes.OP_DROP,
    bitcoin.opcodes.OP_DUP,
    bitcoin.opcodes.OP_HASH160,
    Buffer.from(hex.stripPrefix(revokerH160Addr), 'hex'),
    bitcoin.opcodes.OP_ENDIF,

    bitcoin.opcodes.OP_EQUALVERIFY,
    bitcoin.opcodes.OP_CHECKSIG,
  ]);

  const addressPay = bitcoin.payments.p2sh({
    redeem: { output: redeemScript, network: bitcoinNetwork },
    network: bitcoinNetwork,
  });

  const { address } = addressPay;

  return {
    // contract
    address,
    redeemScript: redeemScript.toString('hex'),

    // params
    xHash,
    lockTime,
    redeemer: hex.ensurePrefix(destH160Addr),
    revoker: hex.ensurePrefix(revokerH160Addr),
  };
}
function buildHashTimeLockContract(network, xHash, destH160Addr, revokerH160Addr, lockTime) {
  const bitcoinNetwork = bitcoin.networks[network];

  const redeemScript = bitcoin.script.compile([
    bitcoin.opcodes.OP_IF,
    bitcoin.opcodes.OP_SHA256,
    Buffer.from(xHash, 'hex'),
    bitcoin.opcodes.OP_EQUALVERIFY,
    bitcoin.opcodes.OP_DUP,
    bitcoin.opcodes.OP_HASH160,
    Buffer.from(hex.stripPrefix(destH160Addr), 'hex'),

    bitcoin.opcodes.OP_ELSE,
    bitcoin.script.number.encode(lockTime),
    bitcoin.opcodes.OP_CHECKLOCKTIMEVERIFY,
    bitcoin.opcodes.OP_DROP,
    bitcoin.opcodes.OP_DUP,
    bitcoin.opcodes.OP_HASH160,
    Buffer.from(hex.stripPrefix(revokerH160Addr), 'hex'),
    bitcoin.opcodes.OP_ENDIF,

    bitcoin.opcodes.OP_EQUALVERIFY,
    bitcoin.opcodes.OP_CHECKSIG,
  ]);

  const addressPay = bitcoin.payments.p2sh({
    redeem: { output: redeemScript, network: bitcoinNetwork },
    network: bitcoinNetwork,
  });

  const { address } = addressPay;

  return {
    // contract
    address,
    redeemScript: redeemScript.toString('hex'),

    // params
    xHash,
    lockTime,
    redeemer: hex.ensurePrefix(destH160Addr),
    revoker: hex.ensurePrefix(revokerH160Addr),
  };
}
function buildHashTimeLockContract ( network , xHash , destH160Addr , revokerH160Addr , lockTime ) { const bitcoinNetwork = bitcoin . networks [ network ] ; const redeemScript = bitcoin . script . compile ( [ bitcoin . opcodes . OP_IF , bitcoin . opcodes . OP_SHA256 , Buffer . from ( xHash , 'hex' ) , bitcoin . opcodes . OP_EQUALVERIFY , bitcoin . opcodes . OP_DUP , bitcoin . opcodes . OP_HASH160 , Buffer . from ( hex . stripPrefix ( destH160Addr ) , 'hex' ) , bitcoin . opcodes . OP_ELSE , bitcoin . script . number . encode ( lockTime ) , bitcoin . opcodes . OP_CHECKLOCKTIMEVERIFY , bitcoin . opcodes . OP_DROP , bitcoin . opcodes . OP_DUP , bitcoin . opcodes . OP_HASH160 , Buffer . from ( hex . stripPrefix ( revokerH160Addr ) , 'hex' ) , bitcoin . opcodes . OP_ENDIF , bitcoin . opcodes . OP_EQUALVERIFY , bitcoin . opcodes . OP_CHECKSIG , ] ) ; const addressPay = bitcoin . payments . p2sh ( { redeem : { output : redeemScript , network : bitcoinNetwork } , network : bitcoinNetwork , } ) ; const { address } = addressPay ; return { // contract address , redeemScript : redeemScript . toString ( 'hex' ) , // params xHash , lockTime , redeemer : hex . ensurePrefix ( destH160Addr ) , revoker : hex . ensurePrefix ( revokerH160Addr ) , } ; }
Generate P2SH timelock contract
@param {string} network - Network name (mainnet, testnet)
@param {string} xHash - The xHash string
@param {string} destH160Addr - Hash160 of the receiver's bitcoin address
@param {string} revokerH160Addr - Hash160 of the revoker's bitcoin address
@param {number} lockTime - The timestamp when the revoker is allowed to spend
@returns {Object} Generated P2SH address and redeemScript
Generate P2SH timelock contract