question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I derive new Accounts from xPub?

See original GitHub issue

I am trying to derive a new address based on xPub key on Testnet.

const rootKey = bitcoin.HDNode.fromBase58(WALLET_XPUB_KEY, bitcoin.networks.testnet).neutered();
const key = { accountNumber: 20, lastAddressNumber: 10 };
const txtPath = `m/49'/1'/${key.accountNumber}'/0/${key.lastAddressNumber}`;
const child = rootKey.derivePath(txtPath);
return child.getAddress();

It fails on derivePath with message Error: Not a master node - what is the correct way of getting N’th account addresses?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dabura667commented, Feb 18, 2018
function getBIP44AccountXPub(xprv, accountNumber) {
  const rootKey = bitcoin.HDNode.fromBase58(xprv, bitcoin.networks.testnet);
  const txtPath = `m/44'/1'/${accountNumber}'`;
  const child = rootKey.derivePath(txtPath);
  return child.neutered().toBase58(); // returns xpub
}

function xPubToAddress(xpub, index, isChange) {
  if (isChange === undefined) isChange = 0
  else isChange = 1

  if (index === undefined) index = 0

  if (xpub === undefined) throw new Error('Need xpub')

  const accountRootKey = bitcoin.HDNode.fromBase58(xpub, bitcoin.networks.testnet);
  const txtPath = `${isChange}/${index}`;
  const child = accountRootKey.derivePath(txtPath);
  return child.keyPair.getAddress();
  // Note: getAddress() only returns a non-segwit address that starts with a 1 (or m/n in testnet)
  // So to generate BIP49 addresses (P2WPKH-in-P2SH) you need to manually hash the public key
  // and generate the scripts to hash for the P2SH.
}
0reactions
vzelenkocommented, Feb 18, 2018

I’d be happy to give it a shot today/tomorrow.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding the xPub and address generation
When you create a new sub-wallet (or wallet category) in the Addresses section of your wallet Settings, you generate a new xPub for...
Read more >
How to get the derived public key from xpub?
The pubkey derivation functionality is in detemrinistic.py take a look at the node_from_str() function to see about creating PubBIP32Node ...
Read more >
Extended public key (xPub) - Ledger Support
Go to the account to get the xpub for and click on the wrench icon. · Expand the Advanced logs in the Edit...
Read more >
What exactly does creating multiple "accounts" in Ledger Live ...
A new account in ledger live will perform a new account level hardened derivation and create an associated xpub as per BIP44.
Read more >
hd-wallet-derive/README.md at master - GitHub
Derive addresses from xpub key; We can derive segwit keys ... We can easily generate a new random master key, seed and extended...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found