feat(keycard): `RemoveMigratedAccountsForKeycard` endpoint added

This commit is contained in:
Sale Djenic 2023-01-06 10:57:22 +01:00 committed by saledjenic
parent d95b259777
commit d7caf67a52
3 changed files with 51 additions and 1 deletions

View File

@ -201,7 +201,7 @@ func TestKeypairs(t *testing.T) {
KeycardUID: "00000000000000000000000000000001",
KeycardName: "Card01",
KeycardLocked: false,
AccountsAddresses: []types.Address{{0x01}, {0x02}, {0x03}},
AccountsAddresses: []types.Address{{0x01}, {0x02}, {0x03}, {0x04}},
KeyUID: "0000000000000000000000000000000000000000000000000000000000000001",
}
keyPair2 := keypairs.KeyPair{
@ -302,6 +302,17 @@ func TestKeypairs(t *testing.T) {
}
require.Equal(t, true, locked)
// Test detleting accounts (addresses) for a certain keycard
const numOfAccountsToRemove = 2
require.Greater(t, len(keyPair1.AccountsAddresses), numOfAccountsToRemove)
accountsToRemove := keyPair1.AccountsAddresses[:numOfAccountsToRemove]
err = db.RemoveMigratedAccountsForKeycard(keyPair1.KeycardUID, accountsToRemove)
require.NoError(t, err)
rows, err = db.GetMigratedKeyPairByKeyUID(keyPair1.KeyUID)
require.NoError(t, err)
require.Equal(t, 1, len(rows))
require.Equal(t, len(keyPair1.AccountsAddresses)-numOfAccountsToRemove, len(rows[0].AccountsAddresses))
// Test update keycard uid
err = db.UpdateKeycardUID(keyPair1.KeycardUID, keycardUID)
require.NoError(t, err)

View File

@ -3,6 +3,7 @@ package keypairs
import (
"database/sql"
"fmt"
"strings"
"github.com/status-im/status-go/eth-node/types"
)
@ -170,6 +171,35 @@ func (kp *KeyPairs) AddMigratedKeyPair(kcUID string, kpName string, KeyUID strin
return nil
}
func (kp *KeyPairs) RemoveMigratedAccountsForKeycard(kcUID string, accountAddresses []types.Address) (err error) {
inVector := strings.Repeat(",?", len(accountAddresses)-1)
query := `
DELETE
FROM
keypairs
WHERE
keycard_uid = ?
AND
account_address IN (?` + inVector + `)
`
delete, err := kp.db.Prepare(query)
if err != nil {
return err
}
args := make([]interface{}, len(accountAddresses)+1)
args[0] = kcUID
for i, addr := range accountAddresses {
args[i+1] = addr
}
defer delete.Close()
_, err = delete.Exec(args...)
return err
}
func (kp *KeyPairs) SetKeycardName(kcUID string, kpName string) (err error) {
update, err := kp.db.Prepare(`
UPDATE

View File

@ -445,6 +445,15 @@ func (api *API) AddMigratedKeyPair(ctx context.Context, kcUID string, kpName str
return nil
}
func (api *API) RemoveMigratedAccountsForKeycard(ctx context.Context, kcUID string, accountAddresses []string) error {
var addresses []types.Address
for _, addr := range accountAddresses {
addresses = append(addresses, types.Address(common.HexToAddress(addr)))
}
return api.db.RemoveMigratedAccountsForKeycard(kcUID, addresses)
}
func (api *API) GetAllKnownKeycards(ctx context.Context) ([]*keypairs.KeyPair, error) {
return api.db.GetAllKnownKeycards()
}