Bitcoin

xpub – Generate random Bitcoin addresses from your own extended public key using Coinb.in Wallet

Xpubs does not generate random addresses. This is part of a hierarchical deterministic wallet structure.

CoinBean uses its own libraries or wrappers, so when you try to hack it, it may not work as expected. To me it looks like you are feeding the xpub for the public key to solve the conversion, which is not how the extended public key (xpub) is used.

The HD derived path is equivalent to m/84’/0’/0’/0 for a BIP84 bech32 address. The example code in the question does not include derived path instructions, so it will always produce the same address.

To generate deterministically from an extended public key, you must specify the change and index being used in the derivation path, the last two numbers of the path: m/84’/0’/0‘/0. The m/84’/0′ part of the derivation path is already encoded in xpub/zpub.

for example:
The derived path for the first address in the keychain is: m/84’/0’/0’/0

The derived path for the second address in the keychain is: m/84’/0’/0’/1

Derived path of the first change address: m/84’/0’/1’/0 …

etc.

If you use any other library derived from xpub, it will look similar to below (source), requiring a derived path parameter.

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

`addr: "bc1qzsg5xf3kmdrd8629p29vtvj39ep82rhjwx58dh"`

The last two parameters of the fromXpub function pass what changes and index we want in the derived path (in this case, the first address in the keychain).

Also, as a warning, do not create “random” derived indexes. HD wallets have a preview interval where you can only see the first 20 addresses or so. For example, sending funds to m/84’/0’/34’/251 will never find them in your wallet unless you have a custom wallet. Look over there.

Related Articles

Back to top button