From c6e7c6b1b652630e7a4afab5fa3c942dbd1172fa Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 12 Nov 2015 17:45:09 -0800 Subject: [PATCH] scrap the multi-sig key pool, better to just use HD paths * Better to just keep the single file back-up IMO --- wallet/multisig_keypool.go | 60 -------------------------------------- wallet/wallet.go | 20 +++++++++---- 2 files changed, 15 insertions(+), 65 deletions(-) delete mode 100644 wallet/multisig_keypool.go diff --git a/wallet/multisig_keypool.go b/wallet/multisig_keypool.go deleted file mode 100644 index 6830a34b..00000000 --- a/wallet/multisig_keypool.go +++ /dev/null @@ -1,60 +0,0 @@ -package wallet - -import ( - "container/list" - "sync" - - "github.com/btcsuite/btcd/btcec" -) - -// multiSigKeyPool... -// TODO(roasbeef): actually, this is dumb. should use an HD key branch instead. -// * instead, use wallet.Manager.NextExternalAddresses, cast to -// ManagedPubKeyAddress, then .PrivKey() -// * on shutdown, write state of pending keys, then read back? -type multiSigKeyPool struct { - sync.RWMutex - keyPool *list.List -} - -// newMultiSigKeyPool... -func newMultiSigKeyPool() *multiSigKeyPool { - // TODO(roasbeef): pre-generate or nah? - return &multiSigKeyPool{keyPool: list.New()} -} - -// getNextMultiSigKey... -func (l *multiSigKeyPool) getNextMultiSigKey() (*btcec.PrivateKey, error) { - if l.Size() == 0 { - return btcec.NewPrivateKey(btcec.S256()) - } - - l.Lock() - defer l.Unlock() - nextKey := l.keyPool.Remove(l.keyPool.Front()).(*btcec.PrivateKey) - return nextKey, nil -} - -// releaseMultiSigKey... -func (l *multiSigKeyPool) releaseMultiSigKey(key *btcec.PrivateKey) { - l.keyPool.PushBack(key) -} - -// Size... -func (l *multiSigKeyPool) Size() int { - l.RLock() - defer l.RUnlock() - return l.keyPool.Len() -} - -// preAllocateKeys... -func (l *multiSigKeyPool) preAllocateKeys(numKeys int) error { - for i := 0; i < numKeys; i-- { - newKey, err := btcec.NewPrivateKey(btcec.S256()) - if err != nil { - return err - } - l.keyPool.PushBack(newKey) - } - return nil -} diff --git a/wallet/wallet.go b/wallet/wallet.go index f616e067..9ad59117 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -193,8 +193,6 @@ type LightningWallet struct { nextFundingID uint64 // TODO(roasbeef): monotonic or random? fundingLimbo map[uint64]*partialFundingState - keyPool *multiSigKeyPool - started int32 shutdown int32 @@ -258,6 +256,19 @@ out: l.wg.Done() } +// nextMultiSigKey... +// TODO(roasbeef): on shutdown, write state of pending keys, then read back? +func (l *LightningWallet) getNextMultiSigKey() (*btcec.PrivateKey, error) { + nextAddr, err := l.Manager.NextExternalAddresses(waddrmgr.DefaultAccountNum, 1) + if err != nil { + return nil, err + } + + pkAddr := nextAddr[0].(waddrmgr.ManagedPubKeyAddress) + + return pkAddr.PrivKey() +} + // RequestFundingReservation... func (l *LightningWallet) RequestFundingReservation(a btcutil.Amount, t FundingType) (*FundingReservation, error) { errChan := make(chan error, 1) @@ -347,7 +358,7 @@ func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg // TODO(roasbeef): re-calculate fees here to minFeePerKB - multiSigKey, err := l.keyPool.getNextMultiSigKey() + multiSigKey, err := l.getNextMultiSigKey() if err != nil { req.err <- err req.resp <- nil @@ -397,8 +408,7 @@ func (l *LightningWallet) handleFundingCancelRequest(req *fundingReserveCancelMs l.UnlockOutpoint(unusedInput.PreviousOutPoint) } - // Return the unused multi-sig key back to the pool. - l.keyPool.releaseMultiSigKey(pendingReservation.ourKey) + // TODO(roasbeef): is it even worth it to keep track of unsed keys? // TODO(roasbeef): Is it possible to mark the unused change also as // available?