bitcoin core – Partially Signing multisig rawtransaction returns redeemScript/witnessScript does not match scriptPubKey

I want to partially sign a rawtransactions using bitcoind through RPC but I am really stuck almost at the end of the process when I try to sign my manually created raw transaction and I spent many hours trying to find the error but I do not understand it. I am going to replicate here all the steps I did.

  1. First I create a 2-3 multisig address
$user1_pubkey = "03e33af80b92e4efd64852166d6126e10aafdcc741579c67c6aa9eb89080cebeb2";
$user2_pubkey = "02df4bf6a54f79b333b7796d2941de13b1755c00c09f4dbf696c7729aea2946210";
$user3_pubkey = $this->bitcoin->getaddressinfo("2Msqe8jLEReQpD4CYnRj29cAqQS8c81zidv")('pubkey');
$keys() = $user1_pubkey;
$keys() = $user2_pubkey;
$keys() = $user3_pubkey;
$multisig_address = $this->bitcoin->addmultisigaddress(2, $keys);

Returned values:

("address")=>
    string(35) "2NBet88aY57CSYCkH7nXKb2Bxae4K3xdRBT"
    ("redeemScript")=>
    string(210) "522103e33af80b92e4efd64852166d6126e10aafdcc741579c67c6aa9eb89080cebeb22102df4bf6a54f79b333b7796d2941de13b1755c00c09f4dbf696c7729aea29462102102c099a12b6cba679ad56d8e793847f422634bc84b42cb3fae0b00a6943f56921253ae"
  1. I have sent two small transactions to the multisig address (one of 0.005 and another of 0.004)
ef669e71fb08a2dd7b9d2500a86d53ba75e41c3ab67b4ec58435904d87b749c4
8a9e837e94a639cd104631a29b57a86d55b5dc32b8372b7ec3bacdf777ba57e5
  1. Now I retrieve all the information related to those txids:
$transaction1 = $this->bitcoin->getrawtransaction("ef669e71fb08a2dd7b9d2500a86d53ba75e41c3ab67b4ec58435904d87b749c4", true);
$transaction2 = $this->bitcoin->getrawtransaction("8a9e837e94a639cd104631a29b57a86d55b5dc32b8372b7ec3bacdf777ba57e5", true);

3.1. Result of previous variables:

$transaction1 =>

