<p>I'm trying to create P2SH-Segwit bitcoin address from the WIF private key. I'm using the <strong>BitcoinJ library in Java</strong>. See the following code.</p>
<pre><code>String base58PrivateKeyString = &quot;KyyJ5vVWjZck5nsAgDWvoN1u7Q8qp5FzE8WiCq97MbnRgdLesqJZ&quot;;

NetworkParameters params = MainNetParams.get();
ECKey key;

if (base58PrivateKeyString.length() == 51 || base58PrivateKeyString.length() == 52) {
DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, base58PrivateKeyString);
            key = dumpedPrivateKey.getKey();
} else {
            BigInteger privKey = Base58.decodeToBigInteger(base58PrivateKeyString);
            key = ECKey.fromPrivate(privKey);
}
        
// Getting the public key
String pubKeyStr = key.getPublicKeyAsHex();
System.out.println(&quot;Public key is: &quot; + pubKeyStr + &quot;\n&quot;);
        
//Getting the P2SH address
List&lt;ECKey&gt; eckeyAList = new ArrayList&lt;&gt;();
eckeyAList.add(key);
Script redeemScript = ScriptBuilder.createRedeemScript(1, eckeyAList);
Script script = ScriptBuilder.createP2SHOutputScript(redeemScript);
byte[] scriptHash = ScriptPattern.extractHashFromP2SH(script);
LegacyAddress legacyAddress = LegacyAddress.fromScriptHash(params, scriptHash);
System.out.println(&quot;P2S address from the WIF pivate key is: &quot; + legacyAddress.toString()); //3Az5wdibtPRGac41aGtyqzT1ejtobvb6qW
            
</code></pre>
<p>Its output public key is 03b5319c83adf4a2e274c37401623c7bf0ba453cee3e119f3bc2c523d27059b64f.
Its <strong>output P2SH address is 3Az5wdibtPRGac41aGtyqzT1ejtobvb6qW</strong> which is far different from the <strong>correct P2SH address 38SGXvkMvq8Tsop8rFx2K4JnaFKYZkjd5z</strong>.</p>
<p>If any of you know to rectifying the issue I'm happy to take your opinion.</p>

CC BY-SA 4.0
2021-06-20T03:05:02.523Z
You would need to lost a lot more details. How did you figure out the correct address for your same script?
noone392
CC BY-SA 4.0
2021-07-04T16:38:39.483Z
There are online tools to generate from the base58 private key. Just googled it.
Lakshitha Kanchana
<bitcoinj><p2sh><java>
Generate P2SH bitcoin address from WIF in BitcoinJ Java
Lakshitha Kanchana
# Generate P2SH bitcoin address from WIF in BitcoinJ Java

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

I'm trying to create P2SH-Segwit bitcoin address from the WIF private key. I'm using the BitcoinJ library in Java. See the following code.
String base58PrivateKeyString = "KyyJ5vVWjZck5nsAgDWvoN1u7Q8qp5FzE8WiCq97MbnRgdLesqJZ";

NetworkParameters params = MainNetParams.get();
ECKey key;

if (base58PrivateKeyString.length() == 51 || base58PrivateKeyString.length() == 52) {
DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, base58PrivateKeyString);
            key = dumpedPrivateKey.getKey();
} else {
            BigInteger privKey = Base58.decodeToBigInteger(base58PrivateKeyString);
            key = ECKey.fromPrivate(privKey);
}
        
// Getting the public key
String pubKeyStr = key.getPublicKeyAsHex();
System.out.println("Public key is: " + pubKeyStr + "\n");
        
//Getting the P2SH address
List eckeyAList = new ArrayList<>();
eckeyAList.add(key);
Script redeemScript = ScriptBuilder.createRedeemScript(1, eckeyAList);
Script script = ScriptBuilder.createP2SHOutputScript(redeemScript);
byte[] scriptHash = ScriptPattern.extractHashFromP2SH(script);
LegacyAddress legacyAddress = LegacyAddress.fromScriptHash(params, scriptHash);
System.out.println("P2S address from the WIF pivate key is: " + legacyAddress.toString()); //3Az5wdibtPRGac41aGtyqzT1ejtobvb6qW
            

Its output public key is 03b5319c83adf4a2e274c37401623c7bf0ba453cee3e119f3bc2c523d27059b64f.
Its output P2SH address is 3Az5wdibtPRGac41aGtyqzT1ejtobvb6qW which is far different from the correct P2SH address 38SGXvkMvq8Tsop8rFx2K4JnaFKYZkjd5z.
If any of you know to rectifying the issue I'm happy to take your opinion.

**Tags:** bitcoinj, p2sh, java

> **Comments:**
> **username_1**: You would need to lost a lot more details. How did you figure out the correct address for your same script?  
> **username_0**: There are online tools to generate from the base58 private key. Just googled it.  

---

## 0 answers

