2021-09-10 18:08:22 +00:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2022-09-14 10:46:11 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2021-09-10 18:08:22 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
2022-09-14 10:46:11 +00:00
|
|
|
type savedAddressMeta struct {
|
|
|
|
Removed bool
|
|
|
|
UpdateClock uint64 // wall clock used to deconflict concurrent updates
|
|
|
|
}
|
|
|
|
|
2021-09-10 18:08:22 +00:00
|
|
|
type SavedAddress struct {
|
|
|
|
Address common.Address `json:"address"`
|
2023-03-06 13:26:46 +00:00
|
|
|
// TODO: Add Emoji
|
|
|
|
// Emoji string `json:"emoji"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Favourite bool `json:"favourite"`
|
|
|
|
ChainShortNames string `json:"chainShortNames"` // used with address only, not with ENSName
|
|
|
|
ENSName string `json:"ens"`
|
|
|
|
IsTest bool `json:"isTest"`
|
2022-09-14 10:46:11 +00:00
|
|
|
savedAddressMeta
|
2021-09-10 18:08:22 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 20:09:20 +00:00
|
|
|
func (s *SavedAddress) ID() string {
|
2023-03-06 13:26:46 +00:00
|
|
|
return fmt.Sprintf("%s-%s-%t", s.Address.Hex(), s.ENSName, s.IsTest)
|
2023-02-13 20:09:20 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 18:08:22 +00:00
|
|
|
type SavedAddressesManager struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
2022-09-14 10:46:11 +00:00
|
|
|
func NewSavedAddressesManager(db *sql.DB) *SavedAddressesManager {
|
|
|
|
return &SavedAddressesManager{db: db}
|
|
|
|
}
|
2021-09-10 18:08:22 +00:00
|
|
|
|
2023-03-06 13:26:46 +00:00
|
|
|
const rawQueryColumnsOrder = "address, name, favourite, removed, update_clock, chain_short_names, ens_name, is_test"
|
2022-09-14 10:46:11 +00:00
|
|
|
|
|
|
|
// getSavedAddressesFromDBRows retrieves all data based on SELECT Query using rawQueryColumnsOrder
|
|
|
|
func getSavedAddressesFromDBRows(rows *sql.Rows) ([]SavedAddress, error) {
|
|
|
|
var addresses []SavedAddress
|
2021-09-10 18:08:22 +00:00
|
|
|
for rows.Next() {
|
2022-05-10 07:48:05 +00:00
|
|
|
sa := SavedAddress{}
|
2022-09-14 10:46:11 +00:00
|
|
|
// based on rawQueryColumnsOrder
|
2023-03-06 13:26:46 +00:00
|
|
|
err := rows.Scan(&sa.Address, &sa.Name, &sa.Favourite, &sa.Removed, &sa.UpdateClock, &sa.ChainShortNames, &sa.ENSName, &sa.IsTest)
|
2021-09-10 18:08:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-09-14 10:46:11 +00:00
|
|
|
addresses = append(addresses, sa)
|
|
|
|
}
|
|
|
|
|
|
|
|
return addresses, nil
|
|
|
|
}
|
|
|
|
|
2023-03-06 13:26:46 +00:00
|
|
|
func (sam *SavedAddressesManager) getSavedAddresses(condition string) ([]SavedAddress, error) {
|
|
|
|
var whereCondition string
|
|
|
|
if condition != "" {
|
|
|
|
whereCondition = fmt.Sprintf("WHERE %s", condition)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := sam.db.Query(fmt.Sprintf("SELECT %s FROM saved_addresses %s", rawQueryColumnsOrder, whereCondition))
|
2022-09-14 10:46:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
addresses, err := getSavedAddressesFromDBRows(rows)
|
|
|
|
return addresses, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sam *SavedAddressesManager) GetSavedAddresses() ([]SavedAddress, error) {
|
2023-03-06 13:26:46 +00:00
|
|
|
return sam.getSavedAddresses("removed != 1")
|
2021-09-10 18:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-14 10:46:11 +00:00
|
|
|
// GetRawSavedAddresses provides access to the soft-delete and sync metadata
|
|
|
|
func (sam *SavedAddressesManager) GetRawSavedAddresses() ([]SavedAddress, error) {
|
2023-03-06 13:26:46 +00:00
|
|
|
return sam.getSavedAddresses("")
|
2022-09-14 10:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sam *SavedAddressesManager) upsertSavedAddress(sa SavedAddress, tx *sql.Tx) error {
|
2023-03-06 13:26:46 +00:00
|
|
|
sqlStatement := "INSERT OR REPLACE INTO saved_addresses (address, name, favourite, removed, update_clock, chain_short_names, ens_name, is_test) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
|
2022-09-14 10:46:11 +00:00
|
|
|
var err error
|
|
|
|
var insert *sql.Stmt
|
|
|
|
if tx != nil {
|
|
|
|
insert, err = tx.Prepare(sqlStatement)
|
|
|
|
} else {
|
|
|
|
insert, err = sam.db.Prepare(sqlStatement)
|
|
|
|
}
|
2021-09-10 18:08:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-14 10:46:11 +00:00
|
|
|
defer insert.Close()
|
2023-03-06 13:26:46 +00:00
|
|
|
_, err = insert.Exec(sa.Address, sa.Name, sa.Favourite, sa.Removed, sa.UpdateClock, sa.ChainShortNames, sa.ENSName, sa.IsTest)
|
2021-09-10 18:08:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-14 10:46:11 +00:00
|
|
|
func (sam *SavedAddressesManager) UpdateMetadataAndUpsertSavedAddress(sa SavedAddress) (updatedClock uint64, err error) {
|
|
|
|
sa.UpdateClock = uint64(time.Now().Unix())
|
|
|
|
err = sam.upsertSavedAddress(sa, nil)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return sa.UpdateClock, nil
|
|
|
|
}
|
|
|
|
|
2023-03-06 13:26:46 +00:00
|
|
|
func (sam *SavedAddressesManager) startTransactionAndCheckIfNewerChange(address common.Address, ens string, isTest bool, updateClock uint64) (newer bool, tx *sql.Tx, err error) {
|
2022-09-14 10:46:11 +00:00
|
|
|
tx, err = sam.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
2023-03-06 13:26:46 +00:00
|
|
|
row := tx.QueryRow("SELECT update_clock FROM saved_addresses WHERE address = ? AND is_test = ? AND ens_name = ?", address, isTest, ens)
|
2022-09-14 10:46:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, tx, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var dbUpdateClock uint64
|
|
|
|
err = row.Scan(&dbUpdateClock)
|
|
|
|
if err != nil {
|
|
|
|
return err == sql.ErrNoRows, tx, err
|
|
|
|
}
|
2023-03-06 13:26:46 +00:00
|
|
|
return dbUpdateClock < updateClock, tx, nil
|
2022-09-14 10:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sam *SavedAddressesManager) AddSavedAddressIfNewerUpdate(sa SavedAddress, updateClock uint64) (insertedOrUpdated bool, err error) {
|
2023-03-06 13:26:46 +00:00
|
|
|
newer, tx, err := sam.startTransactionAndCheckIfNewerChange(sa.Address, sa.ENSName, sa.IsTest, updateClock)
|
2022-09-14 10:46:11 +00:00
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
if !newer {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sa.UpdateClock = updateClock
|
|
|
|
err = sam.upsertSavedAddress(sa, tx)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
2023-03-06 13:26:46 +00:00
|
|
|
func (sam *SavedAddressesManager) DeleteSavedAddress(address common.Address, ens string, isTest bool, updateClock uint64) (deleted bool, err error) {
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
newer, tx, err := sam.startTransactionAndCheckIfNewerChange(address, ens, isTest, updateClock)
|
2022-09-14 10:46:11 +00:00
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
if !newer {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2023-03-06 13:26:46 +00:00
|
|
|
update, err := tx.Prepare(`UPDATE saved_addresses SET removed = 1, update_clock = ? WHERE address = ? AND is_test = ? AND ens_name = ?`)
|
2022-09-14 10:46:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2023-03-06 13:26:46 +00:00
|
|
|
defer update.Close()
|
|
|
|
res, err := update.Exec(updateClock, address, isTest, ens)
|
2022-09-14 10:46:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2023-03-06 13:26:46 +00:00
|
|
|
nRows, err := res.RowsAffected()
|
2022-09-14 10:46:11 +00:00
|
|
|
if err != nil {
|
2023-03-06 13:26:46 +00:00
|
|
|
return false, err
|
2022-09-14 10:46:11 +00:00
|
|
|
}
|
2023-03-06 13:26:46 +00:00
|
|
|
|
|
|
|
return nRows > 0, nil
|
2022-09-14 10:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sam *SavedAddressesManager) DeleteSoftRemovedSavedAddresses(threshold uint64) error {
|
|
|
|
_, err := sam.db.Exec(`DELETE FROM saved_addresses WHERE removed = 1 AND update_clock < ?`, threshold)
|
2021-09-10 18:08:22 +00:00
|
|
|
return err
|
|
|
|
}
|