array(14) {
  ("txid")=>
  string(64) "ef669e71fb08a2dd7b9d2500a86d53ba75e41c3ab67b4ec58435904d87b749c4"
  ("hash")=>
  string(64) "e586e88456cef32c01820d94541296aab85d776d890d8879184b8e70344a6353"
  ("version")=>
  int(2)
  ("size")=>
  int(247)
  ("vsize")=>
  int(166)
  ("weight")=>
  int(661)
  ("locktime")=>
  int(1891405)
  ("vin")=>
  array(1) {
    (0)=>
    array(5) {
      ("txid")=>
      string(64) "9d0c2b8203b1b8f63ac8a8dc17a0e48b36efceee43d38fde50a6b3c92f811830"
      ("vout")=>
      int(0)
      ("scriptSig")=>
      array(2) {
        ("asm")=>
        string(44) "0014839c3b61637073d6aec4c74a5b2e8fe52fcc4d83"
        ("hex")=>
        string(46) "160014839c3b61637073d6aec4c74a5b2e8fe52fcc4d83"
      }
      ("txinwitness")=>
      array(2) {
        (0)=>
        string(142) "3044022055bbad9ea94df56180ded2760dd77b0fc959e633b459bc906e6bd4456f72c71302201d9430ac2ed9debc9508442484accac54021c990e8906ebbf7a6b205ec9e827101"
        (1)=>
        string(66) "0326ae8d30a90a6926b81718dfa07b0b5058636f9c86465a1c3f37b0322af023c8"
      }
      ("sequence")=>
      int(4294967294)
    }
  }
  ("vout")=>
  array(2) {
    (0)=>
    array(3) {
      ("value")=>
      float(0.005)
      ("n")=>
      int(0)
      ("scriptPubKey")=>
      array(5) {
        ("asm")=>
        string(60) "OP_HASH160 c9ebaa3be1c12351c9e07ea5758a68eb20ee5098 OP_EQUAL"
        ("hex")=>
        string(46) "a914c9ebaa3be1c12351c9e07ea5758a68eb20ee509887"
        ("reqSigs")=>
        int(1)
        ("type")=>
        string(10) "scripthash"
        ("addresses")=>
        array(1) {
          (0)=>
          string(35) "2NBet88aY57CSYCkH7nXKb2Bxae4K3xdRBT"
        }
      }
    }
    (1)=>
    array(3) {
      ("value")=>
      float(0.01040373)
      ("n")=>
      int(1)
      ("scriptPubKey")=>
      array(5) {
        ("asm")=>
        string(60) "OP_HASH160 301c49c04ede304bdef0ab48e2b365a9eef737e4 OP_EQUAL"
        ("hex")=>
        string(46) "a914301c49c04ede304bdef0ab48e2b365a9eef737e487"
        ("reqSigs")=>
        int(1)
        ("type")=>
        string(10) "scripthash"
        ("addresses")=>
        array(1) {
          (0)=>
          string(35) "2MwdcJyxRAxqQ4tf6TQSubcLMZd21MrTskx"
        }
      }
    }
  }
  ("hex")=>
  string(494) "020000000001013018812fc9b3a650de8fd343eeceef368be4a017dca8c83af6b8b103822b0c9d0000000017160014839c3b61637073d6aec4c74a5b2e8fe52fcc4d83feffffff0220a107000000000017a914c9ebaa3be1c12351c9e07ea5758a68eb20ee509887f5df0f000000000017a914301c49c04ede304bdef0ab48e2b365a9eef737e48702473044022055bbad9ea94df56180ded2760dd77b0fc959e633b459bc906e6bd4456f72c71302201d9430ac2ed9debc9508442484accac54021c990e8906ebbf7a6b205ec9e827101210326ae8d30a90a6926b81718dfa07b0b5058636f9c86465a1c3f37b0322af023c84ddc1c00"
  ("blockhash")=>
  string(64) "00000000b1c75e4431f9c23a75779c964fa51cd714f9703781670f1a2c50a8c7"
  ("confirmations")=>
  int(388)
  ("time")=>
  int(1605141275)
  ("blocktime")=>
  int(1605141275)
}


$transaction2 =>

