2019-08-20 15:38:40 +00:00
|
|
|
package accounts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2022-05-11 09:47:17 +00:00
|
|
|
"encoding/json"
|
2023-03-20 07:33:08 +00:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
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"
|
2023-05-09 18:48:56 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/keycards"
|
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"
|
2023-03-20 07:33:08 +00:00
|
|
|
statusWalletRootPath = "m/44'/60'/0'/0/"
|
2019-08-20 15:38:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Account struct {
|
2023-03-20 07:33:08 +00:00
|
|
|
Address types.Address `json:"address"`
|
|
|
|
KeyUID string `json:"key-uid"`
|
|
|
|
Wallet bool `json:"wallet"`
|
|
|
|
Chat bool `json:"chat"`
|
|
|
|
Type AccountType `json:"type,omitempty"`
|
|
|
|
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"`
|
|
|
|
KeypairName string `json:"keypair-name"`
|
|
|
|
LastUsedDerivationIndex uint64 `json:"last-used-derivation-index"`
|
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 {
|
2023-03-20 07:33:08 +00:00
|
|
|
Address types.Address `json:"address"`
|
|
|
|
MixedcaseAddress string `json:"mixedcase-address"`
|
|
|
|
KeyUID string `json:"key-uid"`
|
|
|
|
Wallet bool `json:"wallet"`
|
|
|
|
Chat bool `json:"chat"`
|
|
|
|
Type AccountType `json:"type,omitempty"`
|
|
|
|
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"`
|
|
|
|
Removed bool `json:"removed"`
|
|
|
|
KeypairName string `json:"keypair-name"`
|
|
|
|
LastUsedDerivationIndex uint64 `json:"last-used-derivation-index"`
|
2022-05-11 09:47:17 +00:00
|
|
|
}{
|
2023-03-20 07:33:08 +00:00
|
|
|
Address: a.Address,
|
|
|
|
MixedcaseAddress: a.Address.Hex(),
|
|
|
|
KeyUID: a.KeyUID,
|
|
|
|
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,
|
|
|
|
Clock: a.Clock,
|
|
|
|
Removed: a.Removed,
|
|
|
|
KeypairName: a.KeypairName,
|
|
|
|
LastUsedDerivationIndex: a.LastUsedDerivationIndex,
|
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
|
2023-05-09 18:48:56 +00:00
|
|
|
*keycards.Keycards
|
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)
|
2023-05-09 18:48:56 +00:00
|
|
|
kp := keycards.NewKeycards(db)
|
2019-08-20 15:38:40 +00:00
|
|
|
|
2023-03-20 07:33:08 +00:00
|
|
|
err = updateKeypairNameAndLastDerivationIndexIfNeeded(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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
|
2023-01-06 12:21:14 +00:00
|
|
|
func (db *Database) DB() *sql.DB {
|
2022-03-23 18:47:00 +00:00
|
|
|
return db.db
|
2019-12-27 09:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
// Close closes database.
|
2023-01-06 12:21:14 +00:00
|
|
|
func (db *Database) Close() error {
|
2022-03-23 18:47:00 +00:00
|
|
|
return db.db.Close()
|
2019-08-08 07:31:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 07:33:08 +00:00
|
|
|
func updateKeypairNameAndLastUsedIndex(tx *sql.Tx, keyUID string, keypairName string, index uint64) (err error) {
|
|
|
|
if tx == nil {
|
|
|
|
return errors.ErrDbTransactionIsNil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tx.Exec(`
|
|
|
|
UPDATE
|
|
|
|
accounts
|
|
|
|
SET
|
|
|
|
keypair_name = ?,
|
|
|
|
last_used_derivation_index = ?
|
|
|
|
WHERE
|
|
|
|
key_uid = ?
|
|
|
|
AND
|
|
|
|
NOT chat`,
|
|
|
|
keypairName, index, keyUID)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateKeypairNameAndLastDerivationIndexIfNeeded(db *sql.DB) error {
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
|
|
|
|
var displayName string
|
|
|
|
err = tx.QueryRow("SELECT display_name FROM settings WHERE synthetic_id = 'id'").Scan(&displayName)
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-27 14:33:00 +00:00
|
|
|
if displayName == "" {
|
|
|
|
displayName = "Status"
|
|
|
|
}
|
|
|
|
|
2023-03-20 07:33:08 +00:00
|
|
|
var (
|
|
|
|
seedKeyPairIndex int
|
|
|
|
keyKeyPairIndex int
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
// Except for the Status chat account, keypair must not be empty and it must be unique per keypair.
|
|
|
|
rows, err := tx.Query(`SELECT wallet, type, path, derived_from, key_uid FROM accounts WHERE keypair_name = "" AND NOT chat ORDER BY key_uid`)
|
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var dbAccounts []*Account
|
|
|
|
for rows.Next() {
|
|
|
|
acc := &Account{}
|
|
|
|
err := rows.Scan(&acc.Wallet, &acc.Type, &acc.Path, &acc.DerivedFrom, &acc.KeyUID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dbAccounts = append(dbAccounts, acc)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resolveLastUsedIndex := func(keyUID string) uint64 {
|
|
|
|
lastUsedIndex := uint64(0)
|
|
|
|
for _, acc := range dbAccounts {
|
|
|
|
if acc.KeyUID == keyUID && strings.HasPrefix(acc.Path, statusWalletRootPath) {
|
|
|
|
index, err := strconv.ParseUint(acc.Path[len(statusWalletRootPath):], 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if index > lastUsedIndex {
|
|
|
|
lastUsedIndex = index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lastUsedIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dbAccounts) > 0 {
|
|
|
|
acc := dbAccounts[0]
|
|
|
|
keypairName := displayName
|
|
|
|
if acc.Type == AccountTypeSeed {
|
|
|
|
seedKeyPairIndex++
|
|
|
|
keypairName = fmt.Sprintf(`Seed imported %d`, seedKeyPairIndex)
|
|
|
|
} else if acc.Type == AccountTypeKey {
|
|
|
|
keyKeyPairIndex++
|
|
|
|
keypairName = fmt.Sprintf(`Key imported %d`, keyKeyPairIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = updateKeypairNameAndLastUsedIndex(tx, acc.KeyUID, keypairName, resolveLastUsedIndex(acc.KeyUID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
func (db *Database) GetAccountsByKeyUID(keyUID string) ([]*Account, error) {
|
|
|
|
accounts, err := db.GetAccounts()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
filteredAccounts := make([]*Account, 0)
|
|
|
|
for _, account := range accounts {
|
|
|
|
if account.KeyUID == keyUID {
|
|
|
|
filteredAccounts = append(filteredAccounts, account)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filteredAccounts, nil
|
|
|
|
}
|
|
|
|
|
2022-05-18 10:42:51 +00:00
|
|
|
func (db *Database) GetAccounts() ([]*Account, error) {
|
2023-03-20 07:33:08 +00:00
|
|
|
rows, err := db.db.Query(`
|
|
|
|
SELECT
|
|
|
|
address,
|
|
|
|
wallet,
|
|
|
|
chat,
|
|
|
|
type,
|
|
|
|
storage,
|
|
|
|
pubkey,
|
|
|
|
path,
|
|
|
|
name,
|
|
|
|
emoji,
|
|
|
|
color,
|
|
|
|
hidden,
|
|
|
|
derived_from,
|
|
|
|
clock,
|
|
|
|
key_uid,
|
|
|
|
keypair_name,
|
|
|
|
last_used_derivation_index
|
|
|
|
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(
|
2023-03-20 07:33:08 +00:00
|
|
|
&acc.Address, &acc.Wallet, &acc.Chat, &acc.Type, &acc.Storage, &pubkey, &acc.Path, &acc.Name, &acc.Emoji,
|
|
|
|
&acc.Color, &acc.Hidden, &acc.DerivedFrom, &acc.Clock, &acc.KeyUID, &acc.KeypairName, &acc.LastUsedDerivationIndex)
|
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) {
|
2023-03-20 07:33:08 +00:00
|
|
|
row := db.db.QueryRow(`
|
|
|
|
SELECT
|
|
|
|
address,
|
|
|
|
wallet,
|
|
|
|
chat,
|
|
|
|
type,
|
|
|
|
storage,
|
|
|
|
pubkey,
|
|
|
|
path,
|
|
|
|
name,
|
|
|
|
emoji,
|
|
|
|
color,
|
|
|
|
hidden,
|
|
|
|
derived_from,
|
|
|
|
clock,
|
|
|
|
key_uid,
|
|
|
|
keypair_name,
|
|
|
|
last_used_derivation_index
|
|
|
|
FROM
|
|
|
|
accounts
|
|
|
|
WHERE
|
|
|
|
address = ? COLLATE NOCASE`,
|
|
|
|
address)
|
2020-10-28 07:56:14 +00:00
|
|
|
|
|
|
|
acc := &Account{}
|
|
|
|
pubkey := []byte{}
|
|
|
|
err = row.Scan(
|
2023-03-20 07:33:08 +00:00
|
|
|
&acc.Address, &acc.Wallet, &acc.Chat, &acc.Type, &acc.Storage, &pubkey, &acc.Path, &acc.Name, &acc.Emoji,
|
|
|
|
&acc.Color, &acc.Hidden, &acc.DerivedFrom, &acc.Clock, &acc.KeyUID, &acc.KeypairName, &acc.LastUsedDerivationIndex)
|
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) {
|
2023-03-20 07:33:08 +00:00
|
|
|
// Once mobile app introduces keypair we should check for the "KeypairName" and "DerivedFrom" field and return error
|
|
|
|
// if those fields are missing.
|
|
|
|
//
|
|
|
|
// Note:
|
|
|
|
// - Status chat account doesn't have "KeypairName" and "DerivedFrom" fields set
|
|
|
|
// - default Status wallet account has "KeypairName" and "DerivedFrom" fields set
|
|
|
|
// - accounts generated from the Status wallet master key have "KeypairName" and "DerivedFrom" fields set
|
|
|
|
// - accounts added importing private key don't have "DerivedFrom" (they have "KeypairName")
|
|
|
|
// - accounts added importing seed phrase or generated from already imported seed phrase have "KeypairName" and "DerivedFrom" fields set
|
|
|
|
// - watch only accounts don't have "KeypairName" and "DerivedFrom" fields set
|
|
|
|
|
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()
|
|
|
|
}()
|
2023-03-20 07:33:08 +00:00
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
// 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'),
|
2023-03-20 07:33:08 +00:00
|
|
|
clock = ?,
|
|
|
|
keypair_name = ?,
|
|
|
|
last_used_derivation_index = ?
|
2022-09-05 11:52:53 +00:00
|
|
|
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
|
|
|
|
}
|
2023-03-20 07:33:08 +00:00
|
|
|
|
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,
|
2023-03-20 07:33:08 +00:00
|
|
|
acc.Hidden, acc.DerivedFrom, acc.KeyUID, acc.Clock, acc.KeypairName, acc.LastUsedDerivationIndex, 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
|
|
|
|
}
|
2023-03-20 07:33:08 +00:00
|
|
|
|
|
|
|
err = updateKeypairNameAndLastUsedIndex(tx, acc.KeyUID, acc.KeypairName, acc.LastUsedDerivationIndex)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
2023-03-20 07:33:08 +00:00
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
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
|
|
|
}
|