2019-08-20 15:38:40 +00:00
|
|
|
package accounts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2022-05-11 09:47:17 +00:00
|
|
|
"encoding/json"
|
2019-08-20 15:38:40 +00:00
|
|
|
|
2019-12-11 13:59:37 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2022-03-23 18:47:00 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/errors"
|
2022-09-06 07:10:40 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/keypairs"
|
2022-03-23 18:47:00 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/settings"
|
2022-05-20 08:59:36 +00:00
|
|
|
notificationssettings "github.com/status-im/status-go/multiaccounts/settings_notifications"
|
2022-08-02 12:56:26 +00:00
|
|
|
sociallinkssettings "github.com/status-im/status-go/multiaccounts/settings_social_links"
|
2022-03-21 14:18:36 +00:00
|
|
|
"github.com/status-im/status-go/nodecfg"
|
|
|
|
"github.com/status-im/status-go/params"
|
2019-08-20 15:38:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
uniqueChatConstraint = "UNIQUE constraint failed: accounts.chat"
|
|
|
|
uniqueWalletConstraint = "UNIQUE constraint failed: accounts.wallet"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Account struct {
|
2022-05-30 06:19:49 +00:00
|
|
|
Address types.Address `json:"address"`
|
2022-09-05 11:52:53 +00:00
|
|
|
KeyUID string `json:"key-uid"`
|
2022-05-30 06:19:49 +00:00
|
|
|
Wallet bool `json:"wallet"`
|
|
|
|
Chat bool `json:"chat"`
|
2022-08-25 15:09:08 +00:00
|
|
|
Type AccountType `json:"type,omitempty"`
|
2022-05-30 06:19:49 +00:00
|
|
|
Storage string `json:"storage,omitempty"`
|
|
|
|
Path string `json:"path,omitempty"`
|
|
|
|
PublicKey types.HexBytes `json:"public-key,omitempty"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Emoji string `json:"emoji"`
|
|
|
|
Color string `json:"color"`
|
|
|
|
Hidden bool `json:"hidden"`
|
|
|
|
DerivedFrom string `json:"derived-from,omitempty"`
|
|
|
|
Clock uint64 `json:"clock,omitempty"`
|
|
|
|
Removed bool `json:"removed,omitempty"`
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 15:09:08 +00:00
|
|
|
type AccountType string
|
|
|
|
|
|
|
|
func (a AccountType) String() string {
|
|
|
|
return string(a)
|
|
|
|
}
|
|
|
|
|
2020-12-07 14:03:18 +00:00
|
|
|
const (
|
2022-08-25 15:09:08 +00:00
|
|
|
AccountTypeGenerated AccountType = "generated"
|
|
|
|
AccountTypeKey AccountType = "key"
|
|
|
|
AccountTypeSeed AccountType = "seed"
|
|
|
|
AccountTypeWatch AccountType = "watch"
|
2020-12-07 14:03:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// IsOwnAccount returns true if this is an account we have the private key for
|
|
|
|
// NOTE: Wallet flag can't be used as it actually indicates that it's the default
|
|
|
|
// Wallet
|
|
|
|
func (a *Account) IsOwnAccount() bool {
|
2022-07-06 16:12:49 +00:00
|
|
|
return a.Wallet || a.Type == AccountTypeSeed || a.Type == AccountTypeGenerated || a.Type == AccountTypeKey
|
2020-12-07 14:03:18 +00:00
|
|
|
}
|
|
|
|
|
2022-05-11 09:47:17 +00:00
|
|
|
func (a *Account) MarshalJSON() ([]byte, error) {
|
|
|
|
item := struct {
|
|
|
|
Address types.Address `json:"address"`
|
|
|
|
MixedcaseAddress string `json:"mixedcase-address"`
|
2022-09-05 11:52:53 +00:00
|
|
|
KeyUID string `json:"key-uid"`
|
2022-05-11 09:47:17 +00:00
|
|
|
Wallet bool `json:"wallet"`
|
|
|
|
Chat bool `json:"chat"`
|
2022-08-25 15:09:08 +00:00
|
|
|
Type AccountType `json:"type,omitempty"`
|
2022-05-11 09:47:17 +00:00
|
|
|
Storage string `json:"storage,omitempty"`
|
|
|
|
Path string `json:"path,omitempty"`
|
|
|
|
PublicKey types.HexBytes `json:"public-key,omitempty"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Emoji string `json:"emoji"`
|
|
|
|
Color string `json:"color"`
|
|
|
|
Hidden bool `json:"hidden"`
|
|
|
|
DerivedFrom string `json:"derived-from,omitempty"`
|
2022-05-18 10:42:51 +00:00
|
|
|
Clock uint64 `json:"clock"`
|
|
|
|
Removed bool `json:"removed"`
|
2022-05-11 09:47:17 +00:00
|
|
|
}{
|
|
|
|
Address: a.Address,
|
|
|
|
MixedcaseAddress: a.Address.Hex(),
|
2022-09-05 11:52:53 +00:00
|
|
|
KeyUID: a.KeyUID,
|
2022-05-11 09:47:17 +00:00
|
|
|
Wallet: a.Wallet,
|
|
|
|
Chat: a.Chat,
|
|
|
|
Type: a.Type,
|
|
|
|
Storage: a.Storage,
|
|
|
|
Path: a.Path,
|
|
|
|
PublicKey: a.PublicKey,
|
|
|
|
Name: a.Name,
|
|
|
|
Emoji: a.Emoji,
|
|
|
|
Color: a.Color,
|
|
|
|
Hidden: a.Hidden,
|
|
|
|
DerivedFrom: a.DerivedFrom,
|
2022-05-18 10:42:51 +00:00
|
|
|
Clock: a.Clock,
|
|
|
|
Removed: a.Removed,
|
2022-05-11 09:47:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(item)
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
// Database sql wrapper for operations with browser objects.
|
|
|
|
type Database struct {
|
2022-03-23 18:47:00 +00:00
|
|
|
*settings.Database
|
2022-05-20 08:59:36 +00:00
|
|
|
*notificationssettings.NotificationsSettings
|
2022-08-02 12:56:26 +00:00
|
|
|
*sociallinkssettings.SocialLinksSettings
|
2022-09-06 07:10:40 +00:00
|
|
|
*keypairs.KeyPairs
|
2019-08-20 15:38:40 +00:00
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
// NewDB returns a new instance of *Database
|
|
|
|
func NewDB(db *sql.DB) (*Database, error) {
|
|
|
|
sDB, err := settings.MakeNewDB(db)
|
2022-01-12 20:04:43 +00:00
|
|
|
if err != nil {
|
2022-03-23 18:47:00 +00:00
|
|
|
return nil, err
|
2022-01-12 20:04:43 +00:00
|
|
|
}
|
2022-05-20 08:59:36 +00:00
|
|
|
sn := notificationssettings.NewNotificationsSettings(db)
|
2022-08-02 12:56:26 +00:00
|
|
|
ssl := sociallinkssettings.NewSocialLinksSettings(db)
|
2022-09-06 07:10:40 +00:00
|
|
|
kp := keypairs.NewKeyPairs(db)
|
2019-08-20 15:38:40 +00:00
|
|
|
|
2022-09-06 07:10:40 +00:00
|
|
|
return &Database{sDB, sn, ssl, kp, db}, nil
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
// DB Gets db sql.DB
|
|
|
|
func (db Database) DB() *sql.DB {
|
|
|
|
return db.db
|
2019-12-27 09:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
// Close closes database.
|
|
|
|
func (db Database) Close() error {
|
|
|
|
return db.db.Close()
|
2019-08-08 07:31:24 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 10:42:51 +00:00
|
|
|
func (db *Database) GetAccounts() ([]*Account, error) {
|
2022-09-05 11:52:53 +00:00
|
|
|
rows, err := db.db.Query(`SELECT address, wallet, chat, type, storage, pubkey, path, name, emoji, color, hidden, derived_from, clock, key_uid
|
|
|
|
FROM accounts ORDER BY created_at`)
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
defer rows.Close()
|
2022-05-18 10:42:51 +00:00
|
|
|
accounts := []*Account{}
|
2019-08-20 15:38:40 +00:00
|
|
|
pubkey := []byte{}
|
|
|
|
for rows.Next() {
|
2022-05-18 10:42:51 +00:00
|
|
|
acc := &Account{}
|
2019-08-20 15:38:40 +00:00
|
|
|
err := rows.Scan(
|
|
|
|
&acc.Address, &acc.Wallet, &acc.Chat, &acc.Type, &acc.Storage,
|
2022-09-05 11:52:53 +00:00
|
|
|
&pubkey, &acc.Path, &acc.Name, &acc.Emoji, &acc.Color, &acc.Hidden, &acc.DerivedFrom, &acc.Clock, &acc.KeyUID)
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if lth := len(pubkey); lth > 0 {
|
2019-12-11 13:59:37 +00:00
|
|
|
acc.PublicKey = make(types.HexBytes, lth)
|
2019-08-20 15:38:40 +00:00
|
|
|
copy(acc.PublicKey, pubkey)
|
|
|
|
}
|
|
|
|
accounts = append(accounts, acc)
|
|
|
|
}
|
|
|
|
return accounts, nil
|
|
|
|
}
|
|
|
|
|
2020-10-28 07:56:14 +00:00
|
|
|
func (db *Database) GetAccountByAddress(address types.Address) (rst *Account, err error) {
|
2022-09-05 11:52:53 +00:00
|
|
|
row := db.db.QueryRow(`SELECT address, wallet, chat, type, storage, pubkey, path, name, emoji, color, hidden, derived_from, clock, key_uid
|
|
|
|
FROM accounts WHERE address = ? COLLATE NOCASE`, address)
|
2020-10-28 07:56:14 +00:00
|
|
|
|
|
|
|
acc := &Account{}
|
|
|
|
pubkey := []byte{}
|
|
|
|
err = row.Scan(
|
|
|
|
&acc.Address, &acc.Wallet, &acc.Chat, &acc.Type, &acc.Storage,
|
2022-09-05 11:52:53 +00:00
|
|
|
&pubkey, &acc.Path, &acc.Name, &acc.Emoji, &acc.Color, &acc.Hidden, &acc.DerivedFrom, &acc.Clock, &acc.KeyUID)
|
2020-10-28 07:56:14 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
acc.PublicKey = pubkey
|
|
|
|
return acc, nil
|
|
|
|
}
|
|
|
|
|
2022-05-18 10:42:51 +00:00
|
|
|
func (db *Database) SaveAccounts(accounts []*Account) (err error) {
|
2019-08-20 15:38:40 +00:00
|
|
|
var (
|
|
|
|
tx *sql.Tx
|
|
|
|
insert *sql.Stmt
|
2022-05-18 10:42:51 +00:00
|
|
|
delete *sql.Stmt
|
2019-08-20 15:38:40 +00:00
|
|
|
update *sql.Stmt
|
|
|
|
)
|
|
|
|
tx, err = db.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
// NOTE(dshulyak) replace all record values using address (primary key)
|
|
|
|
// can't use `insert or replace` because of the additional constraints (wallet and chat)
|
2019-12-16 15:23:36 +00:00
|
|
|
insert, err = tx.Prepare("INSERT OR IGNORE INTO accounts (address, created_at, updated_at) VALUES (?, datetime('now'), datetime('now'))")
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-18 10:42:51 +00:00
|
|
|
delete, err = tx.Prepare("DELETE FROM accounts WHERE address = ?")
|
2022-09-05 11:52:53 +00:00
|
|
|
update, err = tx.Prepare(`UPDATE accounts
|
|
|
|
SET
|
|
|
|
wallet = ?,
|
|
|
|
chat = ?,
|
|
|
|
type = ?,
|
|
|
|
storage = ?,
|
|
|
|
pubkey = ?,
|
|
|
|
path = ?,
|
|
|
|
name = ?,
|
|
|
|
emoji = ?,
|
|
|
|
color = ?,
|
|
|
|
hidden = ?,
|
|
|
|
derived_from = ?,
|
|
|
|
key_uid = ?,
|
|
|
|
updated_at = datetime('now'),
|
|
|
|
clock = ?
|
|
|
|
WHERE
|
|
|
|
address = ?`)
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := range accounts {
|
2022-05-18 10:42:51 +00:00
|
|
|
acc := accounts[i]
|
|
|
|
if acc.Removed {
|
|
|
|
_, err = delete.Exec(acc.Address)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2019-08-20 15:38:40 +00:00
|
|
|
_, err = insert.Exec(acc.Address)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-09-05 11:52:53 +00:00
|
|
|
_, err = update.Exec(acc.Wallet, acc.Chat, acc.Type, acc.Storage, acc.PublicKey, acc.Path, acc.Name, acc.Emoji, acc.Color,
|
|
|
|
acc.Hidden, acc.DerivedFrom, acc.KeyUID, acc.Clock, acc.Address)
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
switch err.Error() {
|
|
|
|
case uniqueChatConstraint:
|
2022-03-23 18:47:00 +00:00
|
|
|
err = errors.ErrChatNotUnique
|
2019-08-20 15:38:40 +00:00
|
|
|
case uniqueWalletConstraint:
|
2022-03-23 18:47:00 +00:00
|
|
|
err = errors.ErrWalletNotUnique
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-11 13:59:37 +00:00
|
|
|
func (db *Database) DeleteAccount(address types.Address) error {
|
2019-12-16 15:23:36 +00:00
|
|
|
_, err := db.db.Exec("DELETE FROM accounts WHERE address = ?", address)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-11 13:59:37 +00:00
|
|
|
func (db *Database) GetWalletAddress() (rst types.Address, err error) {
|
2019-08-20 15:38:40 +00:00
|
|
|
err = db.db.QueryRow("SELECT address FROM accounts WHERE wallet = 1").Scan(&rst)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
status-im/status-react#9203 Faster tx fetching with less request
*** How it worked before this PR on multiaccount creation:
- On multiacc creation we scanned chain for eth and erc20 transfers. For
each address of a new empty multiaccount this scan required
1. two `eth_getBalance` requests to find out that there is no any
balance change between zero and the last block, for eth transfers
2. and `chain-size/100000` (currently ~100) `eth_getLogs` requests,
for erc20 transfers
- For some reason we scanned an address of the chat account as well, and
also accounts were not deduplicated. So even for an empty multiacc we
scanned chain twice for each chat and main wallet addresses, in result
app had to execute about 400 requests.
- As mentioned above, `eth_getBalance` requests were used to check if
there were any eth transfers, and that caused empty history in case
if user already used all available eth (so that both zero and latest
blocks show 0 eth for an address). There might have been transactions
but we wouldn't fetch/show them.
- There was no upper limit for the number of rpc requests during the
scan, so it could require indefinite number of requests; the scanning
algorithm was written so that we persisted the whole history of
transactions or tried to scan form the beginning again in case of
failure, giving up only after 10 minutes of failures. In result
addresses with sufficient number of transactions would never be fully
scanned and during these 10 minutes app could use gigabytes of
internet data.
- Failures were caused by `eth_getBlockByNumber`/`eth_getBlockByHash`
requests. These requests return significantly bigger responses than
`eth_getBalance`/`eth_transactionsCount` and it is likely that
execution of thousands of them in parallel caused failures for
accounts with hundreds of transactions. Even for an account with 12k
we could successfully determine blocks with transaction in a few
minutes using `eth_getBalance` requests, but `eth_getBlock...`
couldn't be processed for this acc.
- There was no caching for for `eth_getBalance` requests, and this
caused in average 3-4 times more such requests than is needed.
*** How it works now on multiaccount creation:
- On multiacc creation we scan chain for last ~30 eth transactions and
then check erc20 in the range where these eth transactions were found.
For an empty address in multiacc this means:
1. two `eth_getBalance` transactions to determine that there was no
balance change between zero and the last block; two
`eth_transactionsCount` requests to determine there are no outgoing
transactions for this address; total 4 requests for eth transfers
2. 20 `eth_getLogs` for erc20 transfers. This number can be lowered,
but that's not a big deal
- Deduplication of addresses is added and also we don't scan chat
account, so a new multiacc requires ~25 (we also request latest block
number and probably execute a few other calls) request to determine
that multiacc is empty (comparing to ~400 before)
- In case if address contains transactions we:
1. determine the range which contains 20-25 outgoing eth/erc20
transactions. This usually requires up to 10 `eth_transactionCount`
requests
2. then we scan chain for eth transfers using `eth_getBalance` and
`eth_transactionCount` (for double checking zero balances)
3. we make sure that we do not scan db for more than 30 blocks with
transfers. That's important for accounts with mostly incoming
transactions, because the range found on the first step might
contain any number of incoming transfers, but only 20-25 outgoing
transactions
4. when we found ~30 blocks in a given range, we update initial
range `from` block using the oldest found block
5. and now we scan db for erc20transfers using `eth_getLogs`
`oldest-found-eth-block`-`latest-block`, we make not more than 20 calls
6. when all blocks which contain incoming/outgoing transfers for a
given address are found, we save these blocks to db and mark that
transfers from these blocks are still to be fetched
7. Then we select latest ~30 (the number can be adjusted) blocks from
these which were found and fetch transfers, this requires 3-4
requests per transfer.
8. we persist scanned range so that we know were to start next time
9. we dispatch an event which tells client that transactions are found
10. client fetches latest 20 transfers
- when user presses "fetch more" button we check if app's db contains next
20 transfers, if not we scan chain again and return transfers after
small fixes
2019-12-18 11:01:46 +00:00
|
|
|
func (db *Database) GetWalletAddresses() (rst []types.Address, err error) {
|
|
|
|
rows, err := db.db.Query("SELECT address FROM accounts WHERE chat = 0 ORDER BY created_at")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
addr := types.Address{}
|
|
|
|
err = rows.Scan(&addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rst = append(rst, addr)
|
|
|
|
}
|
|
|
|
return rst, nil
|
|
|
|
}
|
|
|
|
|
2019-12-11 13:59:37 +00:00
|
|
|
func (db *Database) GetChatAddress() (rst types.Address, err error) {
|
2019-08-20 15:38:40 +00:00
|
|
|
err = db.db.QueryRow("SELECT address FROM accounts WHERE chat = 1").Scan(&rst)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-11 13:59:37 +00:00
|
|
|
func (db *Database) GetAddresses() (rst []types.Address, err error) {
|
2019-12-16 15:23:36 +00:00
|
|
|
rows, err := db.db.Query("SELECT address FROM accounts ORDER BY created_at")
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
defer rows.Close()
|
2019-08-20 15:38:40 +00:00
|
|
|
for rows.Next() {
|
2019-12-11 13:59:37 +00:00
|
|
|
addr := types.Address{}
|
2019-08-20 15:38:40 +00:00
|
|
|
err = rows.Scan(&addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rst = append(rst, addr)
|
|
|
|
}
|
|
|
|
return rst, nil
|
|
|
|
}
|
2019-08-29 08:06:22 +00:00
|
|
|
|
|
|
|
// AddressExists returns true if given address is stored in database.
|
2019-12-11 13:59:37 +00:00
|
|
|
func (db *Database) AddressExists(address types.Address) (exists bool, err error) {
|
2019-08-29 08:06:22 +00:00
|
|
|
err = db.db.QueryRow("SELECT EXISTS (SELECT 1 FROM accounts WHERE address = ?)", address).Scan(&exists)
|
|
|
|
return exists, err
|
|
|
|
}
|
2022-03-21 14:18:36 +00:00
|
|
|
|
2022-07-06 16:12:49 +00:00
|
|
|
// GetPath returns true if account with given address was recently key and doesn't have a key yet
|
|
|
|
func (db *Database) GetPath(address types.Address) (path string, err error) {
|
|
|
|
err = db.db.QueryRow("SELECT path FROM accounts WHERE address = ?", address).Scan(&path)
|
|
|
|
return path, err
|
|
|
|
}
|
|
|
|
|
2022-03-21 14:18:36 +00:00
|
|
|
func (db *Database) GetNodeConfig() (*params.NodeConfig, error) {
|
2022-04-22 08:11:40 +00:00
|
|
|
return nodecfg.GetNodeConfigFromDB(db.db)
|
2022-03-21 14:18:36 +00:00
|
|
|
}
|