array(14) {
  ("txid")=>
  string(64) "8a9e837e94a639cd104631a29b57a86d55b5dc32b8372b7ec3bacdf777ba57e5"
  ("hash")=>
  string(64) "0e8dfd742d3ea5f12ee7c033112f5e6f585b41e317b3f9d3fef61db4200c4bbd"
  ("version")=>
  int(2)
  ("size")=>
  int(247)
  ("vsize")=>
  int(166)
  ("weight")=>
  int(661)
  ("locktime")=>
  int(1891405)
  ("vin")=>
  array(1) {
    (0)=>
    array(5) {
      ("txid")=>
      string(64) "ef669e71fb08a2dd7b9d2500a86d53ba75e41c3ab67b4ec58435904d87b749c4"
      ("vout")=>
      int(1)
      ("scriptSig")=>
      array(2) {
        ("asm")=>
        string(44) "0014c7f49ae39660b15632f46a3c69a67ccb4de7dc20"
        ("hex")=>
        string(46) "160014c7f49ae39660b15632f46a3c69a67ccb4de7dc20"
      }
      ("txinwitness")=>
      array(2) {
        (0)=>
        string(142) "304402207cc7558274cd9458c44bf7554d0b049bba1388cd82057b3f8d0786bc28690aad022057eea44e01ff11c608e254a844b620f428fb515db6ef33cd199c57d3008ce89c01"
        (1)=>
        string(66) "0303c01176f2db9efaa18e169e0ea7da87784208e05fcb1e0258ccb834805ed02d"
      }
      ("sequence")=>
      int(4294967294)
    }
  }
  ("vout")=>
  array(2) {
    (0)=>
    array(3) {
      ("value")=>
      float(0.004)
      ("n")=>
      int(0)
      ("scriptPubKey")=>
      array(5) {
        ("asm")=>
        string(60) "OP_HASH160 c9ebaa3be1c12351c9e07ea5758a68eb20ee5098 OP_EQUAL"
        ("hex")=>
        string(46) "a914c9ebaa3be1c12351c9e07ea5758a68eb20ee509887"
        ("reqSigs")=>
        int(1)
        ("type")=>
        string(10) "scripthash"
        ("addresses")=>
        array(1) {
          (0)=>
          string(35) "2NBet88aY57CSYCkH7nXKb2Bxae4K3xdRBT"
        }
      }
    }
    (1)=>
    array(3) {
      ("value")=>
      float(0.00640207)
      ("n")=>
      int(1)
      ("scriptPubKey")=>
      array(5) {
        ("asm")=>
        string(60) "OP_HASH160 0f670955696f3835ef9204dad881b1c9f3002cc7 OP_EQUAL"
        ("hex")=>
        string(46) "a9140f670955696f3835ef9204dad881b1c9f3002cc787"
        ("reqSigs")=>
        int(1)
        ("type")=>
        string(10) "scripthash"
        ("addresses")=>
        array(1) {
          (0)=>
          string(35) "2MtefZG3K2Zs38dYntJwJKg1WqSEqNoW9sF"
        }
      }
    }
  }
  ("hex")=>
  string(494) "02000000000101c449b7874d903584c54e7bb63a1ce475ba536da800259d7bdda208fb719e66ef0100000017160014c7f49ae39660b15632f46a3c69a67ccb4de7dc20feffffff02801a06000000000017a914c9ebaa3be1c12351c9e07ea5758a68eb20ee509887cfc409000000000017a9140f670955696f3835ef9204dad881b1c9f3002cc7870247304402207cc7558274cd9458c44bf7554d0b049bba1388cd82057b3f8d0786bc28690aad022057eea44e01ff11c608e254a844b620f428fb515db6ef33cd199c57d3008ce89c01210303c01176f2db9efaa18e169e0ea7da87784208e05fcb1e0258ccb834805ed02d4ddc1c00"
  ("blockhash")=>
  string(64) "00000000b1c75e4431f9c23a75779c964fa51cd714f9703781670f1a2c50a8c7"
  ("confirmations")=>
  int(388)
  ("time")=>
  int(1605141275)
  ("blocktime")=>
  int(1605141275)
}

  1. I want to create a raw transaction to spend both utxos and I want to spent it in two different address. So having in mind the fee for the transaction I proceed to create manually the transaction.
        $fee = number_format(floatval($this->bitcoin->estimateSmartFee(6)('feerate'))/5, 8);
        $raw_amount = 0.00000000;
        $txids = ();
        $vouts = ();
        $amounts = ();
        $scriptPubs = ();
        for($i=0; $i<count($transactions); $i++){
            $total_amount+=$transactions($i)("vout")(0)("value");
            $txids() = $transactions($i)("txid");
            $vouts() = $transactions($i)("vout")(0)("n");
            $amounts() = $transactions($i)("vout")(0)("value");
            $scriptPubs() = $transactions($i)("vout")(0)("scriptPubKey")("hex");
        }

        $inputs = '(';
        for($i=0; $i<count($vouts); $i++){
            $inputs.='{
                        "txid": "'.$txids($i).'",
                        "vout": '.$vouts($i).'
                      }';
            if($i+1!=count($vouts)){
                $inputs.=',';
            }
        }
        $inputs.=')';

        $minus_fee = $raw_amount-floatval($fee);
        $amount1 = $minus_fee*0.6;
        $amount2 = $minus_fee*0.4;

        $address1 = "tb1qhjcqny2rvaqd7q0700m968e3vuqctmuhhyk9fk";
        $address2 = "tb1q2nrysxf8nwxv8euwtu470wgjruw25guxkal3wd";
        $outputs = '{
                      "'.$address1.'": '.$amount1.',
                      "'.$address2.'": '.$amount2.'
                    }';

        $raw_transaction = $this->bitcoin->createrawtransaction(json_decode($inputs), json_decode($outputs));

4.1. Result of $raw_transaction is the following:

0200000002c449b7874d903584c54e7bb63a1ce475ba536da800259d7bdda208fb719e66ef0000000000ffffffffe557ba77f7cdbac37e2b37b832dcb5556da8579ba2314610cd39a6947e839e8a0000000000ffffffff02e83c080000000000160014bcb00991436740df01fe7bf65d1f31670185ef97f07d050000000000160014b92b9f7f7bf6e4e1cdd73d3d23a1dbc98aa57d4e00000000
  1. I decode $raw_transaction
$decoded = $this->bitcoin->decoderawtransaction($raw_transaction);

Content of $decoded:

array(9) {
  ("txid")=>
  string(64) "ba11b2caf11d79c1f4bc349f40895c380218e9ab91ec4c1d52aaee8284749549"
  ("hash")=>
  string(64) "ba11b2caf11d79c1f4bc349f40895c380218e9ab91ec4c1d52aaee8284749549"
  ("version")=>
  int(2)
  ("size")=>
  int(154)
  ("vsize")=>
  int(154)
  ("weight")=>
  int(616)
  ("locktime")=>
  int(0)
  ("vin")=>
  array(2) {
    (0)=>
    array(4) {
      ("txid")=>
      string(64) "ef669e71fb08a2dd7b9d2500a86d53ba75e41c3ab67b4ec58435904d87b749c4"
      ("vout")=>
      int(0)
      ("scriptSig")=>
      array(2) {
        ("asm")=>
        string(0) ""
        ("hex")=>
        string(0) ""
      }
      ("sequence")=>
      int(4294967295)
    }
    (1)=>
    array(4) {
      ("txid")=>
      string(64) "8a9e837e94a639cd104631a29b57a86d55b5dc32b8372b7ec3bacdf777ba57e5"
      ("vout")=>
      int(0)
      ("scriptSig")=>
      array(2) {
        ("asm")=>
        string(0) ""
        ("hex")=>
        string(0) ""
      }
      ("sequence")=>
      int(4294967295)
    }
  }
  ("vout")=>
  array(2) {
    (0)=>
    array(3) {
      ("value")=>
      float(0.0053988)
      ("n")=>
      int(0)
      ("scriptPubKey")=>
      array(5) {
        ("asm")=>
        string(42) "0 bcb00991436740df01fe7bf65d1f31670185ef97"
        ("hex")=>
        string(44) "0014bcb00991436740df01fe7bf65d1f31670185ef97"
        ("reqSigs")=>
        int(1)
        ("type")=>
        string(18) "witness_v0_keyhash"
        ("addresses")=>
        array(1) {
          (0)=>
          string(42) "tb1qhjcqny2rvaqd7q0700m968e3vuqctmuhhyk9fk"
        }
      }
    }
    (1)=>
    array(3) {
      ("value")=>
      float(0.0035992)
      ("n")=>
      int(1)
      ("scriptPubKey")=>
      array(5) {
        ("asm")=>
        string(42) "0 b92b9f7f7bf6e4e1cdd73d3d23a1dbc98aa57d4e"
        ("hex")=>
        string(44) "0014b92b9f7f7bf6e4e1cdd73d3d23a1dbc98aa57d4e"
        ("reqSigs")=>
        int(1)
        ("type")=>
        string(18) "witness_v0_keyhash"
        ("addresses")=>
        array(1) {
          (0)=>
          string(42) "tb1qhy4e7lmm7mjwrnwh857j8gwmex922l2wwc830y"
        }
      }
    }
  }
}
  1. Now I proceed to partially sign the previous created transaction.
        $private_key = $this->bitcoin->dumpprivkey("2Msqe8jLEReQpD4CYnRj29cAqQS8c81zidv"); // cS6e5CojfNy2ELWcsB4tLFVQV9wSSQUw1PU4YrSwRSjYLaC8ikrU
        $redeemScript = "522103e33af80b92e4efd64852166d6126e10aafdcc741579c67c6aa9eb89080cebeb22102df4bf6a54f79b333b7796d2941de13b1755c00c09f4dbf696c7729aea29462102102c099a12b6cba679ad56d8e793847f422634bc84b42cb3fae0b00a6943f56921253ae";
        $vin = $raw_transaction("vin");
        $vout = $raw_transaction("vout");

        $prevtxs = '(';
        for($i=0; $i<count($txids); $i++){
            $prevtxs .= '{
                    "txid": "'.$txids($i).'",
                    "vout": '.$vouts($i).',
                    "scriptPubKey": "'.$scriptPubs($i).'", 
                    "redeemScript": "'.$redeemScript.'",
                    "amount": '.$amounts($i).'
                  }';
            if($i+1!=count($txids)){
                $json2.=',';
            }
        }
        $prevtxs.=')';

        $partially_signed = $this->bitcoin->signrawtransactionwithkey($raw_transaction, json_decode($private_key), json_decode($prevtxs));
  1. And in the previous step is where the process fails. The transaction is not signed and the bitcoind returns this error:
