<p>In general is should be like:</p>
<pre><code>// input index to sign
int index = 0;
// amount to sign (must equal to actual input value)
Coin value = Coin.valueOf(123456);

TransactionSignature signature = tx.calculateWitnessSignature(
   index, 
   prvKey, 
   ScriptBuilder()
      .data(ScriptBuilder.createP2PKHOutputScript(prvKey).program)
      .build(), 
   coin, 
   Transaction.SigHash.ALL, 
   false
);

tx.getInput(index).setScriptSig(ScriptBuilder.createEmpty());
tx.getInput(index).setWitness(
  TransactionWitness.redeemP2WPKH(signature, prvKey)
);
</code></pre>

CC BY-SA 4.0
2024-01-22T07:59:26.243Z
Most importantly, **double-check that your outpoint txids are reversed (to little-endian) the sighash before signing**. Maybe BitcoinJ does this for you, but since I never used it I'm not sure if it does.
Zenul_Abidin
CC BY-SA 4.0
2020-09-07T20:49:07.403Z
2020-09-07T20:49:07.403Z
Igor Artamonov
<p>I want to spend from a segwit address and it has a P2WPH script, I created this transaction just like creating a normal P2PKH transaction, but just changed the address of the input to the segwit address, I don't know if this is correct, and signing this transaction in the following method, but it says script was NOT verified successfully...</p>

<pre><code>Transaction signedtx = signInput(rawtx.getHashAsString(),privkey,rawtx);

    public static Transaction signInput(String hash, String prvKey,Transaction spendTx) {
        ECKey ecKey = ECKey.fromPrivate(Utils.HEX.decode(prvKey));
        ECKey.ECDSASignature signature = ecKey.sign(Sha256Hash.wrap(hash));
        TransactionSignature transactionSignature = new TransactionSignature(signature,Transaction.SigHash.ALL,true);
        TransactionWitness witness = TransactionWitness.redeemP2WPKH(transactionSignature,ecKey);
        TransactionInput input = spendTx.getInput(0);
        input.setWitness(witness);
        return spendTx;
    }
</code></pre>

<p>I am not very familiar with bitcoinj, and currently there is very little technical guidance related to segwit, I hope someone can help me！</p>

<segregated-witness><bitcoinj><java>
Fail to sign the Segwit Transaction's input in bitcoinj
Oiiiwk
# Fail to sign the Segwit Transaction's input in bitcoinj

(Asked by: username_0 on 2020-03-27)

I want to spend from a segwit address and it has a P2WPH script, I created this transaction just like creating a normal P2PKH transaction, but just changed the address of the input to the segwit address, I don't know if this is correct, and signing this transaction in the following method, but it says script was NOT verified successfully...

```
Transaction signedtx = signInput(rawtx.getHashAsString(),privkey,rawtx);

    public static Transaction signInput(String hash, String prvKey,Transaction spendTx) {
        ECKey ecKey = ECKey.fromPrivate(Utils.HEX.decode(prvKey));
        ECKey.ECDSASignature signature = ecKey.sign(Sha256Hash.wrap(hash));
        TransactionSignature transactionSignature = new TransactionSignature(signature,Transaction.SigHash.ALL,true);
        TransactionWitness witness = TransactionWitness.redeemP2WPKH(transactionSignature,ecKey);
        TransactionInput input = spendTx.getInput(0);
        input.setWitness(witness);
        return spendTx;
    }

```

I am not very familiar with bitcoinj, and currently there is very little technical guidance related to segwit, I hope someone can help me！

**Tags:** segregated-witness, bitcoinj, java


---

## 1 answers

### Answer by username_1 on 2020-09-07. Score: 0

In general is should be like:

```
// input index to sign
int index = 0;
// amount to sign (must equal to actual input value)
Coin value = Coin.valueOf(123456);

TransactionSignature signature = tx.calculateWitnessSignature(
   index, 
   prvKey, 
   ScriptBuilder()
      .data(ScriptBuilder.createP2PKHOutputScript(prvKey).program)
      .build(), 
   coin, 
   Transaction.SigHash.ALL, 
   false
);

tx.getInput(index).setScriptSig(ScriptBuilder.createEmpty());
tx.getInput(index).setWitness(
  TransactionWitness.redeemP2WPKH(signature, prvKey)
);

```

> **Comments:**
> **username_2**: Most importantly, **double-check that your outpoint txids are reversed (to little-endian) the sighash before signing**. Maybe BitcoinJ does this for you, but since I never used it I'm not sure if it does.  

---

