utxo – Send transaction with 2 inputs and 2 outputs using Blockcypher

0
5


I’m using a node.js client of Blockcypher in order to broadcast a Bitcoin transaction.

My code:

const originWalletBalance = getBalance(originAddress)
const fee = calculateFee()
const txBody = {
        fees: fee,
        inputs: [{ addresses: [originAddress] }],
        outputs: [
          { addresses: [targetAddress], value: amountToSend }
        ]
      }

    //@see https://www.blockcypher.com/dev/bitcoin/#creating-transactions 

    const txSkeleton = await this.bcapi.newTX(body)
    // build signer from WIF
    let keys: ECPair = ECPair.fromWIF(signingKey)
    //signing the transaction
    let tmptx = txSkeleton
    tmptx.pubkeys = []
    const SIGHASH_ALL = 0x01
    tmptx["signatures"] = tmptx["tosign"].map((tosign) => {
      tmptx["pubkeys"].push(keys.publicKey.toString("hex"))
      return bitcoin.script.signature.encode(
        keys.sign(Buffer.from(tosign, "hex")),
        SIGHASH_ALL
      ).toString("hex")
    })
    //broadcast tx
    return await this.bcapi.sendTX(tmptx)

Where the balance of the origin wallet is 190689, the amount to send is 74500, the fee is 25500.

txSkeleton is crated with 2 inputs and 2 outputs (one for the target address and the change to the origin address, as expected):

It all seem right, but I’m getting the following errors:

"error": "Error building input: Error generating scriptsig when building transaction: Invalid signature: Non-canonical signature: wrong length marker."
"error": "Error building input: Error generating scriptsig when building transaction: Invalid signature: Non-canonical signature: wrong length marker."
"error": "Not enough funds in 2 inputs to pay for 2 outputs, missing -74500."
"error": "Not enough funds in 2 inputs to pay for 2 outputs, missing -122500."
"error": "Not enough funds after fees in 0 inputs to pay for 2 outputs, missing -148000."

Any idea what am I doing wrong here?



Source link

Leave a reply