2
2
2Ernest52021-12-17 14:33:08
JavaScript
2Ernest5, 2021-12-17 14:33:08

How to fix Assertion failed when sending transaction to bitcore?

Here is my method to send a transaction:

const sochain_network = 'BTCTEST';
    const privateKey = this.keys.privateKey; // efbfd14f3a00e033ba8a6958d27aa6180a0f59992fcca65844dccf5bc1f48c9f
    const sourceAddress = this.keys.publicKey; // mjW7yNouLRyxaKudkQUrUTSMd57sJkXYXR
    const satoshiToSend = Math.trunc(data.amount * 100000000);
    let fee = 0;
    let inputCount = 0;
    let outputCount = 2;
    const utxos = await axios.get(`https://sochain.com/api/v2/get_tx_unspent/${sochain_network}/${sourceAddress}`);
    const transaction = new bitcore.Transaction();
    let totalAmountAvailable = 0;

    let inputs: any[] = [];
    utxos.data.data.txs.forEach(async (element: any) => {
      let utxo: any = {};
      utxo.satoshis = Math.floor(Number(element.value) * 100000000);
      utxo.script = element.script_hex;
      utxo.address = utxos.data.data.address;
      utxo.txId = element.txid;
      utxo.outputIndex = element.output_no;
      totalAmountAvailable += utxo.satoshis;
      inputCount += 1;
      inputs.push(utxo);
    });

    let transactionSize = inputCount * 146 + outputCount * 34 + 10 - inputCount;
    // Check if we have enough funds to cover the transaction and the fees assuming we want to pay 20 satoshis per byte

    fee = transactionSize * 20;

    if (totalAmountAvailable - satoshiToSend - fee < 0) {
      throw new Error('Balance is too low for this transaction');
    }

    //Set transaction input
    inputs.forEach((input: any) => {
      transaction.from(input);
    });
    console.log(satoshiToSend);

    // set the recieving address and the amount to send
    transaction.to(data.receiverAddress, satoshiToSend);

    // Set change address - Address to receive the left over funds after transfer
    transaction.change(sourceAddress);

    //manually set transaction fees: 20 satoshis per byte
    transaction.fee(fee * 20);

    // Sign transaction with your private key
    bitcore.Networks.defaultNetwork = bitcore.Networks.testnet;

    transaction.sign(privateKey);

    // serialize Transactions
    const serializedTransaction = transaction.serialize();
    console.log(
      '%cMyProject%cline:214%cserializedTransaction',
      'color:#fff;background:#ee6f57;padding:3px;border-radius:2px',
      'color:#fff;background:#1f3c88;padding:3px;border-radius:2px',
      'color:#fff;background:rgb(3, 38, 58);padding:3px;border-radius:2px',
      serializedTransaction
    );
    // Send transaction
    const result = await axios({
      method: 'POST',
      url: `https://sochain.com/api/v2/send_tx/${sochain_network}`,
      data: {
        tx_hex: serializedTransaction,
      },
    });

    console.log(result);

    return result.data.data;


while signing a transaction, an error occurs that cannot be dealt with in any way

Error: Assertion failed
    at assert (bn.js?f242:6)
    at BN.toBuffer (bn.js?f242:529)
    at Signature.toBuffer.Signature.toDER (signature.js?b47e:169)
    at PublicKeyHashInput.addSignature (publickeyhash.js?af5e:132)
    at Transaction.applySignature (transaction.js?0d8b:1220)
    at eval (transaction.js?0d8b:1189)
    at arrayEach (lodash.js?f6f6:530)
    at Function.forEach (lodash.js?f6f6:9410)
    at Transaction.sign (transaction.js?0d8b:1188)
    at bitcoinService._callee7$ (Bitcoin.service.ts?39c3:243)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question