chore: `UpdateKeycardUID` function exposed

This commit is contained in:
Sale Djenic 2022-09-20 12:45:50 +02:00 committed by saledjenic
parent e4ca8a256c
commit 698c32f3e3
3 changed files with 25 additions and 14 deletions

View File

@ -196,6 +196,7 @@ func TestKeypairs(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
keycardUID := "00000000000000000000000000000000"
keyPair1 := keypairs.KeyPair{
KeycardUID: "00000000000000000000000000000001",
KeycardName: "Card01",
@ -270,8 +271,12 @@ func TestKeypairs(t *testing.T) {
}
require.Equal(t, true, locked)
// Test update keycard uid
err = db.UpdateKeycardUID(keyPair1.KeycardUID, keycardUID)
require.NoError(t, err)
// Test unlocking a locked keycard
err = db.KeycardUnlocked(keyPair1.KeycardUID)
err = db.KeycardUnlocked(keycardUID)
require.NoError(t, err)
rows, err = db.GetAllMigratedKeyPairs()
require.NoError(t, err)
@ -284,7 +289,7 @@ func TestKeypairs(t *testing.T) {
require.Equal(t, false, locked)
// Test detleting a keycard
err = db.DeleteKeycard(keyPair1.KeycardUID)
err = db.DeleteKeycard(keycardUID)
require.NoError(t, err)
rows, err = db.GetAllMigratedKeyPairs()
require.NoError(t, err)

View File

@ -2,6 +2,7 @@ package keypairs
import (
"database/sql"
"fmt"
"github.com/status-im/status-go/eth-node/types"
)
@ -161,31 +162,32 @@ func (kp *KeyPairs) SetKeycardName(kcUID string, kpName string) (err error) {
return err
}
func (kp *KeyPairs) updateKeycardLocked(kcUID string, locked bool) (err error) {
update, err := kp.db.Prepare(`
UPDATE
keypairs
SET
keycard_locked = ?
WHERE
keycard_uid = ?
`)
func (kp *KeyPairs) execUpdateQuery(kcUID string, field string, value interface{}) (err error) {
var sql string
sql = fmt.Sprintf(`UPDATE keypairs SET %s = ? WHERE keycard_uid = ?`, field)
update, err := kp.db.Prepare(sql)
if err != nil {
return err
}
defer update.Close()
_, err = update.Exec(locked, kcUID)
_, err = update.Exec(value, kcUID)
return err
}
func (kp *KeyPairs) KeycardLocked(kcUID string) (err error) {
return kp.updateKeycardLocked(kcUID, true)
return kp.execUpdateQuery(kcUID, "keycard_locked", true)
}
func (kp *KeyPairs) KeycardUnlocked(kcUID string) (err error) {
return kp.updateKeycardLocked(kcUID, false)
return kp.execUpdateQuery(kcUID, "keycard_locked", false)
}
func (kp *KeyPairs) UpdateKeycardUID(oldKcUID string, newKcUID string) (err error) {
return kp.execUpdateQuery(oldKcUID, "keycard_uid", newKcUID)
}
func (kp *KeyPairs) DeleteKeycard(kcUID string) (err error) {

View File

@ -350,3 +350,7 @@ func (api *API) KeycardUnlocked(ctx context.Context, kcUID string) error {
func (api *API) DeleteKeycard(ctx context.Context, kcUID string) error {
return api.db.DeleteKeycard(kcUID)
}
func (api *API) UpdateKeycardUID(ctx context.Context, oldKcUID string, newKcUID string) error {
return api.db.UpdateKeycardUID(oldKcUID, newKcUID)
}