Skip to content

Commit

Permalink
Encrypt database with same password taht is used for pfs db
Browse files Browse the repository at this point in the history
  • Loading branch information
dshulyak committed Jun 7, 2019
1 parent 72ca011 commit 402837a
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
8 changes: 4 additions & 4 deletions api/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ func (b *StatusBackend) reSelectAccount() error {
default:
return err
}
return b.startWallet()
return nil
}

// SelectAccount selects current wallet and chat accounts, by verifying that each address has corresponding account which can be decrypted
Expand Down Expand Up @@ -551,10 +551,10 @@ func (b *StatusBackend) SelectAccount(walletAddress, chatAddress, password strin
return err
}
}
return b.startWallet()
return b.startWallet(password)
}

func (b *StatusBackend) startWallet() error {
func (b *StatusBackend) startWallet(password string) error {
if !b.statusNode.Config().WalletConfig.Enabled {
return nil
}
Expand All @@ -567,7 +567,7 @@ func (b *StatusBackend) startWallet() error {
return err
}
path := path.Join(b.statusNode.Config().DataDir, fmt.Sprintf("wallet-%x.sql", account.Address))
return wallet.StartReactor(path,
return wallet.StartReactor(path, password,
b.statusNode.RPCClient().Ethclient(),
[]common.Address{account.Address},
new(big.Int).SetUint64(b.statusNode.Config().NetworkID))
Expand Down
1 change: 0 additions & 1 deletion services/wallet/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ func (c *erc20HistoricalCommand) Run(ctx context.Context) (err error) {
break
}
headers := headersFromTransfers(transfers)
log.Info("storing header of the iterator", "header", c.iterator.Header().Number)
headers = append(headers, c.iterator.Header())
err = c.db.ProcessTranfers(transfers, []common.Address{c.address}, headers, nil, erc20Sync)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions services/wallet/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const (
)

// InitializeDB creates db file at a given path and applies migrations.
func InitializeDB(path string) (*Database, error) {
db, err := sqlite.OpenDB(path)
func InitializeDB(path, password string) (*Database, error) {
db, err := sqlite.OpenDB(path, password)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion services/wallet/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func setupTestDB(t *testing.T) (*Database, func()) {
tmpfile, err := ioutil.TempFile("", "wallet-tests-")
require.NoError(t, err)
db, err := InitializeDB(tmpfile.Name())
db, err := InitializeDB(tmpfile.Name(), "wallet-tests")
require.NoError(t, err)
return db, func() {
require.NoError(t, db.Close())
Expand Down
4 changes: 2 additions & 2 deletions services/wallet/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func (s *Service) Start(*p2p.Server) error {
}

// StartReactor separately because it requires known ethereum address, which will become available only after login.
func (s *Service) StartReactor(dbpath string, client *ethclient.Client, accounts []common.Address, chain *big.Int) error {
db, err := InitializeDB(dbpath)
func (s *Service) StartReactor(dbpath, password string, client *ethclient.Client, accounts []common.Address, chain *big.Int) error {
db, err := InitializeDB(dbpath, password)
if err != nil {
return err
}
Expand Down
12 changes: 9 additions & 3 deletions sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package sqlite

import (
"database/sql"
"errors"
"fmt"

_ "github.com/mutecomm/go-sqlcipher" // We require go sqlcipher that overrides default implementation
)

func openDB(path string) (*sql.DB, error) {
func openDB(path, key string) (*sql.DB, error) {
db, err := sql.Open("sqlite3", path)
if err != nil {
return nil, err
Expand All @@ -19,6 +20,11 @@ func openDB(path string) (*sql.DB, error) {
if _, err = db.Exec("PRAGMA foreign_keys=ON"); err != nil {
return nil, err
}
keyString := fmt.Sprintf("PRAGMA key = '%s'", key)
if _, err = db.Exec(keyString); err != nil {
return nil, errors.New("failed to set key pragma")
}

// readers do not block writers and faster i/o operations
// https://www.sqlite.org/draft/wal.html
// must be set after db is encrypted
Expand All @@ -35,6 +41,6 @@ func openDB(path string) (*sql.DB, error) {
}

// OpenDB opens not-encrypted database.
func OpenDB(path string) (*sql.DB, error) {
return openDB(path)
func OpenDB(path, key string) (*sql.DB, error) {
return openDB(path, key)
}

0 comments on commit 402837a

Please sign in to comment.