Skip to content

Commit 07c60c8

Browse files
committed
lnwallet: remove direct walletdb reference
To make sure the db layer is not exposed and is only managed by the `btcwallet` package.
1 parent 6e1c098 commit 07c60c8

File tree

2 files changed

+23
-217
lines changed

2 files changed

+23
-217
lines changed

lnwallet/btcwallet/btcwallet.go

Lines changed: 20 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,6 @@ const (
5757
)
5858

5959
var (
60-
// waddrmgrNamespaceKey is the namespace key that the waddrmgr state is
61-
// stored within the top-level walletdb buckets of btcwallet.
62-
waddrmgrNamespaceKey = []byte("waddrmgr")
63-
64-
// wtxmgrNamespaceKey is the namespace key that the wtxmgr state is
65-
// stored within the top-level waleltdb buckets of btcwallet.
66-
wtxmgrNamespaceKey = []byte("wtxmgr")
67-
6860
// lightningAddrSchema is the scope addr schema for all keys that we
6961
// derive. We'll treat them all as p2wkh addresses, as atm we must
7062
// specify a particular type.
@@ -350,18 +342,7 @@ func (b *BtcWallet) Start() error {
350342
// it was added recently and older wallets don't know it
351343
// yet. Let's add it now.
352344
addrSchema := waddrmgr.ScopeAddrMap[scope]
353-
err := walletdb.Update(
354-
b.db, func(tx walletdb.ReadWriteTx) error {
355-
addrmgrNs := tx.ReadWriteBucket(
356-
waddrmgrNamespaceKey,
357-
)
358-
359-
_, err := b.wallet.Manager.NewScopedKeyManager(
360-
addrmgrNs, scope, addrSchema,
361-
)
362-
return err
363-
},
364-
)
345+
_, err := b.wallet.AddScopeManager(scope, addrSchema)
365346
if err != nil {
366347
return err
367348
}
@@ -373,65 +354,27 @@ func (b *BtcWallet) Start() error {
373354
// If the scope hasn't yet been created (it wouldn't been
374355
// loaded by default if it was), then we'll manually create the
375356
// scope for the first time ourselves.
376-
err := walletdb.Update(b.db, func(tx walletdb.ReadWriteTx) error {
377-
addrmgrNs := tx.ReadWriteBucket(waddrmgrNamespaceKey)
378-
379-
scope, err = b.wallet.Manager.NewScopedKeyManager(
380-
addrmgrNs, b.chainKeyScope, lightningAddrSchema,
381-
)
382-
return err
383-
})
357+
manager, err := b.wallet.AddScopeManager(
358+
b.chainKeyScope, lightningAddrSchema,
359+
)
384360
if err != nil {
385361
return err
386362
}
363+
364+
scope = manager
387365
}
388366

367+
// If the wallet is not watch-only atm, and the user wants to migrate it
368+
// to watch-only, we will set `convertToWatchOnly` to true so the wallet
369+
// accounts are created and converted.
370+
convertToWatchOnly := !walletIsWatchOnly && b.cfg.WatchOnly &&
371+
b.cfg.MigrateWatchOnly
372+
389373
// Now that the wallet is unlocked, we'll go ahead and make sure we
390374
// create accounts for all the key families we're going to use. This
391375
// will make it possible to list all the account/family xpubs in the
392376
// wallet list RPC.
393-
err = walletdb.Update(b.db, func(tx walletdb.ReadWriteTx) error {
394-
addrmgrNs := tx.ReadWriteBucket(waddrmgrNamespaceKey)
395-
396-
// Generate all accounts that we could ever need. This includes
397-
// all lnd key families as well as some key families used in
398-
// external liquidity tools.
399-
for keyFam := uint32(1); keyFam <= 255; keyFam++ {
400-
// Otherwise, we'll check if the account already exists,
401-
// if so, we can once again bail early.
402-
_, err := scope.AccountName(addrmgrNs, keyFam)
403-
if err == nil {
404-
continue
405-
}
406-
407-
// If we reach this point, then the account hasn't yet
408-
// been created, so we'll need to create it before we
409-
// can proceed.
410-
err = scope.NewRawAccount(addrmgrNs, keyFam)
411-
if err != nil {
412-
return err
413-
}
414-
}
415-
416-
// If this is the first startup with remote signing and wallet
417-
// migration turned on and the wallet wasn't previously
418-
// migrated, we can do that now that we made sure all accounts
419-
// that we need were derived correctly.
420-
if !walletIsWatchOnly && b.cfg.WatchOnly &&
421-
b.cfg.MigrateWatchOnly {
422-
423-
log.Infof("Migrating wallet to watch-only mode, " +
424-
"purging all private key material")
425-
426-
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
427-
err = b.wallet.Manager.ConvertToWatchingOnly(ns)
428-
if err != nil {
429-
return err
430-
}
431-
}
432-
433-
return nil
434-
})
377+
err = b.wallet.InitAccounts(scope, convertToWatchOnly, 255)
435378
if err != nil {
436379
return err
437380
}
@@ -769,29 +712,8 @@ func (b *BtcWallet) ListAddresses(name string,
769712

770713
for _, accntDetails := range accounts {
771714
accntScope := accntDetails.KeyScope
772-
scopedMgr, err := b.wallet.Manager.FetchScopedKeyManager(
773-
accntScope,
774-
)
775-
if err != nil {
776-
return nil, err
777-
}
778-
779-
var managedAddrs []waddrmgr.ManagedAddress
780-
err = walletdb.View(
781-
b.wallet.Database(), func(tx walletdb.ReadTx) error {
782-
managedAddrs = nil
783-
addrmgrNs := tx.ReadBucket(waddrmgrNamespaceKey)
784-
return scopedMgr.ForEachAccountAddress(
785-
addrmgrNs, accntDetails.AccountNumber,
786-
func(a waddrmgr.ManagedAddress) error {
787-
managedAddrs = append(
788-
managedAddrs, a,
789-
)
790-
791-
return nil
792-
},
793-
)
794-
},
715+
managedAddrs, err := b.wallet.AccountManagedAddresses(
716+
accntDetails.KeyScope, accntDetails.AccountNumber,
795717
)
796718
if err != nil {
797719
return nil, err
@@ -1817,18 +1739,8 @@ func (b *BtcWallet) GetRecoveryInfo() (bool, float64, error) {
18171739
return isRecoveryMode, progress, nil
18181740
}
18191741

1820-
// Query the wallet's birthday block height from db.
1821-
var birthdayBlock waddrmgr.BlockStamp
1822-
err := walletdb.View(b.db, func(tx walletdb.ReadTx) error {
1823-
var err error
1824-
addrmgrNs := tx.ReadBucket(waddrmgrNamespaceKey)
1825-
birthdayBlock, _, err = b.wallet.Manager.BirthdayBlock(addrmgrNs)
1826-
if err != nil {
1827-
return err
1828-
}
1829-
return nil
1830-
})
1831-
1742+
// Query the wallet's birthday block from db.
1743+
birthdayBlock, err := b.wallet.BirthdayBlock()
18321744
if err != nil {
18331745
// The wallet won't start until the backend is synced, thus the birthday
18341746
// block won't be set and this particular error will be returned. We'll
@@ -1886,43 +1798,20 @@ func (b *BtcWallet) GetRecoveryInfo() (bool, float64, error) {
18861798
// by the passed transaction hash. If the transaction can't be found, then a
18871799
// nil pointer is returned.
18881800
func (b *BtcWallet) FetchTx(txHash chainhash.Hash) (*wire.MsgTx, error) {
1889-
var targetTx *wtxmgr.TxDetails
1890-
err := walletdb.View(b.db, func(tx walletdb.ReadTx) error {
1891-
wtxmgrNs := tx.ReadBucket(wtxmgrNamespaceKey)
1892-
txDetails, err := b.wallet.TxStore.TxDetails(wtxmgrNs, &txHash)
1893-
if err != nil {
1894-
return err
1895-
}
1896-
1897-
targetTx = txDetails
1898-
1899-
return nil
1900-
})
1801+
tx, err := b.wallet.GetTransaction(txHash)
19011802
if err != nil {
19021803
return nil, err
19031804
}
19041805

1905-
if targetTx == nil {
1906-
return nil, nil
1907-
}
1908-
1909-
return &targetTx.TxRecord.MsgTx, nil
1806+
return tx.Summary.Tx, nil
19101807
}
19111808

19121809
// RemoveDescendants attempts to remove any transaction from the wallet's tx
19131810
// store (that may be unconfirmed) that spends outputs created by the passed
19141811
// transaction. This remove propagates recursively down the chain of descendent
19151812
// transactions.
19161813
func (b *BtcWallet) RemoveDescendants(tx *wire.MsgTx) error {
1917-
txRecord, err := wtxmgr.NewTxRecordFromMsgTx(tx, time.Now())
1918-
if err != nil {
1919-
return err
1920-
}
1921-
1922-
return walletdb.Update(b.db, func(tx walletdb.ReadWriteTx) error {
1923-
wtxmgrNs := tx.ReadWriteBucket(wtxmgrNamespaceKey)
1924-
return b.wallet.TxStore.RemoveUnminedTx(wtxmgrNs, txRecord)
1925-
})
1814+
return b.wallet.RemoveDescendants(tx)
19261815
}
19271816

19281817
// CheckMempoolAcceptance is a wrapper around `TestMempoolAccept` which checks

lnwallet/btcwallet/signer.go

Lines changed: 3 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/btcsuite/btcd/txscript"
1414
"github.com/btcsuite/btcd/wire"
1515
"github.com/btcsuite/btcwallet/waddrmgr"
16-
"github.com/btcsuite/btcwallet/walletdb"
1716
"github.com/lightningnetwork/lnd/input"
1817
"github.com/lightningnetwork/lnd/keychain"
1918
"github.com/lightningnetwork/lnd/lnwallet"
@@ -71,26 +70,6 @@ func (b *BtcWallet) ScriptForOutput(output *wire.TxOut) (
7170
return b.wallet.ScriptForOutput(output)
7271
}
7372

74-
// deriveFromKeyLoc attempts to derive a private key using a fully specified
75-
// KeyLocator.
76-
func deriveFromKeyLoc(scopedMgr *waddrmgr.ScopedKeyManager,
77-
addrmgrNs walletdb.ReadWriteBucket,
78-
keyLoc keychain.KeyLocator) (*btcec.PrivateKey, error) {
79-
80-
path := waddrmgr.DerivationPath{
81-
InternalAccount: uint32(keyLoc.Family),
82-
Account: uint32(keyLoc.Family),
83-
Branch: 0,
84-
Index: keyLoc.Index,
85-
}
86-
addr, err := scopedMgr.DeriveFromKeyPath(addrmgrNs, path)
87-
if err != nil {
88-
return nil, err
89-
}
90-
91-
return addr.(waddrmgr.ManagedPubKeyAddress).PrivKey()
92-
}
93-
9473
// deriveKeyByBIP32Path derives a key described by a BIP32 path. We expect the
9574
// first three elements of the path to be hardened according to BIP44, so they
9675
// must be a number >= 2^31.
@@ -169,36 +148,14 @@ func (b *BtcWallet) deriveKeyByBIP32Path(path []uint32) (*btcec.PrivateKey,
169148
Purpose: purpose,
170149
Coin: coinType,
171150
}
172-
scopedMgr, err := b.wallet.Manager.FetchScopedKeyManager(scope)
173-
if err != nil {
174-
return nil, fmt.Errorf("error fetching manager for scope %v: "+
175-
"%w", scope, err)
176-
}
177-
178-
// Let's see if we can hit the private key cache.
179151
keyPath := waddrmgr.DerivationPath{
180152
InternalAccount: account,
181153
Account: account,
182154
Branch: change,
183155
Index: index,
184156
}
185-
privKey, err := scopedMgr.DeriveFromKeyPathCache(keyPath)
186-
if err == nil {
187-
return privKey, nil
188-
}
189-
190-
// The key wasn't in the cache, let's fully derive it now.
191-
err = walletdb.View(b.db, func(tx walletdb.ReadTx) error {
192-
addrmgrNs := tx.ReadBucket(waddrmgrNamespaceKey)
193157

194-
addr, err := scopedMgr.DeriveFromKeyPath(addrmgrNs, keyPath)
195-
if err != nil {
196-
return fmt.Errorf("error deriving private key: %w", err)
197-
}
198-
199-
privKey, err = addr.(waddrmgr.ManagedPubKeyAddress).PrivKey()
200-
return err
201-
})
158+
privKey, err := b.wallet.DeriveFromKeyPath(scope, keyPath)
202159
if err != nil {
203160
return nil, fmt.Errorf("error deriving key from path %#v: %w",
204161
keyPath, err)
@@ -225,55 +182,15 @@ func (b *BtcWallet) deriveKeyByLocator(
225182
keyLoc keychain.KeyLocator) (*btcec.PrivateKey, error) {
226183

227184
// We'll assume the special lightning key scope in this case.
228-
scopedMgr, err := b.wallet.Manager.FetchScopedKeyManager(
229-
b.chainKeyScope,
230-
)
231-
if err != nil {
232-
return nil, err
233-
}
234-
235-
// First try to read the key from the cached store, if this fails, then
236-
// we'll fall through to the method below that requires a database
237-
// transaction.
185+
scope := b.chainKeyScope
238186
path := waddrmgr.DerivationPath{
239187
InternalAccount: uint32(keyLoc.Family),
240188
Account: uint32(keyLoc.Family),
241189
Branch: 0,
242190
Index: keyLoc.Index,
243191
}
244-
privKey, err := scopedMgr.DeriveFromKeyPathCache(path)
245-
if err == nil {
246-
return privKey, nil
247-
}
248192

249-
var key *btcec.PrivateKey
250-
err = walletdb.Update(b.db, func(tx walletdb.ReadWriteTx) error {
251-
addrmgrNs := tx.ReadWriteBucket(waddrmgrNamespaceKey)
252-
253-
key, err = deriveFromKeyLoc(scopedMgr, addrmgrNs, keyLoc)
254-
if waddrmgr.IsError(err, waddrmgr.ErrAccountNotFound) {
255-
// If we've reached this point, then the account
256-
// doesn't yet exist, so we'll create it now to ensure
257-
// we can sign.
258-
acctErr := scopedMgr.NewRawAccount(
259-
addrmgrNs, uint32(keyLoc.Family),
260-
)
261-
if acctErr != nil {
262-
return acctErr
263-
}
264-
265-
// Now that we know the account exists, we'll attempt
266-
// to re-derive the private key.
267-
key, err = deriveFromKeyLoc(
268-
scopedMgr, addrmgrNs, keyLoc,
269-
)
270-
if err != nil {
271-
return err
272-
}
273-
}
274-
275-
return err
276-
})
193+
key, err := b.wallet.DeriveFromKeyPathAddAccount(scope, path)
277194
if err != nil {
278195
return nil, err
279196
}

0 commit comments

Comments
 (0)