当前位置: 首页 > 工具软件 > BitcoinJS > 使用案例 >

bitcoinJs@6签名广播

艾善
2023-12-01

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

const network = bitcoin.networks.bitcoin; //正式网络

const ecpair = require("ecpair");

const ecc = require("tiny-secp256k1");

const ECPair = ecpair.ECPairFactory(ecc);

const alice = ECPair.fromWIF(

  "L2uPYXe17xSTqbCjZvL2DsyXPCbXspvcu5mHLDYUgzdUbZGSKrSr"

);

const validator = (pubkey, msghash, signature) =>

  ECPair.fromPublicKey(pubkey).verify(msghash, signature);

function bitcoinSign(

  privateKey,

  amount,

  utxo,

  sendFee,

  toAddress,

  changeAddress

) {

  set = ECPair.fromWIF(privateKey); //私钥签名

  console.log("打印签名:", set);

  const psbt = new bitcoin.Psbt();

  var sendAmount = parseFloat(amount); //支付金额

  var fee = parseFloat(sendFee); //矿工费用

  sendAmount += fee; //支付总额

  psbt.setVersion(2);

  psbt.setLocktime(0);

  var totalMoney = 0;

  for (var i = 0; i < utxo.length; i++) {

    //遍历utxo

    psbt.addInput({

      hash: "28aea84b4dc403180c43c62ff78cc0b33c54efefa48a945d2448f2afa4f9a914",

      index: 0,

      sequence: 0xffffffff,

      // nonWitnessUtxo : "",

      nonWitnessUtxo: Buffer.from(

        "02000000013ce55c60908a92673fcbf87e4b7d171ca32845a1ec50976218037f1b55111047000000006a47304402202ced84082c08ac64068737b4e83a7a0c03f696d54ff7c4926937985c141b1ca50220752a915cf2f0ae91ccccfc883916d1f70d0cad08c56341512fb39d73db5df527012103640176d13079d3a8fbc1d63f824cb2f6773020e0cb5b13b0faadb036cbc56a05feffffff02007841cb020000001976a914dd7db20a4d664469d2e526da5bc707f8d4547b3588ac00a1ea18060000001976a914c51b45f3b50a225e7424b25c73916a16ce6d48a988ac4b860000",

        "hex"

      ),

      witnessUtxo: {

        script: Buffer.from(

          "76a9148bbc95d2709c71607c60ee3f097c1217482f518d88ac",

          "hex"

        ),

        value: 90000,

      },

    });

    totalMoney += utxo[i].value;

  }

  // console.log(totalMoney);

  psbt.addOutput({

    address: toAddress,

    value: 80000,

  });

  psbt.signInput(0, alice);

  psbt.validateSignaturesOfInput(0, validator);

  psbt.finalizeAllInputs();

  return psbt.extractTransaction().toHex();

}

var bitUtxo = {

  unspent_outputs: [

    {

      tx_hash:

        "8ee886ba0c66ba2df2c0e3da3beee526996d9a5e6bbbdfea43e1a78340cb0128",

      tx_hash_big_endian:

        "2801cb4083a7e143eadfbb6b5e9a6d9926e5ee3bdae3c0f22dba660cba86e88e",

      tx_index: 382253932,

      tx_output_n: 0,

      script: "76a914ca45c6eceea7aed14b6aea7e0ed466c6134f14bc88ac",

      value: 1899000,

      value_hex: "1cf9f8",

      confirmations: 2,

    },

  ],

};

var privateKey = "L52SZiAXhEiQM5ZdDihwUwh3ovY1KtHNfSJzqLhqHMQqbkPE6eU5";

var amount = 1898000;

var utxo = bitUtxo.unspent_outputs;

var sendFee = 1000;

var toAddress = "12zEJohMNqSZLXH1Msxpw41ykkk3rxgx1s"; //接收地址

var changeAddress = "Ccf2UujvzxytG3EmMgXwdA2uBDBM9NEfp2";

var sign = bitcoinSign(

  privateKey,

  amount,

  bitUtxo.unspent_outputs,

  sendFee,

  toAddress,

  changeAddress

);

console.log(sign);

// let id = psbt.extractTransaction().getId()  // 签名id

// let hash = psbt.extractTransaction().toHex()  //签名hash

 类似资料: