bdk icon indicating copy to clipboard operation
bdk copied to clipboard

docs: show example migration from bdk to bdk_wallet

Open ValuedMammal opened this issue 1 year ago • 1 comments

I created a migration workflow in the form of a runnable example that does everything in one go. In the example we

  1. Open an existing sled db
  2. Get the last revealed addresses
  3. Create a new bdk_wallet::Wallet with new database
  4. Restore previously revealed addresses
  5. Sync with electrum

I also provide commentary around special considerations including how signers can be transferred and why it's necessary to do a full scan. One scenario that might be difficult to show with this example is how to migrate from an old sqlite-backed wallet to a new wallet that also uses sqlite due to conflicting versions of libsqlite3-sys in the same crate

*UPDATED example code to use sqlite instead of sled, but it's waiting for a release with the changes from #1651

candidate for solving #1606

Changelog notice

Checklists

All Submissions:

  • [x] I've signed all my commits
  • [x] I followed the contribution guidelines
  • [x] I ran cargo fmt and cargo clippy before committing

ValuedMammal avatar Oct 15 '24 19:10 ValuedMammal

Looks good, simpler and safer than dealing directly with database or intermediate text files as I'd originally proposed. A few suggestions:

  1. would read better with more descriptive variable names, ie. old_wallet, new_wallet
  2. should add sanity check asserts after sync for things like balances, indexes; maybe also utxos and tx ids.
  3. need to update PR description with sqlite instead of sled
  4. make it clearer in code comments that you're opening an "old" (will be) 0.30 wallet and a "new" 1.0 wallet

notmandatory avatar Oct 22 '24 16:10 notmandatory

I tested this by first syncing a bdk wallet using electrum_backend.rs, adapted to use this tprv

tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L

and persisted with sqlite. Then I ran the example in this PR, initializing BDK_DB_PATH to the old db path.

should add sanity check asserts after sync for things like balances, indexes; maybe also utxos and tx ids

I added assertions for utxos, balance, and revealed addresses. Just to note: this assumes the old database is up to date and no new information is presented as a result of syncing the new wallet.

I'm wondering whether example-crates/ is the best place for this compared to maybe a page in Book of BDK - I guess either is fine

ValuedMammal avatar Oct 23 '24 15:10 ValuedMammal

Yes I agree it might make a better book section with companion code to take snippets from.

notmandatory avatar Oct 24 '24 03:10 notmandatory

Maintenance version v0.30.0 of bdk crate with upgrade to rusqlite 0.31 is now published:

https://crates.io/crates/bdk/0.30.0

notmandatory avatar Nov 23 '24 02:11 notmandatory

Overall looks great, short and too the point. I have a couple suggestions but otherwise this looks ready to go.

notmandatory avatar Dec 09 '24 23:12 notmandatory

As discussed on call today, this can be closed in favor of bitcoindevkit/book-of-bdk#81. That PR is already using sync instead of full_scan and I understand and agree it's best to use AddressIndex::LastUnused.

notmandatory avatar Dec 10 '24 15:12 notmandatory