pokkst/bitcoincashj

Sending raw transaction shows mandatory-script-verify-flag-failed error

fereshteh-jvz opened this issue · 3 comments

I'm trying to create a raw transaction for bch testnet. But when sending it, I get this error: mandatory-script-verify-flag-failed (Script failed an OP_EQUALVERIFY operation) (code 16). I run out of ideas and I don't which part I'm doing wrong.

This is my code:

    val creationtime = System.currentTimeMillis() / 1000
    val network = TestNet3Params.get()

    val path = HDPath.M(ChildNumber(44, true))
        .extend(
            ChildNumber(145, true),
            ChildNumber.ZERO_HARDENED,
            ChildNumber.ZERO,
            ChildNumber.ZERO
        )

    val seed = DeterministicSeed(seedCode, null, passphrase, creationtime)


    val wallet = Wallet.fromSeed(
        network,
        seed,
        Script.ScriptType.P2PKH,
        path
    ) // this is a test wallet to see if it will work


    val privateKey = wallet.watchingKey.privateKeyAsHex

    val fromAddress = "bchtest:qrxqjk7d7f4kqzvjqux0g2qwr42kzfwjcs7plfytvm"
    val toAddress = "bchtest:qzqxaef45wvwxqq38r6h7d2q2kgxa90a25aghw98ua"
    val prevTxHash = "e2d06c0b446d9bfe4b91e000fd6451b48643e2d74c9023b5fa9d5344092bcf12"
    val outputAmount = "0.02".toSatoshi()
    val feeAmountInLong = "0.00000300".toSatoshi()


    val fromAddr = AddressFactory.create().getAddress(network, fromAddress)
    val toAddr = AddressFactory.create().getAddress(network, toAddress)

    val tx = Transaction(network)
    val value1 = "0.10".toSatoshi()

    val changeAmountInLong = (value1) - outputAmount.toLong() - feeAmountInLong
    val transactionOutpoint1 = TransactionOutPoint(network, 0, Sha256Hash.wrap(prevTxHash)) //prevTransaction
    val scriptBytes1 = HEX.decode("76a914cc095bcdf26b600992070cf4280e1d556125d2c488ac")

    val txInput1 = TransactionInput(network, tx, scriptBytes1 ,transactionOutpoint1, Coin.valueOf(value1))
    tx.addInput(txInput1)

    tx.addOutput(Coin.valueOf(outputAmountInLong), toAddr)
    tx.addOutput(Coin.valueOf(changeAmountInLong), fromAddr)

    val scriptPubKey = ScriptBuilder.createOutputScript(fromAddr)
    val hashForSignature: Sha256Hash = tx.hashForSignature(0, scriptPubKey, SigHash.ALL, false)

    val ecKey: ECKey = ECKey.fromPrivate(HEX.decode(privateKey))
    val ecSig = ecKey.sign(hashForSignature)
    val txSig = TransactionSignature(ecSig, SigHash.ALL, false, true)
    txInput1.scriptSig = ScriptBuilder.createInputScript(txSig, ecKey);`

I'd appreciate your help @pokkst

Can I see an example raw transaction hex?

Can I see an example raw transaction hex?

Sure. This is my raw transaction hex:

010000000112cf2b0944539dfab523904cd7e24386b45164fd00e0914bfe9b6d440b6cd0e2000000006a47304402203519d04d2a7ef3c9fe0dd0823cfc9818d468e077832e3d5083b66e9e19b0684202204e5ae7ea1b68665d4873d3cfdafcd56738f7277916d57fad7a0e3e688489ff724121032a5ca9f5391712e636ed2ebc62fc5801eff1c666d9db35f3957b191eebddfc77ffffffff0280841e00000000001976a914806ee535a398e3001138f57f354055906e95fd5588acd4107a00000000001976a914cc095bcdf26b600992070cf4280e1d556125d2c488ac00000000

I figured out my fromAddress was wrong and was not the same as the one that my seed produced and that was the reason I faced that error. But now that I fixed it I run into another error:
mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation)

this is my raw transaction: 0100000001766d266bc5b02fbc28074fc0c356cf136ea765287b627e728491fe746d33c4a9000000006b483045022100da0fe5666274051a9667d458e87c60c2805860747fb94ccf42f9be4e95a4b69e02207feec061f42b2f9148277b298681b2eddfec81f3531080d79ddd6c63521f2ff44121032a5ca9f5391712e636ed2ebc62fc5801eff1c666d9db35f3957b191eebddfc77ffffffff0280841e00000000001976a914806ee535a398e3001138f57f354055906e95fd5588ac70237b00000000001976a9146f8227901f4b2ba31c53449a6b68737d5180a07388ac00000000

Is there anything wrong with my code in creating script? Any help would be greatly appreciated.