src/btc/utils.js
function buildRedeemTx(network, txid, value, redeemScript, x, publicKey, signedSigHash, toAddress) {
  const bitcoinNetwork = bitcoin.networks[network];

  // if toAddress is not supplied, derive it from the publicKey
  if (! toAddress) {

    const { address } = bitcoin.payments.p2pkh({
      network: bitcoinNetwork,
      pubkey: Buffer.from(publicKey, 'hex'),
    });

    toAddress = address;
  }

  const tx = btcUtil.buildIncompleteRedeem(network, txid, toAddress, value);

  const signature = bitcoin.script.signature.encode(
    new Buffer.from(signedSigHash, 'base64'),
    bitcoin.Transaction.SIGHASH_ALL
  );

  const scriptSig = bitcoin.payments.p2sh({
    redeem: {
      input: bitcoin.script.compile([
        signature,
        Buffer.from(publicKey, 'hex'),
        Buffer.from(x, 'hex'),
        bitcoin.opcodes.OP_TRUE,
      ]),
      output: new Buffer.from(redeemScript, 'hex'),
    },
    network: bitcoinNetwork,
  }).input;

  tx.setInputScript(0, scriptSig);

  return tx.toHex();
}
function buildRedeemTx(network, txid, value, redeemScript, x, publicKey, signedSigHash, toAddress) {
  const bitcoinNetwork = bitcoin.networks[network];

  // if toAddress is not supplied, derive it from the publicKey
  if (! toAddress) {

    const { address } = bitcoin.payments.p2pkh({
      network: bitcoinNetwork,
      pubkey: Buffer.from(publicKey, 'hex'),
    });

    toAddress = address;
  }

  const tx = btcUtil.buildIncompleteRedeem(network, txid, toAddress, value);

  const signature = bitcoin.script.signature.encode(
    new Buffer.from(signedSigHash, 'base64'),
    bitcoin.Transaction.SIGHASH_ALL
  );

  const scriptSig = bitcoin.payments.p2sh({
    redeem: {
      input: bitcoin.script.compile([
        signature,
        Buffer.from(publicKey, 'hex'),
        Buffer.from(x, 'hex'),
        bitcoin.opcodes.OP_TRUE,
      ]),
      output: new Buffer.from(redeemScript, 'hex'),
    },
    network: bitcoinNetwork,
  }).input;

  tx.setInputScript(0, scriptSig);

  return tx.toHex();
}
function buildRedeemTx ( network , txid , value , redeemScript , x , publicKey , signedSigHash , toAddress ) { const bitcoinNetwork = bitcoin . networks [ network ] ; // if toAddress is not supplied, derive it from the publicKey if ( ! toAddress ) { const { address } = bitcoin . payments . p2pkh ( { network : bitcoinNetwork , pubkey : Buffer . from ( publicKey , 'hex' ) , } ) ; toAddress = address ; } const tx = btcUtil . buildIncompleteRedeem ( network , txid , toAddress , value ) ; const signature = bitcoin . script . signature . encode ( new Buffer . from ( signedSigHash , 'base64' ) , bitcoin . Transaction . SIGHASH_ALL ) ; const scriptSig = bitcoin . payments . p2sh ( { redeem : { input : bitcoin . script . compile ( [ signature , Buffer . from ( publicKey , 'hex' ) , Buffer . from ( x , 'hex' ) , bitcoin . opcodes . OP_TRUE , ] ) , output : new Buffer . from ( redeemScript , 'hex' ) , } , network : bitcoinNetwork , } ) . input ; tx . setInputScript ( 0 , scriptSig ) ; return tx . toHex ( ) ; }
Create redeem transaction using signed sigHash
@param {string} network - Network name (mainnet, testnet)
@param {string} txid - The txid for the UTXO being spent
@param {string|number} value - The amount of funds to be sent (in Satoshis)
@param {string} redeemScript - The redeemScript of the P2SH address
@param {string} x - The x value for the transaction
@param {string} publicKey - The publicKey of the redeemer
@param {string} signedSigHash - The sigHash signed by the redeemer
@param {string} toAddress - The address where to send funds (defaults to redeemer)
@returns {string} Signed transaction as hex string
Create redeem transaction using signed sigHash