redeemScript/witnessScript does not match scriptPubKey

I am using bitcoin testnet to test it and I think that I added all the information. If someone knows where it is the mistake, please let me know. I searched information to fix it but I could not find a solution for my issue.

Edited:
I followed the next tutorial where I could notice that I was using the wrong scriptpub and I retrieved funding inputs using getrawtransaction passing the second argument (true) to get the decoded transaction. And well, now the previous error is gone but now it throws a new error: redeemScript/witnessScript does not match scriptPubKey

Thanks

bitcoin core – Partially Signing multisig rawtransaction returns scriptPubKey mismatch

I want to partially sign a rawtransactions using bitcoind through RPC but I am really stuck almost at the end of the process when I try to sign my manually created raw transaction and I spent many hours trying to find the error but I do not understand it. I am going to replicate here all the steps I did.

  1. First I create a 2-3 multisig address
$user1_pubkey = "03e33af80b92e4efd64852166d6126e10aafdcc741579c67c6aa9eb89080cebeb2";
$user2_pubkey = "02df4bf6a54f79b333b7796d2941de13b1755c00c09f4dbf696c7729aea2946210";
$user3_pubkey = $this->bitcoin->getaddressinfo("2Msqe8jLEReQpD4CYnRj29cAqQS8c81zidv")('pubkey');
$keys() = $user1_pubkey;
$keys() = $user2_pubkey;
$keys() = $user3_pubkey;
$multisig_address = $this->bitcoin->addmultisigaddress(2, $keys);

Returned values:

("address")=>
    string(35) "2NBet88aY57CSYCkH7nXKb2Bxae4K3xdRBT"
    ("redeemScript")=>
    string(210) "522103e33af80b92e4efd64852166d6126e10aafdcc741579c67c6aa9eb89080cebeb22102df4bf6a54f79b333b7796d2941de13b1755c00c09f4dbf696c7729aea29462102102c099a12b6cba679ad56d8e793847f422634bc84b42cb3fae0b00a6943f56921253ae"
  1. I have sent two small transactions to the multisig address (one of 0.005 and another of 0.004)
ef669e71fb08a2dd7b9d2500a86d53ba75e41c3ab67b4ec58435904d87b749c4
8a9e837e94a639cd104631a29b57a86d55b5dc32b8372b7ec3bacdf777ba57e5
  1. Now I retrieve all the information related to those txids:
