Skip to content

Commit

Permalink
implement tCrypter.Encrypt & tCrypter.Decrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
amass01 committed Mar 11, 2021
1 parent fe30adc commit f739e48
Showing 1 changed file with 66 additions and 51 deletions.
117 changes: 66 additions & 51 deletions client/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package core
import (
"bytes"
"context"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
Expand Down Expand Up @@ -549,7 +550,7 @@ type TXCWallet struct {
preRedeem *asset.PreRedeem
}

func newTWallet(assetID uint32) (*xcWallet, *TXCWallet) {
func newTWallet(assetID uint32, crypter *tCrypter) (*xcWallet, *TXCWallet) {
w := &TXCWallet{
changeCoin: &tCoin{id: encode.RandomBytes(36)},
syncStatus: func() (synced bool, progress float32, err error) { return true, 1, nil },
Expand All @@ -566,7 +567,8 @@ func newTWallet(assetID uint32) (*xcWallet, *TXCWallet) {
syncProgress: 1,
pw: string(tPW),
}
xcWallet.setEncPW([]byte{0x01})
encPW, _ := crypter.Encrypt([]byte{0x01})
xcWallet.setEncPW(encPW)

return xcWallet, w
}
Expand Down Expand Up @@ -753,9 +755,18 @@ type tCrypter struct {
recryptErr error
}

func (c *tCrypter) Encrypt(b []byte) ([]byte, error) { return b, c.encryptErr }
// Encrypt appends 8 random bytes to given []byte to mock.
func (c *tCrypter) Encrypt(b []byte) ([]byte, error) {
randSuffix := make([]byte, 8)
rand.Read(randSuffix)
b = append(b, randSuffix...)
return b, c.encryptErr
}

func (c *tCrypter) Decrypt(b []byte) ([]byte, error) { return b, c.decryptErr }
// Decrypt deletes the last 8 bytes from given []byte.
func (c *tCrypter) Decrypt(b []byte) ([]byte, error) {
return b[:len(b)-8], c.decryptErr
}

func (c *tCrypter) Serialize() []byte { return nil }

Expand Down Expand Up @@ -1255,7 +1266,7 @@ func TestCreateWallet(t *testing.T) {
}

// Try to add an existing wallet.
wallet, tWallet := newTWallet(tILT.ID)
wallet, tWallet := newTWallet(tILT.ID, rig.crypter)
tCore.wallets[tILT.ID] = wallet
ensureErr("existing wallet")
delete(tCore.wallets, tILT.ID)
Expand Down Expand Up @@ -1357,7 +1368,7 @@ func TestRegister(t *testing.T) {
dc := rig.dc
delete(tCore.conns, tDexHost)

wallet, tWallet := newTWallet(tDCR.ID)
wallet, tWallet := newTWallet(tDCR.ID, rig.crypter)
tCore.wallets[tDCR.ID] = wallet

// When registering, successfully retrieving *db.AccountInfo from the DB is
Expand Down Expand Up @@ -1707,9 +1718,9 @@ func TestLogin(t *testing.T) {
dbOrder.MetaData.Status = order.OrderStatusBooked // leave unfunded to have it canceled on auth/'connect'
oid := lo.ID()
mkt := dc.marketConfig(tDcrBtcMktName)
dcrWallet, _ := newTWallet(tDCR.ID)
dcrWallet, _ := newTWallet(tDCR.ID, rig.crypter)
tCore.wallets[tDCR.ID] = dcrWallet
btcWallet, tBtcWallet := newTWallet(tBTC.ID)
btcWallet, tBtcWallet := newTWallet(tBTC.ID, rig.crypter)
tCore.wallets[tBTC.ID] = btcWallet
walletSet, _ := tCore.walletSet(dc, tDCR.ID, tBTC.ID, true)
tracker := newTrackedTrade(dbOrder, preImg, dc, mkt.EpochLen, rig.core.lockTimeTaker, rig.core.lockTimeMaker,
Expand Down Expand Up @@ -1831,7 +1842,7 @@ func TestLoginAccountNotFoundError(t *testing.T) {
accountNotFoundError := msgjson.NewError(msgjson.AccountNotFoundError, expectedErrorMessage)
rig.queueConnect(accountNotFoundError, nil, nil)

wallet, _ := newTWallet(tDCR.ID)
wallet, _ := newTWallet(tDCR.ID, rig.crypter)
tCore.wallets[tDCR.ID] = wallet
rig.queueConnect(nil, nil, nil)

Expand Down Expand Up @@ -1989,7 +2000,7 @@ func TestWithdraw(t *testing.T) {
rig := newTestRig()
defer rig.shutdown()
tCore := rig.core
wallet, tWallet := newTWallet(tDCR.ID)
wallet, tWallet := newTWallet(tDCR.ID, rig.crypter)
tCore.wallets[tDCR.ID] = wallet
address := "addr"

Expand Down Expand Up @@ -2044,12 +2055,12 @@ func TestTrade(t *testing.T) {
rig := newTestRig()
defer rig.shutdown()
tCore := rig.core
dcrWallet, tDcrWallet := newTWallet(tDCR.ID)
dcrWallet, tDcrWallet := newTWallet(tDCR.ID, rig.crypter)
tCore.wallets[tDCR.ID] = dcrWallet
dcrWallet.address = "DsVmA7aqqWeKWy461hXjytbZbgCqbB8g2dq"
dcrWallet.Unlock(rig.crypter)

btcWallet, tBtcWallet := newTWallet(tBTC.ID)
btcWallet, tBtcWallet := newTWallet(tBTC.ID, rig.crypter)
tCore.wallets[tBTC.ID] = btcWallet
btcWallet.address = "12DXGkvxFjuq5btXYkwWfBZaz1rVwFgini"
btcWallet.Unlock(rig.crypter)
Expand Down Expand Up @@ -2457,15 +2468,15 @@ func TestHandleRevokeOrderMsg(t *testing.T) {
defer rig.shutdown()
dc := rig.dc
tCore := rig.core
dcrWallet, tDcrWallet := newTWallet(tDCR.ID)
dcrWallet, tDcrWallet := newTWallet(tDCR.ID, rig.crypter)
tCore.wallets[tDCR.ID] = dcrWallet
dcrWallet.address = "DsVmA7aqqWeKWy461hXjytbZbgCqbB8g2dq"
dcrWallet.Unlock(rig.crypter)

fundCoinDcrID := encode.RandomBytes(36)
fundCoinDcr := &tCoin{id: fundCoinDcrID}

btcWallet, _ := newTWallet(tBTC.ID)
btcWallet, _ := newTWallet(tBTC.ID, rig.crypter)
tCore.wallets[tBTC.ID] = btcWallet
btcWallet.address = "12DXGkvxFjuq5btXYkwWfBZaz1rVwFgini"
btcWallet.Unlock(rig.crypter)
Expand Down Expand Up @@ -2529,15 +2540,15 @@ func TestHandleRevokeMatchMsg(t *testing.T) {
defer rig.shutdown()
dc := rig.dc
tCore := rig.core
dcrWallet, tDcrWallet := newTWallet(tDCR.ID)
dcrWallet, tDcrWallet := newTWallet(tDCR.ID, rig.crypter)
tCore.wallets[tDCR.ID] = dcrWallet
dcrWallet.address = "DsVmA7aqqWeKWy461hXjytbZbgCqbB8g2dq"
dcrWallet.Unlock(rig.crypter)

fundCoinDcrID := encode.RandomBytes(36)
fundCoinDcr := &tCoin{id: fundCoinDcrID}

btcWallet, _ := newTWallet(tBTC.ID)
btcWallet, _ := newTWallet(tBTC.ID, rig.crypter)
tCore.wallets[tBTC.ID] = btcWallet
btcWallet.address = "12DXGkvxFjuq5btXYkwWfBZaz1rVwFgini"
btcWallet.Unlock(rig.crypter)
Expand Down Expand Up @@ -2602,12 +2613,12 @@ func TestTradeTracking(t *testing.T) {
defer rig.shutdown()
dc := rig.dc
tCore := rig.core
dcrWallet, tDcrWallet := newTWallet(tDCR.ID)
dcrWallet, tDcrWallet := newTWallet(tDCR.ID, rig.crypter)
tCore.wallets[tDCR.ID] = dcrWallet
dcrWallet.address = "DsVmA7aqqWeKWy461hXjytbZbgCqbB8g2dq"
dcrWallet.Unlock(rig.crypter)

btcWallet, tBtcWallet := newTWallet(tBTC.ID)
btcWallet, tBtcWallet := newTWallet(tBTC.ID, rig.crypter)
tCore.wallets[tBTC.ID] = btcWallet
btcWallet.address = "12DXGkvxFjuq5btXYkwWfBZaz1rVwFgini"
btcWallet.Unlock(rig.crypter)
Expand Down Expand Up @@ -3193,8 +3204,8 @@ func TestReconcileTrades(t *testing.T) {
dc := rig.dc

mkt := dc.marketConfig(tDcrBtcMktName)
rig.core.wallets[mkt.Base], _ = newTWallet(mkt.Base)
rig.core.wallets[mkt.Quote], _ = newTWallet(mkt.Quote)
rig.core.wallets[mkt.Base], _ = newTWallet(mkt.Base, rig.crypter)
rig.core.wallets[mkt.Quote], _ = newTWallet(mkt.Quote, rig.crypter)
walletSet, err := rig.core.walletSet(dc, mkt.Base, mkt.Quote, true)
if err != nil {
t.Fatalf("walletSet error: %v", err)
Expand Down Expand Up @@ -3456,12 +3467,12 @@ func TestRefunds(t *testing.T) {

dc := rig.dc
tCore := rig.core
dcrWallet, tDcrWallet := newTWallet(tDCR.ID)
dcrWallet, tDcrWallet := newTWallet(tDCR.ID, rig.crypter)
tCore.wallets[tDCR.ID] = dcrWallet
dcrWallet.address = "DsVmA7aqqWeKWy461hXjytbZbgCqbB8g2dq"
dcrWallet.Unlock(rig.crypter)

btcWallet, tBtcWallet := newTWallet(tBTC.ID)
btcWallet, tBtcWallet := newTWallet(tBTC.ID, rig.crypter)
tCore.wallets[tBTC.ID] = btcWallet
btcWallet.address = "12DXGkvxFjuq5btXYkwWfBZaz1rVwFgini"
btcWallet.Unlock(rig.crypter)
Expand Down Expand Up @@ -3636,10 +3647,10 @@ func TestResolveActiveTrades(t *testing.T) {
defer rig.shutdown()
tCore := rig.core

btcWallet, tBtcWallet := newTWallet(tBTC.ID)
btcWallet, tBtcWallet := newTWallet(tBTC.ID, rig.crypter)
tCore.wallets[tBTC.ID] = btcWallet

dcrWallet, tDcrWallet := newTWallet(tDCR.ID)
dcrWallet, tDcrWallet := newTWallet(tDCR.ID, rig.crypter)
tCore.wallets[tDCR.ID] = dcrWallet

rig.acct.auth() // Short path through initializeDEXConnections
Expand Down Expand Up @@ -4254,10 +4265,10 @@ func TestLogout(t *testing.T) {
defer rig.shutdown()
tCore := rig.core

dcrWallet, tDcrWallet := newTWallet(tDCR.ID)
dcrWallet, tDcrWallet := newTWallet(tDCR.ID, rig.crypter)
tCore.wallets[tDCR.ID] = dcrWallet

btcWallet, _ := newTWallet(tBTC.ID)
btcWallet, _ := newTWallet(tBTC.ID, rig.crypter)
tCore.wallets[tBTC.ID] = btcWallet

ord := &order.LimitOrder{P: order.Prefix{ServerTime: time.Now()}}
Expand Down Expand Up @@ -4466,7 +4477,7 @@ func TestAssetBalance(t *testing.T) {
defer rig.shutdown()
tCore := rig.core

wallet, tWallet := newTWallet(tDCR.ID)
wallet, tWallet := newTWallet(tDCR.ID, rig.crypter)
tCore.wallets[tDCR.ID] = wallet
bal := &asset.Balance{
Available: 4e7,
Expand Down Expand Up @@ -4507,11 +4518,11 @@ func TestHandleTradeSuspensionMsg(t *testing.T) {

tCore := rig.core
dc := rig.dc
dcrWallet, tDcrWallet := newTWallet(tDCR.ID)
dcrWallet, tDcrWallet := newTWallet(tDCR.ID, rig.crypter)
tCore.wallets[tDCR.ID] = dcrWallet
dcrWallet.Unlock(rig.crypter)

btcWallet, _ := newTWallet(tBTC.ID)
btcWallet, _ := newTWallet(tBTC.ID, rig.crypter)
tCore.wallets[tBTC.ID] = btcWallet
btcWallet.Unlock(rig.crypter)

Expand Down Expand Up @@ -4704,11 +4715,11 @@ func TestHandleTradeResumptionMsg(t *testing.T) {
defer rig.shutdown()

tCore := rig.core
dcrWallet, _ := newTWallet(tDCR.ID)
dcrWallet, _ := newTWallet(tDCR.ID, rig.crypter)
tCore.wallets[tDCR.ID] = dcrWallet
dcrWallet.Unlock(rig.crypter)

btcWallet, _ := newTWallet(tBTC.ID)
btcWallet, _ := newTWallet(tBTC.ID, rig.crypter)
tCore.wallets[tBTC.ID] = btcWallet
btcWallet.Unlock(rig.crypter)

Expand Down Expand Up @@ -4804,9 +4815,9 @@ func TestHandleNomatch(t *testing.T) {
dc := rig.dc
mkt := dc.marketConfig(tDcrBtcMktName)

dcrWallet, _ := newTWallet(tDCR.ID)
dcrWallet, _ := newTWallet(tDCR.ID, rig.crypter)
tCore.wallets[tDCR.ID] = dcrWallet
btcWallet, _ := newTWallet(tBTC.ID)
btcWallet, _ := newTWallet(tBTC.ID, rig.crypter)
tCore.wallets[tBTC.ID] = btcWallet

walletSet, err := tCore.walletSet(dc, tDCR.ID, tBTC.ID, true)
Expand Down Expand Up @@ -4963,7 +4974,7 @@ func TestChangeAppPass(t *testing.T) {
}
rig.crypter.recryptErr = nil

wallet, _ := newTWallet(assetID)
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) {
Expand All @@ -4975,7 +4986,7 @@ func TestChangeAppPass(t *testing.T) {
}
defer wallet.Disconnect()

origPass := wallet.encPW()
origPass := string(wallet.encPW())
err = tCore.ChangeAppPass(tPW, newTPW)
if err != nil {
t.Fatal(err)
Expand All @@ -4992,8 +5003,9 @@ func TestChangeAppPass(t *testing.T) {
}
// Ensure xcWallet encrypted password updated.
nWallet := tCore.wallets[assetID]
if bytes.Equal(origPass, nWallet.encPW()) {
t.Fatalf("expected xcwallet to change: old %x new %x", origPass, nWallet.encPW())
if origPass == string(nWallet.encPW()) {
t.Fatalf("expected xcwallet to change: old %x new %x",
origPass, nWallet.encPW())
}
}

Expand Down Expand Up @@ -5025,7 +5037,7 @@ func TestReconfigureWallet(t *testing.T) {
t.Fatalf("wrong error for missing wallet: %v", err)
}

xyzWallet, tXyzWallet := newTWallet(assetID)
xyzWallet, tXyzWallet := newTWallet(assetID, rig.crypter)
tCore.wallets[assetID] = xyzWallet
asset.Register(assetID, &tDriver{
f: func(wCfg *asset.WalletConfig, logger dex.Logger, net dex.Network) (asset.Wallet, error) {
Expand Down Expand Up @@ -5101,8 +5113,10 @@ func TestReconfigureWallet(t *testing.T) {

// Check that the xcWallet was updated.
xyzWallet = tCore.wallets[assetID]
if !bytes.Equal(xyzWallet.encPW(), newWalletPW) {
t.Fatalf("xcWallet encPW field not updated want: %x got: %x", newWalletPW, xyzWallet.encPW())
decNewPW, _ := rig.crypter.Decrypt(xyzWallet.encPW())
if !bytes.Equal(decNewPW, newWalletPW) {
t.Fatalf("xcWallet encPW field not updated want: %x got: %x",
newWalletPW, decNewPW)
}
}

Expand Down Expand Up @@ -5136,7 +5150,7 @@ func TestSetWalletPassword(t *testing.T) {
t.Fatalf("wrong error for missing wallet: %v", err)
}

xyzWallet, tXyzWallet := newTWallet(assetID)
xyzWallet, tXyzWallet := newTWallet(assetID, rig.crypter)
tCore.wallets[assetID] = xyzWallet

// Connection error
Expand Down Expand Up @@ -5172,7 +5186,8 @@ func TestSetWalletPassword(t *testing.T) {
}

// Check that the xcWallet was updated.
if !bytes.Equal(xyzWallet.encPW(), newPW) {
decNewPW, _ := rig.crypter.Decrypt(xyzWallet.encPW())
if !bytes.Equal(decNewPW, newPW) {
t.Fatalf("xcWallet encPW field not updated")
}
}
Expand Down Expand Up @@ -5249,11 +5264,11 @@ func TestPreimageSync(t *testing.T) {
rig := newTestRig()
defer rig.shutdown()
tCore := rig.core
dcrWallet, tDcrWallet := newTWallet(tDCR.ID)
dcrWallet, tDcrWallet := newTWallet(tDCR.ID, rig.crypter)
tCore.wallets[tDCR.ID] = dcrWallet
dcrWallet.Unlock(rig.crypter)

btcWallet, tBtcWallet := newTWallet(tBTC.ID)
btcWallet, tBtcWallet := newTWallet(tBTC.ID, rig.crypter)
tCore.wallets[tBTC.ID] = btcWallet
btcWallet.Unlock(rig.crypter)

Expand Down Expand Up @@ -5345,9 +5360,9 @@ func TestMatchStatusResolution(t *testing.T) {
dc := rig.dc
mkt := dc.marketConfig(tDcrBtcMktName)

dcrWallet, _ := newTWallet(tDCR.ID)
dcrWallet, _ := newTWallet(tDCR.ID, rig.crypter)
tCore.wallets[tDCR.ID] = dcrWallet
btcWallet, tBtcWallet := newTWallet(tBTC.ID)
btcWallet, tBtcWallet := newTWallet(tBTC.ID, rig.crypter)
tCore.wallets[tBTC.ID] = btcWallet
walletSet, _ := tCore.walletSet(dc, tDCR.ID, tBTC.ID, true)

Expand Down Expand Up @@ -5838,9 +5853,9 @@ func TestSuspectTrades(t *testing.T) {
dc := rig.dc
tCore := rig.core

dcrWallet, tDcrWallet := newTWallet(tDCR.ID)
dcrWallet, tDcrWallet := newTWallet(tDCR.ID, rig.crypter)
tCore.wallets[tDCR.ID] = dcrWallet
btcWallet, tBtcWallet := newTWallet(tBTC.ID)
btcWallet, tBtcWallet := newTWallet(tBTC.ID, rig.crypter)
tCore.wallets[tBTC.ID] = btcWallet
walletSet, _ := tCore.walletSet(dc, tDCR.ID, tBTC.ID, true)

Expand Down Expand Up @@ -6030,7 +6045,7 @@ func TestWalletSyncing(t *testing.T) {
tCore := rig.core

noteFeed := tCore.NotificationFeed()
dcrWallet, tDcrWallet := newTWallet(tDCR.ID)
dcrWallet, tDcrWallet := newTWallet(tDCR.ID, rig.crypter)
dcrWallet.synced = false
dcrWallet.syncProgress = 0
_ = dcrWallet.Connect()
Expand Down Expand Up @@ -6114,9 +6129,9 @@ func TestPreOrder(t *testing.T) {
rig := newTestRig()
tCore := rig.core

btcWallet, tBtcWallet := newTWallet(tBTC.ID)
btcWallet, tBtcWallet := newTWallet(tBTC.ID, rig.crypter)
tCore.wallets[tBTC.ID] = btcWallet
dcrWallet, tDcrWallet := newTWallet(tDCR.ID)
dcrWallet, tDcrWallet := newTWallet(tDCR.ID, rig.crypter)
tCore.wallets[tDCR.ID] = dcrWallet

var rate uint64 = 1e8
Expand Down

0 comments on commit f739e48

Please sign in to comment.