From 326eff08398b62f5fb2a2fe85218f2b82cc677ae Mon Sep 17 00:00:00 2001 From: amass Date: Tue, 16 Mar 2021 23:24:32 +0200 Subject: [PATCH] polish TestChangeAppPass --- client/core/core.go | 7 +++---- client/core/core_test.go | 30 +++++++++++------------------- client/core/trade_simnet_test.go | 2 +- client/core/wallet.go | 23 +++++++++++------------ 4 files changed, 26 insertions(+), 36 deletions(-) diff --git a/client/core/core.go b/client/core/core.go index 0a02f36187..d4edfedf64 100644 --- a/client/core/core.go +++ b/client/core/core.go @@ -1524,7 +1524,7 @@ func (c *Core) loadWallet(dbWallet *db.Wallet) (*xcWallet, error) { // Construct the unconnected xcWallet. contractLockedAmt, orderLockedAmt := c.lockedAmounts(assetID) - xcWallet := &xcWallet{ + return &xcWallet{ Wallet: w, connector: dex.NewConnectionMaster(w), AssetID: assetID, @@ -1533,11 +1533,10 @@ func (c *Core) loadWallet(dbWallet *db.Wallet) (*xcWallet, error) { OrderLocked: orderLockedAmt, ContractLocked: contractLockedAmt, }, + encPass: dbWallet.EncryptedPW, address: dbWallet.Address, dbID: dbWallet.ID(), - } - xcWallet.setEncPW(dbWallet.EncryptedPW) - return xcWallet, nil + }, nil } // WalletState returns the *WalletState for the asset ID. diff --git a/client/core/core_test.go b/client/core/core_test.go index c8e40601df..e025e38003 100644 --- a/client/core/core_test.go +++ b/client/core/core_test.go @@ -4965,7 +4965,6 @@ func TestChangeAppPass(t *testing.T) { tCore := rig.core var assetID uint32 = 54322 newTPW := []byte("apppass") - acctEncKey := string(tCore.conns[tDexHost].acct.encKey) // App Password error rig.crypter.recryptErr = tErr @@ -4977,36 +4976,29 @@ func TestChangeAppPass(t *testing.T) { wallet, _ := newTWallet(assetID, rig.crypter) tCore.wallets[assetID] = wallet - asset.Register(assetID, &tDriver{ - f: func(wCfg *asset.WalletConfig, logger dex.Logger, net dex.Network) (asset.Wallet, error) { - return wallet.Wallet, nil - }, - }) - if err = wallet.Connect(); err != nil { - t.Fatal(err) - } - defer wallet.Disconnect() - origPass := string(wallet.encPW()) + acctEncKey := make([]byte, len(tCore.conns[tDexHost].acct.encKey)) + copy(acctEncKey, tCore.conns[tDexHost].acct.encKey) + origPass := make([]byte, len(wallet.encPW())) + copy(origPass, wallet.encPW()) err = tCore.ChangeAppPass(tPW, newTPW) if err != nil { t.Fatal(err) } - // Ensure db's UpdateAccount func was called & db's account encrypted - // key didn't change. + // Ensure db's UpdateAccount func was called. if !rig.db.verifyUpdateAccount { t.Fatal("expected execution of db.UpdateAccount") } - if string(rig.db.accountInfoPersisted.EncKey) == acctEncKey { - t.Fatalf("expected db account EncKey to change, old: %v new: %v", - acctEncKey, string(rig.db.accountInfoPersisted.EncKey)) + // Ensure db's account encrypted key changed. + if bytes.Equal(rig.db.accountInfoPersisted.EncKey, acctEncKey) { + t.Fatalf("expected db account EncKey to change, old: %x new: %x", + acctEncKey, rig.db.accountInfoPersisted.EncKey) } // Ensure xcWallet encrypted password updated. - nWallet := tCore.wallets[assetID] - if origPass == string(nWallet.encPW()) { + if bytes.Equal(origPass, wallet.encPW()) { t.Fatalf("expected xcwallet encPW to change: old %x new %x", - origPass, nWallet.encPW()) + origPass, wallet.encPW()) } } diff --git a/client/core/trade_simnet_test.go b/client/core/trade_simnet_test.go index c290954f8e..62ad30b28e 100644 --- a/client/core/trade_simnet_test.go +++ b/client/core/trade_simnet_test.go @@ -1492,8 +1492,8 @@ func (client *tClient) disableWallets() { // NOTE: this is not reversible, but could be made so with the undo data: // walletPasses[cid] = make(map[uint32]passes, len(client.core.wallets)) for _, wallet := range client.core.wallets { - wallet.setEncPW([]byte{0}) wallet.mtx.Lock() + wallet.encPass = []byte{0} wallet.pw = "" wallet.mtx.Unlock() } diff --git a/client/core/wallet.go b/client/core/wallet.go index 126e32068c..35fd8da930 100644 --- a/client/core/wallet.go +++ b/client/core/wallet.go @@ -84,12 +84,12 @@ func (w *xcWallet) refreshUnlock() (unlockAttempted bool, err error) { return false, nil // unlocked } // Locked backend requires both encrypted and decrypted passwords. - if len(w.encPW()) == 0 { + w.mtx.RLock() + defer w.mtx.RUnlock() + if len(w.encPass) == 0 { return false, fmt.Errorf("%s wallet reporting as locked but no password"+ " has been set", unbip(w.AssetID)) } - w.mtx.RLock() - defer w.mtx.RUnlock() if len(w.pw) == 0 { return false, fmt.Errorf("cannot refresh unlock on a locked %s wallet", unbip(w.AssetID)) @@ -100,12 +100,12 @@ func (w *xcWallet) refreshUnlock() (unlockAttempted bool, err error) { // Lock the wallet. For encrypted wallets (encPW set), this clears the cached // decrypted password and attempts to lock the wallet backend. func (w *xcWallet) Lock() error { - if len(w.encPW()) == 0 { + w.mtx.Lock() + defer w.mtx.Unlock() + if len(w.encPass) == 0 { return nil } - w.mtx.Lock() w.pw = "" - w.mtx.Unlock() return w.Wallet.Lock() } @@ -122,29 +122,28 @@ func (w *xcWallet) unlocked() bool { // this is true only if the decrypted password is cached. Use this to determine // if the wallet may be unlocked without user interaction (via refreshUnlock). func (w *xcWallet) locallyUnlocked() bool { - if len(w.encPW()) == 0 { - return true // unencrypted wallet - } w.mtx.RLock() defer w.mtx.RUnlock() + if len(w.encPass) == 0 { + return true // unencrypted wallet + } return len(w.pw) > 0 // cached password for encrypted wallet } // state returns the current WalletState. func (w *xcWallet) state() *WalletState { - encPW := w.encPW() w.mtx.RLock() defer w.mtx.RUnlock() winfo := w.Info() return &WalletState{ Symbol: unbip(w.AssetID), AssetID: w.AssetID, - Open: len(encPW) == 0 || len(w.pw) > 0, + Open: len(w.encPass) == 0 || len(w.pw) > 0, Running: w.connector.On(), Balance: w.balance, Address: w.address, Units: winfo.Units, - Encrypted: len(encPW) > 0, + Encrypted: len(w.encPass) > 0, Synced: w.synced, SyncProgress: w.syncProgress, }