feat(keycard): `ConvertToRegularAccount` endpoint added

It converts a keycard user to a regular user, setting all necessary details
that logged in account looks like it has never been a keycard account.
This commit is contained in:
Sale Djenic 2023-01-25 11:25:07 +01:00 committed by saledjenic
parent 902d4b7501
commit 6e656448ba
3 changed files with 92 additions and 3 deletions

View File

@ -565,7 +565,7 @@ func (b *GethStatusBackend) ChangeDatabasePassword(keyUID string, password strin
}
func (b *GethStatusBackend) ConvertToKeycardAccount(keyStoreDir string, account multiaccounts.Account, s settings.Settings, password string, newPassword string) error {
err := b.multiaccountsDB.UpdateAccountKeycardPairing(account)
err := b.multiaccountsDB.UpdateAccountKeycardPairing(account.KeyUID, account.KeycardPairing)
if err != nil {
return err
}
@ -650,6 +650,87 @@ func (b *GethStatusBackend) ConvertToKeycardAccount(keyStoreDir string, account
return nil
}
func (b *GethStatusBackend) ConvertToRegularAccount(mnemonic string, currPassword string, newPassword string) error {
mnemonicNoExtraSpaces := strings.Join(strings.Fields(mnemonic), " ")
accountInfo, err := b.accountManager.AccountsGenerator().ImportMnemonic(mnemonicNoExtraSpaces, "")
if err != nil {
return err
}
kdfIterations, err := b.multiaccountsDB.GetAccountKDFIterationsNumber(accountInfo.KeyUID)
if err != nil {
return err
}
err = b.ensureAppDBOpened(multiaccounts.Account{KeyUID: accountInfo.KeyUID, KDFIterations: kdfIterations}, newPassword)
if err != nil {
return err
}
db, err := accounts.NewDB(b.appDB)
if err != nil {
return err
}
knownAccounts, err := db.GetAccounts()
if err != nil {
return err
}
// We add these two paths, cause others will be added via `StoreAccount` function call
const pathWalletRoot = "m/44'/60'/0'/0"
const pathEIP1581 = "m/43'/60'/1581'"
var paths []string
paths = append(paths, pathWalletRoot, pathEIP1581)
for _, acc := range knownAccounts {
if accountInfo.KeyUID == acc.KeyUID {
paths = append(paths, acc.Path)
}
}
_, err = b.accountManager.AccountsGenerator().StoreAccount(accountInfo.ID, currPassword)
if err != nil {
return err
}
_, err = b.accountManager.AccountsGenerator().StoreDerivedAccounts(accountInfo.ID, currPassword, paths)
if err != nil {
return err
}
err = b.multiaccountsDB.UpdateAccountKeycardPairing(accountInfo.KeyUID, "")
if err != nil {
return err
}
err = db.DeleteKeypair(accountInfo.KeyUID)
if err != nil {
return err
}
err = db.SaveSettingField(settings.KeycardInstanceUID, "")
if err != nil {
return err
}
err = db.SaveSettingField(settings.KeycardPairedOn, 0)
if err != nil {
return err
}
err = db.SaveSettingField(settings.KeycardPairing, "")
if err != nil {
return err
}
err = b.closeAppDB()
if err != nil {
return err
}
return b.ChangeDatabasePassword(accountInfo.KeyUID, currPassword, newPassword)
}
func (b *GethStatusBackend) VerifyDatabasePassword(keyUID string, password string) error {
kdfIterations, err := b.multiaccountsDB.GetAccountKDFIterationsNumber(keyUID)
if err != nil {

View File

@ -847,6 +847,14 @@ func ConvertToKeycardAccount(keyStoreDir, accountData, settingsJSON, password, n
return makeJSONResponse(nil)
}
func ConvertToRegularAccount(mnemonic, currPassword, newPassword string) string {
err := statusBackend.ConvertToRegularAccount(mnemonic, currPassword, newPassword)
if err != nil {
return makeJSONResponse(err)
}
return makeJSONResponse(nil)
}
func ImageServerTLSCert() string {
cert, err := server.PublicTLSCert()

View File

@ -317,8 +317,8 @@ func (db *Database) UpdateAccount(account Account) error {
return err
}
func (db *Database) UpdateAccountKeycardPairing(account Account) error {
_, err := db.db.Exec("UPDATE accounts SET keycardPairing = ? WHERE keyUid = ?", account.KeycardPairing, account.KeyUID)
func (db *Database) UpdateAccountKeycardPairing(keyUID string, keycardPairing string) error {
_, err := db.db.Exec("UPDATE accounts SET keycardPairing = ? WHERE keyUid = ?", keycardPairing, keyUID)
return err
}