user111898: 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.

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"


I have sent two small transactions to the multisig address (one of 0.005 and another of 0.004)

ef669e71fb08a2dd7b9d2500a86d53ba75e41c3ab67b4ec58435904d87b749c4
8a9e837e94a639cd104631a29b57a86d55b5dc32b8372b7ec3bacdf777ba57e5


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)
}



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


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"
        }
      }
    }
  }
}


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));


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

user111544: Your redeem script hash should be:

RIPEMD160(SHA256(0x522103e33af80b92e4efd64852166d6126e10aafdcc741579c67c6aa9eb89080cebeb22102df4bf6a54f79b333b7796d2941de13b1755c00c09f4dbf696c7729aea29462102102c099a12b6cba679ad56d8e793847f422634bc84b42cb3fae0b00a6943f56921253ae))
= 0bf37781383277cbb4e544c402f5265a51f4d828

Which results this address:
2MtLR5mKgWRZBYtCYiSuaKpTUSuney9Cg2V

However you somehow end up with this hash:

c9ebaa3be1c12351c9e07ea5758a68eb20ee5098

Which results this address:
2NBet88aY57CSYCkH7nXKb2Bxae4K3xdRBT


111898: <p>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.</p>
<ol>
<li>First I create a 2-3 multisig address</li>
</ol>
<pre><code>$user1_pubkey = &quot;03e33af80b92e4efd64852166d6126e10aafdcc741579c67c6aa9eb89080cebeb2&quot;;
$user2_pubkey = &quot;02df4bf6a54f79b333b7796d2941de13b1755c00c09f4dbf696c7729aea2946210&quot;;
$user3_pubkey = $this-&gt;bitcoin-&gt;getaddressinfo(&quot;2Msqe8jLEReQpD4CYnRj29cAqQS8c81zidv&quot;)['pubkey'];
$keys[] = $user1_pubkey;
$keys[] = $user2_pubkey;
$keys[] = $user3_pubkey;
$multisig_address = $this-&gt;bitcoin-&gt;addmultisigaddress(2, $keys);

Returned values:

[&quot;address&quot;]=&gt;
    string(35) &quot;2NBet88aY57CSYCkH7nXKb2Bxae4K3xdRBT&quot;
    [&quot;redeemScript&quot;]=&gt;
    string(210) &quot;522103e33af80b92e4efd64852166d6126e10aafdcc741579c67c6aa9eb89080cebeb22102df4bf6a54f79b333b7796d2941de13b1755c00c09f4dbf696c7729aea29462102102c099a12b6cba679ad56d8e793847f422634bc84b42cb3fae0b00a6943f56921253ae&quot;
</code></pre>
<ol start="2">
<li>I have sent two small transactions to the multisig address (one of 0.005 and another of 0.004)</li>
</ol>
<pre><code>ef669e71fb08a2dd7b9d2500a86d53ba75e41c3ab67b4ec58435904d87b749c4
8a9e837e94a639cd104631a29b57a86d55b5dc32b8372b7ec3bacdf777ba57e5
</code></pre>
<ol start="3">
<li>Now I retrieve all the information related to those txids:</li>
</ol>
<pre><code>$transaction1 = $this-&gt;bitcoin-&gt;getrawtransaction(&quot;ef669e71fb08a2dd7b9d2500a86d53ba75e41c3ab67b4ec58435904d87b749c4&quot;, true);
$transaction2 = $this-&gt;bitcoin-&gt;getrawtransaction(&quot;8a9e837e94a639cd104631a29b57a86d55b5dc32b8372b7ec3bacdf777ba57e5&quot;, true);
</code></pre>
<p>3.1. Result of previous variables:</p>
<pre><code>$transaction1 =&gt;

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


$transaction2 =&gt;

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

</code></pre>
<ol start="4">
<li>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.</li>
</ol>
<pre><code>        $fee = number_format(floatval($this-&gt;bitcoin-&gt;estimateSmartFee(6)['feerate'])/5, 8);
        $raw_amount = 0.00000000;
        $txids = [];
        $vouts = [];
        $amounts = [];
        $scriptPubs = [];
        for($i=0; $i&lt;count($transactions); $i++){
            $total_amount+=$transactions[$i][&quot;vout&quot;][0][&quot;value&quot;];
            $txids[] = $transactions[$i][&quot;txid&quot;];
            $vouts[] = $transactions[$i][&quot;vout&quot;][0][&quot;n&quot;];
            $amounts[] = $transactions[$i][&quot;vout&quot;][0][&quot;value&quot;];
            $scriptPubs[] = $transactions[$i][&quot;vout&quot;][0][&quot;scriptPubKey&quot;][&quot;hex&quot;];
        }

        $inputs = '[';
        for($i=0; $i&lt;count($vouts); $i++){
            $inputs.='{
                        &quot;txid&quot;: &quot;'.$txids[$i].'&quot;,
                        &quot;vout&quot;: '.$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 = &quot;tb1qhjcqny2rvaqd7q0700m968e3vuqctmuhhyk9fk&quot;;
        $address2 = &quot;tb1q2nrysxf8nwxv8euwtu470wgjruw25guxkal3wd&quot;;
        $outputs = '{
                      &quot;'.$address1.'&quot;: '.$amount1.',
                      &quot;'.$address2.'&quot;: '.$amount2.'
                    }';

        $raw_transaction = $this-&gt;bitcoin-&gt;createrawtransaction(json_decode($inputs), json_decode($outputs));
</code></pre>
<p>4.1. Result of $raw_transaction is the following:</p>
<pre><code>0200000002c449b7874d903584c54e7bb63a1ce475ba536da800259d7bdda208fb719e66ef0000000000ffffffffe557ba77f7cdbac37e2b37b832dcb5556da8579ba2314610cd39a6947e839e8a0000000000ffffffff02e83c080000000000160014bcb00991436740df01fe7bf65d1f31670185ef97f07d050000000000160014b92b9f7f7bf6e4e1cdd73d3d23a1dbc98aa57d4e00000000
</code></pre>
<ol start="5">
<li>I decode $raw_transaction</li>
</ol>
<pre><code>$decoded = $this-&gt;bitcoin-&gt;decoderawtransaction($raw_transaction);
</code></pre>
<p>Content of $decoded:</p>
<pre><code>array(9) {
  [&quot;txid&quot;]=&gt;
  string(64) &quot;ba11b2caf11d79c1f4bc349f40895c380218e9ab91ec4c1d52aaee8284749549&quot;
  [&quot;hash&quot;]=&gt;
  string(64) &quot;ba11b2caf11d79c1f4bc349f40895c380218e9ab91ec4c1d52aaee8284749549&quot;
  [&quot;version&quot;]=&gt;
  int(2)
  [&quot;size&quot;]=&gt;
  int(154)
  [&quot;vsize&quot;]=&gt;
  int(154)
  [&quot;weight&quot;]=&gt;
  int(616)
  [&quot;locktime&quot;]=&gt;
  int(0)
  [&quot;vin&quot;]=&gt;
  array(2) {
    [0]=&gt;
    array(4) {
      [&quot;txid&quot;]=&gt;
      string(64) &quot;ef669e71fb08a2dd7b9d2500a86d53ba75e41c3ab67b4ec58435904d87b749c4&quot;
      [&quot;vout&quot;]=&gt;
      int(0)
      [&quot;scriptSig&quot;]=&gt;
      array(2) {
        [&quot;asm&quot;]=&gt;
        string(0) &quot;&quot;
        [&quot;hex&quot;]=&gt;
        string(0) &quot;&quot;
      }
      [&quot;sequence&quot;]=&gt;
      int(4294967295)
    }
    [1]=&gt;
    array(4) {
      [&quot;txid&quot;]=&gt;
      string(64) &quot;8a9e837e94a639cd104631a29b57a86d55b5dc32b8372b7ec3bacdf777ba57e5&quot;
      [&quot;vout&quot;]=&gt;
      int(0)
      [&quot;scriptSig&quot;]=&gt;
      array(2) {
        [&quot;asm&quot;]=&gt;
        string(0) &quot;&quot;
        [&quot;hex&quot;]=&gt;
        string(0) &quot;&quot;
      }
      [&quot;sequence&quot;]=&gt;
      int(4294967295)
    }
  }
  [&quot;vout&quot;]=&gt;
  array(2) {
    [0]=&gt;
    array(3) {
      [&quot;value&quot;]=&gt;
      float(0.0053988)
      [&quot;n&quot;]=&gt;
      int(0)
      [&quot;scriptPubKey&quot;]=&gt;
      array(5) {
        [&quot;asm&quot;]=&gt;
        string(42) &quot;0 bcb00991436740df01fe7bf65d1f31670185ef97&quot;
        [&quot;hex&quot;]=&gt;
        string(44) &quot;0014bcb00991436740df01fe7bf65d1f31670185ef97&quot;
        [&quot;reqSigs&quot;]=&gt;
        int(1)
        [&quot;type&quot;]=&gt;
        string(18) &quot;witness_v0_keyhash&quot;
        [&quot;addresses&quot;]=&gt;
        array(1) {
          [0]=&gt;
          string(42) &quot;tb1qhjcqny2rvaqd7q0700m968e3vuqctmuhhyk9fk&quot;
        }
      }
    }
    [1]=&gt;
    array(3) {
      [&quot;value&quot;]=&gt;
      float(0.0035992)
      [&quot;n&quot;]=&gt;
      int(1)
      [&quot;scriptPubKey&quot;]=&gt;
      array(5) {
        [&quot;asm&quot;]=&gt;
        string(42) &quot;0 b92b9f7f7bf6e4e1cdd73d3d23a1dbc98aa57d4e&quot;
        [&quot;hex&quot;]=&gt;
        string(44) &quot;0014b92b9f7f7bf6e4e1cdd73d3d23a1dbc98aa57d4e&quot;
        [&quot;reqSigs&quot;]=&gt;
        int(1)
        [&quot;type&quot;]=&gt;
        string(18) &quot;witness_v0_keyhash&quot;
        [&quot;addresses&quot;]=&gt;
        array(1) {
          [0]=&gt;
          string(42) &quot;tb1qhy4e7lmm7mjwrnwh857j8gwmex922l2wwc830y&quot;
        }
      }
    }
  }
}
</code></pre>
<ol start="6">
<li>Now I proceed to partially sign the previous created transaction.</li>
</ol>
<pre><code>        $private_key = $this-&gt;bitcoin-&gt;dumpprivkey(&quot;2Msqe8jLEReQpD4CYnRj29cAqQS8c81zidv&quot;); // cS6e5CojfNy2ELWcsB4tLFVQV9wSSQUw1PU4YrSwRSjYLaC8ikrU
        $redeemScript = &quot;522103e33af80b92e4efd64852166d6126e10aafdcc741579c67c6aa9eb89080cebeb22102df4bf6a54f79b333b7796d2941de13b1755c00c09f4dbf696c7729aea29462102102c099a12b6cba679ad56d8e793847f422634bc84b42cb3fae0b00a6943f56921253ae&quot;;
        $vin = $raw_transaction[&quot;vin&quot;];
        $vout = $raw_transaction[&quot;vout&quot;];

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

        $partially_signed = $this-&gt;bitcoin-&gt;signrawtransactionwithkey($raw_transaction, json_decode($private_key), json_decode($prevtxs));
</code></pre>
<ol start="7">
<li>And in the previous step is where the process fails. The transaction is not signed and the bitcoind returns this error:</li>
</ol>
<pre><code>redeemScript/witnessScript does not match scriptPubKey
</code></pre>
<p>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.</p>
<p>Edited:
I followed the next <a href="https://btcinformation.org/en/developer-examples#p2sh-multisig" rel="nofollow noreferrer">tutorial</a> 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: <code>redeemScript/witnessScript does not match scriptPubKey</code></p>
<p>Thanks</p>
 111544: <p>Your redeem script hash should be:</p>
<blockquote>
<p>RIPEMD160(SHA256(0x522103e33af80b92e4efd64852166d6126e10aafdcc741579c67c6aa9eb89080cebeb22102df4bf6a54f79b333b7796d2941de13b1755c00c09f4dbf696c7729aea29462102102c099a12b6cba679ad56d8e793847f422634bc84b42cb3fae0b00a6943f56921253ae))
= <strong>0bf37781383277cbb4e544c402f5265a51f4d828</strong></p>
</blockquote>
<p>Which results this address:</p>
<pre><code>2MtLR5mKgWRZBYtCYiSuaKpTUSuney9Cg2V
</code></pre>
<p>However you somehow end up with this hash:</p>
<blockquote>
<p><strong>c9ebaa3be1c12351c9e07ea5758a68eb20ee5098</strong></p>
</blockquote>
<p>Which results this address:</p>
<pre><code>2NBet88aY57CSYCkH7nXKb2Bxae4K3xdRBT
</code></pre>
