<p>solved</p>
<pre><code>Script redeemScript = ScriptBuilder.createP2WPKHOutputScript(key);

Script witnessScript = ScriptBuilder.createP2PKHOutputScript(key);

TransactionSignature signature = tx.calculateWitnessSignature(inputIndex, key, witnessScript, txIn.getValue(), SigHash.ALL,
        false);

txIn.setWitness(TransactionWitness.redeemP2WPKH(signature, key));
txIn.setScriptSig(new ScriptBuilder().data(redeemScript.getProgram()).build());
</code></pre>

CC BY-SA 4.0
2021-02-19T07:54:49.143Z
2021-02-19T07:54:49.143Z
lingting
<p>I wrote the code for transmitting btc from p2sh-p2wpkh based on the information I found, but when I tried to broadcast the original transaction in <a href="https://live.blockcypher.com/btc-testnet/pushtx/" rel="nofollow noreferrer">https://live.blockcypher.com/btc-testnet/pushtx/</a>, an exception occurred and the exception message was :</p>
<pre><code>Error validating transaction: Error running script for input 0 referencing 
f6ff7b391590c3c161f012e7313c9bd7379846a209219a4722cb8864190466ae at 0: Script was NOT verified successfully..
</code></pre>
<p>This is my broadcast data:</p>
<pre><code>01000000000101ae6604196488cb22479a2109a2469837d79b3c31e712f061c1c39015397bfff600000000160014208a8852a3cec93382c8bebbbd6c9ef4676f5120ffffffff02a08601000000000017a9141f396a0c4994cae1820224bb249a21de3ad4e73287acfd13000000000017a91466e9ecd8e67b63a4022f0420f772a070fa7d6e608702483045022100f3e5b7483760ff764b5a7620ba341499ec41c5b2fd67917bfef233b547f6e5e7022071b3f96e9c1d271b723f161a7a0aeafddef16aeb053fe2b443c0e4d31a785a7a012103ca65ac405e0ea1b514c61b797035674a27457fea63a8972cd473eef05871247400000000
</code></pre>
<p>I have checked a lot of information, but I can't find the problem. I hope to get help!</p>
<p>The test method I executed:
<a href="https://github.com/lingting/VirtualCurrency/blob/0.3.4/src/test/java/live/lingting/virtual/currency/omni/Transfer.java#L387" rel="nofollow noreferrer">https://github.com/lingting/VirtualCurrency/blob/0.3.4/src/test/java/live/lingting/virtual/currency/omni/Transfer.java#L387</a></p>
<p>I signed the p2sh-p2wpkh address processing code:
<a href="https://github.com/lingting/VirtualCurrency/blob/0.3.4/src/main/java/live/lingting/virtual/currency/service/impl/BtcOmniServiceImpl.java#L414" rel="nofollow noreferrer">https://github.com/lingting/VirtualCurrency/blob/0.3.4/src/main/java/live/lingting/virtual/currency/service/impl/BtcOmniServiceImpl.java#L414</a></p>
<pre><code>// Custom redemption script
Script redeemScript = new ScriptBuilder().smallNum(0).data(keys.get(0).getPubKeyHash()).build();
TransactionSignature signature;
signature = tx.calculateWitnessSignature(inputIndex, key, redeemScript, txIn.getValue(), SigHash.ALL,false);
txIn.setScriptSig(redeemScript);
// p2sh-p2wpkh 设置 witness
txIn.setWitness(TransactionWitness.redeemP2WPKH(signature, keys.get(0)));
</code></pre>

<bitcoin-core><transactions><raw-transaction><bitcoinj>
How to use bitcoinj to transfer btc from p2sh-p2wpkh
lingting
# How to use bitcoinj to transfer btc from p2sh-p2wpkh

(Asked by: username_0 on 2021-02-19)

I wrote the code for transmitting btc from p2sh-p2wpkh based on the information I found, but when I tried to broadcast the original transaction in <https://live.blockcypher.com/btc-testnet/pushtx/>, an exception occurred and the exception message was :

```
Error validating transaction: Error running script for input 0 referencing 
f6ff7b391590c3c161f012e7313c9bd7379846a209219a4722cb8864190466ae at 0: Script was NOT verified successfully..

```

This is my broadcast data:

```
01000000000101ae6604196488cb22479a2109a2469837d79b3c31e712f061c1c39015397bfff600000000160014208a8852a3cec93382c8bebbbd6c9ef4676f5120ffffffff02a08601000000000017a9141f396a0c4994cae1820224bb249a21de3ad4e73287acfd13000000000017a91466e9ecd8e67b63a4022f0420f772a070fa7d6e608702483045022100f3e5b7483760ff764b5a7620ba341499ec41c5b2fd67917bfef233b547f6e5e7022071b3f96e9c1d271b723f161a7a0aeafddef16aeb053fe2b443c0e4d31a785a7a012103ca65ac405e0ea1b514c61b797035674a27457fea63a8972cd473eef05871247400000000

```

I have checked a lot of information, but I can't find the problem. I hope to get help!

The test method I executed:
<https://github.com/username_0/VirtualCurrency/blob/0.3.4/src/test/java/live/username_0/virtual/currency/omni/Transfer.java#L387>

I signed the p2sh-p2wpkh address processing code:
<https://github.com/username_0/VirtualCurrency/blob/0.3.4/src/main/java/live/username_0/virtual/currency/service/impl/BtcOmniServiceImpl.java#L414>

```
// Custom redemption script
Script redeemScript = new ScriptBuilder().smallNum(0).data(keys.get(0).getPubKeyHash()).build();
TransactionSignature signature;
signature = tx.calculateWitnessSignature(inputIndex, key, redeemScript, txIn.getValue(), SigHash.ALL,false);
txIn.setScriptSig(redeemScript);
// p2sh-p2wpkh 设置 witness
txIn.setWitness(TransactionWitness.redeemP2WPKH(signature, keys.get(0)));

```

**Tags:** bitcoin-core, transactions, raw-transaction, bitcoinj


---

## 1 answers

### Answer by username_0 on 2021-02-19. Score: 1

solved

```
Script redeemScript = ScriptBuilder.createP2WPKHOutputScript(key);

Script witnessScript = ScriptBuilder.createP2PKHOutputScript(key);

TransactionSignature signature = tx.calculateWitnessSignature(inputIndex, key, witnessScript, txIn.getValue(), SigHash.ALL,
        false);

txIn.setWitness(TransactionWitness.redeemP2WPKH(signature, key));
txIn.setScriptSig(new ScriptBuilder().data(redeemScript.getProgram()).build());

```


---

