Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wallet: rescan and recover addresses in all relevant key scopes #682

Merged
merged 4 commits into from
Mar 31, 2020
Merged

wallet: rescan and recover addresses in all relevant key scopes #682

merged 4 commits into from
Mar 31, 2020

Conversation

wpaulino
Copy link
Contributor

Previously, the wallet would determine the key scope to use for change addresses by locating the one compatible with P2WPKH addresses, but this wasn't always safe like in the case when multiple key scopes that supported these addresses existed within the address manager, leading the change address to be created outside of the intended key scope.

Wallets affected by this now need to ensure they scan the chain when performing rescans or recovery attempts for addresses within all existing key scopes, rather than just the default ones, to reflect their proper balance. For newer wallets not affected by this, they should only scan for addresses within their default key scopes.

Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solid fix! Only main comment concerns a possibly more robust way we can properly check to see if change addresses were created outside the default scope.

wallet/wallet.go Outdated
// Wallets with a birthday after this height are not susceptible
// to the bug described, so they can safely only scan the
// default key scopes.
mainNetHeight = 623000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How were these heights obtained? The heights when the prior active addr change landed in master?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, I think we should just have this be based off of a database migration version which detects if a change address was ever created under a non-default scope. If so, then we need to check for everything constantly, otherwise we can only start to watch the default scopes from there.

@wpaulino
Copy link
Contributor Author

@Roasbeef I've added a migration with some unit tests. I'll be testing this over the next few days on some nodes, but let me know if this is what you had in mind.

Previously, the wallet would determine the key scope to use for change
addresses by locating the one compatible with P2WPKH addresses, but this
wasn't always safe like in the case when multiple key scopes that
supported these addresses existed within the address manager, leading
the change address to be created outside of the intended key scope.
The commit being reverted resulted in the discovery of a bug in which
change addresses could at times be created outside of the default key
scopes, causing us to not properly determine their spends.
Due to a no longer existing bug within the wallet, it was possible for
change addresses to be created outside of their intended key scope (the
default), so wallets affected by this now need to ensure they scan the
chain for all addresses within the default key scopes (as expected), and
all _internal_ addresses (branch used for change addresses) within any
other registered key scopes to reflect their proper balance.
In similar fashion to the previous commit, due to a no longer existing
bug within the wallet, it was possible for change addresses to be
created outside of their intended key scope (the default), so wallets
affected by this now need to ensure upon recovery that they scan the
chain for _all_ existing key scopes, rather than just the default ones,
to reflect their proper balance. Through manual testing, it was shown
that the impact of recovering the additional key scopes is negligible in
most cases for both full nodes and light clients.
Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🦋

Great that we caught this bug while the changes were still in master and not packaged in a tagged release! However we still need to cope with the existing recovery bug, thankfully people will just need to re-attempt a recovery.

Copy link
Contributor

@cfromknecht cfromknecht left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. In the end I'm glad that this simpler approach worked out, saved us a lot of special casing in the recovery/rescan flows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants