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-07-05 12:41:58 +00:00
|
|
|
var (
|
|
|
|
errKeycardDbTransactionIsNil = errors.New("keycard: database transaction is nil")
|
|
|
|
errCannotAddKeycardForUnknownKeypair = errors.New("keycard: cannot add keycard for an unknown keyapir")
|
|
|
|
ErrNoKeycardForPassedKeycardUID = errors.New("keycard: no keycard for the passed keycard uid")
|
|
|
|
)
|
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-07-05 12:41:58 +00:00
|
|
|
Position uint64
|
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{
|
2023-07-05 12:41:58 +00:00
|
|
|
Uid: kp.KeycardUID,
|
|
|
|
Name: kp.KeycardName,
|
|
|
|
Locked: kp.KeycardLocked,
|
|
|
|
KeyUid: kp.KeyUID,
|
|
|
|
Position: kp.Position,
|
2023-02-21 12:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2023-07-05 12:41:58 +00:00
|
|
|
kp.Position = kc.Position
|
2023-02-21 12:35:26 +00:00
|
|
|
|
|
|
|
for _, addr := range kc.Addresses {
|
|
|
|
kp.AccountsAddresses = append(kp.AccountsAddresses, types.BytesToAddress(addr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-05 12:41:58 +00:00
|
|
|
func (db *Database) processResult(rows *sql.Rows) ([]*Keycard, error) {
|
2023-05-09 18:48:56 +00:00
|
|
|
keycards := []*Keycard{}
|
2022-09-06 07:10:40 +00:00
|
|
|
for rows.Next() {
|
2023-05-09 18:48:56 +00:00
|
|
|
keycard := &Keycard{}
|
2023-07-05 12:41:58 +00:00
|
|
|
var accAddress sql.NullString
|
|
|
|
err := rows.Scan(&keycard.KeycardUID, &keycard.KeycardName, &keycard.KeycardLocked, &accAddress, &keycard.KeyUID,
|
|
|
|
&keycard.Position)
|
2022-09-06 07:10:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
addr := types.Address{}
|
|
|
|
if accAddress.Valid {
|
|
|
|
addr = types.BytesToAddress([]byte(accAddress.String))
|
|
|
|
}
|
|
|
|
|
2022-09-06 07:10:40 +00:00
|
|
|
foundAtIndex := -1
|
2023-05-09 18:48:56 +00:00
|
|
|
for i := range keycards {
|
2023-07-05 12:41:58 +00:00
|
|
|
if keycards[i].KeycardUID == keycard.KeycardUID {
|
|
|
|
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-07-05 12:41:58 +00:00
|
|
|
func (db *Database) getKeycards(tx *sql.Tx, keyUID string, keycardUID string) ([]*Keycard, error) {
|
|
|
|
query := `
|
2023-01-13 17:12:46 +00:00
|
|
|
SELECT
|
2023-07-05 12:41:58 +00:00
|
|
|
kc.keycard_uid,
|
|
|
|
kc.keycard_name,
|
|
|
|
kc.keycard_locked,
|
2023-02-14 13:06:59 +00:00
|
|
|
ka.account_address,
|
2023-07-05 12:41:58 +00:00
|
|
|
kc.key_uid,
|
|
|
|
kc.position
|
2023-01-13 17:12:46 +00:00
|
|
|
FROM
|
2023-07-05 12:41:58 +00:00
|
|
|
keycards AS kc
|
2023-06-28 19:45:36 +00:00
|
|
|
LEFT JOIN
|
|
|
|
keycards_accounts AS ka
|
|
|
|
ON
|
2023-07-05 12:41:58 +00:00
|
|
|
kc.keycard_uid = ka.keycard_uid
|
|
|
|
LEFT JOIN
|
|
|
|
keypairs_accounts AS kpa
|
|
|
|
ON
|
|
|
|
ka.account_address = kpa.address
|
|
|
|
%s
|
2023-01-13 17:12:46 +00:00
|
|
|
ORDER BY
|
2023-07-05 12:41:58 +00:00
|
|
|
kc.position, kpa.position`
|
2023-06-28 19:45:36 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
var where string
|
|
|
|
var args []interface{}
|
2023-06-28 19:45:36 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
if keyUID != "" {
|
|
|
|
where = "WHERE kc.key_uid = ?"
|
|
|
|
args = append(args, keyUID)
|
|
|
|
if keycardUID != "" {
|
|
|
|
where += " AND kc.keycard_uid = ?"
|
|
|
|
args = append(args, keycardUID)
|
2023-06-28 19:45:36 +00:00
|
|
|
}
|
2023-07-05 12:41:58 +00:00
|
|
|
} else if keycardUID != "" {
|
|
|
|
where = "WHERE kc.keycard_uid = ?"
|
|
|
|
args = append(args, keycardUID)
|
2022-09-06 07:10:40 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
query = fmt.Sprintf(query, where)
|
2022-12-07 17:14:19 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
var (
|
|
|
|
stmt *sql.Stmt
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if tx == nil {
|
|
|
|
stmt, err = db.db.Prepare(query)
|
|
|
|
} else {
|
|
|
|
stmt, err = tx.Prepare(query)
|
|
|
|
}
|
2022-09-06 07:10:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-05 12:41:58 +00:00
|
|
|
defer stmt.Close()
|
2022-09-06 07:10:40 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
rows, err := stmt.Query(args...)
|
2022-09-06 07:10:40 +00:00
|
|
|
if err != nil {
|
2023-07-05 12:41:58 +00:00
|
|
|
return nil, err
|
2023-02-21 09:50:48 +00:00
|
|
|
}
|
2023-07-05 12:41:58 +00:00
|
|
|
defer rows.Close()
|
2023-02-14 13:06:59 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
return db.processResult(rows)
|
2023-02-21 09:50:48 +00:00
|
|
|
}
|
2023-02-14 13:06:59 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
func (db *Database) getKeycardByKeycardUID(tx *sql.Tx, keycardUID string) (*Keycard, error) {
|
|
|
|
keycards, err := db.getKeycards(tx, "", keycardUID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-02-21 09:50:48 +00:00
|
|
|
}
|
2023-02-14 13:06:59 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
if len(keycards) == 0 {
|
|
|
|
return nil, ErrNoKeycardForPassedKeycardUID
|
|
|
|
}
|
2023-02-14 13:06:59 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
return keycards[0], nil
|
2023-02-21 09:50:48 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
func (db *Database) GetAllKnownKeycards() ([]*Keycard, error) {
|
|
|
|
return db.getKeycards(nil, "", "")
|
|
|
|
}
|
2023-02-21 09:50:48 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
func (db *Database) GetKeycardsWithSameKeyUID(keyUID string) ([]*Keycard, error) {
|
|
|
|
return db.getKeycards(nil, keyUID, "")
|
|
|
|
}
|
2023-02-14 13:06:59 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
func (db *Database) GetKeycardByKeycardUID(keycardUID string) (*Keycard, error) {
|
|
|
|
return db.getKeycardByKeycardUID(nil, keycardUID)
|
2023-02-21 09:50:48 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
func (db *Database) saveOrUpdateKeycardAccounts(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
|
|
|
}
|
|
|
|
|
|
|
|
for i := range accountsAddresses {
|
|
|
|
addr := accountsAddresses[i]
|
2022-09-06 07:10:40 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
_, err = tx.Exec(`
|
|
|
|
INSERT OR IGNORE INTO
|
|
|
|
keycards_accounts
|
|
|
|
(
|
|
|
|
keycard_uid,
|
|
|
|
account_address
|
|
|
|
)
|
|
|
|
VALUES
|
|
|
|
(?, ?);
|
|
|
|
`, 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-07-05 12:41:58 +00:00
|
|
|
func (db *Database) 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-07-20 15:00:39 +00:00
|
|
|
func (db *Database) deleteAllKeycardsWithKeyUID(tx *sql.Tx, keyUID string) (err error) {
|
|
|
|
if tx == nil {
|
|
|
|
return errKeycardDbTransactionIsNil
|
|
|
|
}
|
|
|
|
|
|
|
|
delete, err := tx.Prepare(`
|
|
|
|
DELETE
|
|
|
|
FROM
|
|
|
|
keycards
|
|
|
|
WHERE
|
|
|
|
key_uid = ?
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer delete.Close()
|
|
|
|
|
|
|
|
_, err = delete.Exec(keyUID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
func (db *Database) deleteKeycardAccounts(tx *sql.Tx, kcUID string, accountAddresses []types.Address) (err error) {
|
|
|
|
if tx == nil {
|
|
|
|
return errKeycardDbTransactionIsNil
|
|
|
|
}
|
2023-02-21 09:50:48 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
inVector := strings.Repeat(",?", len(accountAddresses)-1)
|
|
|
|
query := `
|
|
|
|
DELETE
|
|
|
|
FROM
|
|
|
|
keycards_accounts
|
|
|
|
WHERE
|
|
|
|
keycard_uid = ?
|
|
|
|
AND
|
|
|
|
account_address IN (?` + inVector + `)`
|
2023-02-21 09:50:48 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
delete, err := tx.Prepare(query)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer delete.Close()
|
2023-02-21 09:50:48 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
args := make([]interface{}, len(accountAddresses)+1)
|
|
|
|
args[0] = kcUID
|
|
|
|
for i, addr := range accountAddresses {
|
|
|
|
args[i+1] = addr
|
2023-02-21 12:35:26 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
_, err = delete.Exec(args...)
|
|
|
|
|
|
|
|
return err
|
2023-02-21 12:35:26 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
func (db *Database) SaveOrUpdateKeycard(keycard Keycard, clock uint64, updateKeypairClock bool) error {
|
|
|
|
tx, err := db.db.Begin()
|
2023-02-21 12:35:26 +00:00
|
|
|
if err != nil {
|
2023-07-05 12:41:58 +00:00
|
|
|
return err
|
2023-02-21 12:35:26 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
relatedKeypairExists, err := db.keypairExists(tx, keycard.KeyUID)
|
|
|
|
if err != nil {
|
2023-02-21 12:35:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
if !relatedKeypairExists {
|
|
|
|
return errCannotAddKeycardForUnknownKeypair
|
2023-02-14 13:06:59 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
_, err = tx.Exec(`
|
|
|
|
INSERT OR IGNORE INTO
|
|
|
|
keycards
|
|
|
|
(
|
|
|
|
keycard_uid,
|
|
|
|
keycard_name,
|
|
|
|
key_uid
|
|
|
|
)
|
|
|
|
VALUES
|
|
|
|
(?, ?, ?);
|
2023-05-16 10:48:00 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
UPDATE
|
|
|
|
keycards
|
|
|
|
SET
|
|
|
|
keycard_name = ?,
|
|
|
|
keycard_locked = ?,
|
|
|
|
position = ?
|
|
|
|
WHERE
|
|
|
|
keycard_uid = ?;
|
|
|
|
`, keycard.KeycardUID, keycard.KeycardName, keycard.KeyUID,
|
|
|
|
keycard.KeycardName, keycard.KeycardLocked, keycard.Position, keycard.KeycardUID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-21 12:35:26 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
err = db.saveOrUpdateKeycardAccounts(tx, keycard.KeycardUID, keycard.AccountsAddresses)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-05-16 10:48:00 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
if updateKeypairClock {
|
|
|
|
return db.updateKeypairClock(tx, keycard.KeyUID, clock)
|
2023-02-21 12:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-02-14 13:06:59 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
func (db *Database) execKeycardUpdateQuery(kcUID string, clock uint64, field string, value interface{}) (err error) {
|
|
|
|
tx, err := db.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-21 09:50:48 +00:00
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
keycard, err := db.getKeycardByKeycardUID(tx, kcUID)
|
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
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
sql := fmt.Sprintf(`UPDATE keycards SET %s = ? WHERE keycard_uid = ?`, field) // nolint: gosec
|
|
|
|
_, err = tx.Exec(sql, value, kcUID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-21 09:50:48 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
return db.updateKeypairClock(tx, keycard.KeyUID, clock)
|
|
|
|
}
|
2023-02-14 13:06:59 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
func (db *Database) KeycardLocked(kcUID string, clock uint64) (err error) {
|
|
|
|
return db.execKeycardUpdateQuery(kcUID, clock, "keycard_locked", true)
|
|
|
|
}
|
2023-02-14 13:06:59 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
func (db *Database) KeycardUnlocked(kcUID string, clock uint64) (err error) {
|
|
|
|
return db.execKeycardUpdateQuery(kcUID, clock, "keycard_locked", false)
|
|
|
|
}
|
2023-01-06 09:57:22 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
func (db *Database) UpdateKeycardUID(oldKcUID string, newKcUID string, clock uint64) (err error) {
|
|
|
|
return db.execKeycardUpdateQuery(oldKcUID, clock, "keycard_uid", newKcUID)
|
|
|
|
}
|
2023-01-06 09:57:22 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
func (db *Database) SetKeycardName(kcUID string, kpName string, clock uint64) (err error) {
|
|
|
|
return db.execKeycardUpdateQuery(kcUID, clock, "keycard_name", kpName)
|
|
|
|
}
|
2023-01-06 09:57:22 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
func (db *Database) DeleteKeycardAccounts(kcUID string, accountAddresses []types.Address, clock uint64) (err error) {
|
|
|
|
tx, err := db.db.Begin()
|
|
|
|
if err != nil {
|
2023-02-21 12:35:26 +00:00
|
|
|
return err
|
2023-02-21 09:50:48 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
2022-09-20 10:45:50 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
keycard, err := db.getKeycardByKeycardUID(tx, kcUID)
|
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-07-05 12:41:58 +00:00
|
|
|
err = db.deleteKeycardAccounts(tx, kcUID, accountAddresses)
|
|
|
|
if err != nil {
|
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-07-05 12:41:58 +00:00
|
|
|
return db.updateKeypairClock(tx, keycard.KeyUID, clock)
|
2022-09-06 07:10:40 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
func (db *Database) DeleteKeycard(kcUID string, clock uint64) (err error) {
|
|
|
|
tx, err := db.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-21 09:50:48 +00:00
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
keycard, err := db.getKeycardByKeycardUID(tx, kcUID)
|
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-07-05 12:41:58 +00:00
|
|
|
err = db.deleteKeycard(tx, kcUID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-02-21 09:50:48 +00:00
|
|
|
}
|
2022-09-06 07:10:40 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
return db.updateKeypairClock(tx, keycard.KeyUID, clock)
|
2022-09-06 07:10:40 +00:00
|
|
|
}
|
2023-01-25 10:18:28 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
func (db *Database) DeleteAllKeycardsWithKeyUID(keyUID string, clock uint64) (err error) {
|
|
|
|
tx, err := db.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
|
2023-07-20 15:00:39 +00:00
|
|
|
err = db.deleteAllKeycardsWithKeyUID(tx, keyUID)
|
2023-07-05 12:41:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.updateKeypairClock(tx, keyUID, clock)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetPositionForNextNewKeycard() (uint64, error) {
|
|
|
|
var pos sql.NullInt64
|
|
|
|
err := db.db.QueryRow("SELECT MAX(position) FROM keycards").Scan(&pos)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if pos.Valid {
|
|
|
|
return uint64(pos.Int64) + 1, nil
|
|
|
|
}
|
|
|
|
return 0, nil
|
2023-01-25 10:18:28 +00:00
|
|
|
}
|