1

I'm new to Ledger.

As a software engineer, I'd like to write my own app (for my own personal use) that is sort of like a more basic version of Ledger Live.

I want to show my balances on my own private Google Sheet that I'm building for myself.

I want to use Apps Scripts to fetch totals from all of the relevant blockchains (ideally via a simple, free API) in a format like:

{
  BTC: 0.1,
  ETH: 1,
  NEAR: 20
}

where each ticker shows the total (sum) of all tokens that my Ledger "holds".

Since Bitcoin, Ethereum, and other blockchains are all public, I figured I'd easily be able to look up my wallets' balances via the public wallet addresses.

But now my understanding is that my Ledger's public wallet addresses for each blockchain change after transactions.

So how can I accomplish my goal?

Again, I want a simple sum of the tokens as shown in the JSON above.

And I want that sum to be retrievable periodically automatically without me needing to log in on my Ledger.

Thanks!

P.S. I found Is it possible to use xpub address to monitor balance from previously generated addresses? and How to know balance in Ledger Nano S?

But when I go to Ledger Live > Accounts > Bitcoin > wrench icon > Advanced and grab my "xpub" string and go browse it at https://www.blockchain.com/btc/xpub/xxxx, I see 20 wallet addresses (split across 4 pages) with 0 transactions each. So it seems like these are future wallets instead of the ones that my Ledger has used so far.

3 Answers 3

2

Blockonomics API supports balance and tx history of xpub/ypub/zpub

You can use the following sites to search address balance/history of xpub/ypub/zpub :

2
  • It looks like it requires registering an email address (and maybe also paying?), which I might not be willing to do, but I will check out github.com/blockonomics/blockonomics-node#methods. Thanks. I'm very surprised that Ledger hasn't provided an easier way to track balances. I don't want to have to code a solution for each blockchain.
    – Ryan
    Commented Nov 24, 2022 at 14:16
  • 1
    Yes email address is required for the APIKey. Its free as you long as you query withing their limits blockonomics.co/views/api.html#limits Commented Nov 24, 2022 at 14:41
3

I will not repeat the mental model of how keys, addresses, and balances work on Bitcoin, let's assume you're familiar with this: https://bitcoin.stackexchange.com/a/115830/137501

One more thing to note about:

I still don't have the full answer, and the following is only for BTC (I'd prefer an answer that works across many blockchains)

The answer you get for Bitcoin can only apply to other Bitcoin-tech blockchains (like Litecoin, Bitcoin Cash, etc.). Other blockchains like Ethereum use totally different schemes, which I'm not familiar with at all.

Ok, so you know that in order to find your UTXOs you need to find some good libraries that implement these 3 BIPs:

Also, SatoshiLabs (Trezor wallet manufacturer) maintains a list of derivation paths for many blockchains:

On a higher level, you have to:

  1. Generate the master key from mnemonic
  2. Generate the master xpub. Note that generating the master key and the xpub should be done on an offline device, and then copied over to the online device to preserve security of "air-gapping". Maybe the Ledger device can export just the xpub? If that's possible then you can skip the risky business of doing it yourself.
  3. Use the derivation path to produce a list of some N addresses
  4. Scan the UTXO set against those addresses and tally the balance
  5. If you want the wallet history and not just the current balance, you need to scan the entire blockchain against the list of addresses

Note that wallets make a new address each time you receive payment, so the list will grow. When you're restoring a wallet from seed they don't know the N, so what they do is they scan for some number of addresses, like 100, and then find the last address that was actually used, and then scan for 100 more from that one, until they get to the 100 which was never used.

So, how to do the scanning in steps 4. and 5.? I'm not fully familiar with that but I can tell you where to look:

  1. If you're running your own node, find the node's RPC documentation, and see if you can get most of the job done using the node's RPC
  2. Use Electrum APIs to get what you need, either from a public server of from running your own Electrum server alongside your node.
  3. Use Ledger APIs and talk to their servers the same way their wallet talks to them, but I'm not sure how well this is documented.

With 2., there are plenty of public servers you could query, and the data can be verified in a trustless manner, all you need are block headers. However, the server may store your queries and IPs so there's a privacy loss trade-off. If you don't want to use a server, you can run an Electrum server by yourself alongside your node, and the server will produce indexes from the blockchain data so an address history can be easily looked up.

3
  • There's lots to like in this answer (upvoted) but it might be worth mentioning that extracting private keys from a hardware wallet and using them in a general purpose PC has significant security drawbacks. Commented Nov 20, 2022 at 13:47
  • Thanks, added a little note next to step 2: Note that generating the master key and the xpub should be done on an offline device, and then copied over to the online device to preserve security of "air-gapping". Maybe the Ledger device can export just the xpub? If that's possible then you can skip the risky business of doing it yourself. Commented Nov 20, 2022 at 19:32
  • 1
    Thanks for the very thorough answer.
    – Ryan
    Commented Nov 21, 2022 at 2:15
1

I still don't have the full answer, and the following is only for BTC (I'd prefer an answer that works across many blockchains):

I found steps at https://support.ledger.com/hc/en-us/articles/6275459128989-How-to-analyze-a-Bitcoin-BTC-account-xPub?docs=true to be a helpful start:

  1. go to Ledger Live > Accounts > Bitcoin > wrench icon > Advanced and grab my "xpub" string
  2. Use https://www.blockonomics.co/views/segwit_xpub_convert.html to convert it to a zpub string
  3. Use https://blockpath.com/ or https://www.cointracker.io/wallet/bitcoin?address=xxxxxx to view the total balance.

See also https://support.cointracker.io/hc/en-us/articles/4413049732625-What-is-xPub-yPub-zPub-and-UTXO-

I still have not found an API that can return the BTC total balance from a zpub address.

Your Answer

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

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