<p>You haven't provided a derivation path in that code. For p2pkh addresses electrum uses m/j/i where m is the xprv/xpub you use, j is 0 for receive and 1 for change addresses and i is the index of the address. So first receive address would be m/0/0. </p>

CC BY-SA 4.0
2019-05-16T15:59:36.083Z
Thx, but this doesn't work either. I derived a key and send coins to this derived key, while importing its xprv into electrum as well. The sent Satoshis are gone, never appeared. Is there any way to generate a valid address were coins can be sent to? (Pls consider my Update at the bottom of my question)
delete
CC BY-SA 4.0
2019-05-18T13:24:33.233Z
@delete the $masterAddr assignment line is incorrect. You are deriving the address for m/44'/0'/0'. You should be using $addressPriv instead of $accountPriv there. BTW there is a good tool to test these things [here](http://bip32.org/)
Abdussamad
CC BY-SA 4.0
2019-05-16T15:18:29.173Z
2019-05-16T15:18:29.173Z
Abdussamad
<p>There is something I didn't understand regarding different Address formats. In short: I create a Master key, import it via xprv-Key into Electrum, generate P2SH and P2WSH-Adresses, send Coins to it but they don't appear. </p>

<ol>
<li><p>I have created an extended key on the testnet with Bitwasp: <code>tprv8ZgxMBicQKsPd1Euv7Q428UserpbDf6mxhRtjevgfPcL4XwU8aNAXqMp6QAQjPU5bCeskWBUtqZYS1UHRYQuYzrcZFppbsS2JgWjUzgK654</code>.</p></li>
<li><p>I have imported this Key into Electrum running also on testnet.</p></li>
<li>Clicking on "Receive" tells me the following address: <code>n28hWyXh8VPBCV6seKty19dW5ZNbqapB58</code></li>
<li><p>I've sent some testnet coins to this address, which became visible in Electrum very fast.</p></li>
<li><p>Using the <a href="https://github.com/Bit-Wasp/bitcoin-php" rel="nofollow noreferrer">BitWasp-Library</a> I wrote the following Code (inspired by there own <a href="https://github.com/Bit-Wasp/bitcoin-php/blob/9e535f04665ba22ba2b8b63f371ec50d2e7eabb8/examples/addresstypes.script.php#L16-18" rel="nofollow noreferrer">example-code</a>:</p></li>
</ol>



<pre><code>        $network = Bitcoin::setNetwork(NetworkFactory::bitcoinTestnet());

        $random = new Random();
        $hdFactory = new HierarchicalKeyFactory();

        $master = $hdFactory-&gt;fromExtended("tprv8ZgxMBicQKsPd1Euv7Q428UserpbDf6mxhRtjevgfPcL4XwU8aNAXqMp6QAQjPU5bCeskWBUtqZYS1UHRYQuYzrcZFppbsS2JgWjUzgK654");


        $addrReader = new AddressCreator();
        $publicKey = $master-&gt;getPublicKey();
        $helper = new P2pkhScriptDataFactory();
        $scriptData = $helper-&gt;convertKey($publicKey);
        $script = $scriptData-&gt;getScriptPubKey();

        echo "key hash types\n";
        $p2pkh = $scriptData-&gt;getAddress($addrReader);
        echo " * p2pkh address: {$p2pkh-&gt;getAddress()}\n";

        echo "\nscript hash types:\n";

        $redeemScript = new P2shScript($p2pkh-&gt;getScriptPubKey());
        $p2shAddr = $redeemScript-&gt;getAddress();
        echo " * p2sh: {$p2shAddr-&gt;getAddress()}\n";
        $p2wshScript = new WitnessScript($p2pkh-&gt;getScriptPubKey());
        $p2wshAddr = $p2wshScript-&gt;getAddress();
        echo " * p2wsh: {$p2wshAddr-&gt;getAddress()}\n";
        $p2shP2wshScript = new P2shScript(new WitnessScript($p2pkh-&gt;getScriptPubKey()));
        $p2shP2wshAddr = $p2shP2wshScript-&gt;getAddress();
        echo " * p2sh|p2wsh: {$p2shP2wshAddr-&gt;getAddress()}\n";

</code></pre>

<p>It gives me the following output:</p>

<pre><code>WIF: cUhtBXtsVhSikXiD46B5dKsCTjKqZGTWEADxSMNknP1V6VZfm1zR
key hash types
 * p2pkh address: mjCptm7yxr3c7DpA6i17ude5ZkiHQMz7Rr

script hash types:
 * p2sh: 2MsjH4FEdpcwmwaTtbW3HFnx8aw1DCfN74G
 * p2wsh: tb1qnwsk39m5yw4m3h36ddvw9euvl0vxl7vg8mrmyj777pwl74crkassxgg7l2
 * p2sh|p2wsh: 2NFS1fXMuAJuJWaGAwMi7puE86n5XnbLEPo
</code></pre>

<h1>Questions:</h1>

<ol>
<li>Why is the Receive-Address in Electrum so different from those that I've got generated with the Bitwasp-Library. None of the generated addresses is similar to the address out of Electrum</li>
<li>Sending Coins to the Address from Electrum will show up the received coins</li>
<li>Sending Coins to P2shScript-Address for instance (<code>2MsjH4FEdpcwmwaTtbW3HFnx8aw1DCfN74G</code>) will never appear in the Electrum-Wallet, even they are based out of the same private key
I've tried also sending coins to the p2wsh-Address (<code>tb1qnwsk39m5yw4m3h36ddvw9euvl0vxl7vg8mrmyj777pwl74crkassxgg7l2</code>), which also never gets any coins that was sent to.</li>
</ol>

<p>Where is my mistake?</p>

<h1>Update:</h1>

<p>after the answer from @Abdussamad I tried the following: derivePath and send coins to that address then. I also imported the xprv of the derived Key into Electrum and send out some satoshis to its address. Nothing came in.</p>

<p>Additional code:</p>

<pre><code>$purposePriv = $master-&gt;derivePath("{$purpose}'/0'/0'/0/0");
  echo "m/{$purpose}'/0'/0/0/0': ".$purposePriv-&gt;toExtendedPrivateKey().PHP_EOL;
  echo "M/{$purpose}'/0'/0/0/0': ".$purposePriv-&gt;toExtendedPublicKey().PHP_EOL;

  $purposePriv = $master-&gt;derivePath("{$purpose}'/0'/0'");

  $masterAddr = new PayToPubKeyHashAddress($purposePriv-&gt;getPublicKey()-&gt;getPubKeyHash());
  echo "   Address: " . $masterAddr-&gt;getAddress() . "\n\n";
</code></pre>

<p>Sending coins to the address will send them to elsewhere, not to the specific wallet.</p>

<h1>Update 2:</h1>

<p>I tried the following code, suggested <a href="https://github.com/Bit-Wasp/bitcoin-php/issues/792" rel="nofollow noreferrer">here</a>:</p>

<pre><code>$mnemonic = "blahblahblah";
$seed = (new Bip39SeedGenerator())-&gt;getSeed($mnemonic);
$purpose = 44;
$factory = new HierarchicalKeyFactory();
$root = $factory-&gt;fromEntropy($seed);
$accountPriv = $root-&gt;derivePath("{$purpose}'/0'/0'"); // m/44'/0'/0' (change and regular addresses come from here)
echo "master public key: ".$accountPriv-&gt;toExtendedPublicKey().PHP_EOL;
$addressPriv = $accountPriv-&gt;derivePath("0/0"); // // m/44'/0'/0'/0/0 &lt;- 0'th non-change address
$masterAddr = new PayToPubKeyHashAddress($accountPriv-&gt;getPublicKey()-&gt;getPubKeyHash());
echo "   Address: " . $masterAddr-&gt;getAddress() . "\n\n";
</code></pre>

<p>I've got:</p>

<pre><code>master public key: xpub6CXUv9gQCfQNNksArMAnKyu14vji7cK3V5wG7gYSoVntmtXTwzmhz4tFPzAiwagcdTYXXXjKCuFAQcUXM67k7cZL2sPbBEuZjp3GrwURdCo
   Address: 15e5vNCSE9iBUbRW6kdHx84XFEcQgY4zfC
</code></pre>

<p>But after importing the xpub-key into Electrum, it shows the receive-address: <strong>144aHwG4JKtpCjpJLKcB7aZq8nEidrBaKN</strong></p>

<wallet><electrum><bitwasp>
How to create valid P2SH-address with Bitwasp-Library that can be used with Electrum?
delete
# How to create valid P2SH-address with Bitwasp-Library that can be used with Electrum?

(Asked by: username_0 on 2019-05-16)

There is something I didn't understand regarding different Address formats. In short: I create a Master key, import it via xprv-Key into Electrum, generate P2SH and P2WSH-Adresses, send Coins to it but they don't appear.

1. I have created an extended key on the testnet with Bitwasp: `tprv8ZgxMBicQKsPd1Euv7Q428UserpbDf6mxhRtjevgfPcL4XwU8aNAXqMp6QAQjPU5bCeskWBUtqZYS1UHRYQuYzrcZFppbsS2JgWjUzgK654`.
2. I have imported this Key into Electrum running also on testnet.
3. Clicking on "Receive" tells me the following address: `n28hWyXh8VPBCV6seKty19dW5ZNbqapB58`
4. I've sent some testnet coins to this address, which became visible in Electrum very fast.
5. Using the [BitWasp-Library](https://github.com/Bit-Wasp/bitcoin-php) I wrote the following Code (inspired by there own [example-code](https://github.com/Bit-Wasp/bitcoin-php/blob/9e535f04665ba22ba2b8b63f371ec50d2e7eabb8/examples/addresstypes.script.php#L16-18):

```
        $network = Bitcoin::setNetwork(NetworkFactory::bitcoinTestnet());

        $random = new Random();
        $hdFactory = new HierarchicalKeyFactory();

        $master = $hdFactory->fromExtended("tprv8ZgxMBicQKsPd1Euv7Q428UserpbDf6mxhRtjevgfPcL4XwU8aNAXqMp6QAQjPU5bCeskWBUtqZYS1UHRYQuYzrcZFppbsS2JgWjUzgK654");

        $addrReader = new AddressCreator();
        $publicKey = $master->getPublicKey();
        $helper = new P2pkhScriptDataFactory();
        $scriptData = $helper->convertKey($publicKey);
        $script = $scriptData->getScriptPubKey();

        echo "key hash types\n";
        $p2pkh = $scriptData->getAddress($addrReader);
        echo " * p2pkh address: {$p2pkh->getAddress()}\n";

        echo "\nscript hash types:\n";

        $redeemScript = new P2shScript($p2pkh->getScriptPubKey());
        $p2shAddr = $redeemScript->getAddress();
        echo " * p2sh: {$p2shAddr->getAddress()}\n";
        $p2wshScript = new WitnessScript($p2pkh->getScriptPubKey());
        $p2wshAddr = $p2wshScript->getAddress();
        echo " * p2wsh: {$p2wshAddr->getAddress()}\n";
        $p2shP2wshScript = new P2shScript(new WitnessScript($p2pkh->getScriptPubKey()));
        $p2shP2wshAddr = $p2shP2wshScript->getAddress();
        echo " * p2sh|p2wsh: {$p2shP2wshAddr->getAddress()}\n";

```

It gives me the following output:

```
WIF: cUhtBXtsVhSikXiD46B5dKsCTjKqZGTWEADxSMNknP1V6VZfm1zR
key hash types
 * p2pkh address: mjCptm7yxr3c7DpA6i17ude5ZkiHQMz7Rr

script hash types:
 * p2sh: 2MsjH4FEdpcwmwaTtbW3HFnx8aw1DCfN74G
 * p2wsh: tb1qnwsk39m5yw4m3h36ddvw9euvl0vxl7vg8mrmyj777pwl74crkassxgg7l2
 * p2sh|p2wsh: 2NFS1fXMuAJuJWaGAwMi7puE86n5XnbLEPo

```

# Questions:

1. Why is the Receive-Address in Electrum so different from those that I've got generated with the Bitwasp-Library. None of the generated addresses is similar to the address out of Electrum
2. Sending Coins to the Address from Electrum will show up the received coins
3. Sending Coins to P2shScript-Address for instance (`2MsjH4FEdpcwmwaTtbW3HFnx8aw1DCfN74G`) will never appear in the Electrum-Wallet, even they are based out of the same private key
   I've tried also sending coins to the p2wsh-Address (`tb1qnwsk39m5yw4m3h36ddvw9euvl0vxl7vg8mrmyj777pwl74crkassxgg7l2`), which also never gets any coins that was sent to.

Where is my mistake?

# Update:

after the answer from @username_1 I tried the following: derivePath and send coins to that address then. I also imported the xprv of the derived Key into Electrum and send out some satoshis to its address. Nothing came in.

Additional code:

```
$purposePriv = $master->derivePath("{$purpose}'/0'/0'/0/0");
  echo "m/{$purpose}'/0'/0/0/0': ".$purposePriv->toExtendedPrivateKey().PHP_EOL;
  echo "M/{$purpose}'/0'/0/0/0': ".$purposePriv->toExtendedPublicKey().PHP_EOL;

  $purposePriv = $master->derivePath("{$purpose}'/0'/0'");

  $masterAddr = new PayToPubKeyHashAddress($purposePriv->getPublicKey()->getPubKeyHash());
  echo "   Address: " . $masterAddr->getAddress() . "\n\n";

```

Sending coins to the address will send them to elsewhere, not to the specific wallet.

# Update 2:

I tried the following code, suggested [here](https://github.com/Bit-Wasp/bitcoin-php/issues/792):

```
$mnemonic = "blahblahblah";
$seed = (new Bip39SeedGenerator())->getSeed($mnemonic);
$purpose = 44;
$factory = new HierarchicalKeyFactory();
$root = $factory->fromEntropy($seed);
$accountPriv = $root->derivePath("{$purpose}'/0'/0'"); // m/44'/0'/0' (change and regular addresses come from here)
echo "master public key: ".$accountPriv->toExtendedPublicKey().PHP_EOL;
$addressPriv = $accountPriv->derivePath("0/0"); // // m/44'/0'/0'/0/0 <- 0'th non-change address
$masterAddr = new PayToPubKeyHashAddress($accountPriv->getPublicKey()->getPubKeyHash());
echo "   Address: " . $masterAddr->getAddress() . "\n\n";

```

I've got:

```
master public key: xpub6CXUv9gQCfQNNksArMAnKyu14vji7cK3V5wG7gYSoVntmtXTwzmhz4tFPzAiwagcdTYXXXjKCuFAQcUXM67k7cZL2sPbBEuZjp3GrwURdCo
   Address: 15e5vNCSE9iBUbRW6kdHx84XFEcQgY4zfC

```

But after importing the xpub-key into Electrum, it shows the receive-address: **144aHwG4JKtpCjpJLKcB7aZq8nEidrBaKN**

**Tags:** wallet, electrum, bitwasp


---

## 1 answers

### Answer by username_1 on 2019-05-16. Score: 1

You haven't provided a derivation path in that code. For p2pkh addresses electrum uses m/j/i where m is the xprv/xpub you use, j is 0 for receive and 1 for change addresses and i is the index of the address. So first receive address would be m/0/0.

> **Comments:**
> **username_0**: Thx, but this doesn't work either. I derived a key and send coins to this derived key, while importing its xprv into electrum as well. The sent Satoshis are gone, never appeared. Is there any way to generate a valid address were coins can be sent to? (Pls consider my Update at the bottom of my question)  
> **username_1**: @username_0 the $masterAddr assignment line is incorrect. You are deriving the address for m/44'/0'/0'. You should be using $addressPriv instead of $accountPriv there. BTW there is a good tool to test these things [here](http://bip32.org/)  

---

