<p>The following code fixes the issue. The problem was that the class I was using didn't set the network on the bitwasp bitcoin object. The code came from <a href="https://freedomnode.com/blog/58/generate-bitcoin-wallet-addresses-from-extended-public-key-with-php" rel="nofollow noreferrer">freedom node</a>.</p>

<p><strong>hd.php</strong></p>

<pre><code>&lt;?php

require_once(__DIR__.'/../autoload.php');

use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Address\AddressCreator;
use BitWasp\Bitcoin\Key\Deterministic\HdPrefix\GlobalPrefixConfig;
use BitWasp\Bitcoin\Key\Deterministic\HdPrefix\NetworkConfig;
use BitWasp\Bitcoin\Network\Slip132\BitcoinRegistry;
use BitWasp\Bitcoin\Network\Slip132\BitcoinTestnetRegistry;
use BitWasp\Bitcoin\Key\Deterministic\Slip132\Slip132;
use BitWasp\Bitcoin\Key\KeyToScript\KeyToScriptHelper;
use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKeyFactory;
use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKeySequence;
use BitWasp\Bitcoin\Key\Deterministic\MultisigHD;
use BitWasp\Bitcoin\Network\NetworkFactory;
use BitWasp\Bitcoin\Serializer\Key\HierarchicalKey\Base58ExtendedKeySerializer;
use BitWasp\Bitcoin\Serializer\Key\HierarchicalKey\ExtendedKeySerializer;

class HD {
  private $network_name = NULL;
  private $network = NULL;
  private $xpub = NULL;
  private $ypub = NULL;
  private $zpub = NULL;
  private $multisig_xpubs = NULL;

  public function __construct($network = 'bitcoin') {
    $this-&gt;network_name = $network;
    if (version_compare(PHP_VERSION, '5.3') &gt;= 0) {
      $this-&gt;network = NetworkFactory::$network();
    } elseif (version_compare(PHP_VERSION, '5.2.3') &gt;= 0) {
      $this-&gt;network = call_user_func("NetworkFactory::$network");
    } else {
      $this-&gt;network = call_user_func('NetworkFactory', $network);
    }
    Bitcoin::setNetwork($this-&gt;network);
  }

  public function set_xpub($xpub) {
    $this-&gt;xpub = $xpub;
  }

  public function set_ypub($ypub) {
    $this-&gt;ypub = $ypub;
  }

  public function set_zpub($zpub) {
    $this-&gt;zpub = $zpub;
  }

  public function set_multisig_xpubs($xpubs) {
    $this-&gt;multisig_xpubs = $xpubs;
  }

  public function address_from_master_pub($path = '0/0') {
    if ($this-&gt;xpub === NULL &amp;&amp; $this-&gt;ypub === NULL &amp;&amp; $this-&gt;zpub === NULL) {
      throw new Exception("XPUB, YPUB or ZPUB key is not present!");
    }

    $adapter = Bitcoin::getEcAdapter();
    $slip132 = new Slip132(new KeyToScriptHelper($adapter));
    $registry_classname = 'BitWasp\\Bitcoin\\Network\\Slip132\\'.$this-&gt;network_name.'Registry';
    $bitcoin_prefixes = new $registry_classname();

    if ($this-&gt;xpub !== NULL) {
      $pubPrefix = $slip132-&gt;p2pkh($bitcoin_prefixes);
      $pub = $this-&gt;xpub;
    } else if ($this-&gt;ypub !== NULL) {
      $pubPrefix = $slip132-&gt;p2shP2wpkh($bitcoin_prefixes);
      $pub = $this-&gt;ypub;
    } else if ($this-&gt;zpub !== NULL) {
      $pubPrefix = $slip132-&gt;p2wpkh($bitcoin_prefixes);
      $pub = $this-&gt;zpub;
    }

    $config = new GlobalPrefixConfig([
      new NetworkConfig($this-&gt;network, [
        $pubPrefix,
      ])
    ]);

    $serializer = new Base58ExtendedKeySerializer(
      new ExtendedKeySerializer($adapter, $config)
    );

    $key = $serializer-&gt;parse($this-&gt;network, $pub);
    $child_key = $key-&gt;derivePath($path);

    return $child_key-&gt;getAddress(new AddressCreator())-&gt;getAddress();
  }

  public function multisig_address_from_xpub($m, $path = '0/0') {
    if (count($this-&gt;multisig_xpubs) &lt; 2) {
      throw new Exception("XPUB keys are not present!");
    }

    $keys = array();

    foreach ($this-&gt;multisig_xpubs as $xpub) {
      $keys[] = HierarchicalKeyFactory::fromExtended($xpub, $this-&gt;network);
    }

    $sequences = new HierarchicalKeySequence();
    $hd = new MultisigHD($m, 'm', $keys, $sequences, TRUE);

    $child_key = $hd-&gt;derivePath($path);

    return $child_key-&gt;getAddress()-&gt;getAddress($this-&gt;network);
  }
}
</code></pre>

CC BY-SA 4.0
2018-06-29T22:30:46.300Z
2018-06-29T22:30:46.300Z
toddmo
<p>I'm using this code to get a new address from an extended public key:</p>

<pre><code>    $index = 97;
    $change = false;
    $key = HierarchicalKeyFactory::fromExtended($tpubaddress, NetworkFactory::bitcoinTestnet());
    $path = ($change ? '1' : '0').'/'.$index;
    $child_key = $key-&gt;derivePath($path);
    $address = new PayToPubKeyHashAddress($child_key-&gt;getPublicKey()-&gt;getPubKeyHash()))-&gt;getAddress();
    echo $address;
</code></pre>

<p>This is resulting an an address like </p>

<pre><code>15fuesDBKKnEMnrZz3mT9fxbm7dkjwDN3z
</code></pre>

<p>which is a mainnet P2PKH address.</p>

<p>Is there a way I can get it to give me a testnet P2PKH address (e.g. <code>mjKxMDjShxKS5LymC43EmctDoB5Sg3n6UZ</code>)?</p>

<p>This is breaking my attempt to create a raw transaction with the address b/c it's from the wrong network. If not, should I report this as an issue on their github project?</p>

<testnet><address-generation><bip32-hd-wallets><bitwasp>
bitcoinTestnet Network returning a mainnet address
toddmo
# bitcoinTestnet Network returning a mainnet address

(Asked by: username_0 on 2018-04-24)

I'm using this code to get a new address from an extended public key:

```
    $index = 97;
    $change = false;
    $key = HierarchicalKeyFactory::fromExtended($tpubaddress, NetworkFactory::bitcoinTestnet());
    $path = ($change ? '1' : '0').'/'.$index;
    $child_key = $key->derivePath($path);
    $address = new PayToPubKeyHashAddress($child_key->getPublicKey()->getPubKeyHash()))->getAddress();
    echo $address;

```

This is resulting an an address like

```
15fuesDBKKnEMnrZz3mT9fxbm7dkjwDN3z

```

which is a mainnet P2PKH address.

Is there a way I can get it to give me a testnet P2PKH address (e.g. `mjKxMDjShxKS5LymC43EmctDoB5Sg3n6UZ`)?

This is breaking my attempt to create a raw transaction with the address b/c it's from the wrong network. If not, should I report this as an issue on their github project?

**Tags:** testnet, address-generation, bip32-hd-wallets, bitwasp


---

## 1 answers

### Answer by username_0 on 2018-06-29. Score: 3 | Accepted answer

The following code fixes the issue. The problem was that the class I was using didn't set the network on the bitwasp bitcoin object. The code came from [freedom node](https://freedomnode.com/blog/58/generate-bitcoin-wallet-addresses-from-extended-public-key-with-php).

**hd.php**

```
<?php

require_once(__DIR__.'/../autoload.php');

use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Address\AddressCreator;
use BitWasp\Bitcoin\Key\Deterministic\HdPrefix\GlobalPrefixConfig;
use BitWasp\Bitcoin\Key\Deterministic\HdPrefix\NetworkConfig;
use BitWasp\Bitcoin\Network\Slip132\BitcoinRegistry;
use BitWasp\Bitcoin\Network\Slip132\BitcoinTestnetRegistry;
use BitWasp\Bitcoin\Key\Deterministic\Slip132\Slip132;
use BitWasp\Bitcoin\Key\KeyToScript\KeyToScriptHelper;
use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKeyFactory;
use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKeySequence;
use BitWasp\Bitcoin\Key\Deterministic\MultisigHD;
use BitWasp\Bitcoin\Network\NetworkFactory;
use BitWasp\Bitcoin\Serializer\Key\HierarchicalKey\Base58ExtendedKeySerializer;
use BitWasp\Bitcoin\Serializer\Key\HierarchicalKey\ExtendedKeySerializer;

class HD {
  private $network_name = NULL;
  private $network = NULL;
  private $xpub = NULL;
  private $ypub = NULL;
  private $zpub = NULL;
  private $multisig_xpubs = NULL;

  public function __construct($network = 'bitcoin') {
    $this->network_name = $network;
    if (version_compare(PHP_VERSION, '5.3') >= 0) {
      $this->network = NetworkFactory::$network();
    } elseif (version_compare(PHP_VERSION, '5.2.3') >= 0) {
      $this->network = call_user_func("NetworkFactory::$network");
    } else {
      $this->network = call_user_func('NetworkFactory', $network);
    }
    Bitcoin::setNetwork($this->network);
  }

  public function set_xpub($xpub) {
    $this->xpub = $xpub;
  }

  public function set_ypub($ypub) {
    $this->ypub = $ypub;
  }

  public function set_zpub($zpub) {
    $this->zpub = $zpub;
  }

  public function set_multisig_xpubs($xpubs) {
    $this->multisig_xpubs = $xpubs;
  }

  public function address_from_master_pub($path = '0/0') {
    if ($this->xpub === NULL && $this->ypub === NULL && $this->zpub === NULL) {
      throw new Exception("XPUB, YPUB or ZPUB key is not present!");
    }

    $adapter = Bitcoin::getEcAdapter();
    $slip132 = new Slip132(new KeyToScriptHelper($adapter));
    $registry_classname = 'BitWasp\\Bitcoin\\Network\\Slip132\\'.$this->network_name.'Registry';
    $bitcoin_prefixes = new $registry_classname();

    if ($this->xpub !== NULL) {
      $pubPrefix = $slip132->p2pkh($bitcoin_prefixes);
      $pub = $this->xpub;
    } else if ($this->ypub !== NULL) {
      $pubPrefix = $slip132->p2shP2wpkh($bitcoin_prefixes);
      $pub = $this->ypub;
    } else if ($this->zpub !== NULL) {
      $pubPrefix = $slip132->p2wpkh($bitcoin_prefixes);
      $pub = $this->zpub;
    }

    $config = new GlobalPrefixConfig([
      new NetworkConfig($this->network, [
        $pubPrefix,
      ])
    ]);

    $serializer = new Base58ExtendedKeySerializer(
      new ExtendedKeySerializer($adapter, $config)
    );

    $key = $serializer->parse($this->network, $pub);
    $child_key = $key->derivePath($path);

    return $child_key->getAddress(new AddressCreator())->getAddress();
  }

  public function multisig_address_from_xpub($m, $path = '0/0') {
    if (count($this->multisig_xpubs) < 2) {
      throw new Exception("XPUB keys are not present!");
    }

    $keys = array();

    foreach ($this->multisig_xpubs as $xpub) {
      $keys[] = HierarchicalKeyFactory::fromExtended($xpub, $this->network);
    }

    $sequences = new HierarchicalKeySequence();
    $hd = new MultisigHD($m, 'm', $keys, $sequences, TRUE);

    $child_key = $hd->derivePath($path);

    return $child_key->getAddress()->getAddress($this->network);
  }
}

```


---

