bitcoinjs – On the lookout for JS/Node.js code instance to signal a transaction (with a number of UTXOs) with solely the prolonged non-public key

0
87


Positive, here is an instance of how you might signal a transaction with a number of UTXOs utilizing solely the prolonged non-public key in JavaScript:

const bitcoin = require('bitcoinjs-lib');

// Outline the community (e.g. testnet or mainnet)
const community = bitcoin.networks.testnet;

// Outline the transaction inputs
const utxos = [
  {
    txid: 'txid1',
    vout: 0,
    value: 5000000,
    scriptPubKey: 'scriptPubKey1'
  },
  {
    txid: 'txid2',
    vout: 0,
    value: 7000000,
    scriptPubKey: 'scriptPubKey2'
  }
];

// Outline the transaction outputs
const outputs = [
  {
    address: 'recipientAddress1',
    value: 10000000
  }
];

// Outline the prolonged non-public key
const extendedPrivateKey = 'xprvXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

// Create a brand new transaction builder
const txb = new bitcoin.TransactionBuilder(community);

// Add every UTXO as an enter to the transaction
for (const utxo of utxos) {
  txb.addInput(utxo.txid, utxo.vout);
}

// Add every output to the transaction
for (const output of outputs) {
  txb.addOutput(output.deal with, output.worth);
}

// Derive the non-public key for every enter utilizing the prolonged non-public key
for (let i = 0; i < utxos.size; i++) {
  const key = bitcoin.bip32.fromBase58(extendedPrivateKey, community).derivePath(`m/0/${i}`).privateKey;
  txb.signal(i, key, null, bitcoin.Transaction.SIGHASH_ALL, utxos[i].worth);
}

// Construct the transaction and print it
const tx = txb.construct();
console.log(tx.toHex());

LEAVE A REPLY

Please enter your comment!
Please enter your name here