$transaction1 = $this->bitcoin->gettransaction("ef669e71fb08a2dd7b9d2500a86d53ba75e41c3ab67b4ec58435904d87b749c4");
$transaction2 = $this->bitcoin->gettransaction("8a9e837e94a639cd104631a29b57a86d55b5dc32b8372b7ec3bacdf777ba57e5");

3.1. Result of previous variables:

$transaction1 =>

array(13) {
  ("amount")=>
  float(-0.005)
  ("fee")=>
  float(-1.66E-6)
  ("confirmations")=>
  int(8)
  ("blockhash")=>
  string(64) "00000000b1c75e4431f9c23a75779c964fa51cd714f9703781670f1a2c50a8c7"
  ("blockindex")=>
  int(60)
  ("blocktime")=>
  int(1605141275)
  ("txid")=>
  string(64) "ef669e71fb08a2dd7b9d2500a86d53ba75e41c3ab67b4ec58435904d87b749c4"
  ("walletconflicts")=>
  array(0) {
  }
  ("time")=>
  int(1605140906)
  ("timereceived")=>
  int(1605140906)
  ("bip125-replaceable")=>
  string(2) "no"
  ("details")=>
  array(1) {
    (0)=>
    array(7) {
      ("address")=>
      string(35) "2NBet88aY57CSYCkH7nXKb2Bxae4K3xdRBT"
      ("category")=>
      string(4) "send"
      ("amount")=>
      float(-0.005)
      ("label")=>
      string(0) ""
      ("vout")=>
      int(0)
      ("fee")=>
      float(-1.66E-6)
      ("abandoned")=>
      bool(false)
    }
  }
  ("hex")=>
  string(494) "020000000001013018812fc9b3a650de8fd343eeceef368be4a017dca8c83af6b8b103822b0c9d0000000017160014839c3b61637073d6aec4c74a5b2e8fe52fcc4d83feffffff0220a107000000000017a914c9ebaa3be1c12351c9e07ea5758a68eb20ee509887f5df0f000000000017a914301c49c04ede304bdef0ab48e2b365a9eef737e48702473044022055bbad9ea94df56180ded2760dd77b0fc959e633b459bc906e6bd4456f72c71302201d9430ac2ed9debc9508442484accac54021c990e8906ebbf7a6b205ec9e827101210326ae8d30a90a6926b81718dfa07b0b5058636f9c86465a1c3f37b0322af023c84ddc1c00"
}


$transaction2 =>

array(13) {
  ("amount")=>
  float(-0.004)
  ("fee")=>
  float(-1.66E-6)
  ("confirmations")=>
  int(8)
  ("blockhash")=>
  string(64) "00000000b1c75e4431f9c23a75779c964fa51cd714f9703781670f1a2c50a8c7"
  ("blockindex")=>
  int(64)
  ("blocktime")=>
  int(1605141275)
  ("txid")=>
  string(64) "8a9e837e94a639cd104631a29b57a86d55b5dc32b8372b7ec3bacdf777ba57e5"
  ("walletconflicts")=>
  array(0) {
  }
  ("time")=>
  int(1605140915)
  ("timereceived")=>
  int(1605140915)
  ("bip125-replaceable")=>
  string(2) "no"
  ("details")=>
  array(1) {
    (0)=>
    array(7) {
      ("address")=>
      string(35) "2NBet88aY57CSYCkH7nXKb2Bxae4K3xdRBT"
      ("category")=>
      string(4) "send"
      ("amount")=>
      float(-0.004)
      ("label")=>
      string(0) ""
      ("vout")=>
      int(0)
      ("fee")=>
      float(-1.66E-6)
      ("abandoned")=>
      bool(false)
    }
  }
  ("hex")=>
  string(494) "02000000000101c449b7874d903584c54e7bb63a1ce475ba536da800259d7bdda208fb719e66ef0100000017160014c7f49ae39660b15632f46a3c69a67ccb4de7dc20feffffff02801a06000000000017a914c9ebaa3be1c12351c9e07ea5758a68eb20ee509887cfc409000000000017a9140f670955696f3835ef9204dad881b1c9f3002cc7870247304402207cc7558274cd9458c44bf7554d0b049bba1388cd82057b3f8d0786bc28690aad022057eea44e01ff11c608e254a844b620f428fb515db6ef33cd199c57d3008ce89c01210303c01176f2db9efaa18e169e0ea7da87784208e05fcb1e0258ccb834805ed02d4ddc1c00"
}

  1. I want to create a raw transaction to spend both utxos and I want to spent it in two different address. So having in mind the fee for the transaction I proceed to create manually the transaction.
        $fee = number_format(floatval($this->bitcoin->estimateSmartFee(6)('feerate'))/5, 8);
        $raw_amount = 0.00000000;
        $txids = ();
        $vouts = ();
        $amounts = ();
        for($i=0; $i<count($transactions); $i++){
            $raw_amount+=number_format($transactions($i)("details")(0)("amount")*(-1),8);
            $txids() = $transactions($i)("txid");
            $vouts() = $transactions($i)("details")(0)("vout");
            $amounts() = $transactions($i)("details")(0)("amount")*(-1);
        }

        $inputs = '(';
        for($i=0; $i<count($vouts); $i++){
            $inputs.='{
                        "txid": "'.$txids($i).'",
                        "vout": '.$vouts($i).'
                      }';
            if($i+1!=count($vouts)){
                $inputs.=',';
            }
        }
        $inputs.=')';

        $minus_fee = $raw_amount-floatval($fee);
        $amount1 = $minus_fee*0.6;
        $amount2 = $minus_fee*0.4;

        $address1 = "tb1qhjcqny2rvaqd7q0700m968e3vuqctmuhhyk9fk";
        $address2 = "tb1q2nrysxf8nwxv8euwtu470wgjruw25guxkal3wd";
        $outputs = '{
                      "'.$address1.'": '.$amount1.',
                      "'.$address2.'": '.$amount2.'
                    }';

        $raw_transaction = $this->bitcoin->createrawtransaction(json_decode($inputs), json_decode($outputs));

4.1. Result of $raw_transaction is the following:

0200000002c449b7874d903584c54e7bb63a1ce475ba536da800259d7bdda208fb719e66ef0000000000ffffffffe557ba77f7cdbac37e2b37b832dcb5556da8579ba2314610cd39a6947e839e8a0000000000ffffffff02e83c080000000000160014bcb00991436740df01fe7bf65d1f31670185ef97f07d05000000000016001454c64819279b8cc3e78e5f2be7b9121f1caa238600000000
  1. I decode $raw_transaction
$decoded = $this->bitcoin->decoderawtransaction($raw_transaction);

Content of $decoded:

array(9) {
  ("txid")=>
  string(64) "e42b678b8e320fbf267d9508092c34217e506aa85ac9794c8122f8f6d4df57f3"
  ("hash")=>
  string(64) "e42b678b8e320fbf267d9508092c34217e506aa85ac9794c8122f8f6d4df57f3"
  ("version")=>
  int(2)
  ("size")=>
  int(154)
  ("vsize")=>
  int(154)
  ("weight")=>
  int(616)
  ("locktime")=>
  int(0)
  ("vin")=>
  array(2) {
    (0)=>
    array(4) {
      ("txid")=>
      string(64) "ef669e71fb08a2dd7b9d2500a86d53ba75e41c3ab67b4ec58435904d87b749c4"
      ("vout")=>
      int(0)
      ("scriptSig")=>
      array(2) {
        ("asm")=>
        string(0) ""
        ("hex")=>
        string(0) ""
      }
      ("sequence")=>
      int(4294967295)
    }
    (1)=>
    array(4) {
      ("txid")=>
      string(64) "8a9e837e94a639cd104631a29b57a86d55b5dc32b8372b7ec3bacdf777ba57e5"
      ("vout")=>
      int(0)
      ("scriptSig")=>
      array(2) {
        ("asm")=>
        string(0) ""
        ("hex")=>
        string(0) ""
      }
      ("sequence")=>
      int(4294967295)
    }
  }
  ("vout")=>
  array(2) {
    (0)=>
    array(3) {
      ("value")=>
      float(0.0053988)
      ("n")=>
      int(0)
      ("scriptPubKey")=>
      array(5) {
        ("asm")=>
        string(42) "0 bcb00991436740df01fe7bf65d1f31670185ef97"
        ("hex")=>
        string(44) "0014bcb00991436740df01fe7bf65d1f31670185ef97"
        ("reqSigs")=>
        int(1)
        ("type")=>
        string(18) "witness_v0_keyhash"
        ("addresses")=>
        array(1) {
          (0)=>
          string(42) "tb1qhjcqny2rvaqd7q0700m968e3vuqctmuhhyk9fk"
        }
      }
    }
    (1)=>
    array(3) {
      ("value")=>
      float(0.0035992)
      ("n")=>
      int(1)
      ("scriptPubKey")=>
      array(5) {
        ("asm")=>
        string(42) "0 54c64819279b8cc3e78e5f2be7b9121f1caa2386"
        ("hex")=>
        string(44) "001454c64819279b8cc3e78e5f2be7b9121f1caa2386"
        ("reqSigs")=>
        int(1)
        ("type")=>
        string(18) "witness_v0_keyhash"
        ("addresses")=>
        array(1) {
          (0)=>
          string(42) "tb1q2nrysxf8nwxv8euwtu470wgjruw25guxkal3wd"
        }
      }
    }
  }
}
  1. Now I proceed to partially sign the previous created transaction.
        $private_key = $this->bitcoin->dumpprivkey("2Msqe8jLEReQpD4CYnRj29cAqQS8c81zidv"); // cS6e5CojfNy2ELWcsB4tLFVQV9wSSQUw1PU4YrSwRSjYLaC8ikrU
        $redeemScript = "522103e33af80b92e4efd64852166d6126e10aafdcc741579c67c6aa9eb89080cebeb22102df4bf6a54f79b333b7796d2941de13b1755c00c09f4dbf696c7729aea29462102102c099a12b6cba679ad56d8e793847f422634bc84b42cb3fae0b00a6943f56921253ae";
        $vin = $raw_transaction("vin");
        $vout = $raw_transaction("vout");

        $prevtxs = '(';
        for($i=0; $i<count($vin); $i++){
            $prevtxs .= '{
                    "txid": "'.$vin($i)('txid').'",
                    "vout": '.$vin($i)('vout').',
                    "scriptPubKey": "'.$vout($i)('scriptPubKey')('hex').'", 
                    "redeemScript": "'.$redeemScript.'",
                    "amount": '.$vout($i)('value').'
                  }';
            if($i+1!=count($vouts)){
                $prevtxs.=',';
            }
        }
        $prevtxs.=')';

        $partially_signed = $this->bitcoin->signrawtransactionwithkey($raw_transaction, json_decode($private_key), json_decode($prevtxs));
  1. And in the previous step is where the process fails. The transaction is not signed and the bitcoind returns this error:
Previous output scriptPubKey mismatch:
OP_HASH160 c9ebaa3be1c12351c9e07ea5758a68eb20ee5098 OP_EQUAL
vs:
0 bcb00991436740df01fe7bf65d1f31670185ef97

I am using bitcoin testnet to test it and I think that I added all the information. If someone knows where it is the mistake, please let me know. I searched information to fix it but I could not find a solution for my issue.

Thanks

raw transaction – How to sign rawtransaction with a private key

To sign an incomplete transaction with a private key.
that's my raw transaction:

0200000001fcd54594bba82795afa84ebf500b3944546117c9371e951fc4539e7455ebafb20000000000ffffffff01b8f902000000000017a914b4e6b1471691d9e9e54cd19d39c8565bcadeb3338700000000

which is done by bitcoin-cli createrawtransaction.

This raw transaction was made in the server.

And I want to sign this raw transaction in my local PC.
How do I sign this with a private key?

Please let me know with the example code.
My language is Java.

Many Thanks