-
Notifications
You must be signed in to change notification settings - Fork 2.3k
config_builder: preserve watch-only account numbers #10644
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
Open
starius
wants to merge
5
commits into
lightningnetwork:master
Choose a base branch
from
starius:preserve-accounts
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
77ad5ae
go.mod: pin btcwallet
starius 21127f3
config_builder: preserve watch-only account number
starius 3e81832
itest/remote_signer: test sparse account import
starius 5c4777c
docs: update remote-signer account guidance
starius 54ecbaf
docs: note watch-only account fix in 0.21.0
starius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| package lnd | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/btcsuite/btcd/btcutil/hdkeychain" | ||
| "github.com/btcsuite/btcd/chaincfg" | ||
| "github.com/btcsuite/btcwallet/waddrmgr" | ||
| "github.com/btcsuite/btcwallet/wallet" | ||
| "github.com/btcsuite/btcwallet/walletdb" | ||
| "github.com/lightningnetwork/lnd/keychain" | ||
| "github.com/lightningnetwork/lnd/kvdb" | ||
| "github.com/lightningnetwork/lnd/walletunlocker" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestImportWatchOnlyAccountsPreservesSparseAccountNumbers ensures watch-only | ||
| // initialization preserves explicit sparse account indices so deriving keys by | ||
| // KeyFamily uses the intended internal account number. | ||
| func TestImportWatchOnlyAccountsPreservesSparseAccountNumbers(t *testing.T) { | ||
| const ( | ||
| pubPass = "public-pass" | ||
| sparseAccount = uint32(43210) | ||
| masterFingerprint = uint32(0x12345678) | ||
| ) | ||
|
|
||
| loader := wallet.NewLoader( | ||
| &chaincfg.TestNet3Params, t.TempDir(), true, | ||
| kvdb.DefaultDBTimeout, 0, | ||
| ) | ||
| testWallet, err := loader.CreateNewWatchingOnlyWallet( | ||
| []byte(pubPass), time.Unix(0, 0), | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| t.Cleanup(func() { | ||
| require.NoError(t, loader.UnloadWallet()) | ||
| }) | ||
|
|
||
| rootKey, err := hdkeychain.NewMaster( | ||
| []byte("01234567890123456789012345678901"), | ||
| &chaincfg.TestNet3Params, | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| bip84AccountXpub, err := deriveAccountXpub( | ||
| rootKey, waddrmgr.KeyScopeBIP0084.Purpose, | ||
| waddrmgr.KeyScopeBIP0084.Coin, 0, | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| customScope := waddrmgr.KeyScope{ | ||
| Purpose: keychain.BIP0043Purpose, | ||
| Coin: keychain.CoinTypeTestnet, | ||
| } | ||
| customAccountXpub, err := deriveAccountXpub( | ||
| rootKey, customScope.Purpose, customScope.Coin, sparseAccount, | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| watchOnlyAccounts := map[waddrmgr.ScopedIndex]*hdkeychain.ExtendedKey{ | ||
| { | ||
| Scope: waddrmgr.KeyScopeBIP0084, | ||
| Index: 0, | ||
| }: bip84AccountXpub, | ||
| { | ||
| Scope: customScope, | ||
| Index: sparseAccount, | ||
| }: customAccountXpub, | ||
| } | ||
| initMsg := &walletunlocker.WalletInitMsg{ | ||
| WatchOnlyMasterFingerprint: masterFingerprint, | ||
| WatchOnlyAccounts: watchOnlyAccounts, | ||
| } | ||
| require.NoError(t, importWatchOnlyAccounts(testWallet, initMsg)) | ||
|
|
||
| // This is the same call path WalletKit.DeriveKey uses for watch-only | ||
| // wallets: no account auto-creation, account number must already exist. | ||
| keyRing := keychain.NewBtcWalletKeyRing( | ||
| testWallet, keychain.CoinTypeTestnet, | ||
| ) | ||
| keyDesc, err := keyRing.DeriveKey(keychain.KeyLocator{ | ||
| Family: keychain.KeyFamily(sparseAccount), | ||
| Index: 0, | ||
| }) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, keyDesc.PubKey) | ||
|
|
||
| db := testWallet.Database() | ||
|
|
||
| err = walletdb.View(db, func(tx walletdb.ReadTx) error { | ||
| addrmgrNs := tx.ReadBucket(waddrmgrNamespaceKey) | ||
| if addrmgrNs == nil { | ||
| return fmt.Errorf("waddrmgr namespace not found") | ||
| } | ||
|
|
||
| // Ensure the sparse account number was preserved. | ||
| customMgr, err := testWallet.Manager.FetchScopedKeyManager( | ||
| customScope, | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| name, err := customMgr.AccountName(addrmgrNs, sparseAccount) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| expectedName := fmt.Sprintf( | ||
| "%s/%d'", customScope.String(), sparseAccount, | ||
| ) | ||
| if name != expectedName { | ||
| return fmt.Errorf("expected account %d name %q, got %q", | ||
| sparseAccount, expectedName, name) | ||
| } | ||
|
|
||
| // The old behavior would have created this account | ||
| // contiguously. | ||
| _, err = customMgr.AccountName(addrmgrNs, 1) | ||
| var managerErr waddrmgr.ManagerError | ||
| if !errors.As(err, &managerErr) || | ||
| managerErr.ErrorCode != waddrmgr.ErrAccountNotFound { | ||
|
|
||
| return fmt.Errorf("expected account 1 to be missing, "+ | ||
| "got: %v", err) | ||
| } | ||
|
|
||
| // Ensure account 0 in the default BIP84 scope keeps its | ||
| // "default" naming behavior. | ||
| bip84Mgr, err := testWallet.Manager.FetchScopedKeyManager( | ||
| waddrmgr.KeyScopeBIP0084, | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defaultName, err := bip84Mgr.AccountName(addrmgrNs, 0) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if defaultName != "default" { | ||
| return fmt.Errorf("expected account 0 name default, "+ | ||
| "got %q", defaultName) | ||
| } | ||
|
|
||
| return nil | ||
| }) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| // deriveAccountXpub derives and neuters an account-level key at | ||
| // m/purpose'/coin_type'/account'. | ||
| func deriveAccountXpub(rootKey *hdkeychain.ExtendedKey, purpose, coinType, | ||
| account uint32) (*hdkeychain.ExtendedKey, error) { | ||
|
|
||
| path := []uint32{ | ||
| purpose + hdkeychain.HardenedKeyStart, | ||
| coinType + hdkeychain.HardenedKeyStart, | ||
| account + hdkeychain.HardenedKeyStart, | ||
| } | ||
|
|
||
| current := rootKey | ||
| var err error | ||
| for _, pathPart := range path { | ||
| current, err = current.Derive(pathPart) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| return current.Neuter() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there's value in adding some docs for explaining the logic here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a comment: