scrap the multi-sig key pool, better to just use HD paths

* Better to just keep the single file back-up IMO
This commit is contained in:
Olaoluwa Osuntokun 2015-11-12 17:45:09 -08:00
parent bf5f630e53
commit c6e7c6b1b6
2 changed files with 15 additions and 65 deletions

@ -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
}

@ -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?