<p>Based on your example it looks like you are signing the same sighash twice, so it only looks like one key signed. </p>

<p>This example might help: <a href="https://github.com/magmahindenburg/MultisigExamples/blob/master/src/se/moar/bitcoin/multisig/MultisigPresentation.java" rel="nofollow noreferrer">https://github.com/magmahindenburg/MultisigExamples/blob/master/src/se/moar/bitcoin/multisig/MultisigPresentation.java</a></p>

<pre><code>    // Sign the first part of the transaction using private key #1
    Sha256Hash sighash = spendTx.hashForSignature(0, redeemScript, Transaction.SigHash.ALL, false);
    ECKey.ECDSASignature ecdsaSignature = key1.sign(sighash);
    TransactionSignature transactionSignarture = new TransactionSignature(ecdsaSignature, Transaction.SigHash.ALL, false);

    // Create the sighash2 using the redeem script
    Sha256Hash sighash2 = spendTx.hashForSignature(0, redeemScript, Transaction.sighash2.ALL, false);
    ECKey.ECDSASignature secondSignature;

    // Take out the key and sign the signhash
    ECKey key2 = createKeyFromSha256Passphrase("Super secret key 2");
    secondSignature = key2.sign(sighash2);

    // Add the second signature to the signature list
    TransactionSignature transactionSignarture = new TransactionSignature(secondSignature, Transaction.sighash2.ALL, false);
    signatureList.add(transactionSignarture);

    // Rebuild p2sh multisig input script
    inputScript = ScriptBuilder.createP2SHMultiSigInputScript(signatureList, redeemScript);
    spendTx.getInput(0).setScriptSig(inputScript);

    System.out.println(byteArrayToHex(spendTx.bitcoinSerialize()));
</code></pre>

CC BY-SA 4.0
2019-12-26T06:24:14.923Z
Thank you very much to spend time thinking my problem. The example you give is what my code refer and i simplify the code to one function. You give me a direction to solve the problem，the key problem maybe is you said "signing the same sighash twice", i will try later. Thank you again.
little fish
CC BY-SA 4.0
2019-12-27T02:33:18.887Z
sorry for that i can not click the useful button for my low  reputation
little fish
CC BY-SA 4.0
2019-12-24T05:17:29.990Z
2019-12-24T05:17:29.990Z
m1xolyd1an
<p>i am try to spend the coins on a p2sh</p>

<p>first, i generate p2sh and send coins to that p2sh</p>

<pre><code>ECKey clientKey = new ECKey();
ECKey serverKey = new ECKey();
ECKey thirdPartyKey = new ECKey();
List&lt;ECKey&gt; keys = ImmutableList.of(clientKey, serverKey,thirdPartyKey);
Script multisigScript = ScriptBuilder.createP2SHOutputScript(2, keys);
String address = multisigScript.getToAddress(params).toString();
</code></pre>

<p>then i send coins to the "address" on testnet3</p>

<p>second, i use the wallet in bitcoinj to watch the "address" and get the tx</p>

<pre><code>WalletAppKit walletAppKit = new WalletAppKit(params, new File("./btcWallet"), "test");
walletAppKit.startAsync();
walletAppKit.awaitRunning();
Wallet walletTemp = walletAppKit.wallet();
Address watchAddress = Address.fromString(params, "2NGY4n3Wc2iftWufhUg2MzZtccbkQWgdUiz");
walletTemp.addWatchedAddress(watchAddress,1576573079);
 Set&lt;Transaction&gt; transSet = walletTemp.getTransactions(false);
</code></pre>

<p>third, after i get the tx and the output, i want to spend it</p>

<pre><code>TransactionOutput out = ...//output that the second step i get from the tx
Script scriptOut = out.getScriptPubKey()
Coin outValue = Coin.valueOf(10000);
Script redeemScript = ScriptBuilder.createRedeemScript(2, keys);
Address sendAddress = LegacyAddress.fromBase58(params, "2N6846hvswyZEdJ5DuEcjQZPaT5Xwb6PdGT");
Transaction spendTx = new Transaction(params);
spendTx.addOutput(outValue, sendAddress);
TransactionInput input = spendTx.addInput(out);

Sha256Hash sighash = spendTx.hashForSignature(0, redeemScript, Transaction.SigHash.ALL,
                        false);
ECKey.ECDSASignature cSignature = clientKey.sign(sighash);
ECKey.ECDSASignature sSignature = serverKey.sign(sighash);
TransactionSignature cTransactionSignature = new TransactionSignature(cSignature, Transaction.SigHash.ALL,
                        false);
TransactionSignature sTransactionSignature = new TransactionSignature(sSignature, Transaction.SigHash.ALL,
                        false);

Script inputScript = ScriptBuilder.createP2SHMultiSigInputScript(ImmutableList.of(cTransactionSignature, sTransactionSignature), scriptOut);
input.setScriptSig(inputScript);
input.verify(out);
</code></pre>

<p>it have error at "input.verify(out);",<code>Script resulted in a non-true stack: [] [3044022043d0cdddb087c4db2937a3b656c37bf240fd95e16714125c0672875bb18b4c1902200f96e1ec0adc2258e324cbacba598cb72d502a4e3185212dea40c50b365d3fb901] []</code></p>

<p>i do not kown where my code is wrong and how to solve it.</p>

<p>find a similar problem, but not the same [1]: <a href="https://bitcoin.stackexchange.com/questions/83589/errors-when-building-sending-multisig-transactions/83592#83592?newreg=db616c49d87946e5bf78ab5ca583321d">Errors when building/sending multisig transactions</a></p>

CC BY-SA 4.0
2019-12-24T02:09:29.353Z
Your second signature in a 2 of 3 multisig should be signing the resulting tx hex from the first signature. Based on a quick glance of your example looks like you are signing the same `sighash` twice, so it only looks like one key signed.
m1xolyd1an
CC BY-SA 4.0
2019-12-24T02:54:13.327Z
how can i sign from the first signature，i change the code like this：Sha256Hash sighash1 = spendTx.hashForSignature(0, redeemScript, Transaction.SigHash.ALL,
                            false);
                    ECKey.ECDSASignature cSignature = clientKey.sign(sighash1);

                    Sha256Hash sighash2 = spendTx.hashForSignature(0, redeemScript, Transaction.SigHash.ALL,
                            false);
                    ECKey.ECDSASignature sSignature = serverKey.sign(sighash2);
little fish
<bitcoinj><p2sh>
spend from p2sh with bitcoinj
little fish
# spend from p2sh with bitcoinj

(Asked by: username_0 on 2019-12-20)

i am try to spend the coins on a p2sh

first, i generate p2sh and send coins to that p2sh

```
ECKey clientKey = new ECKey();
ECKey serverKey = new ECKey();
ECKey thirdPartyKey = new ECKey();
List<ECKey> keys = ImmutableList.of(clientKey, serverKey,thirdPartyKey);
Script multisigScript = ScriptBuilder.createP2SHOutputScript(2, keys);
String address = multisigScript.getToAddress(params).toString();

```

then i send coins to the "address" on testnet3

second, i use the wallet in bitcoinj to watch the "address" and get the tx

```
WalletAppKit walletAppKit = new WalletAppKit(params, new File("./btcWallet"), "test");
walletAppKit.startAsync();
walletAppKit.awaitRunning();
Wallet walletTemp = walletAppKit.wallet();
Address watchAddress = Address.fromString(params, "2NGY4n3Wc2iftWufhUg2MzZtccbkQWgdUiz");
walletTemp.addWatchedAddress(watchAddress,1576573079);
 Set<Transaction> transSet = walletTemp.getTransactions(false);

```

third, after i get the tx and the output, i want to spend it

```
TransactionOutput out = ...//output that the second step i get from the tx
Script scriptOut = out.getScriptPubKey()
Coin outValue = Coin.valueOf(10000);
Script redeemScript = ScriptBuilder.createRedeemScript(2, keys);
Address sendAddress = LegacyAddress.fromBase58(params, "2N6846hvswyZEdJ5DuEcjQZPaT5Xwb6PdGT");
Transaction spendTx = new Transaction(params);
spendTx.addOutput(outValue, sendAddress);
TransactionInput input = spendTx.addInput(out);

Sha256Hash sighash = spendTx.hashForSignature(0, redeemScript, Transaction.SigHash.ALL,
                        false);
ECKey.ECDSASignature cSignature = clientKey.sign(sighash);
ECKey.ECDSASignature sSignature = serverKey.sign(sighash);
TransactionSignature cTransactionSignature = new TransactionSignature(cSignature, Transaction.SigHash.ALL,
                        false);
TransactionSignature sTransactionSignature = new TransactionSignature(sSignature, Transaction.SigHash.ALL,
                        false);

Script inputScript = ScriptBuilder.createP2SHMultiSigInputScript(ImmutableList.of(cTransactionSignature, sTransactionSignature), scriptOut);
input.setScriptSig(inputScript);
input.verify(out);

```

it have error at "input.verify(out);",`Script resulted in a non-true stack: [] [3044022043d0cdddb087c4db2937a3b656c37bf240fd95e16714125c0672875bb18b4c1902200f96e1ec0adc2258e324cbacba598cb72d502a4e3185212dea40c50b365d3fb901] []`

i do not kown where my code is wrong and how to solve it.

find a similar problem, but not the same [1]: [Errors when building/sending multisig transactions](https://bitcoin.stackexchange.com/questions/83589/errors-when-building-sending-multisig-transactions/83592#83592?newreg=db616c49d87946e5bf78ab5ca583321d)

**Tags:** bitcoinj, p2sh

> **Comments:**
> **username_1**: Your second signature in a 2 of 3 multisig should be signing the resulting tx hex from the first signature. Based on a quick glance of your example looks like you are signing the same `sighash` twice, so it only looks like one key signed.  
> **username_0**: how can i sign from the first signature，i change the code like this：Sha256Hash sighash1 = spendTx.hashForSignature(0, redeemScript, Transaction.SigHash.ALL,                             false);                     ECKey.ECDSASignature cSignature = clientKey.sign(sighash1);                      Sha256Hash sighash2 = spendTx.hashForSignature(0, redeemScript, Transaction.SigHash.ALL,                             false);                     ECKey.ECDSASignature sSignature = serverKey.sign(sighash2);  

---

## 1 answers

### Answer by username_1 on 2019-12-24. Score: 1

Based on your example it looks like you are signing the same sighash twice, so it only looks like one key signed.

This example might help: <https://github.com/magmahindenburg/MultisigExamples/blob/master/src/se/moar/bitcoin/multisig/MultisigPresentation.java>

```
    // Sign the first part of the transaction using private key #1
    Sha256Hash sighash = spendTx.hashForSignature(0, redeemScript, Transaction.SigHash.ALL, false);
    ECKey.ECDSASignature ecdsaSignature = key1.sign(sighash);
    TransactionSignature transactionSignarture = new TransactionSignature(ecdsaSignature, Transaction.SigHash.ALL, false);

    // Create the sighash2 using the redeem script
    Sha256Hash sighash2 = spendTx.hashForSignature(0, redeemScript, Transaction.sighash2.ALL, false);
    ECKey.ECDSASignature secondSignature;

    // Take out the key and sign the signhash
    ECKey key2 = createKeyFromSha256Passphrase("Super secret key 2");
    secondSignature = key2.sign(sighash2);

    // Add the second signature to the signature list
    TransactionSignature transactionSignarture = new TransactionSignature(secondSignature, Transaction.sighash2.ALL, false);
    signatureList.add(transactionSignarture);

    // Rebuild p2sh multisig input script
    inputScript = ScriptBuilder.createP2SHMultiSigInputScript(signatureList, redeemScript);
    spendTx.getInput(0).setScriptSig(inputScript);

    System.out.println(byteArrayToHex(spendTx.bitcoinSerialize()));

```

> **Comments:**
> **username_0**: Thank you very much to spend time thinking my problem. The example you give is what my code refer and i simplify the code to one function. You give me a direction to solve the problem，the key problem maybe is you said "signing the same sighash twice", i will try later. Thank you again.  
> **username_0**: sorry for that i can not click the useful button for my low  reputation  

---

