2023-05-16 10:48:00 +00:00
|
|
|
package accounts
|
2022-09-06 07:10:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2023-02-21 09:50:48 +00:00
|
|
|
"errors"
|
2022-09-20 10:45:50 +00:00
|
|
|
"fmt"
|
2023-01-06 09:57:22 +00:00
|
|
|
"strings"
|
2022-09-06 07:10:40 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2023-02-21 12:35:26 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2022-09-06 07:10:40 +00:00
|
|
|
)
|
|
|
|
|
2023-05-16 10:48:00 +00:00
|
|
|
var errKeycardDbTransactionIsNil = errors.New("keycard: database transaction is nil")
|
2023-02-21 09:50:48 +00:00
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
type Keycard struct {
|
2022-09-06 07:10:40 +00:00
|
|
|
KeycardUID string `json:"keycard-uid"`
|
|
|
|
KeycardName string `json:"keycard-name"`
|
|
|
|
KeycardLocked bool `json:"keycard-locked"`
|
|
|
|
AccountsAddresses []types.Address `json:"accounts-addresses"`
|
|
|
|
KeyUID string `json:"key-uid"`
|
2023-02-21 09:50:48 +00:00
|
|
|
LastUpdateClock uint64
|
2022-09-06 07:10:40 +00:00
|
|
|
}
|
|
|
|
|
2023-02-21 12:35:26 +00:00
|
|
|
type KeycardAction struct {
|
|
|
|
Action string `json:"action"`
|
|
|
|
OldKeycardUID string `json:"old-keycard-uid,omitempty"`
|
2023-05-09 18:48:56 +00:00
|
|
|
Keycard *Keycard `json:"keycard"`
|
2023-02-21 12:35:26 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycard) ToSyncKeycard() *protobuf.SyncKeycard {
|
2023-02-21 12:35:26 +00:00
|
|
|
kc := &protobuf.SyncKeycard{
|
|
|
|
Uid: kp.KeycardUID,
|
|
|
|
Name: kp.KeycardName,
|
|
|
|
Locked: kp.KeycardLocked,
|
|
|
|
KeyUid: kp.KeyUID,
|
|
|
|
Clock: kp.LastUpdateClock,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range kp.AccountsAddresses {
|
|
|
|
kc.Addresses = append(kc.Addresses, addr.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
return kc
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycard) FromSyncKeycard(kc *protobuf.SyncKeycard) {
|
2023-02-21 12:35:26 +00:00
|
|
|
kp.KeycardUID = kc.Uid
|
|
|
|
kp.KeycardName = kc.Name
|
|
|
|
kp.KeycardLocked = kc.Locked
|
|
|
|
kp.KeyUID = kc.KeyUid
|
|
|
|
kp.LastUpdateClock = kc.Clock
|
|
|
|
|
|
|
|
for _, addr := range kc.Addresses {
|
|
|
|
kp.AccountsAddresses = append(kp.AccountsAddresses, types.BytesToAddress(addr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeElementAtIndex[T any](s []T, index int) []T {
|
|
|
|
if index < 0 || index >= len(s) {
|
|
|
|
panic("keycard: index out of the range")
|
|
|
|
}
|
|
|
|
return append(s[:index], s[index+1:]...)
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
type Keycards struct {
|
2022-09-06 07:10:40 +00:00
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func NewKeycards(db *sql.DB) *Keycards {
|
|
|
|
return &Keycards{
|
2022-09-06 07:10:40 +00:00
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 17:14:19 +00:00
|
|
|
func containsAddress(addresses []types.Address, address types.Address) bool {
|
|
|
|
for _, addr := range addresses {
|
|
|
|
if addr == address {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycards) processResult(rows *sql.Rows, groupByKeycard bool) ([]*Keycard, error) {
|
|
|
|
keycards := []*Keycard{}
|
2022-09-06 07:10:40 +00:00
|
|
|
for rows.Next() {
|
2023-05-09 18:48:56 +00:00
|
|
|
keycard := &Keycard{}
|
2022-09-06 07:10:40 +00:00
|
|
|
addr := types.Address{}
|
2023-05-09 18:48:56 +00:00
|
|
|
err := rows.Scan(&keycard.KeycardUID, &keycard.KeycardName, &keycard.KeycardLocked, &addr, &keycard.KeyUID,
|
|
|
|
&keycard.LastUpdateClock)
|
2022-09-06 07:10:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
foundAtIndex := -1
|
2023-05-09 18:48:56 +00:00
|
|
|
for i := range keycards {
|
2022-12-07 17:14:19 +00:00
|
|
|
if groupByKeycard {
|
2023-05-09 18:48:56 +00:00
|
|
|
if keycards[i].KeycardUID == keycard.KeycardUID {
|
2022-12-07 17:14:19 +00:00
|
|
|
foundAtIndex = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else {
|
2023-05-09 18:48:56 +00:00
|
|
|
if keycards[i].KeyUID == keycard.KeyUID {
|
2022-12-07 17:14:19 +00:00
|
|
|
foundAtIndex = i
|
|
|
|
break
|
|
|
|
}
|
2022-09-06 07:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if foundAtIndex == -1 {
|
2023-05-09 18:48:56 +00:00
|
|
|
keycard.AccountsAddresses = append(keycard.AccountsAddresses, addr)
|
|
|
|
keycards = append(keycards, keycard)
|
2022-09-06 07:10:40 +00:00
|
|
|
} else {
|
2023-05-09 18:48:56 +00:00
|
|
|
if containsAddress(keycards[foundAtIndex].AccountsAddresses, addr) {
|
2022-12-07 17:14:19 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-05-09 18:48:56 +00:00
|
|
|
keycards[foundAtIndex].AccountsAddresses = append(keycards[foundAtIndex].AccountsAddresses, addr)
|
2022-09-06 07:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
return keycards, nil
|
2022-09-06 07:10:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycards) getAllRows(groupByKeycard bool) ([]*Keycard, error) {
|
2022-09-06 07:10:40 +00:00
|
|
|
rows, err := kp.db.Query(`
|
2023-01-13 17:12:46 +00:00
|
|
|
SELECT
|
2023-02-14 13:06:59 +00:00
|
|
|
k.keycard_uid,
|
|
|
|
k.keycard_name,
|
|
|
|
k.keycard_locked,
|
|
|
|
ka.account_address,
|
2023-02-21 09:50:48 +00:00
|
|
|
k.key_uid,
|
|
|
|
k.last_update_clock
|
2023-01-13 17:12:46 +00:00
|
|
|
FROM
|
2023-02-14 13:06:59 +00:00
|
|
|
keycards AS k
|
|
|
|
LEFT JOIN
|
|
|
|
keycards_accounts AS ka
|
|
|
|
ON
|
|
|
|
k.keycard_uid = ka.keycard_uid
|
2023-01-13 17:12:46 +00:00
|
|
|
ORDER BY
|
2022-09-06 07:10:40 +00:00
|
|
|
key_uid
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rows.Close()
|
2022-12-07 17:14:19 +00:00
|
|
|
return kp.processResult(rows, groupByKeycard)
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycards) GetAllKnownKeycards() ([]*Keycard, error) {
|
2022-12-07 17:14:19 +00:00
|
|
|
return kp.getAllRows(true)
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycards) GetAllKnownKeycardsGroupedByKeyUID() ([]*Keycard, error) {
|
2022-12-07 17:14:19 +00:00
|
|
|
return kp.getAllRows(false)
|
2022-09-06 07:10:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycards) GetKeycardByKeyUID(keyUID string) ([]*Keycard, error) {
|
2022-09-06 07:10:40 +00:00
|
|
|
rows, err := kp.db.Query(`
|
2023-02-14 13:06:59 +00:00
|
|
|
SELECT
|
|
|
|
k.keycard_uid,
|
|
|
|
k.keycard_name,
|
|
|
|
k.keycard_locked,
|
|
|
|
ka.account_address,
|
2023-02-21 09:50:48 +00:00
|
|
|
k.key_uid,
|
|
|
|
k.last_update_clock
|
2023-01-13 17:12:46 +00:00
|
|
|
FROM
|
2023-02-14 13:06:59 +00:00
|
|
|
keycards AS k
|
|
|
|
LEFT JOIN
|
|
|
|
keycards_accounts AS ka
|
|
|
|
ON
|
|
|
|
k.keycard_uid = ka.keycard_uid
|
2022-09-06 07:10:40 +00:00
|
|
|
WHERE
|
2023-02-14 13:06:59 +00:00
|
|
|
k.key_uid = ?
|
2023-01-13 17:12:46 +00:00
|
|
|
ORDER BY
|
2023-02-14 13:06:59 +00:00
|
|
|
k.keycard_uid
|
2022-09-06 07:10:40 +00:00
|
|
|
`, keyUID)
|
|
|
|
if err != nil {
|
2023-05-16 10:48:00 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return []*Keycard{}, nil
|
|
|
|
}
|
2022-09-06 07:10:40 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rows.Close()
|
2022-12-07 17:14:19 +00:00
|
|
|
return kp.processResult(rows, false)
|
2022-09-06 07:10:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycards) startTransactionAndCheckIfNeedToProceed(kcUID string, clock uint64) (tx *sql.Tx, proceed bool, err error) {
|
2022-09-06 07:10:40 +00:00
|
|
|
tx, err = kp.db.Begin()
|
|
|
|
if err != nil {
|
2023-02-21 09:50:48 +00:00
|
|
|
return nil, false, err
|
2022-09-06 07:10:40 +00:00
|
|
|
}
|
2023-02-21 09:50:48 +00:00
|
|
|
var dbLastUpdateClock uint64
|
|
|
|
err = tx.QueryRow(`SELECT last_update_clock FROM keycards WHERE keycard_uid = ?`, kcUID).Scan(&dbLastUpdateClock)
|
2023-02-14 13:06:59 +00:00
|
|
|
if err != nil {
|
2023-02-21 12:35:26 +00:00
|
|
|
return tx, err == sql.ErrNoRows, err
|
2023-02-21 09:50:48 +00:00
|
|
|
}
|
2023-02-14 13:06:59 +00:00
|
|
|
|
2023-02-21 09:50:48 +00:00
|
|
|
return tx, dbLastUpdateClock <= clock, nil
|
|
|
|
}
|
2023-02-14 13:06:59 +00:00
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycards) setLastUpdateClock(tx *sql.Tx, kcUID string, clock uint64) (err error) {
|
2023-02-21 09:50:48 +00:00
|
|
|
if tx == nil {
|
2023-05-16 10:48:00 +00:00
|
|
|
return errKeycardDbTransactionIsNil
|
2023-02-21 09:50:48 +00:00
|
|
|
}
|
2023-02-14 13:06:59 +00:00
|
|
|
|
2023-02-21 09:50:48 +00:00
|
|
|
_, err = tx.Exec(`
|
|
|
|
UPDATE
|
|
|
|
keycards
|
|
|
|
SET
|
|
|
|
last_update_clock = ?
|
|
|
|
WHERE
|
|
|
|
keycard_uid = ?`,
|
|
|
|
clock, kcUID)
|
2023-02-14 13:06:59 +00:00
|
|
|
|
2023-02-21 09:50:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycards) getAccountsForKeycard(tx *sql.Tx, kcUID string) ([]types.Address, error) {
|
2023-02-21 09:50:48 +00:00
|
|
|
var accountAddresses []types.Address
|
|
|
|
if tx == nil {
|
2023-05-16 10:48:00 +00:00
|
|
|
return accountAddresses, errKeycardDbTransactionIsNil
|
2023-02-21 09:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := tx.Query(`SELECT account_address FROM keycards_accounts WHERE keycard_uid = ?`, kcUID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var accAddress types.Address
|
|
|
|
err = rows.Scan(&accAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-02-14 13:06:59 +00:00
|
|
|
}
|
2023-02-21 09:50:48 +00:00
|
|
|
accountAddresses = append(accountAddresses, accAddress)
|
2023-02-14 13:06:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-21 09:50:48 +00:00
|
|
|
return accountAddresses, nil
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycards) addAccounts(tx *sql.Tx, kcUID string, accountsAddresses []types.Address) (err error) {
|
2023-02-21 09:50:48 +00:00
|
|
|
if tx == nil {
|
2023-05-16 10:48:00 +00:00
|
|
|
return errKeycardDbTransactionIsNil
|
2023-02-21 09:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
insertKcAcc, err := tx.Prepare(`
|
2023-01-13 17:12:46 +00:00
|
|
|
INSERT INTO
|
2023-02-14 13:06:59 +00:00
|
|
|
keycards_accounts
|
2022-09-06 07:10:40 +00:00
|
|
|
(
|
2023-01-13 17:12:46 +00:00
|
|
|
keycard_uid,
|
2023-02-14 13:06:59 +00:00
|
|
|
account_address
|
2023-01-13 17:12:46 +00:00
|
|
|
)
|
2022-09-06 07:10:40 +00:00
|
|
|
VALUES
|
2023-02-14 13:06:59 +00:00
|
|
|
(?, ?);
|
2022-09-06 07:10:40 +00:00
|
|
|
`)
|
2023-02-14 13:06:59 +00:00
|
|
|
|
2022-09-06 07:10:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-14 13:06:59 +00:00
|
|
|
defer insertKcAcc.Close()
|
2022-09-06 07:10:40 +00:00
|
|
|
|
2023-02-21 09:50:48 +00:00
|
|
|
for i := range accountsAddresses {
|
|
|
|
addr := accountsAddresses[i]
|
2022-09-06 07:10:40 +00:00
|
|
|
|
2023-02-21 09:50:48 +00:00
|
|
|
_, err = insertKcAcc.Exec(kcUID, addr)
|
2022-09-06 07:10:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-02-21 09:50:48 +00:00
|
|
|
|
2022-09-06 07:10:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycards) deleteKeycard(tx *sql.Tx, kcUID string) (err error) {
|
2023-02-21 09:50:48 +00:00
|
|
|
if tx == nil {
|
2023-05-16 10:48:00 +00:00
|
|
|
return errKeycardDbTransactionIsNil
|
2023-02-21 09:50:48 +00:00
|
|
|
}
|
2023-02-14 13:06:59 +00:00
|
|
|
|
2023-02-21 09:50:48 +00:00
|
|
|
delete, err := tx.Prepare(`
|
|
|
|
DELETE
|
|
|
|
FROM
|
|
|
|
keycards
|
|
|
|
WHERE
|
|
|
|
keycard_uid = ?
|
|
|
|
`)
|
2023-02-14 13:06:59 +00:00
|
|
|
if err != nil {
|
2023-02-21 09:50:48 +00:00
|
|
|
return err
|
2023-02-14 13:06:59 +00:00
|
|
|
}
|
2023-02-21 09:50:48 +00:00
|
|
|
defer delete.Close()
|
2023-02-14 13:06:59 +00:00
|
|
|
|
2023-02-21 09:50:48 +00:00
|
|
|
_, err = delete.Exec(kcUID)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycards) AddKeycardOrAddAccountsIfKeycardIsAdded(keycard Keycard) (addedKc bool, addedAccs bool, err error) {
|
|
|
|
tx, proceed, err := kp.startTransactionAndCheckIfNeedToProceed(keycard.KeycardUID, keycard.LastUpdateClock)
|
2023-02-21 09:50:48 +00:00
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
|
2023-02-21 12:35:26 +00:00
|
|
|
if proceed {
|
|
|
|
// insert only if there is no such keycard, otherwise just add accounts
|
|
|
|
if err != nil && err == sql.ErrNoRows {
|
2023-02-21 09:50:48 +00:00
|
|
|
_, err = tx.Exec(`
|
|
|
|
INSERT INTO
|
|
|
|
keycards
|
|
|
|
(
|
|
|
|
keycard_uid,
|
|
|
|
keycard_name,
|
|
|
|
keycard_locked,
|
|
|
|
key_uid,
|
|
|
|
last_update_clock
|
|
|
|
)
|
|
|
|
VALUES
|
|
|
|
(?, ?, ?, ?, ?);`,
|
2023-05-09 18:48:56 +00:00
|
|
|
keycard.KeycardUID, keycard.KeycardName, keycard.KeycardLocked, keycard.KeyUID, keycard.LastUpdateClock)
|
2023-02-21 09:50:48 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2023-02-21 12:35:26 +00:00
|
|
|
return false, false, err
|
2023-02-21 09:50:48 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
err = kp.addAccounts(tx, keycard.KeycardUID, keycard.AccountsAddresses)
|
2023-02-21 12:35:26 +00:00
|
|
|
return err == nil, false, err
|
2023-02-21 09:50:48 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
err = kp.setLastUpdateClock(tx, keycard.KeycardUID, keycard.LastUpdateClock)
|
2023-02-14 13:06:59 +00:00
|
|
|
if err != nil {
|
2023-02-21 12:35:26 +00:00
|
|
|
return false, false, err
|
2023-02-14 13:06:59 +00:00
|
|
|
}
|
2023-02-21 09:50:48 +00:00
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
err = kp.addAccounts(tx, keycard.KeycardUID, keycard.AccountsAddresses)
|
2023-02-21 12:35:26 +00:00
|
|
|
return false, err == nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, false, err
|
|
|
|
}
|
|
|
|
|
2023-05-16 10:48:00 +00:00
|
|
|
func (kp *Keycards) ApplyKeycardsForKeypairWithKeyUID(keyUID string, keycardsToSync []*Keycard) (err error) {
|
2023-02-21 12:35:26 +00:00
|
|
|
tx, err := kp.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
|
2023-05-16 10:48:00 +00:00
|
|
|
rows, err := tx.Query(`SELECT * FROM keycards WHERE key_uid = ?`, keyUID)
|
2023-02-21 12:35:26 +00:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
var dbKeycards []*Keycard
|
2023-02-21 12:35:26 +00:00
|
|
|
for rows.Next() {
|
2023-05-09 18:48:56 +00:00
|
|
|
keycard := &Keycard{}
|
|
|
|
err := rows.Scan(&keycard.KeycardUID, &keycard.KeycardName, &keycard.KeycardLocked, &keycard.KeyUID,
|
|
|
|
&keycard.LastUpdateClock)
|
2023-02-21 12:35:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
dbKeycards = append(dbKeycards, keycard)
|
2023-02-14 13:06:59 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
// apply those from `keycardsToSync` which are newer
|
2023-05-16 10:48:00 +00:00
|
|
|
for _, syncKc := range keycardsToSync {
|
2023-02-21 12:35:26 +00:00
|
|
|
foundAtIndex := -1
|
2023-05-09 18:48:56 +00:00
|
|
|
for i := range dbKeycards {
|
2023-05-16 10:48:00 +00:00
|
|
|
if dbKeycards[i].KeycardUID == syncKc.KeycardUID {
|
2023-02-21 12:35:26 +00:00
|
|
|
foundAtIndex = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if foundAtIndex > -1 {
|
2023-05-16 10:48:00 +00:00
|
|
|
dbClock := dbKeycards[foundAtIndex].LastUpdateClock
|
2023-05-09 18:48:56 +00:00
|
|
|
dbKeycards = removeElementAtIndex(dbKeycards, foundAtIndex)
|
2023-02-21 12:35:26 +00:00
|
|
|
|
2023-05-16 10:48:00 +00:00
|
|
|
if dbClock > syncKc.LastUpdateClock {
|
|
|
|
continue
|
2023-02-21 12:35:26 +00:00
|
|
|
}
|
2023-05-16 10:48:00 +00:00
|
|
|
err = kp.deleteKeycard(tx, syncKc.KeycardUID)
|
2023-02-21 12:35:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-16 10:48:00 +00:00
|
|
|
_, err = tx.Exec(`
|
|
|
|
INSERT OR REPLACE INTO
|
|
|
|
keycards
|
|
|
|
(
|
|
|
|
keycard_uid,
|
|
|
|
keycard_name,
|
|
|
|
keycard_locked,
|
|
|
|
key_uid,
|
|
|
|
last_update_clock
|
|
|
|
)
|
|
|
|
VALUES
|
|
|
|
(?, ?, ?, ?, ?);`,
|
|
|
|
syncKc.KeycardUID, syncKc.KeycardName, syncKc.KeycardLocked, syncKc.KeyUID, syncKc.LastUpdateClock)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-02-21 12:35:26 +00:00
|
|
|
}
|
|
|
|
|
2023-05-16 10:48:00 +00:00
|
|
|
err = kp.addAccounts(tx, syncKc.KeycardUID, syncKc.AccountsAddresses)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove those from the db if they are not in `keycardsToSync`
|
|
|
|
for _, dbKp := range dbKeycards {
|
2023-02-21 12:35:26 +00:00
|
|
|
err = kp.deleteKeycard(tx, dbKp.KeycardUID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-02-14 13:06:59 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycards) RemoveMigratedAccountsForKeycard(kcUID string, accountAddresses []types.Address,
|
2023-02-21 12:35:26 +00:00
|
|
|
clock uint64) (err error) {
|
2023-02-21 09:50:48 +00:00
|
|
|
tx, proceed, err := kp.startTransactionAndCheckIfNeedToProceed(kcUID, clock)
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
|
2023-02-14 13:06:59 +00:00
|
|
|
if err != nil {
|
2023-02-21 12:35:26 +00:00
|
|
|
return err
|
2023-02-14 13:06:59 +00:00
|
|
|
}
|
2023-02-21 09:50:48 +00:00
|
|
|
|
|
|
|
if proceed {
|
|
|
|
err = kp.setLastUpdateClock(tx, kcUID, clock)
|
|
|
|
if err != nil {
|
2023-02-21 12:35:26 +00:00
|
|
|
return err
|
2023-02-21 09:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dbAccountAddresses, err := kp.getAccountsForKeycard(tx, kcUID)
|
|
|
|
if err != nil {
|
2023-02-21 12:35:26 +00:00
|
|
|
return err
|
2023-02-21 09:50:48 +00:00
|
|
|
}
|
|
|
|
deleteKeycard := true
|
|
|
|
for _, dbAddr := range dbAccountAddresses {
|
|
|
|
found := false
|
|
|
|
for _, addr := range accountAddresses {
|
|
|
|
if dbAddr == addr {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
deleteKeycard = false
|
2023-02-14 13:06:59 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-21 09:50:48 +00:00
|
|
|
|
|
|
|
if deleteKeycard {
|
2023-02-21 12:35:26 +00:00
|
|
|
return kp.deleteKeycard(tx, kcUID)
|
2023-02-14 13:06:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-21 09:50:48 +00:00
|
|
|
inVector := strings.Repeat(",?", len(accountAddresses)-1)
|
|
|
|
query := `
|
|
|
|
DELETE
|
|
|
|
FROM
|
|
|
|
keycards_accounts
|
|
|
|
WHERE
|
|
|
|
keycard_uid = ?
|
|
|
|
AND
|
|
|
|
account_address IN (?` + inVector + `)
|
|
|
|
`
|
|
|
|
delete, err := tx.Prepare(query)
|
|
|
|
if err != nil {
|
2023-02-21 12:35:26 +00:00
|
|
|
return err
|
2023-02-21 09:50:48 +00:00
|
|
|
}
|
2023-02-14 13:06:59 +00:00
|
|
|
|
2023-02-21 09:50:48 +00:00
|
|
|
args := make([]interface{}, len(accountAddresses)+1)
|
|
|
|
args[0] = kcUID
|
|
|
|
for i, addr := range accountAddresses {
|
|
|
|
args[i+1] = addr
|
|
|
|
}
|
2023-01-06 09:57:22 +00:00
|
|
|
|
2023-02-21 09:50:48 +00:00
|
|
|
defer delete.Close()
|
2023-01-06 09:57:22 +00:00
|
|
|
|
2023-02-21 09:50:48 +00:00
|
|
|
_, err = delete.Exec(args...)
|
2023-01-06 09:57:22 +00:00
|
|
|
|
2023-02-21 12:35:26 +00:00
|
|
|
return err
|
2023-02-21 09:50:48 +00:00
|
|
|
}
|
2023-01-06 09:57:22 +00:00
|
|
|
|
2023-02-21 12:35:26 +00:00
|
|
|
return err
|
2023-01-06 09:57:22 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycards) execUpdateQuery(kcUID string, clock uint64, field string, value interface{}) (err error) {
|
2023-02-21 09:50:48 +00:00
|
|
|
tx, proceed, err := kp.startTransactionAndCheckIfNeedToProceed(kcUID, clock)
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
2022-09-20 10:45:50 +00:00
|
|
|
|
2022-09-06 07:10:40 +00:00
|
|
|
if err != nil {
|
2023-02-21 12:35:26 +00:00
|
|
|
return err
|
2022-09-06 07:10:40 +00:00
|
|
|
}
|
|
|
|
|
2023-02-21 09:50:48 +00:00
|
|
|
if proceed {
|
|
|
|
sql := fmt.Sprintf(`UPDATE keycards SET %s = ?, last_update_clock = ? WHERE keycard_uid = ?`, field) // nolint: gosec
|
|
|
|
_, err = tx.Exec(sql, value, clock, kcUID)
|
2023-02-21 12:35:26 +00:00
|
|
|
return err
|
2023-02-21 09:50:48 +00:00
|
|
|
}
|
2022-09-06 07:10:40 +00:00
|
|
|
|
2023-02-21 12:35:26 +00:00
|
|
|
return nil
|
2022-09-06 07:10:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycards) KeycardLocked(kcUID string, clock uint64) (err error) {
|
2023-02-21 09:50:48 +00:00
|
|
|
return kp.execUpdateQuery(kcUID, clock, "keycard_locked", true)
|
2022-09-06 07:10:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycards) KeycardUnlocked(kcUID string, clock uint64) (err error) {
|
2023-02-21 09:50:48 +00:00
|
|
|
return kp.execUpdateQuery(kcUID, clock, "keycard_locked", false)
|
2022-09-20 10:45:50 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycards) UpdateKeycardUID(oldKcUID string, newKcUID string, clock uint64) (err error) {
|
2023-02-21 09:50:48 +00:00
|
|
|
return kp.execUpdateQuery(oldKcUID, clock, "keycard_uid", newKcUID)
|
2022-09-06 07:10:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycards) SetKeycardName(kcUID string, kpName string, clock uint64) (err error) {
|
2023-02-21 09:50:48 +00:00
|
|
|
return kp.execUpdateQuery(kcUID, clock, "keycard_name", kpName)
|
2023-02-14 13:06:59 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycards) DeleteKeycard(kcUID string, clock uint64) (err error) {
|
2023-02-21 09:50:48 +00:00
|
|
|
tx, proceed, err := kp.startTransactionAndCheckIfNeedToProceed(kcUID, clock)
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
|
2022-09-06 07:10:40 +00:00
|
|
|
if err != nil {
|
2023-02-21 12:35:26 +00:00
|
|
|
return err
|
2022-09-06 07:10:40 +00:00
|
|
|
}
|
|
|
|
|
2023-02-21 09:50:48 +00:00
|
|
|
if proceed {
|
2023-02-21 12:35:26 +00:00
|
|
|
return kp.deleteKeycard(tx, kcUID)
|
2023-02-21 09:50:48 +00:00
|
|
|
}
|
2022-09-06 07:10:40 +00:00
|
|
|
|
2023-02-21 12:35:26 +00:00
|
|
|
return err
|
2022-09-06 07:10:40 +00:00
|
|
|
}
|
2023-01-25 10:18:28 +00:00
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func (kp *Keycards) DeleteAllKeycardsWithKeyUID(keyUID string) (err error) {
|
2023-01-25 10:18:28 +00:00
|
|
|
delete, err := kp.db.Prepare(`
|
|
|
|
DELETE
|
|
|
|
FROM
|
2023-02-14 13:06:59 +00:00
|
|
|
keycards
|
2023-01-25 10:18:28 +00:00
|
|
|
WHERE
|
|
|
|
key_uid = ?
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer delete.Close()
|
|
|
|
_, err = delete.Exec(keyUID)
|
|
|
|
return err
|
|
|
|
}
|