NBitcoin, how to implement wallet restoration? How does it work?


at first sorry for my ignorance, I have little experience at bitcoin programming.

Im trying to implement a very simple wallet using NBitcoin library. I have done some work, but now Im stucked with wallet restoration function.

Lets say I have mnemonic private key, then I calculate its public key.

With public key am I able to track all transactions associated with this key? Even if I used multiple adresses? How its done(NBitcoin)? Any advice? Do I have to iterate throught whole blockchain and chceck signatures?

Thank you.

Martin Lipták

Posted 2019-02-02T22:39:37.917

Reputation: 11

Is it an HD wallet? – MCCCS – 2019-02-03T13:50:06.863

No I want to keep it as simplest as posible for start. – Martin Lipták – 2019-02-03T14:15:05.287



With public key am I able to track all transactions associated with this key?

A public key has two corresponding addresses: Uncompressed and compressed. I suggest only generating compressed public keys for simplicity. The relevant methods are Compress and Decompress here.

So, yes.

Even if I used multiple adresses?

Multiple addresses: Yes, you should track all of them.

Do I have to iterate throughout whole blockchain and check the signatures?

You should use SPV: Download a block, verify it, keep the headers, skip to the next block. Also you should keep the transactions that are incoming/outgoing. Writing the current block height to the wallet file is a good idea, as after you launch the wallet for the second time, you'll only need to download blocks and scan for relevant transactions after that block height.


Posted 2019-02-02T22:39:37.917

Reputation: 8 689

Thank you for the answer. What API would you recomend for a beginer(query a blockchain)? I read a NBitcoin c# book and couple of articles + Mastering bitcoin, but Im still confused about the restoration thing. – Martin Lipták – 2019-02-03T21:46:31.543


Restoring the wallet balance:

To restore the wallet balance from just the mnemonic passphrase, and determine your spendable UTXO's, you need to perform a UTXO-scan with a trusted local node which has validated the Blockchain.

With Bitcoind server, consider querying scantxoutset, which allows you to determine (per public key/address) which unspent outputs are controlled by your wallets.

Restoring the wallet history:

Restoring the receiving/spending history of the wallet will require you to build an "address-index" over all transaction, indexing the tx's associated with addresses you control.

Libbitcoin-Server already indexes all transactions by standard addresses per default, so you can query a standard Libbitcoin server to get the full-history for each address in your wallet.

Note: You do not need to validate signatures during wallet restoration, because your local bitcoin server has validated these. You don't need to build an SPV client if you are running your own validating full-node.

James C.

Posted 2019-02-02T22:39:37.917

Reputation: 2 323