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;

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).

1 Answer 1


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) where derivation path parameters are needed:


`{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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.