lnd.xprv/wallet/multisig_keypool.go
Olaoluwa Osuntokun 02844741e8 add resource pool for multi-sig keys
* Need to change to instead just use the HD chain so backups aren’t
fragmented

* Optional pre-load on start available, will push EC computations up
front
2015-11-05 12:33:35 -08:00

61 lines
1.3 KiB
Go

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
}