add account conversion (#2277)

This commit is contained in:
Bitgamma 2021-07-20 14:48:10 +03:00 committed by GitHub
parent 2f6b32b1f5
commit 6c2e9652d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 227 additions and 1 deletions

View File

@ -1 +1 @@
0.82.0 0.83.0

View File

@ -478,6 +478,39 @@ func TestLoginWithKey(t *testing.T) {
require.Equal(t, crypto.PubkeyToAddress(chatKey.PublicKey), extkey.Address) require.Equal(t, crypto.PubkeyToAddress(chatKey.PublicKey), extkey.Address)
} }
func TestVerifyDatabasePassword(t *testing.T) {
utils.Init()
b := NewGethStatusBackend()
chatKey, err := gethcrypto.GenerateKey()
require.NoError(t, err)
walletKey, err := gethcrypto.GenerateKey()
require.NoError(t, err)
keyUIDHex := sha256.Sum256(gethcrypto.FromECDSAPub(&chatKey.PublicKey))
keyUID := types.EncodeHex(keyUIDHex[:])
main := multiaccounts.Account{
KeyUID: keyUID,
}
tmpdir, err := ioutil.TempDir("", "verify-database-password-")
require.NoError(t, err)
defer os.Remove(tmpdir)
conf, err := params.NewNodeConfig(tmpdir, 1777)
require.NoError(t, err)
keyhex := hex.EncodeToString(gethcrypto.FromECDSA(chatKey))
require.NoError(t, b.AccountManager().InitKeystore(conf.KeyStoreDir))
b.UpdateRootDataDir(conf.DataDir)
require.NoError(t, b.OpenAccounts())
address := crypto.PubkeyToAddress(walletKey.PublicKey)
require.NoError(t, b.SaveAccountAndStartNodeWithKey(main, "test-pass", settings, conf, []accounts.Account{{Address: address, Wallet: true}}, keyhex))
require.NoError(t, b.Logout())
require.NoError(t, b.StopNode())
require.Error(t, b.VerifyDatabasePassword(main.KeyUID, "wrong-pass"))
require.NoError(t, b.VerifyDatabasePassword(main.KeyUID, "test-pass"))
}
func TestDeleteMulticcount(t *testing.T) { func TestDeleteMulticcount(t *testing.T) {
backend := NewGethStatusBackend() backend := NewGethStatusBackend()
@ -553,3 +586,92 @@ func TestDeleteMulticcount(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 3, len(files)) require.Equal(t, 3, len(files))
} }
func TestConvertAccount(t *testing.T) {
backend := NewGethStatusBackend()
password := "123123"
rootDataDir, err := ioutil.TempDir("", "test-keystore-dir")
require.NoError(t, err)
defer os.Remove(rootDataDir)
keyStoreDir := filepath.Join(rootDataDir, "keystore")
backend.rootDataDir = rootDataDir
err = backend.AccountManager().InitKeystore(keyStoreDir)
require.NoError(t, err)
backend.AccountManager()
accs, err := backend.AccountManager().
AccountsGenerator().
GenerateAndDeriveAddresses(12, 1, "", []string{"m/44'/60'/0'/0"})
require.NoError(t, err)
generateAccount := accs[0]
accountInfo, err := backend.AccountManager().
AccountsGenerator().
StoreAccount(generateAccount.ID, password)
require.NoError(t, err)
account := multiaccounts.Account{
Name: "foo",
Timestamp: 1,
KeyUID: generateAccount.KeyUID,
}
err = backend.ensureAppDBOpened(account, password)
require.NoError(t, err)
settings := accounts.Settings{
Address: types.HexToAddress(accountInfo.Address),
CurrentNetwork: "mainnet_rpc",
DappsAddress: types.HexToAddress(accountInfo.Address),
EIP1581Address: types.HexToAddress(accountInfo.Address),
InstallationID: "d3efcff6-cffa-560e-a547-21d3858cbc51",
KeyUID: account.KeyUID,
LatestDerivedPath: 0,
Name: "Jittery Cornflowerblue Kingbird",
Networks: &networks,
PhotoPath: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAIAAACRXR/mAAAAjklEQVR4nOzXwQmFMBAAUZXUYh32ZB32ZB02sxYQQSZGsod55/91WFgSS0RM+SyjA56ZRZhFmEWYRRT6h+M6G16zrxv6fdJpmUWYRbxsYr13dKfanpN0WmYRZhGzXz6AWYRZRIfbaX26fT9Jk07LLMIsosPt9I/dTDotswizCG+nhFmEWYRZhFnEHQAA///z1CFkYamgfQAAAABJRU5ErkJggg==",
PreviewPrivacy: false,
PublicKey: accountInfo.PublicKey,
SigningPhrase: "yurt joey vibe",
WalletRootAddress: types.HexToAddress(accountInfo.Address)}
err = backend.saveAccountsAndSettings(
settings,
&params.NodeConfig{},
nil)
require.NoError(t, err)
err = backend.OpenAccounts()
require.NoError(t, err)
err = backend.SaveAccount(account)
require.NoError(t, err)
files, err := ioutil.ReadDir(rootDataDir)
require.NoError(t, err)
require.NotEqual(t, 3, len(files))
keycardPassword := "0xcafecafe"
keycardAccount := account
keycardAccount.KeycardPairing = "pairing"
keycardSettings := accounts.Settings{
KeycardInstanceUID: "0xdeadbeef",
KeycardPAiredOn: 1,
KeycardPairing: "pairing",
}
err = backend.ConvertToKeycardAccount(keyStoreDir, keycardAccount, keycardSettings, password, keycardPassword)
require.NoError(t, err)
_, err = os.Stat(keyStoreDir)
require.Error(t, err)
require.True(t, os.IsNotExist(err))
err = backend.ensureAppDBOpened(keycardAccount, keycardPassword)
require.NoError(t, err)
}

View File

@ -463,6 +463,77 @@ func (b *GethStatusBackend) ChangeDatabasePassword(keyUID string, password strin
return nil return nil
} }
func (b *GethStatusBackend) ConvertToKeycardAccount(keyStoreDir string, account multiaccounts.Account, settings accounts.Settings, password string, newPassword string) error {
err := b.multiaccountsDB.UpdateAccountKeycardPairing(account)
if err != nil {
return err
}
err = b.ensureAppDBOpened(account, password)
if err != nil {
return err
}
accountDB := accounts.NewDB(b.appDB)
err = accountDB.SaveSetting("keycard-instance_uid", settings.KeycardInstanceUID)
if err != nil {
return err
}
err = accountDB.SaveSetting("keycard-paired_on", settings.KeycardPAiredOn)
if err != nil {
return err
}
err = accountDB.SaveSetting("keycard-pairing", settings.KeycardPairing)
if err != nil {
return err
}
err = accountDB.SaveSetting("mnemonic", nil)
if err != nil {
return err
}
err = accountDB.DeleteSeedAndKeyAccounts()
if err != nil {
return err
}
dbPath := filepath.Join(b.rootDataDir, fmt.Sprintf("%s.db", account.KeyUID))
err = appdatabase.ChangeDatabasePassword(dbPath, password, newPassword)
if err != nil {
return err
}
err = b.closeAppDB()
if err != nil {
return err
}
return os.RemoveAll(keyStoreDir)
}
func (b *GethStatusBackend) VerifyDatabasePassword(keyUID string, password string) error {
err := b.ensureAppDBOpened(multiaccounts.Account{KeyUID: keyUID}, password)
if err != nil {
return err
}
accountsDB := accounts.NewDB(b.appDB)
_, err = accountsDB.GetWalletAddress()
if err != nil {
return err
}
err = b.closeAppDB()
if err != nil {
return err
}
return nil
}
func (b *GethStatusBackend) SaveAccountAndStartNodeWithKey(acc multiaccounts.Account, password string, settings accounts.Settings, nodecfg *params.NodeConfig, subaccs []accounts.Account, keyHex string) error { func (b *GethStatusBackend) SaveAccountAndStartNodeWithKey(acc multiaccounts.Account, password string, settings accounts.Settings, nodecfg *params.NodeConfig, subaccs []accounts.Account, keyHex string) error {
err := b.SaveAccount(acc) err := b.SaveAccount(acc)
if err != nil { if err != nil {

View File

@ -175,6 +175,10 @@ func VerifyAccountPassword(keyStoreDir, address, password string) string {
return makeJSONResponse(err) return makeJSONResponse(err)
} }
func VerifyDatabasePassword(keyUID, password string) string {
return makeJSONResponse(statusBackend.VerifyDatabasePassword(keyUID, password))
}
// MigrateKeyStoreDir migrates key files to a new directory // MigrateKeyStoreDir migrates key files to a new directory
func MigrateKeyStoreDir(accountData, password, oldDir, newDir string) string { func MigrateKeyStoreDir(accountData, password, oldDir, newDir string) string {
var account multiaccounts.Account var account multiaccounts.Account
@ -663,3 +667,22 @@ func ChangeDatabasePassword(keyUID, password, newPassword string) string {
} }
return makeJSONResponse(nil) return makeJSONResponse(nil)
} }
func ConvertToKeycardAccount(keyStoreDir, accountData, settingsJSON, password, newPassword string) string {
var account multiaccounts.Account
err := json.Unmarshal([]byte(accountData), &account)
if err != nil {
return makeJSONResponse(err)
}
var settings accounts.Settings
err = json.Unmarshal([]byte(settingsJSON), &settings)
if err != nil {
return makeJSONResponse(err)
}
err = statusBackend.ConvertToKeycardAccount(keyStoreDir, account, settings, password, newPassword)
if err != nil {
return makeJSONResponse(err)
}
return makeJSONResponse(nil)
}

View File

@ -551,6 +551,11 @@ func (db *Database) DeleteAccount(address types.Address) error {
return err return err
} }
func (db *Database) DeleteSeedAndKeyAccounts() error {
_, err := db.db.Exec("DELETE FROM accounts WHERE type = ? OR type = ?", accountTypeSeed, accountTypeKey)
return err
}
func (db *Database) GetNotificationsEnabled() (bool, error) { func (db *Database) GetNotificationsEnabled() (bool, error) {
var result bool var result bool
err := db.db.QueryRow("SELECT notifications_enabled FROM settings WHERE synthetic_id = 'id'").Scan(&result) err := db.db.QueryRow("SELECT notifications_enabled FROM settings WHERE synthetic_id = 'id'").Scan(&result)

View File

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