简体   繁体   中英

Got Assertion.failed error while trying to sign a transaction

Try to sign a transaction and get an Assert error. I don't know what I doing wrong, please help, bitcoin just killing me, I spent a full week on this error. bla bal asd asd sad asd asd asd asd asdas das dasd asd asd asd as da dasd

 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.tes.net; 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;

Got. I don't know what I doing wrong. I go through the tutorial ( https://blog.logrocket.com/sending-bitcoin-with-javascript/ ) and get error

 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)

Have you gotten a solution to this problem?

have you found a fix for this error? If not, this worked for me. It seems to be an issue with the bn.js library which bitcore is dependent on and bitcore itself.

Doing npm i bn.js to install the most recent version seems to do the trick for me. In case this doesn't work and you still need a fix or more info on this issue, check out this issue on the official repo: issue 3357

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM