<p>Xpubs do not create random addresses. They are part of the Hierarchal Deterministic wallet structure.</p>
<p>Coinbin uses his own library or wrapper so it may not work as you expect when trying to hack it. To me it looks like you are feeding an xpub into a public key to address conversion which isn't how an extended public key (xpub) is used.</p>
<p>An HD derivation path looks like m/84'/0'/0'/0 for BIP84 bech32 addresses. The example code in your question does not contain any derivation path instructions, so you will always create the same address.</p>
<p>In order to deterministically create from an extended public key you need to specify which change and index you are using in the derivation path which is the last two numbers in the path: m/84'/0'/<em><strong>0</strong></em>'/<em><strong>0</strong></em>. The m/84'/0' portion of the derivation path is already encoded in your xpub/zpub.</p>
<p>For example:<br />
The derivation path for the first address in the keychain would be: m/84'/0'/0'/0</p>
<p>The derivation path for the second address in the keychain would be: m/84'/0'/0'/1</p>
<p>The derivation path for the first change address: m/84'/0'/1'/0 ...</p>
<p>and so on.</p>
<p>Although using a different library deriving from an xpub should look something more like the below (<a href="https://github.com/coinables/buidljs#5-hd-wallets-generate-new-mnemonic-convert-to-seed-extended-public-key-and-address" rel="nofollow noreferrer">source</a>) where derivation path parameters are needed:</p>
<pre><code>buidljs.fromXpub(&quot;zpub6sCAx5BGhzosvmBsLyj8P1xjQHmuwHMoFm4ykHMmeP16Zn6iKpxy4BBDsHPRj4RsU5YvXigGvnYRYo4n5sA4QH2kgZUoTgQVTmzt5XuC6qD&quot;,0,0)

`{addr: &quot;bc1qzsg5xf3kmdrd8629p29vtvj39ep82rhjwx58dh&quot;}`
</code></pre>
<p>Note the last two parameters of the fromXpub function communicates which change and index from the derivation path we want, in this case the first address in the keychain.</p>
<p>Also just as a warning, do not generate &quot;random&quot; derivation indexes. HD wallets have look-ahead gaps which can only see about 20 address ahead, sending funds to m/84'/0'/34'/251 for example could possibly never be found by a wallet unless you had a custom wallet that knew to look there.</p>

CC BY-SA 4.0
2022-05-26T04:27:34.310Z
2022-05-26T04:27:34.310Z
m1xolyd1an
<p>I'm customizing <a href="https://coinb.in/" rel="nofollow noreferrer">https://coinb.in/</a> wallet.</p>
<pre><code>    var compressed = coinjs.compressed;
    coinjs.compressed = true;
    var coin = coinjs.newKeys(); // keys are changing... 
    var sw = coinjs.bech32Address(coin.pubkey);
    var address = sw.address;

    coinjs.compressed = compressed;
    console.log(address);
</code></pre>
<p>This works as expected, but it generates new bitcoin addresses because of <strong>newKeys()</strong>. I don't own them.</p>
<p>I need set my own xpub. So I'm trying this:</p>
<pre><code>    var pubkey = &quot;xpub661MyMwAqRbcFAYRXx71gFgohbagK22EwLd24pQE98PfQKvtJDP6bFWge2N6WJjhhNriBYdjHadGwtFj8HaTG6s4HnprSpNZ7x2xo9Ag4RU&quot;; // I need set my own xpub.
    var b32 = coinjs.bech32Address(pubkey);
    addr = b32.address;
    console.log(addr); // bc1q9m6yz90ekwdrgum294w7z85cth7muk600gnvxh
</code></pre>
<p>The issue here is that <strong>it gives always the same address</strong> (bc1q9m6yz90ekwdrgum294w7z85cth7muk600gnvxh).</p>
<p>I need somehow generate always new random bitcoin addresses (from my own xpub).</p>

<xpub>
Generate Random Bitcoin Addresses from my own Extended Public Key with Coinb.in Wallet
Jozef Pintér
# Generate Random Bitcoin Addresses from my own Extended Public Key with Coinb.in Wallet

(Asked by: username_0 on 2022-05-25)

I'm customizing <https://coinb.in/> wallet.

```
    var compressed = coinjs.compressed;
    coinjs.compressed = true;
    var coin = coinjs.newKeys(); // keys are changing... 
    var sw = coinjs.bech32Address(coin.pubkey);
    var address = sw.address;

    coinjs.compressed = compressed;
    console.log(address);

```

This works as expected, but it generates new bitcoin addresses because of **newKeys()**. I don't own them.

I need set my own xpub. So I'm trying this:

```
    var pubkey = "xpub661MyMwAqRbcFAYRXx71gFgohbagK22EwLd24pQE98PfQKvtJDP6bFWge2N6WJjhhNriBYdjHadGwtFj8HaTG6s4HnprSpNZ7x2xo9Ag4RU"; // I need set my own xpub.
    var b32 = coinjs.bech32Address(pubkey);
    addr = b32.address;
    console.log(addr); // bc1q9m6yz90ekwdrgum294w7z85cth7muk600gnvxh

```

The issue here is that **it gives always the same address** (bc1q9m6yz90ekwdrgum294w7z85cth7muk600gnvxh).

I need somehow generate always new random bitcoin addresses (from my own xpub).

**Tags:** xpub


---

## 1 answers

### Answer by username_1 on 2022-05-26. Score: 2 | Accepted answer

Xpubs do not create random addresses. They are part of the Hierarchal Deterministic wallet structure.

Coinbin uses his own library or wrapper so it may not work as you expect when trying to hack it. To me it looks like you are feeding an xpub into a public key to address conversion which isn't how an extended public key (xpub) is used.

An HD derivation path looks like m/84'/0'/0'/0 for BIP84 bech32 addresses. The example code in your question does not contain any derivation path instructions, so you will always create the same address.

In order to deterministically create from an extended public key you need to specify which change and index you are using in the derivation path which is the last two numbers in the path: m/84'/0'/***0***'/***0***. The m/84'/0' portion of the derivation path is already encoded in your xpub/zpub.

For example:  
The derivation path for the first address in the keychain would be: m/84'/0'/0'/0

The derivation path for the second address in the keychain would be: m/84'/0'/0'/1

The derivation path for the first change address: m/84'/0'/1'/0 ...

and so on.

Although using a different library deriving from an xpub should look something more like the below ([source](https://github.com/coinables/buidljs#5-hd-wallets-generate-new-mnemonic-convert-to-seed-extended-public-key-and-address)) where derivation path parameters are needed:

```
buidljs.fromXpub("zpub6sCAx5BGhzosvmBsLyj8P1xjQHmuwHMoFm4ykHMmeP16Zn6iKpxy4BBDsHPRj4RsU5YvXigGvnYRYo4n5sA4QH2kgZUoTgQVTmzt5XuC6qD",0,0)

`{addr: "bc1qzsg5xf3kmdrd8629p29vtvj39ep82rhjwx58dh"}`

```

Note the last two parameters of the fromXpub function communicates which change and index from the derivation path we want, in this case the first address in the keychain.

Also just as a warning, do not generate "random" derivation indexes. HD wallets have look-ahead gaps which can only see about 20 address ahead, sending funds to m/84'/0'/34'/251 for example could possibly never be found by a wallet unless you had a custom wallet that knew to look there.


---

