Friday, November 29, 2024
HomeBitcoinsegregated witness - Signing a Bitcoin SegWit transaction utilizing Bitcoinj

segregated witness – Signing a Bitcoin SegWit transaction utilizing Bitcoinj


I’m engaged on a SegWit Bitcoin transaction, and I am encountering an error when making an attempt to broadcast it:

Code: -26, Error: mandatory-script-verify-flag-failed (Signature should
be zero for failed CHECK(MULTI)SIG operation)

Right here’s the code I’m utilizing to signal the transaction:

non-public static void signInputsOfTransaction(Tackle sourceAddress, Transaction tx, ECKey key) {
    for (int i = 0; i < tx.getInputs().measurement(); i++) {
        Script scriptPubKey = ScriptBuilder.createP2WPKHOutputScript(key);

        Coin inputValue = tx.getInput(i).getValue();

        Sha256Hash hash = tx.hashForWitnessSignature(
            i, scriptPubKey, inputValue, Transaction.SigHash.ALL, false
        );

        ECKey.ECDSASignature ecdsaSignature = key.signal(hash);
        TransactionSignature txSignature = new TransactionSignature(ecdsaSignature, Transaction.SigHash.ALL, false);

        TransactionWitness witness = new TransactionWitness(2);
        witness.setPush(0, txSignature.encodeToBitcoin());
        witness.setPush(1, key.getPubKey());
        tx.getInput(i).setWitness(witness);

        tx.getInput(i).setScriptSig(ScriptBuilder.createEmpty());
    }
}

Right here’s the uncooked transaction hex I’m making an attempt to broadcast:

010000000001011a34c12133e07aceb6b0cd254e2d9e12fde095b5a3afece4907945e36254e0d40100000000ffffffff02e803000000000000160014c9bf27c413655f5894a6a095ccfb5f1d291123f185170000000000001600146edda921d055dece3a894e39fd91a0c50faf675c02473044022000e9341c7c137529db50f706bc2bc1c1057674cda3bc33be5c559a45731b9c2802201a0f435a70e0f3a89d272efa407e0817e964f4c14ee9f9e3e4b5961fa658fc1d01210336069ac77986a9de47f0c371b682906149e43c2f51853b30d31132d1ec33b76900000000

I’ve decoded it utilizing a Bitcoin transaction API (BlockCypher), and right here’s the output:

{
    "outputs": [
        {
            "addresses": ["bc1qexlj03qnv404399x5z2ue76lr553zgl3kfx7va"],
            "script_type": "pay-to-witness-pubkey-hash",
            "worth": 1000,
            "script": "0014c9bf27c413655f5894a6a095ccfb5f1d291123f1"
        },
        {
            "addresses": ["bc1qdmw6jgws2h0vuw5ffculmydqc5867e6ulzzm97"],
            "script_type": "pay-to-witness-pubkey-hash",
            "worth": 6021,
            "script": "00146edda921d055dece3a894e39fd91a0c50faf675c"
        }
    ],
    "addresses": [
        "bc1qdmw6jgws2h0vuw5ffculmydqc5867e6ulzzm97",
        "bc1qexlj03qnv404399x5z2ue76lr553zgl3kfx7va"
    ],
    "charges": 1130,
    "ver": 1,
    "vsize": 141,
    "inputs": [{
        "sequence": 4294967295,
        "witness": [
            "3044022000e9341c7c137529db50f706bc2bc1c1057674cda3bc33be5c559a45731b9c2802201a0f435a70e0f3a89d272efa407e0817e964f4c14ee9f9e3e4b5961fa658fc1d01",
            "0336069ac77986a9de47f0c371b682906149e43c2f51853b30d31132d1ec33b769"
        ],
        "addresses": ["bc1qdmw6jgws2h0vuw5ffculmydqc5867e6ulzzm97"],
        "prev_hash": "d4e05462e3457990e4ecafa3b595e0fd129e2d4e25cdb0b6ce7ae03321c1341a",
        "output_value": 8151,
        "script_type": "pay-to-witness-pubkey-hash",
        "output_index": 1,
        "age": 698646
    }],
    "choice": "low",
    "acquired": "2024-11-28T15:26:00.690177102Z",
    "block_height": -1,
    "confirmations": 0,
    "relayed_by": "189.37.69.88",
    "complete": 7021,
    "measurement": 222,
    "block_index": -1,
    "double_spend": false,
    "vin_sz": 1,
    "hash": "0cafb57866d9a412e6cd0f806e03363baab1bdbf6434181ea5a927aa62930f77",
    "vout_sz": 2 }

Is there one thing mistaken with the best way I’m signing or organising the TransactionWitness?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments