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

Move keyring implementation from keychain to dcrwallet package #29

Merged
merged 6 commits into from
Aug 29, 2019

Conversation

matheusd
Copy link
Member

The main goal for this changeset is to move the keyring implementation (which requires an active and unlocked backing dcrwallet) from the keychain package to the lnwallet/dcrwallet package.

This makes the keychain package cleaner, more generic and removes its dependency to dcrwallet packages in particular and allows future keyring implementations to not be based on a local backing dcrwallet.

It also binds the existing keyring implementation (WalletKeyRing) to the existing wallet driver, which makes sense since it entirely relies on the internal backing wallet.

Another change introduced is that the public keys generated for use within the LN processes are no longer generated inside the backing dcrwallet (via NextAddress() calls), but rather on the keyring itself, based on the root coin type extended private key provided by the wallet. This removes the need for the wallet to track the large(ish) amount of keys (with their associated addresses) used by LN scripts which should never appear on-chain and that the wallet doesn't even (currently) know how to deal with, given the fact that it doesn't understand how the LN scripts work. It also increases the uncoupling between dcrlnd and the backing dcrwallet, which might eventually allow dcrlnd to use remote (instead of embedded) wallets.

The individual commits should be sane enough to be reviewed individually if needed, performing the required changes in stages.

@matheusd matheusd marked this pull request as ready for review May 16, 2019 17:14
@matheusd matheusd changed the title Dcrwallet keyring Move keyring implementation from keychain to dcrwallet package May 16, 2019
@davecgh
Copy link
Member

davecgh commented Aug 28, 2019

This is an excellent change that I'm happy to see.

This adds two new buckets and a new function to the channel database:
buckets for tracking keychain data and key family indexes and a function
to return unique indices for individual key families.

The main purpose of this is to allow keyring implementations to store
the used indices for their keys without having to rely on an external
store. This is mostly useful for using wallet-backed root HD keys while
the wallet itself does not track off-chain keys and addresses.

This change is self-contained and does not require database migrations.
This adds a test to ensure KeyRing implementations don't generate
duplicate public keys when DeriveNextKey is called twice in a row for
the same key family.
This adds a generic HDKeyRing type that implements both the KeyRing and
SecretKeyRing interfaces.

It can derive public and private keys purely from a set of root keys
provided during setup.
This changes the interface test for the keychain package to use a single
specific test function for each implementation. It also exposes the
testing procedure via Check_X_Impl() functions, so that external
implementations can test their correctness to the interface
specification.
This commit moves the dcrwallet-backed implementation of the KeyRing and
SecretKeyRing keychain interfaces from that package to the
lnwallet/dcrwallet package.

This uncouples the keychain package from dcrwallet, which allows future
implementations to use modules that themselves use hdkeychain without
causing import cycles. It also improves abstraction and possible package
reuse by not having it depend on internal wallet packages and
concentrates all dcrwallet-related functionality into a single package.

The implementation itself of the WalletKeyRing is the same. The
call sites were changed to use the new location for it.

In order to allow moving the implementation to a different package, the
interface tests were refactored to export check functions that are
callable from client packages. This allows the keychain interface tests
to be ran from external packages so that they can ensure they conform to
the required semantics.
This moves the dcrwallet-backed WalletKeyRing from directly using
pubkeys derived within the wallet to keeping track of used indices
directly in dcrlnd's channel db and deriving the keys itself based on
the wallet's root coin type extended private key.

The main goal for this move is to prevent the wallet from tracking a
(potentially) large number of accounts and addresses that were never
meant for onchain use and that the current implementation of the wallet
can't work with anyway due to their use being only on ln related scripts
(which the wallet doesn't understand).

In the upstream lnd this was solved by using a separate purpose branch.
If the wallet eventually implements some way of using different purpose
branches, than this can be easily adjusted in the future.

Another improvement of this commit is dropping the need for a separate
public WalletKeyRing by using it as a composed struct to the DcrWallet
struct. This makes it so that the main package doesn't need to
separately initialize the keyring (which doesn't make sense anyway given
any KeyRing implementation is likely to be intemately tied to a
corresponding WalletController implementation).
@matheusd matheusd merged commit e92cfd6 into decred:master Aug 29, 2019
@matheusd matheusd deleted the dcrwallet-keyring branch April 23, 2020 19:27
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