Refactor len(w.secret) != 32

Now using w.IsLocked() for all instances of above.
Also changed one other place where the logic had to be reversed
in nextChainedAddress (len(w.secret) == 32 was the condition).
This commit is contained in:
Jimmy Song 2014-03-20 11:35:44 -05:00
parent 6dea3789cb
commit c51cbb3332

View file

@ -860,7 +860,7 @@ func (w *Wallet) Lock() (err error) {
} }
// Remove clear text passphrase from wallet. // Remove clear text passphrase from wallet.
if len(w.secret) != 32 { if w.IsLocked() {
err = ErrWalletLocked err = ErrWalletLocked
} else { } else {
zero(w.passphrase) zero(w.passphrase)
@ -900,7 +900,7 @@ func (w *Wallet) ChangePassphrase(new []byte) error {
return ErrWalletIsWatchingOnly return ErrWalletIsWatchingOnly
} }
if len(w.secret) != 32 { if w.IsLocked() {
return ErrWalletLocked return ErrWalletLocked
} }
@ -974,13 +974,13 @@ func (w *Wallet) nextChainedAddress(bs *BlockStamp, keypoolSize uint) (*btcAddre
nextAPKH, ok := w.chainIdxMap[w.highestUsed+1] nextAPKH, ok := w.chainIdxMap[w.highestUsed+1]
if !ok { if !ok {
// Extending the keypool requires an unlocked wallet. // Extending the keypool requires an unlocked wallet.
if len(w.secret) == 32 { if w.IsLocked() {
// Key is available, extend keypool. if err := w.extendLockedWallet(bs); err != nil {
if err := w.extendKeypool(keypoolSize, bs); err != nil {
return nil, err return nil, err
} }
} else { } else {
if err := w.extendLockedWallet(bs); err != nil { // Key is available, extend keypool.
if err := w.extendKeypool(keypoolSize, bs); err != nil {
return nil, err return nil, err
} }
} }
@ -1025,7 +1025,7 @@ func (w *Wallet) extendKeypool(n uint, bs *BlockStamp) error {
return errors.New("expected last chained address not found") return errors.New("expected last chained address not found")
} }
if len(w.secret) != 32 { if w.IsLocked() {
return ErrWalletLocked return ErrWalletLocked
} }
@ -1126,7 +1126,7 @@ func (w *Wallet) createMissingPrivateKeys() error {
return errors.New("missing previous chained address") return errors.New("missing previous chained address")
} }
prevWAddr := w.addrMap[getAddressKey(apkh)] prevWAddr := w.addrMap[getAddressKey(apkh)]
if len(w.secret) != 32 { if w.IsLocked() {
return ErrWalletLocked return ErrWalletLocked
} }
@ -1214,7 +1214,7 @@ func (w *Wallet) AddressKey(a btcutil.Address) (key *ecdsa.PrivateKey, err error
} }
// Wallet must be unlocked to decrypt the private key. // Wallet must be unlocked to decrypt the private key.
if len(w.secret) != 32 { if w.IsLocked() {
return nil, ErrWalletLocked return nil, ErrWalletLocked
} }
@ -1395,7 +1395,7 @@ func (w *Wallet) ImportPrivateKey(privkey []byte, compressed bool, bs *BlockStam
} }
// The wallet must be unlocked to encrypt the imported private key. // The wallet must be unlocked to encrypt the imported private key.
if len(w.secret) != 32 { if w.IsLocked() {
return nil, ErrWalletLocked return nil, ErrWalletLocked
} }